From d33ebec03d20faec1b65dc917d00f622c5284f21 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Sun, 29 May 2022 00:15:49 +0800 Subject: [PATCH 01/34] houhaoyu@huawei.com support variable in StoragePropandLink Signed-off-by: houhaoyu Change-Id: Ic0ab749a1682f74734b647daed006297f72e9d41 --- compiler/src/process_component_member.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/src/process_component_member.ts b/compiler/src/process_component_member.ts index 5766a7b..26bab48 100644 --- a/compiler/src/process_component_member.ts +++ b/compiler/src/process_component_member.ts @@ -512,14 +512,13 @@ function updateSynchedPropertyOneWay(nameIdentifier: ts.Identifier, type: ts.Typ function updateStoragePropAndLinkProperty(node: ts.PropertyDeclaration, name: ts.Identifier, setFuncName: string, log: LogInfo[]): ts.ExpressionStatement { if (isSingleKey(node)) { - const key: string = getDecoratorKey(node); return ts.factory.createExpressionStatement(ts.factory.createBinaryExpression( createPropertyAccessExpressionWithThis(`__${name.getText()}`), ts.factory.createToken(ts.SyntaxKind.EqualsToken), ts.factory.createCallExpression( ts.factory.createPropertyAccessExpression(ts.factory.createCallExpression( ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier(APP_STORAGE), ts.factory.createIdentifier(APP_STORAGE_GET_OR_SET)), undefined, []), - ts.factory.createIdentifier(setFuncName)), undefined, [ts.factory.createStringLiteral(key), + ts.factory.createIdentifier(setFuncName)), undefined, [node.decorators[0].expression.arguments[0], node.initializer, ts.factory.createThis()]))); } else { validateAppStorageDecoractorsNonSingleKey(node, log); From 15fd924ea9f9603b6e3a84211f52da56a4a5148d Mon Sep 17 00:00:00 2001 From: caocan Date: Tue, 31 May 2022 16:09:03 +0800 Subject: [PATCH 02/34] TextInput support CopyOption Signed-off-by: caocan Change-Id: Idd45d0cdb82af0fd624d1527e4937455a97ecd60 --- compiler/components/search.json | 2 +- compiler/components/textarea.json | 2 +- compiler/components/textinput.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/components/search.json b/compiler/components/search.json index 89d7fad..4e9bfc8 100644 --- a/compiler/components/search.json +++ b/compiler/components/search.json @@ -3,6 +3,6 @@ "atomic": true, "attrs": [ "searchButton", "placeholderColor", "placeholderFont", "textFont", "onSubmit", "onChange", - "onCopy", "OnCut", "OnPaste" + "onCopy", "OnCut", "OnPaste", "copyOption" ] } \ No newline at end of file diff --git a/compiler/components/textarea.json b/compiler/components/textarea.json index 0cdc49e..a2e98a0 100644 --- a/compiler/components/textarea.json +++ b/compiler/components/textarea.json @@ -4,6 +4,6 @@ "attrs": [ "placeholderColor", "placeholderFont", "textAlign", "caretColor", "onChange", "onCopy", "OnCut", "OnPaste", "fontSize", "fontColor", "fontStyle", "fontWeight", "fontFamily", - "inputFilter" + "inputFilter", "copyOption" ] } \ No newline at end of file diff --git a/compiler/components/textinput.json b/compiler/components/textinput.json index 0b4c5d7..4905669 100644 --- a/compiler/components/textinput.json +++ b/compiler/components/textinput.json @@ -4,6 +4,7 @@ "attrs": [ "type", "placeholderColor", "placeholderFont", "enterKeyType", "caretColor", "maxLength", "onEditChanged", "onSubmit", "onChange", "onCopy", "OnCut", "OnPaste", "fontSize", - "fontColor", "fontStyle", "fontWeight", "fontFamily", "inputFilter", "onEditChange" + "fontColor", "fontStyle", "fontWeight", "fontFamily", "inputFilter", "onEditChange", + "copyOption" ] } \ No newline at end of file From 3794ec9e97e7d84e70a6e5c703360589f8c0ecfc Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Tue, 31 May 2022 21:44:37 +0800 Subject: [PATCH 03/34] houhaoyu@huawei.com fix @Builder not collect Signed-off-by: houhaoyu Change-Id: I790af78cbea0376b28b3a5322334dee9631ee81f --- compiler/src/validate_ui_syntax.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 318cf08..6cf8848 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -330,7 +330,8 @@ function visitAllNode(node: ts.Node, sourceFileNode: ts.SourceFile, allComponent if (ts.isStructDeclaration(node) && node.name && ts.isIdentifier(node.name)) { collectComponentProps(node); } - if (ts.isMethodDeclaration(node) && hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) { + if ((ts.isMethodDeclaration(node) || ts.isFunctionDeclaration(node)) && + hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) { CUSTOM_BUILDER_METHOD.add(node.name.getText()); } if (ts.isFunctionDeclaration(node) && isExtendFunction(node)) { From 1b1a1484a1fc8165943245e5267e97b935bd45cd Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Tue, 31 May 2022 21:37:36 +0800 Subject: [PATCH 04/34] houhaoyu@huawei.com support import struct through several files Signed-off-by: houhaoyu Change-Id: I18be715f2fb8d04ddec7225077cbf29194d592f5 --- compiler/src/process_import.ts | 91 +++++++++++++++++++++++++++------- 1 file changed, 72 insertions(+), 19 deletions(-) diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index e80549f..8d236e9 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -47,23 +47,29 @@ import { LogInfo, LogType } from './utils'; import { projectConfig } from '../main'; export default function processImport(node: ts.ImportDeclaration | ts.ImportEqualsDeclaration | - ts.ExportDeclaration, pagesDir: string, log: LogInfo[]): void { + ts.ExportDeclaration, pagesDir: string, log: LogInfo[], asName: Map = new Map(), + isEntryPage: boolean = true): void { let filePath: string; let defaultName: string; - const asName: Map = new Map(); if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) { filePath = node.moduleSpecifier.getText().replace(/'|"/g, ''); if (ts.isImportDeclaration(node) && node.importClause && node.importClause.name && ts.isIdentifier(node.importClause.name)) { defaultName = node.importClause.name.escapedText.toString(); + if (isEntryPage) { + asName.set(defaultName, defaultName); + } } if (ts.isImportDeclaration(node) && node.importClause && node.importClause.namedBindings && ts.isNamedImports(node.importClause.namedBindings) && - node.importClause.namedBindings.elements) { + node.importClause.namedBindings.elements && isEntryPage) { node.importClause.namedBindings.elements.forEach(item => { - if (item.name && item.propertyName && ts.isIdentifier(item.name) && - ts.isIdentifier(item.propertyName)) { - asName.set(item.propertyName.escapedText.toString(), item.name.escapedText.toString()); + if (item.name && ts.isIdentifier(item.name)) { + if (item.propertyName && ts.isIdentifier(item.propertyName) && asName) { + asName.set(item.propertyName.escapedText.toString(), item.name.escapedText.toString()); + } else { + asName.set(item.name.escapedText.toString(), item.name.escapedText.toString()); + } } }); } @@ -72,6 +78,9 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua node.moduleReference.expression && ts.isStringLiteral(node.moduleReference.expression)) { filePath = node.moduleReference.expression.text; defaultName = node.name.escapedText.toString(); + if (isEntryPage) { + asName.set(defaultName, defaultName); + } } } if (filePath && path.extname(filePath) !== EXTNAME_ETS && !isModule(filePath)) { @@ -94,7 +103,8 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua })))); const sourceFile: ts.SourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); - visitAllNode(sourceFile, defaultName, asName, path.dirname(fileResolvePath), log, new Set(), new Set()); + visitAllNode(sourceFile, defaultName, asName, path.dirname(fileResolvePath), log, new Set(), + new Set(), new Set(), new Map()); } } catch (e) { // ignore @@ -102,7 +112,8 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua } function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromParent: Map, - pagesDir: string, log: LogInfo[], entryCollection: Set, exportCollection: Set) { + pagesDir: string, log: LogInfo[], entryCollection: Set, exportCollection: Set, + defaultCollection: Set, asExportCollection: Map) { if (isObservedClass(node)) { // @ts-ignore observedClassCollection.add(node.name.getText()); @@ -117,12 +128,18 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa if (ts.isClassDeclaration(node) && ts.isIdentifier(node.name) && isCustomComponent(node)) { addDependencies(node, defaultNameFromParent, asNameFromParent); isExportEntry(node, log, entryCollection, exportCollection); + if (asExportCollection.has(node.name.getText())) { + componentCollection.customComponents.add(asExportCollection.get(node.name.getText())); + } if (!defaultNameFromParent && node.modifiers && node.modifiers.length >= 2 && node.modifiers[0] && node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword && node.modifiers[1] && node.modifiers[1].kind === ts.SyntaxKind.DefaultKeyword && hasCollection(node.name)) { addDefaultExport(node); } + if (defaultCollection.has(node.name.getText())) { + componentCollection.customComponents.add('default'); + } } if (ts.isExportAssignment(node) && node.expression && ts.isIdentifier(node.expression) && hasCollection(node.expression)) { @@ -137,6 +154,12 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa } addDefaultExport(node); } + if (ts.isExportAssignment(node) && node.expression && ts.isIdentifier(node.expression)) { + if (defaultNameFromParent) { + asNameFromParent.set(node.expression.getText(), asNameFromParent.get(defaultNameFromParent)); + } + defaultCollection.add(node.expression.getText()); + } if (ts.isExportDeclaration(node) && node.exportClause && ts.isNamedExports(node.exportClause) && node.exportClause.elements) { node.exportClause.elements.forEach(item => { @@ -145,15 +168,23 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa remindExportEntryComponent(node, log); } if (item.name && item.propertyName && ts.isIdentifier(item.name) && - ts.isIdentifier(item.propertyName) && hasCollection(item.propertyName)) { - let asExportName: string = item.name.escapedText.toString(); - const asExportPropertyName: string = item.propertyName.escapedText.toString(); - if (asNameFromParent.has(asExportName)) { - asExportName = asNameFromParent.get(asExportName); + ts.isIdentifier(item.propertyName)) { + if (hasCollection(item.propertyName)) { + let asExportName: string = item.name.escapedText.toString(); + const asExportPropertyName: string = item.propertyName.escapedText.toString(); + if (asNameFromParent.has(asExportName)) { + asExportName = asNameFromParent.get(asExportName); + } + setDependencies(asExportName, linkCollection.get(asExportPropertyName), + propertyCollection.get(asExportPropertyName), + propCollection.get(asExportPropertyName)); } - setDependencies(asExportName, linkCollection.get(asExportPropertyName), - propertyCollection.get(asExportPropertyName), - propCollection.get(asExportPropertyName)); + asExportCollection.set(item.propertyName.escapedText.toString(), item.name.escapedText.toString()); + } + if (item.name && ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString()) && + item.propertyName && ts.isIdentifier(item.propertyName)) { + asNameFromParent.set(item.propertyName.escapedText.toString(), + asNameFromParent.get(item.name.escapedText.toString())); } }); } @@ -163,12 +194,34 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa node.exportClause.elements) { node.exportClause.elements.forEach(item => { exportCollection.add((item.propertyName ? item.propertyName : item.name).escapedText.toString()); + if (item.propertyName && ts.isIdentifier(item.propertyName) && item.name && + ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString())) { + asNameFromParent.set(item.propertyName.escapedText.toString(), + asNameFromParent.get(item.name.escapedText.toString())); + defaultCollection.add(item.name.escapedText.toString()); + } }); } - processImport(node, pagesDir, log); + processImport(node, pagesDir, log, asNameFromParent); } - node.getChildren().forEach((item: ts.Node) => visitAllNode(item, defaultNameFromParent, - asNameFromParent, pagesDir, log, entryCollection, exportCollection)); + if (ts.isImportDeclaration(node)) { + if (node.importClause && node.importClause.name && ts.isIdentifier(node.importClause.name)) { + processImport(node, pagesDir, log, asNameFromParent, false); + } else if (node.importClause && node.importClause.namedBindings && + ts.isNamedImports(node.importClause.namedBindings) && node.importClause.namedBindings.elements) { + node.importClause.namedBindings.elements.forEach(item => { + if (item.name && ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString())) { + if (item.propertyName && ts.isIdentifier(item.propertyName)) { + asNameFromParent.set(item.propertyName.escapedText.toString(), + asNameFromParent.get(item.name.escapedText.toString())); + } + } + }); + processImport(node, pagesDir, log, asNameFromParent, false); + } + } + node.getChildren().reverse().forEach((item: ts.Node) => visitAllNode(item, defaultNameFromParent, + asNameFromParent, pagesDir, log, entryCollection, exportCollection, defaultCollection, asExportCollection)); } function isExportEntry(node: ts.ClassDeclaration, log: LogInfo[], entryCollection: Set, From 8a4d30eddd7dda0eae3098afc1bd0aff9d196953 Mon Sep 17 00:00:00 2001 From: xiexiyun Date: Sun, 5 Jun 2022 16:45:48 +0800 Subject: [PATCH 05/34] add relative container attributes Signed-off-by: xiexiyun Change-Id: I523ae2dca147b9af03d79ee26514011ed0a74171 --- compiler/components/common_attrs.json | 2 +- compiler/components/relstive_container.json | 4 ++++ compiler/tsconfig.json | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 compiler/components/relstive_container.json diff --git a/compiler/components/common_attrs.json b/compiler/components/common_attrs.json index de624d3..8d9096c 100644 --- a/compiler/components/common_attrs.json +++ b/compiler/components/common_attrs.json @@ -19,6 +19,6 @@ "accessibilityImportance", "onAccessibility", "grayscale", "brightness", "contrast", "saturate", "geometryTransition", "bindPopup", "colorBlend", "invert", "sepia", "hueRotate", "bindMenu", "bindContextMenu", - "onFocus", "onBlur", "onFocusMove", "focusable", "responseRegion" + "onFocus", "onBlur", "onFocusMove", "focusable", "responseRegion", "alignRules" ] } diff --git a/compiler/components/relstive_container.json b/compiler/components/relstive_container.json new file mode 100644 index 0000000..db24994 --- /dev/null +++ b/compiler/components/relstive_container.json @@ -0,0 +1,4 @@ +{ + "name": "RelativeContainer", + "attrs": [] +} \ No newline at end of file diff --git a/compiler/tsconfig.json b/compiler/tsconfig.json index f538393..09ad1cd 100644 --- a/compiler/tsconfig.json +++ b/compiler/tsconfig.json @@ -62,6 +62,7 @@ "Rating", "Rect", "Refresh", + "RelativeContainer", "RemoteWindow", "Row", "RowSplit", @@ -367,6 +368,11 @@ "type": "RectAttribute", "instance": "RectInstance" }, + { + "name": "RelativeContainer", + "type": "RelativeContainerAttribute", + "instance": "RelativeContainerInstance" + }, { "name": "Refresh", "type": "RefreshAttribute", From 71a3e3487337699f0f0bc0c38036ad0e309c564f Mon Sep 17 00:00:00 2001 From: hufeng Date: Tue, 3 May 2022 23:55:42 +0800 Subject: [PATCH 06/34] compile with ohm_url module Signed-off-by: hufeng Change-Id: I84ebf3292aae496a0e2acf546328063eb91d9c33 --- compiler/src/ets_checker.ts | 23 ++++ compiler/src/resolve_ohm_url.ts | 62 ++++++++++ compiler/src/validate_ui_syntax.ts | 186 ++++++++++++++++++++++------- compiler/webpack.config.js | 2 + 4 files changed, 230 insertions(+), 43 deletions(-) create mode 100644 compiler/src/resolve_ohm_url.ts diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index db1c6ef..ec79efb 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -36,6 +36,7 @@ import { JS_BIND_COMPONENTS } from './component_map'; import { getName } from './process_component_build'; import { INNER_COMPONENT_NAMES } from './component_map'; import { props } from './compile_info'; +import { resolveSourceFile } from './resolve_ohm_url'; function readDeaclareFiles(): string[] { const declarationsFileNames: string[] = []; @@ -109,6 +110,21 @@ export function createLanguageService(rootFileNames: string[]): ts.LanguageServi return ts.createLanguageService(servicesHost, ts.createDocumentRegistry()); } +function getOhmUrlFile(moduleName: string): {modulePath: string, suffix: string} { + let modulePath = resolveSourceFile(moduleName); + let suffix: string; + if (modulePath.endsWith('.ets')) { + suffix = '.ets'; + } else if (modulePath.endsWith('.ts')) { + suffix = '.ts'; + } else { + modulePath = modulePath.replace('.js', '.d.ts'); + suffix = '.d.ts'; + } + + return {modulePath, suffix}; +} + function resolveModuleNames(moduleNames: string[], containingFile: string): ts.ResolvedModuleFull[] { const resolvedModules: ts.ResolvedModuleFull[] = []; for (const moduleName of moduleNames) { @@ -122,6 +138,13 @@ function resolveModuleNames(moduleNames: string[], containingFile: string): ts.R }); if (result.resolvedModule) { resolvedModules.push(result.resolvedModule); + } else if (/^@bundle:/.test(moduleName.trim())) { + const module = getOhmUrlFile(moduleName.trim()); + if (ts.sys.fileExists(module.modulePath)) { + resolvedModules.push(getResolveModule(module.modulePath, module.suffix)); + } else { + resolvedModules.push(null); + } } else if (/^@(system|ohos)/.test(moduleName.trim())) { const modulePath: string = path.resolve(__dirname, '../../../api', moduleName + '.d.ts'); if (ts.sys.fileExists(modulePath)) { diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts new file mode 100644 index 0000000..58bd303 --- /dev/null +++ b/compiler/src/resolve_ohm_url.ts @@ -0,0 +1,62 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { logger } from './compile_info'; +const { projectConfig } = require('../main'); + +const red: string = '\u001b[31m'; +const reset: string = '\u001b[39m'; + +const REG_OHM_URL = /^@bundle:(\S+)\/(\S+)\/(ets|js|node_modules)\/(\S+)$/; + +export class OHMResolverPlugin { + private source: any; + private target: any; + + constructor(source = 'resolve', target = 'resolve') { + this.source = source; + this.target = target; + } + + apply(resolver) { + const target = resolver.ensureHook(this.target); + resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => { + if (/^@bundle:/.test(request.request)) { + var resolvedSourceFile: string = resolveSourceFile(request.request); + var obj = Object.assign({}, request, { + request: resolvedSourceFile + }); + return resolver.doResolve(target, obj, null, resolveContext, callback); + } + callback(); + }); + } +} + +export function resolveSourceFile(ohmUrl: string): string { + const result = ohmUrl.match(REG_OHM_URL); + // let bundleName = result[1]; + let moduleName = result[2]; + let srcKind = result[3]; + let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]); + if(srcKind == 'ets') { + if (!file.endsWith('.ets') && !file.endsWith('ts')) { + if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) { + file += '.ets'; + } else { + file += '.ts'; + } + } + } else if(srcKind == 'js') { + if (!file.endsWith('.js')) { + file += '.js'; + } + } else { + // Todo: node_modules + } + + if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { + logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); + } + + return file; +} \ No newline at end of file diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 318cf08..b4d9087 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -126,6 +126,7 @@ export const localStoragePropCollection: Map>> = export const isStaticViewCollection: Map = new Map(); +export const packageCollection: Map> = new Map(); export const moduleCollection: Set = new Set(); export const useOSFiles: Set = new Set(); @@ -829,56 +830,155 @@ export function preprocessNewExtend(content: string, extendCollection?: Set { + if (packageCollection.has(configFile)) { + return packageCollection.get(configFile); + } + + const rawData: string = fs.readFileSync(configFile, 'utf-8'); + const data = JSON.parse(rawData); + const bundleName = data.app.bundleName; + const moduleName = data.module.distro.moduleName; + packageCollection.set(configFile, [bundleName, moduleName]); + return [bundleName, moduleName]; +} + +function replaceSystemApi(item: string, systemValue: string, moduleType: string, systemKey: string) { + moduleCollection.add(`${moduleType}.${systemKey}`); + if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { + item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; + } else if (moduleType === SYSTEM_PLUGIN) { + item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` + + `globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`; + } else if (moduleType === OHOS_PLUGIN) { + item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` + + `(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` + + `globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` + + `? globalThis.systemplugin.${systemKey} : undefined)`; + } + return item; +} + +function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string = null) { + if (sourcePath) { + useOSFiles.add(sourcePath); + } + return `var ${importValue} = globalThis.requireNapi("${libSoKey}", true);`; +} + +function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: string, moduleRequest: string, sourcePath: string = null) { + const result = moduleRequest.match(/^@(\S+):(\S+)$/) + let urlType: string = result[1]; + let url: string = result[2]; + switch(urlType) { + case 'bundle': { + let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); + let moduleKind = urlResult[3]; + if (moduleKind == 'lib') { + item = replaceLibSo(importValue, moduleRequest, sourcePath); + } + break; + } + case 'module': { + let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)$/); + let moduleName = urlResult[1]; + let moduleKind = urlResult[2]; + let modulePath = urlResult[3]; + const configJsonFile: string = + path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main/config.json'); + let bundleName = getPackageInfo(configJsonFile)[0]; + moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; + item = moduleKind == 'lib' ? + replaceLibSo(importValue, moduleRequest, sourcePath) : item.replace(/['"](\S+)['"]/, moduleRequest); + break; + } + case 'ohos': { + url = url.replace('/', '.'); + let urlResult = url.match(/^system\.(\S+)/) + moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; + if (!isSystemModule) { + item = item.replace(/['"](\S+)['"]/, moduleRequest); + } else { + let moduleType = urlResult ? 'system' : 'ohos'; + let systemKey = urlResult ? url.substring(7) : url; + item = replaceSystemApi(item, importValue, moduleType, systemKey); + } + break; + } + case 'lib': { + item = replaceLibSo(importValue, url, sourcePath); + break; + } + case 'local': { + let result = sourcePath.match(/(S+)\/src\/main\/(S+)/); + const configJsonFile: string = `${result[1]}/src/main/config.json`; + let packageInfo = getPackageInfo(configJsonFile); + let urlResult = url.match(/^(ets|js|lib)\/(S+)$/); + let moduleKind = urlResult[1]; + let modulePath = urlResult[2]; + if (moduleKind == 'lib') { + item = replaceLibSo(importValue, modulePath, sourcePath); + } else { + moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; + item = item.replace(/['"](\S+)['"]/, moduleRequest); + } + break; + } + default: + console.error("Incorrect OpenHarmony module kind: ", urlType); + } + return item; +} + +function replaceRelativePath(moduleRequest: string, sourcePath: string) { + let filePath = path.resolve(sourcePath, moduleRequest); + let result = filePath.match(/(S+)\/src\/main\/(ets|js)\/(S+)/); + const configJsonFile: string = `${result[1]}/src/main/config.json`; + let packageInfo = getPackageInfo(configJsonFile); + let bundleName = packageInfo[0]; + let moduleName = packageInfo[1]; + + return `@bundle:${bundleName}/${moduleName}/${result[2]}/${result[3]}`; +} + export function processSystemApi(content: string, isProcessAllowList: boolean = false, sourcePath: string = null, isSystemModule: boolean = false): string { - let REG_SYSTEM: RegExp; - if (isProcessAllowList) { - REG_SYSTEM = - /(import|const)\s+(.+)\s*=\s*(\_\_importDefault\()?require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)(\))?/g; - } else { - REG_SYSTEM = - /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)/g; - } - const REG_LIB_SO: RegExp = - /import\s+(.+)\s+from\s+['"]lib(\S+)\.so['"]|import\s+(.+)\s*=\s*require\(\s*['"]lib(\S+)\.so['"]\s*\)/g; const systemValueCollection: Set = new Set(); - const newContent: string = content.replace(REG_LIB_SO, (_, item1, item2, item3, item4) => { - const libSoValue: string = item1 || item3; - const libSoKey: string = item2 || item4; - if (sourcePath) { - useOSFiles.add(sourcePath); + const REG_IMPORT_DECL = isProcessAllowList ? + /(import|const)\s+(.+)\s*=\s*(\_\_importDefault\()?require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)(\))?/g : + /import\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; + + const processedContent: string = content.replace(REG_IMPORT_DECL, (item, item1, item2, item3, item4, item5) => { + let importValue: string = isProcessAllowList ? item2 : item1 || item3; + + if (isProcessAllowList) { + systemValueCollection.add(importValue); + return replaceSystemApi(item, importValue, item4, item5); } - return `var ${libSoValue} = globalThis.requireNapi("${libSoKey}", true);`; - }).replace(REG_SYSTEM, (item, item1, item2, item3, item4, item5, item6, item7) => { - let moduleType: string = item2 || item5; - let systemKey: string = item3 || item6; - let systemValue: string = item1 || item4; - if (!VALIDATE_MODULE.includes(systemValue)) { - importModuleCollection.add(systemValue); - } - if (!isProcessAllowList && !isSystemModule) { - return item; - } else if (isProcessAllowList) { - systemValue = item2; - moduleType = item4; - systemKey = item5; - systemValueCollection.add(systemValue); - } - moduleCollection.add(`${moduleType}.${systemKey}`); - if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { - item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; - } else if (moduleType === SYSTEM_PLUGIN) { - item = `var ${systemValue} = isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ? ` + - `globalThis.systemplugin.${systemKey} : globalThis.requireNapi('${systemKey}')`; - } else if (moduleType === OHOS_PLUGIN) { - item = `var ${systemValue} = globalThis.requireNapi('${systemKey}') || ` + - `(isSystemplugin('${systemKey}', '${OHOS_PLUGIN}') ? ` + - `globalThis.ohosplugin.${systemKey} : isSystemplugin('${systemKey}', '${SYSTEM_PLUGIN}') ` + - `? globalThis.systemplugin.${systemKey} : undefined)`; + + let moduleRequest: string = item2 || item4; + if (/^@(\S+):/.test(moduleRequest)) { // ohmURL + return replaceOhmUrl(isSystemModule, item, importValue, moduleRequest, sourcePath); + } else if (/^@(system|ohos)\./.test(moduleRequest)) { //ohos/system.api + // ets & ts file need compile with .d.ts, so do not replace at the phase of pre_process + if (!isSystemModule) { + return item; + } + let result = moduleRequest.match(/^@(system|ohos)\.(\S+)$/); + let moduleType: string = result[1]; + let apiName: string = result[2]; + return replaceSystemApi(item, importValue, moduleType, apiName); + } else if (/^(\.|\.\.)\//.test(moduleRequest)) { // relativePath + return replaceRelativePath(moduleRequest, sourcePath); + } else if (/^lib(\S+)\.so$/.test(moduleRequest)) { // libxxx.so + let result = moduleRequest.match(/^lib(\S+)\.so$/); + let libSoKey = result[1]; + return replaceLibSo(importValue, libSoKey, sourcePath); } + // node_modules return item; }); - return processInnerModule(newContent, systemValueCollection); + return isProcessAllowList ? processInnerModule(processedContent, systemValueCollection) : processedContent; } function processInnerModule(content: string, systemValueCollection: Set): string { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 3c6758a..ad3881a 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -18,6 +18,7 @@ const fs = require('fs'); const CopyPlugin = require('copy-webpack-plugin'); const Webpack = require('webpack'); const { GenAbcPlugin } = require('./lib/gen_abc_plugin'); +const { OHMResolverPlugin } = require('./lib/resolve_ohm_url'); const buildPipeServer = require('./server/build_pipe_server'); const { @@ -100,6 +101,7 @@ function initConfig(config) { global: false }, resolve: { + plugins: [new OHMResolverPlugin()], extensions: ['.js', '.ets', '.ts', '.d.ts'], modules: [ projectPath, From 1b844ddf804eb6e93f879135fa60279a00d6e136 Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 4 May 2022 16:59:28 +0800 Subject: [PATCH 07/34] Adapt for process_import Signed-off-by: hufeng Change-Id: I23a3a7c50e5ce70795c77d07e32f439b54de1e96 --- compiler/src/process_import.ts | 8 ++++++-- compiler/src/resolve_ohm_url.ts | 6 +++++- compiler/src/validate_ui_syntax.ts | 26 ++++++++++++++------------ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index e80549f..87b83b8 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -45,6 +45,7 @@ import { } from './validate_ui_syntax'; import { LogInfo, LogType } from './utils'; import { projectConfig } from '../main'; +import { isOhmUrl, resolveSourceFile } from './resolve_ohm_url'; export default function processImport(node: ts.ImportDeclaration | ts.ImportEqualsDeclaration | ts.ExportDeclaration, pagesDir: string, log: LogInfo[]): void { @@ -74,12 +75,15 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua defaultName = node.name.escapedText.toString(); } } - if (filePath && path.extname(filePath) !== EXTNAME_ETS && !isModule(filePath)) { + if (filePath && path.extname(filePath) !== EXTNAME_ETS && !isModule(filePath) && !isOhmUrl(filePath)) { filePath += EXTNAME_ETS; } + try { let fileResolvePath: string; - if (/^(\.|\.\.)\//.test(filePath) && filePath.indexOf(NODE_MODULES) < 0) { + if (isOhmUrl(filePath) && filePath.indexOf(NODE_MODULES) < 0) { + fileResolvePath = resolveSourceFile(filePath); + } else if (/^(\.|\.\.)\//.test(filePath) && filePath.indexOf(NODE_MODULES) < 0) { fileResolvePath = path.resolve(pagesDir, filePath); } else if (/^\//.test(filePath) && filePath.indexOf(NODE_MODULES) < 0) { fileResolvePath = filePath; diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts index 58bd303..098e982 100644 --- a/compiler/src/resolve_ohm_url.ts +++ b/compiler/src/resolve_ohm_url.ts @@ -20,7 +20,7 @@ export class OHMResolverPlugin { apply(resolver) { const target = resolver.ensureHook(this.target); resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => { - if (/^@bundle:/.test(request.request)) { + if (isOhmUrl(request.request)) { var resolvedSourceFile: string = resolveSourceFile(request.request); var obj = Object.assign({}, request, { request: resolvedSourceFile @@ -32,6 +32,10 @@ export class OHMResolverPlugin { } } +export function isOhmUrl(moduleRequest: string) { + return /^@(\S+):/.test(moduleRequest) ? true : false; +} + export function resolveSourceFile(ohmUrl: string): string { const result = ohmUrl.match(REG_OHM_URL); // let bundleName = result[1]; diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index b4d9087..4d23f3f 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -70,6 +70,7 @@ import { projectConfig } from '../main'; import { collectExtend } from './process_ui_syntax'; import { importModuleCollection } from './ets_checker'; import { isExtendFunction } from './process_ui_syntax'; +import { isOhmUrl } from './resolve_ohm_url'; export interface ComponentCollection { localStorageName: string; @@ -835,8 +836,7 @@ function getPackageInfo(configFile: string): Array { return packageCollection.get(configFile); } - const rawData: string = fs.readFileSync(configFile, 'utf-8'); - const data = JSON.parse(rawData); + const data = JSON.parse(fs.readFileSync(configFile).toString()); const bundleName = data.app.bundleName; const moduleName = data.module.distro.moduleName; packageCollection.set(configFile, [bundleName, moduleName]); @@ -910,7 +910,7 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin break; } case 'local': { - let result = sourcePath.match(/(S+)\/src\/main\/(S+)/); + let result = sourcePath.match(/(S+)\/src\/main\/(ets|js)\/(S+)/); const configJsonFile: string = `${result[1]}/src/main/config.json`; let packageInfo = getPackageInfo(configJsonFile); let urlResult = url.match(/^(ets|js|lib)\/(S+)$/); @@ -930,15 +930,17 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin return item; } -function replaceRelativePath(moduleRequest: string, sourcePath: string) { +function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) { let filePath = path.resolve(sourcePath, moduleRequest); let result = filePath.match(/(S+)\/src\/main\/(ets|js)\/(S+)/); - const configJsonFile: string = `${result[1]}/src/main/config.json`; - let packageInfo = getPackageInfo(configJsonFile); - let bundleName = packageInfo[0]; - let moduleName = packageInfo[1]; - - return `@bundle:${bundleName}/${moduleName}/${result[2]}/${result[3]}`; + if (result) { + const configJsonFile: string = `${result[1]}/src/main/config.json`; + let packageInfo = getPackageInfo(configJsonFile); + let bundleName = packageInfo[0]; + let moduleName = packageInfo[1]; + item = `@bundle:${bundleName}/${moduleName}/${result[2]}/${result[3]}`; + } + return item; } export function processSystemApi(content: string, isProcessAllowList: boolean = false, @@ -957,7 +959,7 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = } let moduleRequest: string = item2 || item4; - if (/^@(\S+):/.test(moduleRequest)) { // ohmURL + if (isOhmUrl(moduleRequest)) { // ohmURL return replaceOhmUrl(isSystemModule, item, importValue, moduleRequest, sourcePath); } else if (/^@(system|ohos)\./.test(moduleRequest)) { //ohos/system.api // ets & ts file need compile with .d.ts, so do not replace at the phase of pre_process @@ -969,7 +971,7 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = let apiName: string = result[2]; return replaceSystemApi(item, importValue, moduleType, apiName); } else if (/^(\.|\.\.)\//.test(moduleRequest)) { // relativePath - return replaceRelativePath(moduleRequest, sourcePath); + return replaceRelativePath(item, moduleRequest, sourcePath); } else if (/^lib(\S+)\.so$/.test(moduleRequest)) { // libxxx.so let result = moduleRequest.match(/^lib(\S+)\.so$/); let libSoKey = result[1]; From 51fea3119f165ef297dd639a63b66d2f38f6cf69 Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 4 May 2022 23:47:40 +0800 Subject: [PATCH 08/34] minor bugfixs Signed-off-by: hufeng Change-Id: I52069bf199492867711d9e852146c1c22920abf3 --- compiler/src/ets_checker.ts | 11 ++++++----- compiler/src/resolve_ohm_url.ts | 23 ++++++++--------------- compiler/src/validate_ui_syntax.ts | 29 +++++++++++++++++------------ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index ec79efb..1a13724 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -91,7 +91,7 @@ export function createLanguageService(rootFileNames: string[]): ts.LanguageServi return undefined; } if (/(? { if (packageCollection.has(configFile)) { return packageCollection.get(configFile); } - const data = JSON.parse(fs.readFileSync(configFile).toString()); const bundleName = data.app.bundleName; const moduleName = data.module.distro.moduleName; @@ -889,7 +890,7 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin let bundleName = getPackageInfo(configJsonFile)[0]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; item = moduleKind == 'lib' ? - replaceLibSo(importValue, moduleRequest, sourcePath) : item.replace(/['"](\S+)['"]/, moduleRequest); + replaceLibSo(importValue, moduleRequest, sourcePath) : item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); break; } case 'ohos': { @@ -897,7 +898,7 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin let urlResult = url.match(/^system\.(\S+)/) moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; if (!isSystemModule) { - item = item.replace(/['"](\S+)['"]/, moduleRequest); + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } else { let moduleType = urlResult ? 'system' : 'ohos'; let systemKey = urlResult ? url.substring(7) : url; @@ -910,17 +911,20 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin break; } case 'local': { - let result = sourcePath.match(/(S+)\/src\/main\/(ets|js)\/(S+)/); - const configJsonFile: string = `${result[1]}/src/main/config.json`; + let result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); + const configJsonFile: string = path.join(result[1], 'src/main/config.json'); let packageInfo = getPackageInfo(configJsonFile); - let urlResult = url.match(/^(ets|js|lib)\/(S+)$/); + let urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); let moduleKind = urlResult[1]; let modulePath = urlResult[2]; if (moduleKind == 'lib') { item = replaceLibSo(importValue, modulePath, sourcePath); + } else if (moduleKind == 'node_modules') { + moduleRequest = `${modulePath}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } else { moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; - item = item.replace(/['"](\S+)['"]/, moduleRequest); + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } break; } @@ -931,14 +935,15 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin } function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) { - let filePath = path.resolve(sourcePath, moduleRequest); - let result = filePath.match(/(S+)\/src\/main\/(ets|js)\/(S+)/); + let filePath = path.resolve(path.dirname(sourcePath), moduleRequest); + let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); if (result) { - const configJsonFile: string = `${result[1]}/src/main/config.json`; + const configJsonFile: string = path.join(result[1], 'src/main/config.json'); let packageInfo = getPackageInfo(configJsonFile); let bundleName = packageInfo[0]; let moduleName = packageInfo[1]; - item = `@bundle:${bundleName}/${moduleName}/${result[2]}/${result[3]}`; + moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } return item; } From bdba1995c14f4e568079c737537f382bba1f2005 Mon Sep 17 00:00:00 2001 From: hufeng Date: Sun, 8 May 2022 16:25:07 +0800 Subject: [PATCH 09/34] Read the module.json with sdk of api-9 Signed-off-by: hufeng Change-Id: Iedb4afeae573296059ce1f3781ce5c9f62c9551d --- compiler/src/validate_ui_syntax.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 6e28b20..a9836bb 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -839,7 +839,7 @@ function getPackageInfo(configFile: string): Array { } const data = JSON.parse(fs.readFileSync(configFile).toString()); const bundleName = data.app.bundleName; - const moduleName = data.module.distro.moduleName; + const moduleName = projectConfig.aceModuleJsonPath ? data.module.name : data.module.distro.moduleName; packageCollection.set(configFile, [bundleName, moduleName]); return [bundleName, moduleName]; } @@ -885,7 +885,7 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin let moduleName = urlResult[1]; let moduleKind = urlResult[2]; let modulePath = urlResult[3]; - const configJsonFile: string = + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main/config.json'); let bundleName = getPackageInfo(configJsonFile)[0]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; @@ -912,7 +912,8 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin } case 'local': { let result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); - const configJsonFile: string = path.join(result[1], 'src/main/config.json'); + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : + path.join(result[1], 'src/main/config.json'); let packageInfo = getPackageInfo(configJsonFile); let urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); let moduleKind = urlResult[1]; @@ -938,7 +939,8 @@ function replaceRelativePath(item:string, moduleRequest: string, sourcePath: str let filePath = path.resolve(path.dirname(sourcePath), moduleRequest); let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); if (result) { - const configJsonFile: string = path.join(result[1], 'src/main/config.json'); + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : + path.join(result[1], 'src/main/config.json'); let packageInfo = getPackageInfo(configJsonFile); let bundleName = packageInfo[0]; let moduleName = packageInfo[1]; @@ -953,20 +955,20 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = const systemValueCollection: Set = new Set(); const REG_IMPORT_DECL = isProcessAllowList ? /(import|const)\s+(.+)\s*=\s*(\_\_importDefault\()?require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)(\))?/g : - /import\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; + /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; const processedContent: string = content.replace(REG_IMPORT_DECL, (item, item1, item2, item3, item4, item5) => { - let importValue: string = isProcessAllowList ? item2 : item1 || item3; + let importValue: string = isProcessAllowList ? item2 : item2 || item4; if (isProcessAllowList) { systemValueCollection.add(importValue); return replaceSystemApi(item, importValue, item4, item5); } - let moduleRequest: string = item2 || item4; + let moduleRequest: string = item3 || item5; if (isOhmUrl(moduleRequest)) { // ohmURL return replaceOhmUrl(isSystemModule, item, importValue, moduleRequest, sourcePath); - } else if (/^@(system|ohos)\./.test(moduleRequest)) { //ohos/system.api + } else if (/^@(system|ohos)\./.test(moduleRequest)) { // ohos/system.api // ets & ts file need compile with .d.ts, so do not replace at the phase of pre_process if (!isSystemModule) { return item; From 3a23fc8465ca5448a03bfdecc5b9cb8777fb1234 Mon Sep 17 00:00:00 2001 From: hufeng Date: Tue, 10 May 2022 14:21:37 +0800 Subject: [PATCH 10/34] Fix ut tests Signed-off-by: hufeng Change-Id: Ibba3d45b78bf6d10981d034a5e6e61834f8812df --- compiler/src/ets_checker.ts | 12 +-- compiler/src/resolve_ohm_url.ts | 126 ++++++++++++++++------------- compiler/src/validate_ui_syntax.ts | 26 +++--- 3 files changed, 85 insertions(+), 79 deletions(-) diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index 1a13724..7a13c0a 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -112,14 +112,8 @@ export function createLanguageService(rootFileNames: string[]): ts.LanguageServi function getOhmUrlFile(moduleName: string): {modulePath: string, suffix: string} { let modulePath = resolveSourceFile(moduleName); - let suffix: string; - if (modulePath.endsWith('.ets')) { - suffix = '.ets'; - } else if (modulePath.endsWith('.ts')) { - suffix = '.ts'; - } else if (modulePath.endsWith('.js')) { - suffix = '.js'; - } else { + let suffix: string = path.extname(modulePath); + if (suffix === 'ts' && modulePath.endsWith('.d.ts')) { suffix = '.d.ts'; } @@ -140,7 +134,7 @@ function resolveModuleNames(moduleNames: string[], containingFile: string): ts.R if (result.resolvedModule) { resolvedModules.push(result.resolvedModule); } else if (/^@bundle:/.test(moduleName.trim())) { - const module = getOhmUrlFile(moduleName.trim()); + const module: {modulePath: string, suffix: string} = getOhmUrlFile(moduleName.trim()); if (ts.sys.fileExists(module.modulePath)) { resolvedModules.push(getResolveModule(module.modulePath, module.suffix)); } else { diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts index 959ffba..8a906b1 100644 --- a/compiler/src/resolve_ohm_url.ts +++ b/compiler/src/resolve_ohm_url.ts @@ -1,59 +1,69 @@ -import * as fs from 'fs'; -import * as path from 'path'; -import { logger } from './compile_info'; -const { projectConfig } = require('../main'); - -const red: string = '\u001b[31m'; -const reset: string = '\u001b[39m'; - -const REG_OHM_URL = /^@bundle:(\S+)\/(\S+)\/(ets|js)\/(\S+)$/; - -export class OHMResolverPlugin { - private source: any; - private target: any; - - constructor(source = 'resolve', target = 'resolve') { - this.source = source; - this.target = target; - } - - apply(resolver) { - const target = resolver.ensureHook(this.target); - resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => { - if (isOhmUrl(request.request)) { - var resolvedSourceFile: string = resolveSourceFile(request.request); - var obj = Object.assign({}, request, { - request: resolvedSourceFile - }); - return resolver.doResolve(target, obj, null, resolveContext, callback); - } - callback(); - }); - } -} - -export function isOhmUrl(moduleRequest: string) { - return /^@(\S+):/.test(moduleRequest) ? true : false; -} - -export function resolveSourceFile(ohmUrl: string): string { - const result = ohmUrl.match(REG_OHM_URL); - let moduleName = result[2]; - let srcKind = result[3]; - let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]); - if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) { - file += '.ets'; - } else if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) { - file += '.ts'; - } else if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) { - file += '.js'; - } else { - file += '.d.ts'; - } - - if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { - logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); - } - - return file; +import * as fs from 'fs'; +import * as path from 'path'; +import { logger } from './compile_info'; +const { projectConfig } = require('../main'); + +const red: string = '\u001b[31m'; +const reset: string = '\u001b[39m'; + +const REG_OHM_URL: RegExp = /^@bundle:(\S+)\/(\S+)\/(ets|js)\/(\S+)$/; + +export class OHMResolverPlugin { + private source: any; + private target: any; + + constructor(source = 'resolve', target = 'resolve') { + this.source = source; + this.target = target; + } + + apply(resolver) { + const target = resolver.ensureHook(this.target); + resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => { + if (isOhmUrl(request.request)) { + var resolvedSourceFile: string = resolveSourceFile(request.request); + var obj = Object.assign({}, request, { + request: resolvedSourceFile + }); + return resolver.doResolve(target, obj, null, resolveContext, callback); + } + callback(); + }); + } +} + +export function isOhmUrl(moduleRequest: string): boolean { + return /^@(\S+):/.test(moduleRequest) ? true : false; +} + +function addExtension(file: string): string { + if (path.extname(file) != '') { + return file; + } + + let extension: string = '.d.ts'; + if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) { + extension = '.ets'; + } + if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) { + extension = '.ts'; + } + if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) { + extension = '.js'; + } + return file + extension; +} + +export function resolveSourceFile(ohmUrl: string): string { + const result = ohmUrl.match(REG_OHM_URL); + let moduleName = result[2]; + let srcKind = result[3]; + let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]); + file = addExtension(file); + + if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { + logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); + } + + return file; } \ No newline at end of file diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index a9836bb..a7b9564 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -844,7 +844,7 @@ function getPackageInfo(configFile: string): Array { return [bundleName, moduleName]; } -function replaceSystemApi(item: string, systemValue: string, moduleType: string, systemKey: string) { +function replaceSystemApi(item: string, systemValue: string, moduleType: string, systemKey: string): string { moduleCollection.add(`${moduleType}.${systemKey}`); if (NATIVE_MODULE.has(`${moduleType}.${systemKey}`)) { item = `var ${systemValue} = globalThis.requireNativeModule('${moduleType}.${systemKey}')`; @@ -860,7 +860,7 @@ function replaceSystemApi(item: string, systemValue: string, moduleType: string, return item; } -function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string = null) { +function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string = null): string { if (sourcePath) { useOSFiles.add(sourcePath); } @@ -936,16 +936,18 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin } function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) { - let filePath = path.resolve(path.dirname(sourcePath), moduleRequest); - let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); - if (result) { - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(result[1], 'src/main/config.json'); - let packageInfo = getPackageInfo(configJsonFile); - let bundleName = packageInfo[0]; - let moduleName = packageInfo[1]; - moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`; - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + if (sourcePath) { + let filePath = path.resolve(path.dirname(sourcePath), moduleRequest); + let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); + if (result) { + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : + path.join(result[1], 'src/main/config.json'); + let packageInfo = getPackageInfo(configJsonFile); + let bundleName = packageInfo[0]; + let moduleName = packageInfo[1]; + moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } } return item; } From 78a5eceda7be9468de0895b21a79aa6f1bf7c52b Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 16 May 2022 22:22:49 +0800 Subject: [PATCH 11/34] Switch ts-loader's module to ESM Signed-off-by: hufeng Change-Id: I76cb403661677e3bfb4f610178a10e23470989d7 --- compiler/src/resolve_ohm_url.ts | 20 ++++- compiler/src/validate_ui_syntax.ts | 83 +++++++++---------- compiler/test/ut/import/importAllEts.ts | 30 +------ compiler/test/ut/import/importEts.ts | 46 +++------- compiler/test/ut/import/importExportEts.ts | 15 ++-- compiler/test/ut/import/importTs.ts | 15 ++-- .../render_component/if/if.ts | 10 +-- .../render_decorator/@styles/@stylesExport.ts | 6 +- compiler/tsconfig.json | 2 +- 9 files changed, 86 insertions(+), 141 deletions(-) diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts index 8a906b1..dbf0845 100644 --- a/compiler/src/resolve_ohm_url.ts +++ b/compiler/src/resolve_ohm_url.ts @@ -36,8 +36,8 @@ export function isOhmUrl(moduleRequest: string): boolean { return /^@(\S+):/.test(moduleRequest) ? true : false; } -function addExtension(file: string): string { - if (path.extname(file) != '') { +function addExtension(file: string, srcPath: string): string { + if (path.extname(file) !== '') { return file; } @@ -46,9 +46,15 @@ function addExtension(file: string): string { extension = '.ets'; } if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) { + if (extension != '.d.ts') { + logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); + } extension = '.ts'; } if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) { + if (extension != '.d.ts') { + logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); + } extension = '.js'; } return file + extension; @@ -58,8 +64,16 @@ export function resolveSourceFile(ohmUrl: string): string { const result = ohmUrl.match(REG_OHM_URL); let moduleName = result[2]; let srcKind = result[3]; + let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]); - file = addExtension(file); + + if (projectConfig.aceBuildJson) { + const buildJson = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); + const modulePath = buildJson.modulePathMap[moduleName]; + file = path.join(modulePath, 'src/main', srcKind, result[4]); + } + + file = addExtension(file, result[4]); if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index a7b9564..a3fcb3e 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -874,28 +874,32 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin switch(urlType) { case 'bundle': { let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); - let moduleKind = urlResult[3]; - if (moduleKind == 'lib') { - item = replaceLibSo(importValue, moduleRequest, sourcePath); + if (urlResult) { + let moduleKind = urlResult[3]; + if (moduleKind === 'lib') { + item = replaceLibSo(importValue, moduleRequest, sourcePath); + } } break; } case 'module': { let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)$/); - let moduleName = urlResult[1]; - let moduleKind = urlResult[2]; - let modulePath = urlResult[3]; - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main/config.json'); - let bundleName = getPackageInfo(configJsonFile)[0]; - moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; - item = moduleKind == 'lib' ? - replaceLibSo(importValue, moduleRequest, sourcePath) : item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + if (urlResult) { + let moduleName = urlResult[1]; + let moduleKind = urlResult[2]; + let modulePath = urlResult[3]; + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : + path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main/config.json'); + let bundleName = getPackageInfo(configJsonFile)[0]; + moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; + item = moduleKind === 'lib' ? replaceLibSo(importValue, moduleRequest, sourcePath) : + item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } break; } case 'ohos': { url = url.replace('/', '.'); - let urlResult = url.match(/^system\.(\S+)/) + let urlResult = url.match(/^system\.(\S+)/); moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; if (!isSystemModule) { item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); @@ -912,20 +916,24 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin } case 'local': { let result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(result[1], 'src/main/config.json'); - let packageInfo = getPackageInfo(configJsonFile); - let urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); - let moduleKind = urlResult[1]; - let modulePath = urlResult[2]; - if (moduleKind == 'lib') { - item = replaceLibSo(importValue, modulePath, sourcePath); - } else if (moduleKind == 'node_modules') { - moduleRequest = `${modulePath}`; - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); - } else { - moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + if (result) { + const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : + path.join(result[1], 'src/main/config.json'); + let packageInfo = getPackageInfo(configJsonFile); + let urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); + if (urlResult) { + let moduleKind = urlResult[1]; + let modulePath = urlResult[2]; + if (moduleKind === 'lib') { + item = replaceLibSo(importValue, modulePath, sourcePath); + } else if (moduleKind === 'node_modules') { + moduleRequest = `${modulePath}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } else { + moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } + } } break; } @@ -954,17 +962,14 @@ function replaceRelativePath(item:string, moduleRequest: string, sourcePath: str export function processSystemApi(content: string, isProcessAllowList: boolean = false, sourcePath: string = null, isSystemModule: boolean = false): string { - const systemValueCollection: Set = new Set(); - const REG_IMPORT_DECL = isProcessAllowList ? - /(import|const)\s+(.+)\s*=\s*(\_\_importDefault\()?require\(\s*['"]@(system|ohos)\.(\S+)['"]\s*\)(\))?/g : + const REG_IMPORT_DECL = isProcessAllowList ? /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]/g : /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; const processedContent: string = content.replace(REG_IMPORT_DECL, (item, item1, item2, item3, item4, item5) => { - let importValue: string = isProcessAllowList ? item2 : item2 || item4; + let importValue: string = isProcessAllowList ? item1 : item2 || item4; if (isProcessAllowList) { - systemValueCollection.add(importValue); - return replaceSystemApi(item, importValue, item4, item5); + return replaceSystemApi(item, importValue, item2, item3); } let moduleRequest: string = item3 || item5; @@ -989,17 +994,7 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = // node_modules return item; }); - return isProcessAllowList ? processInnerModule(processedContent, systemValueCollection) : processedContent; -} - -function processInnerModule(content: string, systemValueCollection: Set): string { - systemValueCollection.forEach(element => { - const target: string = element.trim() + '.default'; - while (content.includes(target)) { - content = content.replace(target, element.trim()); - } - }); - return content; + return processedContent; } const VALIDATE_MODULE_REG: RegExp = new RegExp('^(' + VALIDATE_MODULE.join('|') + ')'); diff --git a/compiler/test/ut/import/importAllEts.ts b/compiler/test/ut/import/importAllEts.ts index 17441fd..fc7a23c 100644 --- a/compiler/test/ut/import/importAllEts.ts +++ b/compiler/test/ut/import/importAllEts.ts @@ -49,36 +49,12 @@ struct ImportTest { ` exports.expectResult = -`"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const AllComponent = __importStar(require("./test/pages/NamespaceComponent")); -const TsModule_1 = __importDefault(require("./test/pages/TsModule")); +`import * as AllComponent from './test/pages/NamespaceComponent'; +import TsModule from './test/pages/TsModule'; class ImportTest extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.__myState1 = new ObservedPropertyObject(new TsModule_1.default(1).method(), this, "myState1"); + this.__myState1 = new ObservedPropertyObject(new TsModule(1).method(), this, "myState1"); this.__myState2 = new ObservedPropertySimple(0, this, "myState2"); this.__myState3 = new ObservedPropertySimple(false, this, "myState3"); this.__myState4 = new ObservedPropertySimple('ImportTest', this, "myState4"); diff --git a/compiler/test/ut/import/importEts.ts b/compiler/test/ut/import/importEts.ts index f57ba18..9536544 100644 --- a/compiler/test/ut/import/importEts.ts +++ b/compiler/test/ut/import/importEts.ts @@ -21,7 +21,7 @@ LinkComponentDefault, { LinkComponent3 } from './test/pages/LinkComponent' import DefaultComponent from "./test/pages/DefaultComponent" -import AMDComponentDefault = require('./test/pages/AMDComponent') +import AMDComponentDefault from "./test/pages/AMDComponent" import TsModule from './test/pages/TsModule' @Entry @@ -99,38 +99,14 @@ struct ImportTest { ` exports.expectResult = -`"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); - __setModuleDefault(result, mod); - return result; -}; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const LinkComponent_1 = __importStar(require("./test/pages/LinkComponent")); -const DefaultComponent_1 = __importDefault(require("./test/pages/DefaultComponent")); -const AMDComponentDefault = require("./test/pages/AMDComponent"); -const TsModule_1 = __importDefault(require("./test/pages/TsModule")); +`import LinkComponentDefault, { LinkComponent as LinkComponent1Ref, LinkComponent2 as LinkComponent2Ref, LinkComponent3 } from './test/pages/LinkComponent'; +import DefaultComponent from "./test/pages/DefaultComponent"; +import AMDComponentDefault from "./test/pages/AMDComponent"; +import TsModule from './test/pages/TsModule'; class ImportTest extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.__myState1 = new ObservedPropertyObject(new TsModule_1.default(1).method(), this, "myState1"); + this.__myState1 = new ObservedPropertyObject(new TsModule(1).method(), this, "myState1"); this.__myState2 = new ObservedPropertySimple(0, this, "myState2"); this.__myState3 = new ObservedPropertySimple(false, this, "myState3"); this.__myState4 = new ObservedPropertySimple('ImportTest', this, "myState4"); @@ -185,7 +161,7 @@ class ImportTest extends View { Column.create(); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new LinkComponent_1.LinkComponent2("2", this, { + View.create(new LinkComponent2Ref("2", this, { LinkComponent2Link1: this.__myState1, LinkComponent2Link2: this.__myState2, LinkComponent2Link3: this.__myState3, @@ -211,7 +187,7 @@ class ImportTest extends View { Text.pop(); let earlierCreatedChild_3 = this.findChildById("3"); if (earlierCreatedChild_3 == undefined) { - View.create(new LinkComponent_1.LinkComponent("3", this, { + View.create(new LinkComponent1Ref("3", this, { LinkComponent1Link1: this.__myState1, LinkComponent1Link2: this.__myState2, LinkComponent1Link3: this.__myState3, @@ -233,7 +209,7 @@ class ImportTest extends View { } let earlierCreatedChild_4 = this.findChildById("4"); if (earlierCreatedChild_4 == undefined) { - View.create(new DefaultComponent_1.default("4", this, { + View.create(new DefaultComponent("4", this, { DefaultComponentLink1: this.__myState1, DefaultComponentLink2: this.__myState2, DefaultComponentLink3: this.__myState3, @@ -251,7 +227,7 @@ class ImportTest extends View { } let earlierCreatedChild_5 = this.findChildById("5"); if (earlierCreatedChild_5 == undefined) { - View.create(new LinkComponent_1.default("5", this, { + View.create(new LinkComponentDefault("5", this, { LinkComponent3Link1: this.__myState1, LinkComponent3Link2: this.__myState2, LinkComponent3Link3: this.__myState3, @@ -291,7 +267,7 @@ class ImportTest extends View { } let earlierCreatedChild_7 = this.findChildById("7"); if (earlierCreatedChild_7 == undefined) { - View.create(new LinkComponent_1.LinkComponent3("7", this, { + View.create(new LinkComponent3("7", this, { LinkComponent3Link1: this.__myState1, LinkComponent3Link2: this.__myState2, LinkComponent3Link3: this.__myState3, diff --git a/compiler/test/ut/import/importExportEts.ts b/compiler/test/ut/import/importExportEts.ts index 652a822..8b11bb5 100644 --- a/compiler/test/ut/import/importExportEts.ts +++ b/compiler/test/ut/import/importExportEts.ts @@ -53,17 +53,12 @@ struct ImportTest { ` exports.expectResult = -`"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const ExportStarComponent_1 = require("./test/pages/ExportStarComponent"); -const TsModule_1 = __importDefault(require("./test/pages/TsModule")); +`import { AllStarComponent } from './test/pages/ExportStarComponent'; +import TsModule from './test/pages/TsModule'; class ImportTest extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.__myState1 = new ObservedPropertyObject(new TsModule_1.default(1).method(), this, "myState1"); + this.__myState1 = new ObservedPropertyObject(new TsModule(1).method(), this, "myState1"); this.__myState2 = new ObservedPropertySimple(0, this, "myState2"); this.__myState3 = new ObservedPropertySimple(false, this, "myState3"); this.__myState4 = new ObservedPropertySimple('ImportTest', this, "myState4"); @@ -118,7 +113,7 @@ class ImportTest extends View { Column.create(); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new ExportStarComponent_1.AllStarComponent.ExportComponent("2", this, { + View.create(new AllStarComponent.ExportComponent("2", this, { ExportComponent1Link1: this.__myState1, ExportComponent1Link2: this.__myState2, ExportComponent1Link3: this.__myState3, @@ -140,7 +135,7 @@ class ImportTest extends View { } let earlierCreatedChild_3 = this.findChildById("3"); if (earlierCreatedChild_3 == undefined) { - View.create(new ExportStarComponent_1.AllStarComponent.default("3", this, { + View.create(new AllStarComponent.default("3", this, { ExportComponent4Link1: this.__myState1, ExportComponent4Link2: this.__myState2, ExportComponent4Link3: this.__myState3, diff --git a/compiler/test/ut/import/importTs.ts b/compiler/test/ut/import/importTs.ts index ad5e964..40570ff 100644 --- a/compiler/test/ut/import/importTs.ts +++ b/compiler/test/ut/import/importTs.ts @@ -53,17 +53,12 @@ struct ImportTest { ` exports.expectResult = -`"use strict"; -var __importDefault = (this && this.__importDefault) || function (mod) { - return (mod && mod.__esModule) ? mod : { "default": mod }; -}; -Object.defineProperty(exports, "__esModule", { value: true }); -const ExportStarComponent_1 = require("./test/pages/ExportStarComponent"); -const TsModule_1 = __importDefault(require("./test/pages/TsModule")); +`import { AllStarComponent } from './test/pages/ExportStarComponent'; +import TsModule from './test/pages/TsModule'; class ImportTest extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); - this.__myState1 = new ObservedPropertyObject(new TsModule_1.default(1).method(), this, "myState1"); + this.__myState1 = new ObservedPropertyObject(new TsModule(1).method(), this, "myState1"); this.__myState2 = new ObservedPropertySimple(0, this, "myState2"); this.__myState3 = new ObservedPropertySimple(false, this, "myState3"); this.__myState4 = new ObservedPropertySimple('ImportTest', this, "myState4"); @@ -118,7 +113,7 @@ class ImportTest extends View { Column.create(); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new ExportStarComponent_1.AllStarComponent.ExportComponent("2", this, { + View.create(new AllStarComponent.ExportComponent("2", this, { ExportComponent1Link1: this.__myState1, ExportComponent1Link2: this.__myState2, ExportComponent1Link3: this.__myState3, @@ -140,7 +135,7 @@ class ImportTest extends View { } let earlierCreatedChild_3 = this.findChildById("3"); if (earlierCreatedChild_3 == undefined) { - View.create(new ExportStarComponent_1.AllStarComponent.default("3", this, { + View.create(new AllStarComponent.default("3", this, { ExportComponent4Link1: this.__myState1, ExportComponent4Link2: this.__myState2, ExportComponent4Link3: this.__myState3, diff --git a/compiler/test/ut/inner_commponent_transform/render_component/if/if.ts b/compiler/test/ut/inner_commponent_transform/render_component/if/if.ts index 16dda28..539115c 100644 --- a/compiler/test/ut/inner_commponent_transform/render_component/if/if.ts +++ b/compiler/test/ut/inner_commponent_transform/render_component/if/if.ts @@ -52,10 +52,8 @@ struct MyComponent { ` exports.expectResult = -`"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -const TestComponent_1 = require("./test/pages/TestComponent"); -const TsModule_1 = require("./test/pages/TsModule"); +`import { TestComponent } from './test/pages/TestComponent'; +import { Animal } from './test/pages/TsModule'; class MyComponent extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); @@ -110,11 +108,11 @@ class MyComponent extends View { } If.pop(); If.create(); - if (TsModule_1.Animal.Dog) { + if (Animal.Dog) { If.branchId(0); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new TestComponent_1.TestComponent("2", this, { content: 'if (import enum)' })); + View.create(new TestComponent("2", this, { content: 'if (import enum)' })); } else { earlierCreatedChild_2.updateWithValueParams({ diff --git a/compiler/test/ut/render_decorator/@styles/@stylesExport.ts b/compiler/test/ut/render_decorator/@styles/@stylesExport.ts index df32684..45d3e8a 100644 --- a/compiler/test/ut/render_decorator/@styles/@stylesExport.ts +++ b/compiler/test/ut/render_decorator/@styles/@stylesExport.ts @@ -55,10 +55,7 @@ export struct FancyUseExp { ` exports.expectResult = -`"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.FancyUseExp = void 0; -class FancyUseExp extends View { +`export class FancyUseExp extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); this.__enable = new ObservedPropertySimple(true, this, "enable"); @@ -109,6 +106,5 @@ class FancyUseExp extends View { Column.pop(); } } -exports.FancyUseExp = FancyUseExp; loadDocument(new FancyUseExp("1", undefined, {})); ` diff --git a/compiler/tsconfig.json b/compiler/tsconfig.json index 09ad1cd..b37d725 100644 --- a/compiler/tsconfig.json +++ b/compiler/tsconfig.json @@ -557,7 +557,7 @@ "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, - "module": "commonjs", + "module": "es2020", "target": "es2017", "types": [], "typeRoots": [], From 9f8db0b3c6926b948a4ca33ea73fb159f13b61d0 Mon Sep 17 00:00:00 2001 From: hufeng Date: Sat, 28 May 2022 11:25:05 +0800 Subject: [PATCH 12/34] Change tsconfig's module type automatically Signed-off-by: hufeng Change-Id: I28675a1c6efdfe08cab43847bd78ba90b1735dbf --- compiler/src/compile_info.ts | 2 +- compiler/src/ets_checker.ts | 2 +- compiler/src/resolve_ohm_url.ts | 96 ++--- compiler/src/validate_ui_syntax.ts | 161 ++++---- compiler/tsconfig.cjs.json | 571 +++++++++++++++++++++++++++++ compiler/webpack.config.js | 16 + 6 files changed, 726 insertions(+), 122 deletions(-) create mode 100644 compiler/tsconfig.cjs.json diff --git a/compiler/src/compile_info.ts b/compiler/src/compile_info.ts index 24c37a8..01bfccb 100644 --- a/compiler/src/compile_info.ts +++ b/compiler/src/compile_info.ts @@ -220,7 +220,7 @@ export class ResultStates { if (lastModuleCollection && lastModuleCollection !== 'NULL') { lastModuleCollection.split(',').forEach(item => { moduleCollection.add(item); - }) + }); } } const moduleContent: string = diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index 7a13c0a..f976be3 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -111,7 +111,7 @@ export function createLanguageService(rootFileNames: string[]): ts.LanguageServi } function getOhmUrlFile(moduleName: string): {modulePath: string, suffix: string} { - let modulePath = resolveSourceFile(moduleName); + const modulePath: string = resolveSourceFile(moduleName); let suffix: string = path.extname(modulePath); if (suffix === 'ts' && modulePath.endsWith('.d.ts')) { suffix = '.d.ts'; diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts index dbf0845..0bc8c0b 100644 --- a/compiler/src/resolve_ohm_url.ts +++ b/compiler/src/resolve_ohm_url.ts @@ -13,71 +13,73 @@ export class OHMResolverPlugin { private target: any; constructor(source = 'resolve', target = 'resolve') { - this.source = source; - this.target = target; + this.source = source; + this.target = target; } apply(resolver) { - const target = resolver.ensureHook(this.target); - resolver.getHook(this.source).tapAsync("OHMResolverPlugin", (request, resolveContext, callback) => { - if (isOhmUrl(request.request)) { - var resolvedSourceFile: string = resolveSourceFile(request.request); - var obj = Object.assign({}, request, { - request: resolvedSourceFile - }); - return resolver.doResolve(target, obj, null, resolveContext, callback); - } - callback(); - }); + const target = resolver.ensureHook(this.target); + resolver.getHook(this.source).tapAsync('OHMResolverPlugin', (request, resolveContext, callback) => { + if (isOhmUrl(request.request)) { + const resolvedSourceFile: string = resolveSourceFile(request.request); + const obj = Object.assign({}, request, { + request: resolvedSourceFile + }); + return resolver.doResolve(target, obj, null, resolveContext, callback); + } + callback(); + }); } } export function isOhmUrl(moduleRequest: string): boolean { - return /^@(\S+):/.test(moduleRequest) ? true : false; + return !!/^@(\S+):/.test(moduleRequest); } function addExtension(file: string, srcPath: string): string { - if (path.extname(file) !== '') { - return file; - } + if (path.extname(file) !== '') { + return file; + } - let extension: string = '.d.ts'; - if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) { - extension = '.ets'; + let extension: string = '.d.ts'; + if (fs.existsSync(file + '.ets') && fs.statSync(file + '.ets').isFile()) { + extension = '.ets'; + } + if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) { + if (extension !== '.d.ts') { + logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); } - if (fs.existsSync(file + '.ts') && fs.statSync(file + '.ts').isFile()) { - if (extension != '.d.ts') { - logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); - } - extension = '.ts'; + extension = '.ts'; + } + if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) { + if (extension !== '.d.ts') { + logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); } - if (fs.existsSync(file + '.js') && fs.statSync(file + '.js').isFile()) { - if (extension != '.d.ts') { - logger.error(red, `ETS:ERROR Failed to compile with files with same name ${srcPath} in the same directory`, reset); - } - extension = '.js'; - } - return file + extension; + extension = '.js'; + } + return file + extension; } export function resolveSourceFile(ohmUrl: string): string { - const result = ohmUrl.match(REG_OHM_URL); - let moduleName = result[2]; - let srcKind = result[3]; + const result = ohmUrl.match(REG_OHM_URL); + const moduleName = result[2]; + const srcKind = result[3]; - let file = path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main', srcKind, result[4]); + let file = ''; - if (projectConfig.aceBuildJson) { - const buildJson = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); - const modulePath = buildJson.modulePathMap[moduleName]; - file = path.join(modulePath, 'src/main', srcKind, result[4]); - } + if (projectConfig.aceBuildJson) { + const buildJson = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); + const modulePath = buildJson.modulePathMap[moduleName]; + file = path.join(modulePath, 'src/main', srcKind, result[4]); + } else { + logger.error(red, `ETS:ERROR Failed to resolve OhmUrl because of aceBuildJson not existing `, reset); + } - file = addExtension(file, result[4]); + file = addExtension(file, result[4]); - if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { - logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); - } + if (!fs.existsSync(file) || !fs.statSync(file).isFile()) { + logger.error(red, `ETS:ERROR Failed to resolve existed file by this ohm url ${ohmUrl} `, reset); + } - return file; -} \ No newline at end of file + return file; +} diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index a3fcb3e..94b5239 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -58,7 +58,7 @@ import { EXTEND_ATTRIBUTE, GLOBAL_STYLE_FUNCTION, STYLES_ATTRIBUTE, - CUSTOM_BUILDER_METHOD, + CUSTOM_BUILDER_METHOD } from './component_map'; import { LogType, @@ -839,7 +839,7 @@ function getPackageInfo(configFile: string): Array { } const data = JSON.parse(fs.readFileSync(configFile).toString()); const bundleName = data.app.bundleName; - const moduleName = projectConfig.aceModuleJsonPath ? data.module.name : data.module.distro.moduleName; + const moduleName = data.module.name; packageCollection.set(configFile, [bundleName, moduleName]); return [bundleName, moduleName]; } @@ -867,47 +867,82 @@ function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string return `var ${importValue} = globalThis.requireNapi("${libSoKey}", true);`; } -function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: string, moduleRequest: string, sourcePath: string = null) { - const result = moduleRequest.match(/^@(\S+):(\S+)$/) - let urlType: string = result[1]; - let url: string = result[2]; - switch(urlType) { - case 'bundle': { - let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); - if (urlResult) { - let moduleKind = urlResult[3]; - if (moduleKind === 'lib') { - item = replaceLibSo(importValue, moduleRequest, sourcePath); - } +function replaceOhmStartsWithBundle(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { + const urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); + if (urlResult) { + const moduleKind = urlResult[3]; + if (moduleKind === 'lib') { + item = replaceLibSo(importValue, moduleRequest, sourcePath); + } + } + return item; +} + +function replaceOhmStartsWithModule(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { + const urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)$/); + if (urlResult && projectConfig.aceModuleJsonPath) { + const moduleName = urlResult[1]; + const moduleKind = urlResult[2]; + const modulePath = urlResult[3]; + const bundleName = getPackageInfo(projectConfig.aceModuleJsonPath)[0]; + moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; + item = moduleKind === 'lib' ? replaceLibSo(importValue, moduleRequest, sourcePath) : + item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } + return item; +} + +function replaceOhmStartsWithOhos(url: string, item: string, importValue:string, moduleRequest: string, isSystemModule: boolean) { + url = url.replace('/', '.'); + const urlResult = url.match(/^system\.(\S+)/); + moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; + if (!isSystemModule) { + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } else { + const moduleType = urlResult ? 'system' : 'ohos'; + const systemKey = urlResult ? url.substring(7) : url; + item = replaceSystemApi(item, importValue, moduleType, systemKey); + } + return item; +} + +function replaceOhmStartsWithLocal(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { + const result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); + if (result && projectConfig.aceModuleJsonPath) { + const packageInfo = getPackageInfo(projectConfig.aceModuleJsonPath); + const urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); + if (urlResult) { + const moduleKind = urlResult[1]; + const modulePath = urlResult[2]; + if (moduleKind === 'lib') { + item = replaceLibSo(importValue, modulePath, sourcePath); + } else if (moduleKind === 'node_modules') { + moduleRequest = `${modulePath}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); + } else { + moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; + item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } + } + } + return item; +} + +function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: string, moduleRequest: string, sourcePath: string = null) { + const result = moduleRequest.match(/^@(\S+):(\S+)$/); + const urlType: string = result[1]; + const url: string = result[2]; + switch (urlType) { + case 'bundle': { + item = replaceOhmStartsWithBundle(url, item, importValue, moduleRequest, sourcePath); break; } case 'module': { - let urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)$/); - if (urlResult) { - let moduleName = urlResult[1]; - let moduleKind = urlResult[2]; - let modulePath = urlResult[3]; - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(projectConfig.projectPath, '../../../../../', moduleName, 'src/main/config.json'); - let bundleName = getPackageInfo(configJsonFile)[0]; - moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; - item = moduleKind === 'lib' ? replaceLibSo(importValue, moduleRequest, sourcePath) : - item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); - } + item = replaceOhmStartsWithModule(url, item, importValue, moduleRequest, sourcePath); break; } case 'ohos': { - url = url.replace('/', '.'); - let urlResult = url.match(/^system\.(\S+)/); - moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; - if (!isSystemModule) { - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); - } else { - let moduleType = urlResult ? 'system' : 'ohos'; - let systemKey = urlResult ? url.substring(7) : url; - item = replaceSystemApi(item, importValue, moduleType, systemKey); - } + item = replaceOhmStartsWithOhos(url, item, importValue, moduleRequest, isSystemModule); break; } case 'lib': { @@ -915,44 +950,24 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin break; } case 'local': { - let result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); - if (result) { - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(result[1], 'src/main/config.json'); - let packageInfo = getPackageInfo(configJsonFile); - let urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); - if (urlResult) { - let moduleKind = urlResult[1]; - let modulePath = urlResult[2]; - if (moduleKind === 'lib') { - item = replaceLibSo(importValue, modulePath, sourcePath); - } else if (moduleKind === 'node_modules') { - moduleRequest = `${modulePath}`; - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); - } else { - moduleRequest = `@bundle:${packageInfo[0]}/${packageInfo[1]}/${moduleKind}/${modulePath}`; - item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); - } - } - } + item = replaceOhmStartsWithLocal(url, item, importValue, moduleRequest, sourcePath); break; } default: - console.error("Incorrect OpenHarmony module kind: ", urlType); + console.error('Incorrect OpenHarmony module kind: ', urlType); } return item; } function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) { - if (sourcePath) { - let filePath = path.resolve(path.dirname(sourcePath), moduleRequest); - let result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); - if (result) { - const configJsonFile: string = projectConfig.aceModuleJsonPath ? projectConfig.aceModuleJsonPath : - path.join(result[1], 'src/main/config.json'); - let packageInfo = getPackageInfo(configJsonFile); - let bundleName = packageInfo[0]; - let moduleName = packageInfo[1]; + // Do not replace relativePath to ohmUrl when building bundle + if (sourcePath && projectConfig.compileMode === 'esmodule') { + const filePath = path.resolve(path.dirname(sourcePath), moduleRequest); + const result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); + if (result && projectConfig.aceModuleJsonPath) { + const packageInfo = getPackageInfo(projectConfig.aceModuleJsonPath); + const bundleName = packageInfo[0]; + const moduleName = packageInfo[1]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`; item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } @@ -966,13 +981,13 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; const processedContent: string = content.replace(REG_IMPORT_DECL, (item, item1, item2, item3, item4, item5) => { - let importValue: string = isProcessAllowList ? item1 : item2 || item4; + const importValue: string = isProcessAllowList ? item1 : item2 || item4; if (isProcessAllowList) { return replaceSystemApi(item, importValue, item2, item3); } - let moduleRequest: string = item3 || item5; + const moduleRequest: string = item3 || item5; if (isOhmUrl(moduleRequest)) { // ohmURL return replaceOhmUrl(isSystemModule, item, importValue, moduleRequest, sourcePath); } else if (/^@(system|ohos)\./.test(moduleRequest)) { // ohos/system.api @@ -980,15 +995,15 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = if (!isSystemModule) { return item; } - let result = moduleRequest.match(/^@(system|ohos)\.(\S+)$/); - let moduleType: string = result[1]; - let apiName: string = result[2]; + const result = moduleRequest.match(/^@(system|ohos)\.(\S+)$/); + const moduleType: string = result[1]; + const apiName: string = result[2]; return replaceSystemApi(item, importValue, moduleType, apiName); } else if (/^(\.|\.\.)\//.test(moduleRequest)) { // relativePath return replaceRelativePath(item, moduleRequest, sourcePath); } else if (/^lib(\S+)\.so$/.test(moduleRequest)) { // libxxx.so - let result = moduleRequest.match(/^lib(\S+)\.so$/); - let libSoKey = result[1]; + const result = moduleRequest.match(/^lib(\S+)\.so$/); + const libSoKey = result[1]; return replaceLibSo(importValue, libSoKey, sourcePath); } // node_modules diff --git a/compiler/tsconfig.cjs.json b/compiler/tsconfig.cjs.json new file mode 100644 index 0000000..09ad1cd --- /dev/null +++ b/compiler/tsconfig.cjs.json @@ -0,0 +1,571 @@ +{ + "compileOnSave": false, + "compilerOptions": { + "ets": { + "render": { + "method": ["build", "pageTransition"], + "decorator": "Builder" + }, + "components": [ + "AbilityComponent", + "AlphabetIndexer", + "Animator", + "Badge", + "Blank", + "Button", + "Calendar", + "Camera", + "Canvas", + "Checkbox", + "CheckboxGroup", + "Circle", + "ColorPicker", + "ColorPickerDialog", + "Column", + "ColumnSplit", + "Counter", + "DataPanel", + "DatePicker", + "Divider", + "Ellipse", + "Flex", + "FormComponent", + "Gauge", + "GeometryView", + "Grid", + "GridItem", + "GridContainer", + "Hyperlink", + "Image", + "ImageAnimator", + "Line", + "List", + "ListItem", + "LoadingProgress", + "Marquee", + "Menu", + "Navigation", + "Navigator", + "Option", + "PageTransitionEnter", + "PageTransitionExit", + "Panel", + "Path", + "PatternLock", + "Piece", + "PluginComponent", + "Polygon", + "Polyline", + "Progress", + "QRCode", + "Radio", + "Rating", + "Rect", + "Refresh", + "RelativeContainer", + "RemoteWindow", + "Row", + "RowSplit", + "RichText", + "Scroll", + "ScrollBar", + "Search", + "Section", + "Select", + "Shape", + "Sheet", + "SideBarContainer", + "Slider", + "Span", + "Stack", + "Stepper", + "StepperItem", + "Swiper", + "TabContent", + "Tabs", + "Text", + "TextPicker", + "TextClock", + "TextArea", + "TextInput", + "TextTimer", + "TimePicker", + "Toggle", + "Video", + "Web", + "XComponent" + ], + "extend": { + "decorator": "Extend", + "components": [ + { + "name": "AbilityComponent", + "type": "AbilityComponentAttribute", + "instance": "AbilityComponentInstance" + }, + { + "name": "AlphabetIndexer", + "type": "AlphabetIndexerAttribute", + "instance": "AlphabetIndexerInstance" + }, + { + "name": "Animator", + "type": "AnimatorAttribute", + "instance": "AnimatorInstance" + }, + { + "name": "Badge", + "type": "BadgeAttribute", + "instance": "BadgeInstance" + }, + { + "name": "Blank", + "type": "BlankAttribute", + "instance": "BlankInstance" + }, + { + "name": "Button", + "type": "ButtonAttribute", + "instance": "ButtonInstance" + }, + { + "name": "Calendar", + "type": "CalendarAttribute", + "instance": "CalendarInstance" + }, + { + "name": "Camera", + "type": "CameraAttribute", + "instance": "CameraInstance" + }, + { + "name": "Canvas", + "type": "CanvasAttribute", + "instance": "CanvasInstance" + }, + { + "name": "Checkbox", + "type": "CheckboxAttribute", + "instance": "CheckboxInstance" + }, + { + "name": "CheckboxGroup", + "type": "CheckboxGroupAttribute", + "instance": "CheckboxGroupInstance" + }, + { + "name": "Circle", + "type": "CircleAttribute", + "instance": "CircleInstance" + }, + { + "name": "ColorPicker", + "type": "ColorPickerAttribute", + "instance": "ColorPickerInstance" + }, + { + "name": "ColorPickerDialog", + "type": "ColorPickerDialogAttribute", + "instance": "ColorPickerDialogInstance" + }, + { + "name": "Column", + "type": "ColumnAttribute", + "instance": "ColumnInstance" + }, + { + "name": "ColumnSplit", + "type": "ColumnSplitAttribute", + "instance": "ColumnSplitInstance" + }, + { + "name": "Counter", + "type": "CounterAttribute", + "instance": "CounterInstance" + }, + { + "name": "DataPanel", + "type": "DataPanelAttribute", + "instance": "DataPanelInstance" + }, + { + "name": "DatePicker", + "type": "DatePickerAttribute", + "instance": "DatePickerInstance" + }, + { + "name": "Divider", + "type": "DividerAttribute", + "instance": "DividerInstance" + }, + { + "name": "Ellipse", + "type": "EllipseAttribute", + "instance": "EllipseInstance" + }, + { + "name": "Flex", + "type": "FlexAttribute", + "instance": "FlexInstance" + }, + { + "name": "FormComponent", + "type": "FormComponentAttribute", + "instance": "FormComponentInstance" + }, + { + "name": "Gauge", + "type": "GaugeAttribute", + "instance": "GaugeInstance" + }, + { + "name": "GeometryView", + "type": "GeometryViewAttribute", + "instance": "GeometryViewInstance" + }, + { + "name": "Grid", + "type": "GridAttribute", + "instance": "GridInstance" + }, + { + "name": "GridItem", + "type": "GridItemAttribute", + "instance": "GridItemInstance" + }, + { + "name": "GridContainer", + "type": "GridContainerAttribute", + "instance": "GridContainerInstance" + }, + { + "name": "Hyperlink", + "type": "HyperlinkAttribute", + "instance": "HyperlinkInstance" + }, + { + "name": "Image", + "type": "ImageAttribute", + "instance": "ImageInstance" + }, + { + "name": "ImageAnimator", + "type": "ImageAnimatorAttribute", + "instance": "ImageAnimatorInstance" + }, + { + "name": "Line", + "type": "LineAttribute", + "instance": "LineInstance" + }, + { + "name": "List", + "type": "ListAttribute", + "instance": "ListInstance" + }, + { + "name": "ListItem", + "type": "ListItemAttribute", + "instance": "ListItemInstance" + }, + { + "name": "LoadingProgress", + "type": "LoadingProgressAttribute", + "instance": "LoadingProgressInstance" + }, + { + "name": "Marquee", + "type": "MarqueeAttribute", + "instance": "MarqueeInstance" + }, + { + "name": "Menu", + "type": "MenuAttribute", + "instance": "MenuInstance" + }, + { + "name": "Navigation", + "type": "NavigationAttribute", + "instance": "NavigationInstance" + }, + { + "name": "Navigator", + "type": "NavigatorAttribute", + "instance": "NavigatorInstance" + }, + { + "name": "Option", + "type": "OptionAttribute", + "instance": "OptionInstance" + }, + { + "name": "PageTransitionEnter", + "type": "PageTransitionEnterAttribute", + "instance": "PageTransitionEnterInstance" + }, + { + "name": "PageTransitionExit", + "type": "PageTransitionExitAttribute", + "instance": "PageTransitionExitInstance" + }, + { + "name": "Panel", + "type": "PanelAttribute", + "instance": "PanelInstance" + }, + { + "name": "Path", + "type": "PathAttribute", + "instance": "PathInstance" + }, + { + "name": "PatternLock", + "type": "PatternLockAttribute", + "instance": "PatternLockInstance" + }, + { + "name": "Piece", + "type": "PieceAttribute", + "instance": "PieceInstance" + }, + { + "name": "PluginComponent", + "type": "PluginComponentAttribute", + "instance": "PluginComponentInstance" + }, + { + "name": "Polygon", + "type": "PolygonAttribute", + "instance": "PolygonInstance" + }, + { + "name": "Polyline", + "type": "PolylineAttribute", + "instance": "PolylineInstance" + }, + { + "name": "Progress", + "type": "ProgressAttribute", + "instance": "ProgressInstance" + }, + { + "name": "QRCode", + "type": "QRCodeAttribute", + "instance": "QRCodeInstance" + }, + { + "name": "Radio", + "type": "RadioAttribute", + "instance": "RadioInstance" + }, + { + "name": "Rating", + "type": "RatingAttribute", + "instance": "RatingInstance" + }, + { + "name": "Rect", + "type": "RectAttribute", + "instance": "RectInstance" + }, + { + "name": "RelativeContainer", + "type": "RelativeContainerAttribute", + "instance": "RelativeContainerInstance" + }, + { + "name": "Refresh", + "type": "RefreshAttribute", + "instance": "RefreshInstance" + }, + { + "name": "RemoteWindow", + "type": "RemoteWindowAttribute", + "instance": "RemoteWindowInstance" + }, + { + "name": "Row", + "type": "RowAttribute", + "instance": "RowInstance" + }, + { + "name": "RowSplit", + "type": "RowSplitAttribute", + "instance": "RowSplitInstance" + }, + { + "name": "RichText", + "type": "RichTextAttribute", + "instance": "RichTextInstance" + }, + { + "name": "Scroll", + "type": "ScrollAttribute", + "instance": "ScrollInstance" + }, + { + "name": "ScrollBar", + "type": "ScrollBarAttribute", + "instance": "ScrollBarInstance" + }, + { + "name": "Search", + "type": "SearchAttribute", + "instance": "SearchInstance" + }, + { + "name": "Section", + "type": "SectionAttribute", + "instance": "SectionInstance" + }, + { + "name": "Select", + "type": "SelectAttribute", + "instance": "SelectInstance" + }, + { + "name": "Shape", + "type": "ShapeAttribute", + "instance": "ShapeInstance" + }, + { + "name": "Sheet", + "type": "SheetAttribute", + "instance": "SheetInstance" + }, + { + "name": "SideBarContainer", + "type": "SideBarContainerAttribute", + "instance": "SideBarContainerInstance" + }, + { + "name": "Slider", + "type": "SliderAttribute", + "instance": "SliderInstance" + }, + { + "name": "Span", + "type": "SpanAttribute", + "instance": "SpanInstance" + }, + { + "name": "Stack", + "type": "StackAttribute", + "instance": "StackInstance" + }, + { + "name": "Stepper", + "type": "StepperAttribute", + "instance": "StepperInstance" + }, + { + "name": "StepperItem", + "type": "StepperItemAttribute", + "instance": "StepperItemInstance" + }, + { + "name": "Swiper", + "type": "SwiperAttribute", + "instance": "SwiperInstance" + }, + { + "name": "TabContent", + "type": "TabContentAttribute", + "instance": "TabContentInstance" + }, + { + "name": "Tabs", + "type": "TabsAttribute", + "instance": "TabsInstance" + }, + { + "name": "Text", + "type": "TextAttribute", + "instance": "TextInstance" + }, + { + "name": "TextPicker", + "type": "TextPickerAttribute", + "instance": "TextPickerInstance" + }, + { + "name": "TextClock", + "type": "TextClockAttribute", + "instance": "TextClockInstance" + }, + { + "name": "TextArea", + "type": "TextAreaAttribute", + "instance": "TextAreaInstance" + }, + { + "name": "TextInput", + "type": "TextInputAttribute", + "instance": "TextInputInstance" + }, + { + "name": "TextTimer", + "type": "TextTimerAttribute", + "instance": "TextTimerInstance" + }, + { + "name": "TimePicker", + "type": "TimePickerAttribute", + "instance": "TimePickerInstance" + }, + { + "name": "Toggle", + "type": "ToggleAttribute", + "instance": "ToggleInstance" + }, + { + "name": "Video", + "type": "VideoAttribute", + "instance": "VideoInstance" + }, + { + "name": "Web", + "type": "WebAttribute", + "instance": "WebInstance" + }, + { + "name": "XComponent", + "type": "XComponentAttribute", + "instance": "XComponentInstance" + }, + ] + }, + "styles": { + "decorator": "Styles", + "component": { + "name": "Common", + "type": "T", + "instance": "CommonInstance" + }, + "property": "stateStyles" + }, + }, + "allowJs": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "importsNotUsedAsValues": "preserve", + "noImplicitAny": false, + "noUnusedLocals": false, + "noUnusedParameters": false, + "experimentalDecorators": true, + "moduleResolution": "node", + "resolveJsonModule": true, + "skipLibCheck": true, + "sourceMap": true, + "module": "commonjs", + "target": "es2017", + "types": [], + "typeRoots": [], + "lib": [ + "es2020" + ] + }, + "exclude": [ + "node_modules" + ] +} \ No newline at end of file diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index ad3881a..144c862 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -287,10 +287,26 @@ function setOptimizationConfig(config, workerFile) { } } +function setTsConfigFile() { + let tsconfigTemplate = + path.resolve(__dirname, projectConfig.compileMode === 'esmodule' ? 'tsconfig.esm.json' : 'tsconfig.cjs.json'); + if (fs.existsSync(tsconfigTemplate) && fs.statSync(tsconfigTemplate).isFile()) { + let currentTsconfigFile = path.resolve(__dirname, 'tsconfig.json'); + let tsconfigTemplateNew = + currentTsconfigFile.replace(/.json$/, projectConfig.compileMode === 'esmodule' ? '.cjs.json' : '.esm.json'); + fs.renameSync(currentTsconfigFile, tsconfigTemplateNew); + + let tsconfigFileNew = + tsconfigTemplate.replace(projectConfig.compileMode === 'esmodule' ? /.esm.json$/ : /.cjs.json$/, '.json'); + fs.renameSync(tsconfigTemplate, tsconfigFileNew); + } +} + module.exports = (env, argv) => { const config = {}; setProjectConfig(env); loadEntryObj(projectConfig); + setTsConfigFile(); initConfig(config); const workerFile = readWorkerFile(); setOptimizationConfig(config, workerFile); From e1429bfe80bbb350dcc9b003092b2dcd5a4ea1d3 Mon Sep 17 00:00:00 2001 From: lihong Date: Mon, 6 Jun 2022 15:02:07 +0800 Subject: [PATCH 13/34] lihong67@huawei.com fix buildParam export. Signed-off-by: lihong Change-Id: Ice1252c57d16b88718af008dad1b0293072a85b4 --- compiler/src/process_component_build.ts | 6 +- compiler/src/process_component_member.ts | 13 +--- compiler/src/process_custom_component.ts | 4 +- compiler/src/process_import.ts | 34 +++++++---- compiler/src/validate_ui_syntax.ts | 23 +++++-- compiler/test/pages/TestComponent.ets | 13 ++++ .../@builderParam/@builderParam.ts | 61 ++++++++++++++++--- 7 files changed, 110 insertions(+), 44 deletions(-) diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 94e46f8..aa97d9d 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -76,7 +76,10 @@ import { COMMON_ATTRS, CUSTOM_BUILDER_PROPERTIES } from './component_map'; -import { componentCollection } from './validate_ui_syntax'; +import { + componentCollection, + builderParamObjectCollection +} from './validate_ui_syntax'; import { processCustomComponent } from './process_custom_component'; import { LogType, @@ -84,7 +87,6 @@ import { componentInfo, createFunction } from './utils'; -import { builderParamObjectCollection } from './process_component_member'; import { projectConfig } from '../main'; import { transformLog, contextGlobal } from './process_ui_syntax'; import { props } from './compile_info'; diff --git a/compiler/src/process_component_member.ts b/compiler/src/process_component_member.ts index 26bab48..d86f5e5 100644 --- a/compiler/src/process_component_member.ts +++ b/compiler/src/process_component_member.ts @@ -115,8 +115,6 @@ export const decoratorParamSet: Set = new Set(); export const stateObjectCollection: Set = new Set(); -export const builderParamObjectCollection: Map> = new Map(); - export class UpdateResult { private itemUpdate: boolean = false; private ctorUpdate: boolean = false; @@ -420,21 +418,12 @@ function createUpdateParams(name: ts.Identifier, decorator: string): ts.Statemen updateParamsNode = createUpdateParamsWithIf(name); break; case COMPONENT_PROP_DECORATOR: + case COMPONENT_BUILDERPARAM_DECORATOR: updateParamsNode = createUpdateParamsWithoutIf(name); break; case COMPONENT_OBJECT_LINK_DECORATOR: updateParamsNode = createUpdateParamsWithSet(name); break; - case COMPONENT_BUILDERPARAM_DECORATOR: - if (decorator === COMPONENT_BUILDERPARAM_DECORATOR) { - if (!builderParamObjectCollection.get(componentCollection.currentClassName)) { - builderParamObjectCollection.set(componentCollection.currentClassName, new Set([])); - } - builderParamObjectCollection.get(componentCollection.currentClassName) - .add(name.escapedText.toString()); - } - updateParamsNode = createUpdateParamsWithoutIf(name); - break; } return updateParamsNode; } diff --git a/compiler/src/process_custom_component.ts b/compiler/src/process_custom_component.ts index 0c78a2f..039810e 100644 --- a/compiler/src/process_custom_component.ts +++ b/compiler/src/process_custom_component.ts @@ -43,7 +43,8 @@ import { provideCollection, consumeCollection, objectLinkCollection, - isStaticViewCollection + isStaticViewCollection, + builderParamObjectCollection } from './validate_ui_syntax'; import { propAndLinkDecorators, @@ -52,7 +53,6 @@ import { createViewCreate, createCustomComponentNewExpression } from './process_component_member'; -import { builderParamObjectCollection } from './process_component_member'; import { LogType, LogInfo, diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index 8d236e9..ded48df 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -41,7 +41,8 @@ import { observedClassCollection, enumCollection, getComponentSet, - IComponentSet + IComponentSet, + builderParamObjectCollection } from './validate_ui_syntax'; import { LogInfo, LogType } from './utils'; import { projectConfig } from '../main'; @@ -150,7 +151,8 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa setDependencies(defaultNameFromParent, linkCollection.get(node.expression.escapedText.toString()), propertyCollection.get(node.expression.escapedText.toString()), - propCollection.get(node.expression.escapedText.toString())); + propCollection.get(node.expression.escapedText.toString()), + builderParamObjectCollection.get(node.expression.escapedText.toString())); } addDefaultExport(node); } @@ -177,7 +179,8 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa } setDependencies(asExportName, linkCollection.get(asExportPropertyName), propertyCollection.get(asExportPropertyName), - propCollection.get(asExportPropertyName)); + propCollection.get(asExportPropertyName), + builderParamObjectCollection.get(asExportPropertyName)); } asExportCollection.set(item.propertyName.escapedText.toString(), item.name.escapedText.toString()); } @@ -198,7 +201,7 @@ function visitAllNode(node: ts.Node, defaultNameFromParent: string, asNameFromPa ts.isIdentifier(item.name) && asNameFromParent.has(item.name.escapedText.toString())) { asNameFromParent.set(item.propertyName.escapedText.toString(), asNameFromParent.get(item.name.escapedText.toString())); - defaultCollection.add(item.name.escapedText.toString()); + defaultCollection.add(item.name.escapedText.toString()); } }); } @@ -267,12 +270,13 @@ function addDependencies(node: ts.ClassDeclaration, defaultNameFromParent: strin node.modifiers[1] && node.modifiers[0].kind === ts.SyntaxKind.ExportKeyword && node.modifiers[1].kind === ts.SyntaxKind.DefaultKeyword) { setDependencies(defaultNameFromParent, ComponentSet.links, ComponentSet.properties, - ComponentSet.props); + ComponentSet.props, ComponentSet.builderParams); } else if (asNameFromParent.has(componentName)) { setDependencies(asNameFromParent.get(componentName), ComponentSet.links, ComponentSet.properties, - ComponentSet.props); + ComponentSet.props, ComponentSet.builderParams); } else { - setDependencies(componentName, ComponentSet.links, ComponentSet.properties, ComponentSet.props); + setDependencies(componentName, ComponentSet.links, ComponentSet.properties, ComponentSet.props, + ComponentSet.builderParams); } } @@ -288,18 +292,24 @@ function addDefaultExport(node: ts.ClassDeclaration | ts.ExportAssignment): void setDependencies(CUSTOM_COMPONENT_DEFAULT, linkCollection.has(CUSTOM_COMPONENT_DEFAULT) ? new Set([...linkCollection.get(CUSTOM_COMPONENT_DEFAULT), ...linkCollection.get(name)]) : - linkCollection.get(name), propertyCollection.has(CUSTOM_COMPONENT_DEFAULT) ? - new Set([...propertyCollection.get(CUSTOM_COMPONENT_DEFAULT), ...propertyCollection.get(name)]) : - propertyCollection.get(name), propCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + linkCollection.get(name), + propertyCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + new Set([...propertyCollection.get(CUSTOM_COMPONENT_DEFAULT), + ...propertyCollection.get(name)]) : propertyCollection.get(name), + propCollection.has(CUSTOM_COMPONENT_DEFAULT) ? new Set([...propCollection.get(CUSTOM_COMPONENT_DEFAULT), ...propCollection.get(name)]) : - propCollection.get(name)); + propCollection.get(name), + builderParamObjectCollection.has(CUSTOM_COMPONENT_DEFAULT) ? + new Set([...builderParamObjectCollection.get(CUSTOM_COMPONENT_DEFAULT), + ...builderParamObjectCollection.get(name)]) : builderParamObjectCollection.get(name)); } function setDependencies(component: string, linkArray: Set, propertyArray: Set, - propArray: Set): void { + propArray: Set, builderParamArray: Set): void { linkCollection.set(component, linkArray); propertyCollection.set(component, propertyArray); propCollection.set(component, propArray); + builderParamObjectCollection.set(component, builderParamArray); componentCollection.customComponents.add(component); } diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 6cf8848..1f40f8c 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -46,7 +46,8 @@ import { TTOGGLE_CHECKBOX, TOGGLE_SWITCH, COMPONENT_BUTTON, - COMPONENT_TOGGLE + COMPONENT_TOGGLE, + COMPONENT_BUILDERPARAM_DECORATOR } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -94,6 +95,7 @@ export interface IComponentSet { objectLinks: Set; localStorageLink: Map>; localStorageProp: Map>; + builderParams: Set; } export const componentCollection: ComponentCollection = { @@ -121,6 +123,7 @@ export const storageLinkCollection: Map> = new Map(); export const provideCollection: Map> = new Map(); export const consumeCollection: Map> = new Map(); export const objectLinkCollection: Map> = new Map(); +export const builderParamObjectCollection: Map> = new Map(); export const localStorageLinkCollection: Map>> = new Map(); export const localStoragePropCollection: Map>> = new Map(); @@ -683,6 +686,7 @@ function collectComponentProps(node: ts.StructDeclaration): void { objectLinkCollection.set(componentName, ComponentSet.objectLinks); localStorageLinkCollection.set(componentName, ComponentSet.localStorageLink); localStoragePropCollection.set(componentName, ComponentSet.localStorageProp); + builderParamObjectCollection.set(componentName, ComponentSet.builderParams); } export function getComponentSet(node: ts.StructDeclaration): IComponentSet { @@ -696,13 +700,14 @@ export function getComponentSet(node: ts.StructDeclaration): IComponentSet { const provides: Set = new Set(); const consumes: Set = new Set(); const objectLinks: Set = new Set(); + const builderParams: Set = new Set(); const localStorageLink: Map> = new Map(); const localStorageProp: Map> = new Map(); traversalComponentProps(node, properties, regulars, states, links, props, storageProps, - storageLinks, provides, consumes, objectLinks, localStorageLink, localStorageProp); + storageLinks, provides, consumes, objectLinks, localStorageLink, localStorageProp, builderParams); return { properties, regulars, states, links, props, storageProps, storageLinks, provides, - consumes, objectLinks, localStorageLink, localStorageProp + consumes, objectLinks, localStorageLink, localStorageProp, builderParams }; } @@ -710,7 +715,8 @@ function traversalComponentProps(node: ts.StructDeclaration, properties: Set, states: Set, links: Set, props: Set, storageProps: Set, storageLinks: Set, provides: Set, consumes: Set, objectLinks: Set, - localStorageLink: Map>, localStorageProp: Map>): void { + localStorageLink: Map>, localStorageProp: Map>, + builderParams: Set): void { let isStatic: boolean = true; if (node.members) { const currentMethodCollection: Set = new Set(); @@ -727,7 +733,8 @@ function traversalComponentProps(node: ts.StructDeclaration, properties: Set, links: Set, props: Set, storageProps: Set, storageLinks: Set, provides: Set, consumes: Set, objectLinks: Set, - localStorageLink: Map>, localStorageProp: Map>): void { + localStorageLink: Map>, localStorageProp: Map>, + builderParams: Set): void { switch (decorator) { case COMPONENT_STATE_DECORATOR: states.add(name); @@ -770,6 +778,9 @@ function collectionStates(node: ts.Decorator, decorator: string, name: string, case COMPONENT_OBJECT_LINK_DECORATOR: objectLinks.add(name); break; + case COMPONENT_BUILDERPARAM_DECORATOR: + builderParams.add(name); + break; case COMPONENT_LOCAL_STORAGE_LINK_DECORATOR : collectionlocalStorageParam(node, name, localStorageLink); break; diff --git a/compiler/test/pages/TestComponent.ets b/compiler/test/pages/TestComponent.ets index f958b30..49da1a9 100644 --- a/compiler/test/pages/TestComponent.ets +++ b/compiler/test/pages/TestComponent.ets @@ -23,4 +23,17 @@ export struct TestComponent { .fontSize(32) } } +} + +@Component +export struct CustomContainerExport { + header: string = ""; + @BuilderParam closer: () => void; + build() { + Column() { + Text(this.header) + .fontSize(50) + this.closer() + } + } } \ No newline at end of file diff --git a/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts b/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts index 85cbce8..edff8d5 100644 --- a/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts +++ b/compiler/test/ut/render_decorator/@builderParam/@builderParam.ts @@ -14,6 +14,7 @@ */ exports.source = ` +import { CustomContainerExport } from './test/pages/TestComponent'; @Component struct CustomContainer { header: string = ""; @@ -60,6 +61,15 @@ struct CustomContainerUser { build() { Column() { + CustomContainerExport({ + header: this.text, + }){ + Column(){ + specificParam("111", "22") + }.onClick(()=>{ + this.text = "changeHeader" + }) + } Row(){ CustomContainer({ header: this.text, @@ -84,7 +94,10 @@ struct CustomContainerUser { } ` exports.expectResult = -`class CustomContainer extends View { +`"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const TestComponent_1 = require("./test/pages/TestComponent"); +class CustomContainer extends View { constructor(compilerAssignedUniqueChildId, parent, params) { super(compilerAssignedUniqueChildId, parent); this.header = ""; @@ -178,10 +191,38 @@ class CustomContainerUser extends View { } render() { Column.create(); - Row.create(); let earlierCreatedChild_2 = this.findChildById("2"); if (earlierCreatedChild_2 == undefined) { - View.create(new CustomContainer("2", this, { + View.create(new TestComponent_1.CustomContainerExport("2", this, { + header: this.text, + closer: () => { + Column.create(); + Column.onClick(() => { + this.text = "changeHeader"; + }); + specificParam("111", "22"); + Column.pop(); + } + })); + } + else { + earlierCreatedChild_2.updateWithValueParams({ + header: this.text, + closer: () => { + Column.create(); + Column.onClick(() => { + this.text = "changeHeader"; + }); + specificParam("111", "22"); + Column.pop(); + } + }); + View.create(earlierCreatedChild_2); + } + Row.create(); + let earlierCreatedChild_3 = this.findChildById("3"); + if (earlierCreatedChild_3 == undefined) { + View.create(new CustomContainer("3", this, { header: this.text, content: this.specificParam, callContent: this.callSpecificParam("callContent1", 'callContent2'), @@ -189,19 +230,19 @@ class CustomContainerUser extends View { })); } else { - earlierCreatedChild_2.updateWithValueParams({ + earlierCreatedChild_3.updateWithValueParams({ header: this.text, content: this.specificParam, callContent: this.callSpecificParam("callContent1", 'callContent2'), footer: "Footer" }); - View.create(earlierCreatedChild_2); + View.create(earlierCreatedChild_3); } Row.pop(); Row.create(); - let earlierCreatedChild_3 = this.findChildById("3"); - if (earlierCreatedChild_3 == undefined) { - View.create(new CustomContainer2("3", this, { + let earlierCreatedChild_4 = this.findChildById("4"); + if (earlierCreatedChild_4 == undefined) { + View.create(new CustomContainer2("4", this, { header: this.text, content: () => { Column.create(); @@ -214,7 +255,7 @@ class CustomContainerUser extends View { })); } else { - earlierCreatedChild_3.updateWithValueParams({ + earlierCreatedChild_4.updateWithValueParams({ header: this.text, content: () => { Column.create(); @@ -225,7 +266,7 @@ class CustomContainerUser extends View { Column.pop(); } }); - View.create(earlierCreatedChild_3); + View.create(earlierCreatedChild_4); } Row.pop(); Column.pop(); From af9d6fdc5607987b9aeecb693fb626220b9414c8 Mon Sep 17 00:00:00 2001 From: bixuefeng Date: Mon, 6 Jun 2022 20:24:09 +0800 Subject: [PATCH 14/34] Feature: tabIndex focus Signed-off-by: bixuefeng Change-Id: I570ebd3904a754703d69ae7dd47a7c75d3915123 --- compiler/components/common_attrs.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/components/common_attrs.json b/compiler/components/common_attrs.json index 8d9096c..f01f89e 100644 --- a/compiler/components/common_attrs.json +++ b/compiler/components/common_attrs.json @@ -19,6 +19,6 @@ "accessibilityImportance", "onAccessibility", "grayscale", "brightness", "contrast", "saturate", "geometryTransition", "bindPopup", "colorBlend", "invert", "sepia", "hueRotate", "bindMenu", "bindContextMenu", - "onFocus", "onBlur", "onFocusMove", "focusable", "responseRegion", "alignRules" + "onFocus", "onBlur", "onFocusMove", "focusable", "tabIndex", "responseRegion", "alignRules" ] } From 8295383dc4c0a6d35cb64bee96996b810e933e88 Mon Sep 17 00:00:00 2001 From: hufeng Date: Mon, 6 Jun 2022 15:19:52 +0800 Subject: [PATCH 15/34] Add types Signed-off-by: hufeng Change-Id: I5a1b9949a5360579bb4530cc4e562ae455b78b77 --- compiler/src/resolve_ohm_url.ts | 49 ++++++++++---------- compiler/src/validate_ui_syntax.ts | 72 +++++++++++++++--------------- 2 files changed, 60 insertions(+), 61 deletions(-) diff --git a/compiler/src/resolve_ohm_url.ts b/compiler/src/resolve_ohm_url.ts index 0bc8c0b..68107e8 100644 --- a/compiler/src/resolve_ohm_url.ts +++ b/compiler/src/resolve_ohm_url.ts @@ -9,27 +9,27 @@ const reset: string = '\u001b[39m'; const REG_OHM_URL: RegExp = /^@bundle:(\S+)\/(\S+)\/(ets|js)\/(\S+)$/; export class OHMResolverPlugin { - private source: any; - private target: any; + private source: any; + private target: any; - constructor(source = 'resolve', target = 'resolve') { - this.source = source; - this.target = target; - } + constructor(source = 'resolve', target = 'resolve') { + this.source = source; + this.target = target; + } - apply(resolver) { - const target = resolver.ensureHook(this.target); - resolver.getHook(this.source).tapAsync('OHMResolverPlugin', (request, resolveContext, callback) => { - if (isOhmUrl(request.request)) { - const resolvedSourceFile: string = resolveSourceFile(request.request); - const obj = Object.assign({}, request, { - request: resolvedSourceFile - }); - return resolver.doResolve(target, obj, null, resolveContext, callback); - } - callback(); - }); - } + apply(resolver) { + const target: any = resolver.ensureHook(this.target); + resolver.getHook(this.source).tapAsync('OHMResolverPlugin', (request, resolveContext, callback) => { + if (isOhmUrl(request.request)) { + const resolvedSourceFile: string = resolveSourceFile(request.request); + const obj = Object.assign({}, request, { + request: resolvedSourceFile + }); + return resolver.doResolve(target, obj, null, resolveContext, callback); + } + callback(); + }); + } } export function isOhmUrl(moduleRequest: string): boolean { @@ -61,15 +61,14 @@ function addExtension(file: string, srcPath: string): string { } export function resolveSourceFile(ohmUrl: string): string { - const result = ohmUrl.match(REG_OHM_URL); - const moduleName = result[2]; - const srcKind = result[3]; + const result: RegExpMatchArray = ohmUrl.match(REG_OHM_URL); + const moduleName: string = result[2]; + const srcKind: string = result[3]; - let file = ''; + let file: string = ''; if (projectConfig.aceBuildJson) { - const buildJson = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); - const modulePath = buildJson.modulePathMap[moduleName]; + const modulePath: string = projectConfig.modulePathMap[moduleName]; file = path.join(modulePath, 'src/main', srcKind, result[4]); } else { logger.error(red, `ETS:ERROR Failed to resolve OhmUrl because of aceBuildJson not existing `, reset); diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 94b5239..fd8e5e3 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -70,9 +70,9 @@ import { } from './utils'; import { projectConfig } from '../main'; import { collectExtend } from './process_ui_syntax'; -import { importModuleCollection } from './ets_checker'; import { isExtendFunction } from './process_ui_syntax'; import { isOhmUrl } from './resolve_ohm_url'; +import { logger } from './compile_info'; export interface ComponentCollection { localStorageName: string; @@ -837,9 +837,9 @@ function getPackageInfo(configFile: string): Array { if (packageCollection.has(configFile)) { return packageCollection.get(configFile); } - const data = JSON.parse(fs.readFileSync(configFile).toString()); - const bundleName = data.app.bundleName; - const moduleName = data.module.name; + const data: any = JSON.parse(fs.readFileSync(configFile).toString()); + const bundleName: string = data.app.bundleName; + const moduleName: string = data.module.name; packageCollection.set(configFile, [bundleName, moduleName]); return [bundleName, moduleName]; } @@ -867,10 +867,10 @@ function replaceLibSo(importValue: string, libSoKey: string, sourcePath: string return `var ${importValue} = globalThis.requireNapi("${libSoKey}", true);`; } -function replaceOhmStartsWithBundle(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { - const urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); +function replaceOhmStartsWithBundle(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string): string { + const urlResult: RegExpMatchArray | null = url.match(/^(\S+)\/(\S+)\/(\S+)\/(\S+)$/); if (urlResult) { - const moduleKind = urlResult[3]; + const moduleKind: string = urlResult[3]; if (moduleKind === 'lib') { item = replaceLibSo(importValue, moduleRequest, sourcePath); } @@ -878,13 +878,13 @@ function replaceOhmStartsWithBundle(url: string, item: string, importValue: stri return item; } -function replaceOhmStartsWithModule(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { - const urlResult = url.match(/^(\S+)\/(\S+)\/(\S+)$/); +function replaceOhmStartsWithModule(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string): string { + const urlResult: RegExpMatchArray | null = url.match(/^(\S+)\/(\S+)\/(\S+)$/); if (urlResult && projectConfig.aceModuleJsonPath) { - const moduleName = urlResult[1]; - const moduleKind = urlResult[2]; - const modulePath = urlResult[3]; - const bundleName = getPackageInfo(projectConfig.aceModuleJsonPath)[0]; + const moduleName: string = urlResult[1]; + const moduleKind: string = urlResult[2]; + const modulePath: string = urlResult[3]; + const bundleName: string = getPackageInfo(projectConfig.aceModuleJsonPath)[0]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; item = moduleKind === 'lib' ? replaceLibSo(importValue, moduleRequest, sourcePath) : item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); @@ -892,28 +892,28 @@ function replaceOhmStartsWithModule(url: string, item: string, importValue: stri return item; } -function replaceOhmStartsWithOhos(url: string, item: string, importValue:string, moduleRequest: string, isSystemModule: boolean) { +function replaceOhmStartsWithOhos(url: string, item: string, importValue:string, moduleRequest: string, isSystemModule: boolean): string { url = url.replace('/', '.'); - const urlResult = url.match(/^system\.(\S+)/); + const urlResult: RegExpMatchArray | null = url.match(/^system\.(\S+)/); moduleRequest = urlResult ? `@${url}` : `@ohos.${url}`; if (!isSystemModule) { item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } else { - const moduleType = urlResult ? 'system' : 'ohos'; - const systemKey = urlResult ? url.substring(7) : url; + const moduleType: string = urlResult ? 'system' : 'ohos'; + const systemKey: string = urlResult ? url.substring(7) : url; item = replaceSystemApi(item, importValue, moduleType, systemKey); } return item; } -function replaceOhmStartsWithLocal(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string) { - const result = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); +function replaceOhmStartsWithLocal(url: string, item: string, importValue: string, moduleRequest: string, sourcePath: string): string { + const result: RegExpMatchArray | null = sourcePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); if (result && projectConfig.aceModuleJsonPath) { - const packageInfo = getPackageInfo(projectConfig.aceModuleJsonPath); - const urlResult = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); + const packageInfo: string[] = getPackageInfo(projectConfig.aceModuleJsonPath); + const urlResult: RegExpMatchArray | null = url.match(/^\/(ets|js|lib|node_modules)\/(\S+)$/); if (urlResult) { - const moduleKind = urlResult[1]; - const modulePath = urlResult[2]; + const moduleKind: string = urlResult[1]; + const modulePath: string = urlResult[2]; if (moduleKind === 'lib') { item = replaceLibSo(importValue, modulePath, sourcePath); } else if (moduleKind === 'node_modules') { @@ -928,8 +928,8 @@ function replaceOhmStartsWithLocal(url: string, item: string, importValue: strin return item; } -function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: string, moduleRequest: string, sourcePath: string = null) { - const result = moduleRequest.match(/^@(\S+):(\S+)$/); +function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: string, moduleRequest: string, sourcePath: string = null): string { + const result: RegExpMatchArray = moduleRequest.match(/^@(\S+):(\S+)$/); const urlType: string = result[1]; const url: string = result[2]; switch (urlType) { @@ -954,20 +954,20 @@ function replaceOhmUrl(isSystemModule: boolean, item: string, importValue: strin break; } default: - console.error('Incorrect OpenHarmony module kind: ', urlType); + logger.error('\u001b[31m', `ETS:ERROR Incorrect OpenHarmony module kind: ${urlType}`, '\u001b[39m'); } return item; } -function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string) { +function replaceRelativePath(item:string, moduleRequest: string, sourcePath: string): string { // Do not replace relativePath to ohmUrl when building bundle if (sourcePath && projectConfig.compileMode === 'esmodule') { - const filePath = path.resolve(path.dirname(sourcePath), moduleRequest); - const result = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); + const filePath: string = path.resolve(path.dirname(sourcePath), moduleRequest); + const result: RegExpMatchArray | null = filePath.match(/(\S+)(\/|\\)src(\/|\\)main(\/|\\)(ets|js)(\/|\\)(\S+)/); if (result && projectConfig.aceModuleJsonPath) { - const packageInfo = getPackageInfo(projectConfig.aceModuleJsonPath); - const bundleName = packageInfo[0]; - const moduleName = packageInfo[1]; + const packageInfo: string[] = getPackageInfo(projectConfig.aceModuleJsonPath); + const bundleName: string = packageInfo[0]; + const moduleName: string = packageInfo[1]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${result[5]}/${toUnixPath(result[7])}`; item = item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } @@ -977,7 +977,7 @@ function replaceRelativePath(item:string, moduleRequest: string, sourcePath: str export function processSystemApi(content: string, isProcessAllowList: boolean = false, sourcePath: string = null, isSystemModule: boolean = false): string { - const REG_IMPORT_DECL = isProcessAllowList ? /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]/g : + const REG_IMPORT_DECL: RegExp = isProcessAllowList ? /import\s+(.+)\s+from\s+['"]@(system|ohos)\.(\S+)['"]/g : /(import|export)\s+(.+)\s+from\s+['"](\S+)['"]|import\s+(.+)\s*=\s*require\(\s*['"](\S+)['"]\s*\)/g; const processedContent: string = content.replace(REG_IMPORT_DECL, (item, item1, item2, item3, item4, item5) => { @@ -995,15 +995,15 @@ export function processSystemApi(content: string, isProcessAllowList: boolean = if (!isSystemModule) { return item; } - const result = moduleRequest.match(/^@(system|ohos)\.(\S+)$/); + const result: RegExpMatchArray = moduleRequest.match(/^@(system|ohos)\.(\S+)$/); const moduleType: string = result[1]; const apiName: string = result[2]; return replaceSystemApi(item, importValue, moduleType, apiName); } else if (/^(\.|\.\.)\//.test(moduleRequest)) { // relativePath return replaceRelativePath(item, moduleRequest, sourcePath); } else if (/^lib(\S+)\.so$/.test(moduleRequest)) { // libxxx.so - const result = moduleRequest.match(/^lib(\S+)\.so$/); - const libSoKey = result[1]; + const result: RegExpMatchArray = moduleRequest.match(/^lib(\S+)\.so$/); + const libSoKey: string = result[1]; return replaceLibSo(importValue, libSoKey, sourcePath); } // node_modules From c120766ac7dca531892fd0e3cfd824212b625ca5 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 5 May 2022 09:12:15 +0800 Subject: [PATCH 16/34] adapter module compile Signed-off-by: zhangrengao Change-Id: Ib47c77cf607a8a4af4240e22d8196fd8a350149c --- compiler/src/gen_abc_plugin.ts | 111 ++++++++++++++++++++++++++++- compiler/src/process_js_ast.ts | 35 +++++++++ compiler/src/process_js_file.ts | 8 +++ compiler/src/process_ui_syntax.ts | 9 ++- compiler/src/utils.ts | 113 +++++++++++++++++++++++++++++- compiler/webpack.config.js | 9 ++- 6 files changed, 280 insertions(+), 5 deletions(-) create mode 100644 compiler/src/process_js_ast.ts create mode 100644 compiler/src/process_js_file.ts diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 26bc3e3..1ddd849 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -17,9 +17,15 @@ import * as fs from 'fs'; import * as path from 'path'; import cluster from 'cluster'; import process from 'process'; +import * as child_process from 'child_process' import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; -import { toUnixPath, toHashData } from './utils'; +import { + toUnixPath, + toHashData, + genTemporaryPath } from './utils'; +import { Compilation } from 'webpack'; +import { projectConfig } from '../main'; const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; @@ -65,6 +71,28 @@ export class GenAbcPlugin { } } + compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { + compilation.hooks.finishModules.tap("finishModules", handleFinishModules.bind(this)); + }); + + compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { + compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { + modules.forEach(module => { + if (module != undefined && module.resourceResolveData != undefined) { + let filePath: string = module.resourceResolveData.path; + console.error(filePath); + } + }); + }); + + compilation.hooks.processAssets.tap("processAssets", (assets) => { + Object.keys(compilation.assets).forEach(key => { + delete assets[key]; + }) + }) + + }) + compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { Object.keys(compilation.assets).forEach(key => { // choose *.js @@ -80,9 +108,90 @@ export class GenAbcPlugin { buildPathInfo = output; invokeWorkersToGenAbc(); }); + + } } +function handleFinishModules(modules, callback) { + modules.forEach(module => { + if (module != undefined && module.resourceResolveData != undefined) { + let filePath: string = module.resourceResolveData.path; + console.error(filePath); + if (filePath.endsWith('ets')) { + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); + tempFilePath = toUnixPath(tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + processToAbcFile(tempFilePath, abcFilePath); + } else if (filePath.endsWith('ts')) { + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); + tempFilePath = toUnixPath(tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + processToAbcFile(tempFilePath, abcFilePath); + } else if (filePath.endsWith('js')) { + if (filePath.indexOf("node_modules") !== -1) { + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = toUnixPath(tempFilePath); + const parent: string = path.join(tempFilePath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + fs.copyFileSync(filePath, tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + processToAbcFile(tempFilePath, abcFilePath); + } else { + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = toUnixPath(tempFilePath); + const parent: string = path.join(tempFilePath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + // fs.copyFileSync(filePath, tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + processToAbcFile(tempFilePath, abcFilePath); + } + } else { + console.error("=========================handleFinishModules else==================="); + console.error(filePath); + } + } + }) +} + +function genAbcFileName(temporaryFile: string): string { + let abcFile: string = temporaryFile; + if (temporaryFile.endsWith('ts')) { + abcFile = temporaryFile.replace(/\.ts$/, '.abc'); + } else { + abcFile = temporaryFile.replace(/\.js$/, '.abc'); + } + return abcFile; +} + +function processToAbcFile(tempFilePath, outPutFile) { + let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); + if (isWin) { + js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); + } else if (isMac) { + js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + } + + const args: string[] = [ + '--expose-gc', + js2abc, + '-o', + outPutFile + ]; + if (isDebug) { + args.push('--debug'); + } + args.push(tempFilePath); + + child_process.execFileSync(nodeJs, args); +} + function writeFileSync(inputString: string, output: string, jsBundleFile: string): void { const parent: string = path.join(output, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts new file mode 100644 index 0000000..b46290f --- /dev/null +++ b/compiler/src/process_js_ast.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import ts from 'typescript'; +import path from 'path'; +import { BUILD_ON } from './pre_define'; +import { writeFileSyncByNode } from './utils'; + +export function processJs(program: ts.Program, ut = false): Function { + return (context: ts.TransformationContext) => { + return (node: ts.SourceFile) => { + if (process.env.compiler === BUILD_ON) { + if (process.env.processTs && process.env.processTs === 'false') { + writeFileSyncByNode(node, false); + } + return node; + } else { + return node; + } + + } + } +} \ No newline at end of file diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts new file mode 100644 index 0000000..1a1b1a8 --- /dev/null +++ b/compiler/src/process_js_file.ts @@ -0,0 +1,8 @@ +import { writeFileSyncByString } from './utils'; + +module.exports = function processjs2file(source: string): string { + if (process.env.compilerType && process.env.compilerType === 'ark'){ + writeFileSyncByString(this.resourcePath, source, false); + } + return source; + } \ No newline at end of file diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 04fb029..b3c8ef2 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -49,7 +49,8 @@ import { LogInfo, LogType, hasDecorator, - FileLog + FileLog, + writeFileSyncByNode } from './utils'; import { processComponentBlock, @@ -82,6 +83,9 @@ export function processUISyntax(program: ts.Program, ut = false): Function { if (process.env.compiler === BUILD_ON) { if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) { node = ts.visitEachChild(node, processResourceNode, context); + if (process.env.processTs && process.env.processTs === 'true') { + writeFileSyncByNode(node, true); + } return node; } transformLog.sourceFile = node; @@ -97,6 +101,9 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); + if (process.env.processTs && process.env.processTs === 'true') { + writeFileSyncByNode(node, true); + } return node; } else { return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 97e0a2f..b47450f 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -13,16 +13,22 @@ * limitations under the License. */ -import ts from 'typescript'; import path from 'path'; +import ts from 'typescript'; import fs from 'fs'; +import { projectConfig } from '../main'; import { createHash } from 'crypto'; +import { processSystemApi } from './validate_ui_syntax'; export enum LogType { ERROR = 'ERROR', WARN = 'WARN', NOTE = 'NOTE' } +export const TEMPORARYS: string = 'temporarys'; +export const BUILD: string = 'build'; +export const SRC_MAIN: string = 'src/main'; +const TS_NOCHECK: string = '// @ts-nocheck'; export interface LogInfo { type: LogType, @@ -234,3 +240,108 @@ export function writeFileSync(filePath: string, content: string): void { fs.writeFileSync(filePath, content); } +export function genTemporaryPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { + filePath = toUnixPath(filePath); + projectPath = toUnixPath(projectPath); + + if (filePath.indexOf('node_modules') !== -1) { + const dataTmps = filePath.split('node_modules'); + const preStr = dataTmps[0]; + const sufStr = dataTmps[1]; + const output: string = path.join(buildPath, 'node_modules', sufStr); + return output; + } + + const sufStr = filePath.replace(projectPath, ""); + const output: string = path.join(buildPath, sufStr); + return output; +} + +export function mkdirsSync(dirname: string): boolean { + if (fs.existsSync(dirname)) { + return true; + } else { + if (mkdirsSync(path.dirname(dirname))) { + fs.mkdirSync(dirname); + return true; + } + } + return false; +} + +export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { + let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + mkdirsSync(path.dirname(temporaryFile)); + fs.writeFileSync(temporaryFile, sourceCode); +} + +export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { + if (toTsFile) { + const newStatements = []; + const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); + newStatements.push(tsIgnoreNode); + if (node.statements && node.statements.length) { + newStatements.push(...node.statements); + } + + node = ts.factory.updateSourceFile(node, newStatements); + } + const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + let options : ts.CompilerOptions = { + sourceMap : true + }; + let mapOpions = { + sourceMap : true, + inlineSourceMap : false, + inlineSources : false, + sourceRoot : "", + mapRoot : "", + extendedDiagnostics : false + } + let host = ts.createCompilerHost(options); + let fileName = node.fileName; + // @ts-ignore + let sourceMapGenerator = ts.createSourceMapGenerator( + host, + // @ts-ignore + ts.getBaseFileName(fileName), + "", + "", + mapOpions + ); + // @ts-ignore + let writer = ts.createTextWriter( + // @ts-ignore + ts.getNewLineCharacter({newLine : ts.NewLineKind.LineFeed, removeComments : false})); + printer["writeFile"](node, writer, sourceMapGenerator); + let sourceMapJson = sourceMapGenerator.toJSON(); + sourceMapJson['sources'] = [fileName]; + const result: string = writer.getText(); + let content: string = result; + content = processSystemApi(content, true); + if (toTsFile) { + content = result.replace(`${TS_NOCHECK};`, TS_NOCHECK); + } + const sourceMapContent = JSON.stringify(sourceMapJson); + let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + let temporarySourceMapFile: string = ""; + if (temporaryFile.endsWith("ets")) { + temporarySourceMapFile = temporaryFile.replace(/\.ets$/, '.ets.sourcemap'); + if (toTsFile) { + temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.ts'); + } else { + temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.js'); + } + } else { + if (!toTsFile) { + temporarySourceMapFile = temporaryFile.replace(/\.ts$/, '.ts.sourcemap'); + temporaryFile = temporaryFile.replace(/\.ts$/, '.ts.js'); + } + } + mkdirsSync(path.dirname(temporaryFile)); + fs.writeFileSync(temporaryFile, content); + if (temporarySourceMapFile.length > 0) { + + fs.writeFileSync(temporarySourceMapFile, sourceMapContent); + } +} diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 144c862..6cf9052 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -34,7 +34,7 @@ const { ResultStates } = require('./lib/compile_info'); const { processUISyntax } = require('./lib/process_ui_syntax'); const { IGNORE_ERROR_CODE } = require('./lib/utils'); const { BUILD_SHARE_PATH } = require('./lib/pre_define'); - +const { processJs } = require('./lib/process_js_ast'); process.env.watchMode = (process.env.watchMode && process.env.watchMode === 'true') || 'false'; function initConfig(config) { @@ -80,7 +80,7 @@ function initConfig(config) { getCustomTransformers(program) { return { before: [processUISyntax(program)], - after: [] + after: [processJs(program)] }; }, ignoreDiagnostics: IGNORE_ERROR_CODE @@ -92,6 +92,7 @@ function initConfig(config) { { test: /\.js$/, use: [ + { loader: path.resolve(__dirname, 'lib/process_js_file.js')}, { loader: path.resolve(__dirname, 'lib/process_system_module.js') } ] } @@ -311,9 +312,13 @@ module.exports = (env, argv) => { const workerFile = readWorkerFile(); setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); + + process.env.processTs = false; + if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile); if (env.compilerType && env.compilerType === 'ark') { + process.env.compilerType = 'ark'; let arkDir = path.join(__dirname, 'bin', 'ark'); if (env.arkFrontendDir) { arkDir = env.arkFrontendDir; From 7a66d440a37de4d3bf75f5086713fc2b8750e651 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Fri, 6 May 2022 21:24:25 +0800 Subject: [PATCH 17/34] multi abc Signed-off-by: zhangrengao Change-Id: I0b3d2158ebfac6f473710512ce01302160a0bcd7 --- compiler/src/gen_abc_plugin.ts | 155 ++++++++++++++++++++++++++------- compiler/src/gen_module_abc.ts | 47 ++++++++++ compiler/src/utils.ts | 33 +++++-- 3 files changed, 196 insertions(+), 39 deletions(-) create mode 100644 compiler/src/gen_module_abc.ts diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 1ddd849..1da7db9 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -29,6 +29,7 @@ import { projectConfig } from '../main'; const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; +const genModuleAbcScript = 'gen_module_abc.js'; let output: string; let isWin: boolean = false; let isMac: boolean = false; @@ -50,6 +51,20 @@ const reset: string = '\u001b[39m'; const hashFile = 'gen_hash.json'; const ARK = '/ark/'; +class ModuleInfo { + protected filePath: string; + protected buildFilePath: string; + protected abcFilePath: string; + protected isModuleJs: boolean; + + constructor(filePath: string, buildFilePath: string, abcFilePath: string, isModuleJs: boolean) { + this.filePath = filePath; + this.buildFilePath = buildFilePath; + this.abcFilePath = abcFilePath; + this.isModuleJs = isModuleJs; + } +} + export class GenAbcPlugin { constructor(output_, arkDir_, nodeJs_, isDebug_) { output = output_; @@ -77,6 +92,7 @@ export class GenAbcPlugin { compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { + console.error("==================afterOptimizeModules===================="); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; @@ -114,50 +130,58 @@ export class GenAbcPlugin { } function handleFinishModules(modules, callback) { + console.error("==================handleFinishModules===================="); + let moduleInfos = new Array(); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; console.error(filePath); + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + if (tempFilePath.length === 0) { + return ; + } if (filePath.endsWith('ets')) { - let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); - tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); - tempFilePath = toUnixPath(tempFilePath); - const abcFilePath = genAbcFileName(tempFilePath); - processToAbcFile(tempFilePath, abcFilePath); - } else if (filePath.endsWith('ts')) { - let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); - tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); - tempFilePath = toUnixPath(tempFilePath); - const abcFilePath = genAbcFileName(tempFilePath); - processToAbcFile(tempFilePath, abcFilePath); - } else if (filePath.endsWith('js')) { - if (filePath.indexOf("node_modules") !== -1) { - let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); - tempFilePath = toUnixPath(tempFilePath); - const parent: string = path.join(tempFilePath, '..'); - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent); - } - fs.copyFileSync(filePath, tempFilePath); - const abcFilePath = genAbcFileName(tempFilePath); - processToAbcFile(tempFilePath, abcFilePath); + if (process.env.processTs && process.env.processTs === 'true') { + tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); } else { - let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); - tempFilePath = toUnixPath(tempFilePath); - const parent: string = path.join(tempFilePath, '..'); - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent); - } - // fs.copyFileSync(filePath, tempFilePath); + tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); + } + tempFilePath = toUnixPath(tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } else if (filePath.endsWith('ts')) { + if (process.env.processTs && process.env.processTs === 'false') { + tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); + } + tempFilePath = toUnixPath(tempFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } else if (filePath.endsWith('js')) { + tempFilePath = toUnixPath(tempFilePath); + const parent: string = path.join(tempFilePath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + if (tempFilePath.indexOf("node_modules") !== -1) { const abcFilePath = genAbcFileName(tempFilePath); - processToAbcFile(tempFilePath, abcFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } else { + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, true); + moduleInfos.push(tempModuleInfo); } } else { console.error("=========================handleFinishModules else==================="); console.error(filePath); } } - }) + }); + + invokeWorkersModuleToGenAbc(moduleInfos); + } function genAbcFileName(temporaryFile: string): string { @@ -249,6 +273,75 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { return result; } +function invokeWorkersModuleToGenAbc(moduleInfos: Array) { + let param: string = ''; + if (isDebug) { + param += ' --debug'; + } + + let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); + if (isWin) { + js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); + } else if (isMac) { + js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + } + + const maxWorkerNumber = 3; + const splitedBundles = splitModuleBySize(moduleInfos, maxWorkerNumber); + + const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; + const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; + + const clusterNewApiVersion = 16; + const currentNodeVersion = parseInt(process.version.split('.')[0]); + const useNewApi = currentNodeVersion >= clusterNewApiVersion; + + if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { + if (useNewApi) { + cluster.setupPrimary({ + exec: path.resolve(__dirname, genModuleAbcScript) + }); + } else { + cluster.setupMaster({ + exec: path.resolve(__dirname, genModuleAbcScript) + }); + } + + for (let i = 0; i < workerNumber; ++i) { + const workerData = { + 'inputs': JSON.stringify(splitedBundles[i]), + 'cmd': cmdPrefix + }; + cluster.fork(workerData); + } + + cluster.on('exit', (worker, code, signal) => { + logger.debug(`worker ${worker.process.pid} finished`); + }); + + process.on('exit', (code) => { + writeHashJson(); + }); + } +} + +function splitModuleBySize(moduleInfos: Array, groupNumber: number) { + const result = []; + if (moduleInfos.length < groupNumber) { + result.push(moduleInfos); + return result; + } + + for (let i = 0; i < groupNumber; ++i) { + result.push([]); + } + for (let i =0;i { + const inputPaths = JSON.parse(jsonInput); + for (let i = 0; i < inputPaths.length; ++i) { + const input = inputPaths[i].buildFilePath; + const abcFilePathPath = inputPaths[i].abcFilePath; + const singleCmd = `${cmd} "${input}" -o "${abcFilePathPath}"`; + logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); + try { + childProcess.execSync(singleCmd); + } catch (e) { + logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); + return; + } + } +} + +logger.debug('worker data is: ', JSON.stringify(process.env)); +logger.debug('gen_abc isWorker is: ', cluster.isWorker); +if (cluster.isWorker && process.env['inputs'] !== undefined && process.env['cmd'] !== undefined) { + logger.debug('==>worker #', cluster.worker.id, 'started!'); + js2abcByWorkers(process.env['inputs'], process.env['cmd']); + process.exit(); +} diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index b47450f..b0e61af 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -243,18 +243,29 @@ export function writeFileSync(filePath: string, content: string): void { export function genTemporaryPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); projectPath = toUnixPath(projectPath); + let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let tempFilePath = filePath.replace(hapPath, ""); - if (filePath.indexOf('node_modules') !== -1) { - const dataTmps = filePath.split('node_modules'); - const preStr = dataTmps[0]; - const sufStr = dataTmps[1]; - const output: string = path.join(buildPath, 'node_modules', sufStr); + if (tempFilePath.indexOf('node_modules') !== -1) { + const dataTmps = tempFilePath.split('node_modules'); + let output:string = ""; + if (filePath.indexOf(projectPath) === -1) { + const sufStr = dataTmps[dataTmps.length-1]; + output = path.join(buildPath, 'main', 'node_modules', sufStr); + } else { + const sufStr = dataTmps[dataTmps.length-1]; + output = path.join(buildPath, 'auxiliary', 'node_modules', sufStr); + } return output; } - const sufStr = filePath.replace(projectPath, ""); - const output: string = path.join(buildPath, sufStr); - return output; + if (filePath.indexOf(projectPath) !== -1) { + const sufStr = filePath.replace(projectPath, ""); + const output: string = path.join(buildPath, sufStr); + return output; + } + + return ""; } export function mkdirsSync(dirname: string): boolean { @@ -271,6 +282,9 @@ export function mkdirsSync(dirname: string): boolean { export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + if (temporaryFile.length === 0) { + return ; + } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, sourceCode); } @@ -324,6 +338,9 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } const sourceMapContent = JSON.stringify(sourceMapJson); let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + if (temporaryFile.length === 0) { + return ; + } let temporarySourceMapFile: string = ""; if (temporaryFile.endsWith("ets")) { temporarySourceMapFile = temporaryFile.replace(/\.ets$/, '.ets.sourcemap'); From bd1a0eb2f78bde0dae10901cf47f70a1f706c068 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Mon, 9 May 2022 22:03:13 +0800 Subject: [PATCH 18/34] hash incre compile Signed-off-by: zhangrengao Change-Id: I8f88cfac258bc89a4ad5fe8ca07ef74d69122275 --- compiler/src/gen_abc_plugin.ts | 83 ++++++++++++++++++++++++++++++---- compiler/src/utils.ts | 2 +- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 1da7db9..ee3edf7 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -43,7 +43,10 @@ interface File { } const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; +let moduleInfos = new Array(); +let filterModuleInfos = new Array(); let hashJsonObject = {}; +let moduleHashJsonObject = {}; let buildPathInfo = ''; const red: string = '\u001b[31m'; @@ -52,10 +55,10 @@ const hashFile = 'gen_hash.json'; const ARK = '/ark/'; class ModuleInfo { - protected filePath: string; - protected buildFilePath: string; - protected abcFilePath: string; - protected isModuleJs: boolean; + filePath: string; + buildFilePath: string; + abcFilePath: string; + isModuleJs: boolean; constructor(filePath: string, buildFilePath: string, abcFilePath: string, isModuleJs: boolean) { this.filePath = filePath; @@ -87,6 +90,7 @@ export class GenAbcPlugin { } compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { + buildPathInfo = output; compilation.hooks.finishModules.tap("finishModules", handleFinishModules.bind(this)); }); @@ -130,8 +134,7 @@ export class GenAbcPlugin { } function handleFinishModules(modules, callback) { - console.error("==================handleFinishModules===================="); - let moduleInfos = new Array(); + console.error("==================handleFinishModules===================="); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; @@ -286,9 +289,9 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); } + filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); const maxWorkerNumber = 3; - const splitedBundles = splitModuleBySize(moduleInfos, maxWorkerNumber); - + const splitedBundles = splitModuleBySize(filterModuleInfos, maxWorkerNumber); const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; @@ -320,7 +323,7 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { }); process.on('exit', (code) => { - writeHashJson(); + writeModuleHashJson(); }); } } @@ -394,6 +397,68 @@ function invokeWorkersToGenAbc() { } } +function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Array) { + for (let i = 0; i < moduleInfos.length; ++i) { + filterModuleInfos.push(moduleInfos[i]); + } + const hashFilePath = genHashJsonPath(buildPath); + if (hashFilePath.length === 0) { + return; + } + const updateJsonObject = {}; + let jsonObject = {}; + let jsonFile = ''; + if (fs.existsSync(hashFilePath)) { + jsonFile = fs.readFileSync(hashFilePath).toString(); + jsonObject = JSON.parse(jsonFile); + filterModuleInfos = []; + for (let i = 0; i < moduleInfos.length; ++i) { + const input = moduleInfos[i].buildFilePath; + const abcPath = moduleInfos[i].abcFilePath; + if (!fs.existsSync(input)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + if (fs.existsSync(abcPath)) { + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); + if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { + updateJsonObject[input] = hashInputContentData; + updateJsonObject[abcPath] = hashAbcContentData; + // fs.unlinkSync(input); + } else { + filterModuleInfos.push(moduleInfos[i]); + } + } else { + filterModuleInfos.push(moduleInfos[i]); + } + } + } + + moduleHashJsonObject = updateJsonObject; +} + +function writeModuleHashJson() { + for (let i = 0; i < filterModuleInfos.length; ++i) { + const input = filterModuleInfos[i].buildFilePath; + const abcPath = filterModuleInfos[i].abcFilePath; + if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); + moduleHashJsonObject[input] = hashInputContentData; + moduleHashJsonObject[abcPath] = hashAbcContentData; + // fs.unlinkSync(input); + } + const hashFilePath = genHashJsonPath(buildPathInfo); + if (hashFilePath.length === 0) { + return; + } + fs.writeFileSync(hashFilePath, JSON.stringify(moduleHashJsonObject)); +} + function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: File[]) { for (let i = 0; i < inputPaths.length; ++i) { fileterIntermediateJsBundle.push(inputPaths[i]); diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index b0e61af..b73124d 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -249,7 +249,7 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat if (tempFilePath.indexOf('node_modules') !== -1) { const dataTmps = tempFilePath.split('node_modules'); let output:string = ""; - if (filePath.indexOf(projectPath) === -1) { + if (filePath.indexOf(hapPath) === -1) { const sufStr = dataTmps[dataTmps.length-1]; output = path.join(buildPath, 'main', 'node_modules', sufStr); } else { From 84a78cf259efe1a112b006a46d48383b98072697 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sat, 14 May 2022 14:25:52 +0800 Subject: [PATCH 19/34] multi js to abc Signed-off-by: zhangrengao Change-Id: I08b4d3b84055cd959f4c31755501b24ba97a53b7 --- compiler/src/gen_abc_plugin.ts | 84 +++++++++++++++++++++------------- compiler/src/gen_module_abc.ts | 35 ++++++++++---- compiler/src/utils.ts | 42 ++++++++++++++++- 3 files changed, 118 insertions(+), 43 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index ee3edf7..b224093 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -23,7 +23,10 @@ import { logger } from './compile_info'; import { toUnixPath, toHashData, - genTemporaryPath } from './utils'; + genTemporaryPath, + genBuilldPath, + genAbcFileName, + mkdirsSync} from './utils'; import { Compilation } from 'webpack'; import { projectConfig } from '../main'; @@ -56,12 +59,14 @@ const ARK = '/ark/'; class ModuleInfo { filePath: string; + tempFilePath: string; buildFilePath: string; abcFilePath: string; isModuleJs: boolean; - constructor(filePath: string, buildFilePath: string, abcFilePath: string, isModuleJs: boolean) { + constructor(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, isModuleJs: boolean) { this.filePath = filePath; + this.tempFilePath = tempFilePath; this.buildFilePath = buildFilePath; this.abcFilePath = abcFilePath; this.isModuleJs = isModuleJs; @@ -126,7 +131,7 @@ export class GenAbcPlugin { compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { buildPathInfo = output; - invokeWorkersToGenAbc(); + // invokeWorkersToGenAbc(); }); @@ -134,46 +139,51 @@ export class GenAbcPlugin { } function handleFinishModules(modules, callback) { - console.error("==================handleFinishModules===================="); + console.error("==================handleFinishModules===================="); + let nodeModulesFile = new Array(); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; console.error(filePath); - let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, process.env.cachePath); + let buildFilePath = genBuilldPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = toUnixPath(tempFilePath); + buildFilePath = toUnixPath(buildFilePath); if (tempFilePath.length === 0) { return ; } if (filePath.endsWith('ets')) { if (process.env.processTs && process.env.processTs === 'true') { tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.ts'); } else { tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.js'); } - tempFilePath = toUnixPath(tempFilePath); const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } else if (filePath.endsWith('ts')) { if (process.env.processTs && process.env.processTs === 'false') { tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); + buildFilePath = buildFilePath.replace(/\.ts$/, '.ts.js'); } - tempFilePath = toUnixPath(tempFilePath); const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } else if (filePath.endsWith('js')) { - tempFilePath = toUnixPath(tempFilePath); const parent: string = path.join(tempFilePath, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { mkDir(parent); } if (tempFilePath.indexOf("node_modules") !== -1) { const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); } else { const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, abcFilePath, true); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); moduleInfos.push(tempModuleInfo); } } else { @@ -184,20 +194,11 @@ function handleFinishModules(modules, callback) { }); invokeWorkersModuleToGenAbc(moduleInfos); + processToAbcFile(nodeModulesFile); } -function genAbcFileName(temporaryFile: string): string { - let abcFile: string = temporaryFile; - if (temporaryFile.endsWith('ts')) { - abcFile = temporaryFile.replace(/\.ts$/, '.abc'); - } else { - abcFile = temporaryFile.replace(/\.js$/, '.abc'); - } - return abcFile; -} - -function processToAbcFile(tempFilePath, outPutFile) { +function processToAbcFile(nodeModulesFile: Array) { let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); if (isWin) { js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); @@ -207,14 +208,14 @@ function processToAbcFile(tempFilePath, outPutFile) { const args: string[] = [ '--expose-gc', - js2abc, - '-o', - outPutFile + js2abc ]; if (isDebug) { args.push('--debug'); } - args.push(tempFilePath); + nodeModulesFile.forEach(ele => { + args.push(ele); + }); child_process.execFileSync(nodeJs, args); } @@ -289,6 +290,9 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); } + if (fs.existsSync(buildPathInfo)) { + fs.rmdirSync(buildPathInfo, { recursive : true}); + } filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); const maxWorkerNumber = 3; const splitedBundles = splitModuleBySize(filterModuleInfos, maxWorkerNumber); @@ -379,6 +383,7 @@ function invokeWorkersToGenAbc() { }); } + var count_ = 0; for (let i = 0; i < workerNumber; ++i) { const workerData = { 'inputs': JSON.stringify(splitedBundles[i]), @@ -388,9 +393,15 @@ function invokeWorkersToGenAbc() { } cluster.on('exit', (worker, code, signal) => { + count_++; logger.debug(`worker ${worker.process.pid} finished`); }); + while(true) { + if (count_ === workerNumber) { + break; + } + } process.on('exit', (code) => { writeHashJson(); }); @@ -413,7 +424,7 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra jsonObject = JSON.parse(jsonFile); filterModuleInfos = []; for (let i = 0; i < moduleInfos.length; ++i) { - const input = moduleInfos[i].buildFilePath; + const input = moduleInfos[i].tempFilePath; const abcPath = moduleInfos[i].abcFilePath; if (!fs.existsSync(input)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); @@ -425,7 +436,11 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; - // fs.unlinkSync(input); + console.error("===============filterIntermediateModuleByHashJson copy==============") + console.error(moduleInfos[i].tempFilePath); + mkdirsSync(path.dirname(moduleInfos[i].buildFilePath)); + fs.copyFileSync(moduleInfos[i].tempFilePath, moduleInfos[i].buildFilePath); + fs.copyFileSync(genAbcFileName(moduleInfos[i].tempFilePath), genAbcFileName(moduleInfos[i].buildFilePath)); } else { filterModuleInfos.push(moduleInfos[i]); } @@ -439,8 +454,10 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra } function writeModuleHashJson() { + console.error("===============writeModuleHashJson==============") + console.error(filterModuleInfos); for (let i = 0; i < filterModuleInfos.length; ++i) { - const input = filterModuleInfos[i].buildFilePath; + const input = filterModuleInfos[i].tempFilePath; const abcPath = filterModuleInfos[i].abcFilePath; if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); @@ -450,6 +467,11 @@ function writeModuleHashJson() { const hashAbcContentData = toHashData(abcPath); moduleHashJsonObject[input] = hashInputContentData; moduleHashJsonObject[abcPath] = hashAbcContentData; + mkdirsSync(path.dirname(filterModuleInfos[i].buildFilePath)); + console.error("===============writeModuleHashJson copy==============") + console.error(filterModuleInfos[i].tempFilePath); + fs.copyFileSync(filterModuleInfos[i].tempFilePath, filterModuleInfos[i].buildFilePath); + fs.copyFileSync(genAbcFileName(filterModuleInfos[i].tempFilePath), genAbcFileName(filterModuleInfos[i].buildFilePath)); // fs.unlinkSync(input); } const hashFilePath = genHashJsonPath(buildPathInfo); diff --git a/compiler/src/gen_module_abc.ts b/compiler/src/gen_module_abc.ts index 20d8a33..ffe5323 100644 --- a/compiler/src/gen_module_abc.ts +++ b/compiler/src/gen_module_abc.ts @@ -24,18 +24,33 @@ const reset: string = '\u001b[39m'; function js2abcByWorkers(jsonInput: string, cmd: string): Promise { const inputPaths = JSON.parse(jsonInput); + let inputs = []; for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].buildFilePath; - const abcFilePathPath = inputPaths[i].abcFilePath; - const singleCmd = `${cmd} "${input}" -o "${abcFilePathPath}"`; - logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); - try { - childProcess.execSync(singleCmd); - } catch (e) { - logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); - return; - } + const input = inputPaths[i].tempFilePath; + inputs.push(input); + // const abcFilePathPath = inputPaths[i].abcFilePath; + // const singleCmd = `${cmd} "${input}" -o "${abcFilePathPath}"`; + // logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); + // try { + // console.error(singleCmd); + // childProcess.execSync(singleCmd); + // } catch (e) { + // logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); + // return; + // } } + let inputsStr = inputs.join(','); + const singleCmd = `${cmd} "${inputs}"`; + logger.debug('gen abc cmd is: ', singleCmd); + try { + console.error(singleCmd); + childProcess.execSync(singleCmd); + } catch (e) { + logger.error(red, `ETS:ERROR Failed to convert file ${inputs} to abc `, reset); + return; + } + + return ; } logger.debug('worker data is: ', JSON.stringify(process.env)); diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index b73124d..d3f218e 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -246,6 +246,34 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); let tempFilePath = filePath.replace(hapPath, ""); + if (tempFilePath.indexOf('node_modules') !== -1) { + const dataTmps = tempFilePath.split('node_modules'); + let output:string = ""; + if (filePath.indexOf(hapPath) === -1) { + const sufStr = dataTmps[dataTmps.length-1]; + output = path.join(buildPath, "temprary", 'main', 'node_modules', sufStr); + } else { + const sufStr = dataTmps[dataTmps.length-1]; + output = path.join(buildPath, "temprary", 'auxiliary', 'node_modules', sufStr); + } + return output; + } + + if (filePath.indexOf(projectPath) !== -1) { + const sufStr = filePath.replace(projectPath, ""); + const output: string = path.join(buildPath, "temprary", sufStr); + return output; + } + + return ""; +} + +export function genBuilldPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { + filePath = toUnixPath(filePath); + projectPath = toUnixPath(projectPath); + let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let tempFilePath = filePath.replace(hapPath, ""); + if (tempFilePath.indexOf('node_modules') !== -1) { const dataTmps = tempFilePath.split('node_modules'); let output:string = ""; @@ -281,7 +309,7 @@ export function mkdirsSync(dirname: string): boolean { } export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { - let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return ; } @@ -337,7 +365,7 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { content = result.replace(`${TS_NOCHECK};`, TS_NOCHECK); } const sourceMapContent = JSON.stringify(sourceMapJson); - let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, projectConfig.buildPath, toTsFile); + let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return ; } @@ -362,3 +390,13 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { fs.writeFileSync(temporarySourceMapFile, sourceMapContent); } } + +export function genAbcFileName(temporaryFile: string): string { + let abcFile: string = temporaryFile; + if (temporaryFile.endsWith('ts')) { + abcFile = temporaryFile.replace(/\.ts$/, '.abc'); + } else { + abcFile = temporaryFile.replace(/\.js$/, '.abc'); + } + return abcFile; +} From 1590db82352a5116435e90c7edc5167c6af21edd Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 15 May 2022 14:21:02 +0800 Subject: [PATCH 20/34] fix multi abc bug Signed-off-by: zhangrengao Change-Id: Ia125560323166d642e2979e7864d649bbb45e842 --- compiler/src/gen_abc_plugin.ts | 49 +++++++++++++++++----------------- compiler/src/gen_module_abc.ts | 18 +++---------- compiler/src/utils.ts | 25 +++++++++++++---- compiler/webpack.config.js | 2 ++ 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index b224093..533cd65 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -26,7 +26,8 @@ import { genTemporaryPath, genBuilldPath, genAbcFileName, - mkdirsSync} from './utils'; + mkdirsSync, + genSourceMapFileName} from './utils'; import { Compilation } from 'webpack'; import { projectConfig } from '../main'; @@ -95,22 +96,29 @@ export class GenAbcPlugin { } compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { + if (process.env.moduleAbc === 'false') { + return ; + } buildPathInfo = output; compilation.hooks.finishModules.tap("finishModules", handleFinishModules.bind(this)); }); compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { - console.error("==================afterOptimizeModules===================="); + if (process.env.moduleAbc === 'false') { + return ; + } modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; - console.error(filePath); } }); }); compilation.hooks.processAssets.tap("processAssets", (assets) => { + if (process.env.moduleAbc === 'false') { + return ; + } Object.keys(compilation.assets).forEach(key => { delete assets[key]; }) @@ -119,6 +127,9 @@ export class GenAbcPlugin { }) compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { + if (process.env.moduleAbc === 'true') { + return ; + } Object.keys(compilation.assets).forEach(key => { // choose *.js if (output && path.extname(key) === '.js') { @@ -130,8 +141,11 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { + if (process.env.moduleAbc === 'true') { + return ; + } buildPathInfo = output; - // invokeWorkersToGenAbc(); + invokeWorkersToGenAbc(); }); @@ -139,12 +153,10 @@ export class GenAbcPlugin { } function handleFinishModules(modules, callback) { - console.error("==================handleFinishModules===================="); let nodeModulesFile = new Array(); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; - console.error(filePath); let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, process.env.cachePath); let buildFilePath = genBuilldPath(filePath, projectConfig.projectPath, projectConfig.buildPath); tempFilePath = toUnixPath(tempFilePath); @@ -187,15 +199,12 @@ function handleFinishModules(modules, callback) { moduleInfos.push(tempModuleInfo); } } else { - console.error("=========================handleFinishModules else==================="); - console.error(filePath); + console.error("ETS error: Cannot find resolve this path: ", filePath); } } }); invokeWorkersModuleToGenAbc(moduleInfos); - processToAbcFile(nodeModulesFile); - } function processToAbcFile(nodeModulesFile: Array) { @@ -383,7 +392,6 @@ function invokeWorkersToGenAbc() { }); } - var count_ = 0; for (let i = 0; i < workerNumber; ++i) { const workerData = { 'inputs': JSON.stringify(splitedBundles[i]), @@ -393,15 +401,9 @@ function invokeWorkersToGenAbc() { } cluster.on('exit', (worker, code, signal) => { - count_++; logger.debug(`worker ${worker.process.pid} finished`); }); - while(true) { - if (count_ === workerNumber) { - break; - } - } process.on('exit', (code) => { writeHashJson(); }); @@ -436,11 +438,12 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; - console.error("===============filterIntermediateModuleByHashJson copy==============") - console.error(moduleInfos[i].tempFilePath); mkdirsSync(path.dirname(moduleInfos[i].buildFilePath)); fs.copyFileSync(moduleInfos[i].tempFilePath, moduleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(moduleInfos[i].tempFilePath), genAbcFileName(moduleInfos[i].buildFilePath)); + if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); + } } else { filterModuleInfos.push(moduleInfos[i]); } @@ -454,8 +457,6 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra } function writeModuleHashJson() { - console.error("===============writeModuleHashJson==============") - console.error(filterModuleInfos); for (let i = 0; i < filterModuleInfos.length; ++i) { const input = filterModuleInfos[i].tempFilePath; const abcPath = filterModuleInfos[i].abcFilePath; @@ -468,11 +469,11 @@ function writeModuleHashJson() { moduleHashJsonObject[input] = hashInputContentData; moduleHashJsonObject[abcPath] = hashAbcContentData; mkdirsSync(path.dirname(filterModuleInfos[i].buildFilePath)); - console.error("===============writeModuleHashJson copy==============") - console.error(filterModuleInfos[i].tempFilePath); fs.copyFileSync(filterModuleInfos[i].tempFilePath, filterModuleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(filterModuleInfos[i].tempFilePath), genAbcFileName(filterModuleInfos[i].buildFilePath)); - // fs.unlinkSync(input); + if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); + } } const hashFilePath = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { diff --git a/compiler/src/gen_module_abc.ts b/compiler/src/gen_module_abc.ts index ffe5323..601cca8 100644 --- a/compiler/src/gen_module_abc.ts +++ b/compiler/src/gen_module_abc.ts @@ -27,26 +27,16 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { let inputs = []; for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].tempFilePath; - inputs.push(input); - // const abcFilePathPath = inputPaths[i].abcFilePath; - // const singleCmd = `${cmd} "${input}" -o "${abcFilePathPath}"`; - // logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); - // try { - // console.error(singleCmd); - // childProcess.execSync(singleCmd); - // } catch (e) { - // logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); - // return; - // } + inputs.push('"' + input + '"'); } - let inputsStr = inputs.join(','); - const singleCmd = `${cmd} "${inputs}"`; + let inputsStr = inputs.join(' '); + const singleCmd = `${cmd} ${inputsStr}`; logger.debug('gen abc cmd is: ', singleCmd); try { console.error(singleCmd); childProcess.execSync(singleCmd); } catch (e) { - logger.error(red, `ETS:ERROR Failed to convert file ${inputs} to abc `, reset); + logger.error(red, `ETS:ERROR Failed to convert file ${inputsStr} to abc `, reset); return; } diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index d3f218e..f6a067e 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -309,6 +309,9 @@ export function mkdirsSync(dirname: string): boolean { } export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { + if (process.env.moduleAbc === 'false') { + return ; + } let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return ; @@ -318,6 +321,9 @@ export function writeFileSyncByString(sourcePath: string, sourceCode: string, to } export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { + if (process.env.moduleAbc === 'false') { + return ; + } if (toTsFile) { const newStatements = []; const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); @@ -371,22 +377,21 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } let temporarySourceMapFile: string = ""; if (temporaryFile.endsWith("ets")) { - temporarySourceMapFile = temporaryFile.replace(/\.ets$/, '.ets.sourcemap'); if (toTsFile) { temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.ts'); } else { temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.js'); - } + } + temporarySourceMapFile = genSourceMapFileName(temporaryFile); } else { if (!toTsFile) { - temporarySourceMapFile = temporaryFile.replace(/\.ts$/, '.ts.sourcemap'); temporaryFile = temporaryFile.replace(/\.ts$/, '.ts.js'); + temporarySourceMapFile = genSourceMapFileName(temporaryFile); } } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, content); - if (temporarySourceMapFile.length > 0) { - + if (temporarySourceMapFile.length > 0 && process.env.buildMode === "debug") { fs.writeFileSync(temporarySourceMapFile, sourceMapContent); } } @@ -400,3 +405,13 @@ export function genAbcFileName(temporaryFile: string): string { } return abcFile; } + +export function genSourceMapFileName(temporaryFile: string): string { + let abcFile: string = temporaryFile; + if (temporaryFile.endsWith('ts')) { + abcFile = temporaryFile.replace(/\.ts$/, '.sourcemap'); + } else { + abcFile = temporaryFile.replace(/\.js$/, '.sourcemap'); + } + return abcFile; +} diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 6cf9052..f1bd168 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -313,7 +313,9 @@ module.exports = (env, argv) => { setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); + process.env.moduleAbc = false; process.env.processTs = false; + process.env.buildMode = env.buildMode; if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile); From 4085f936009f24d562350134f7a6a2ce4e427100 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 17 May 2022 19:12:01 +0800 Subject: [PATCH 21/34] featrue of distinguish cjs and esm Signed-off-by: zhangrengao Change-Id: Id5527fc4dd637f30f43fab2aec24b83bb93d7125 --- compiler/src/gen_abc_plugin.ts | 224 ++++++++++++++++++++++++++------- compiler/src/utils.ts | 22 +++- 2 files changed, 193 insertions(+), 53 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 533cd65..49e7843 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -49,6 +49,9 @@ const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; let moduleInfos = new Array(); let filterModuleInfos = new Array(); +let commonJsModuleInfos = new Array(); +let ESMModuleInfos = new Array(); +let entryInfos = new Map(); let hashJsonObject = {}; let moduleHashJsonObject = {}; let buildPathInfo = ''; @@ -63,14 +66,26 @@ class ModuleInfo { tempFilePath: string; buildFilePath: string; abcFilePath: string; - isModuleJs: boolean; + isCommonJs: boolean; - constructor(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, isModuleJs: boolean) { + constructor(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, isCommonJs: boolean) { this.filePath = filePath; this.tempFilePath = tempFilePath; this.buildFilePath = buildFilePath; this.abcFilePath = abcFilePath; - this.isModuleJs = isModuleJs; + this.isCommonJs = isCommonJs; + } +} + +class EntryInfo { + npmInfo: string; + abcFileName: string; + buildPath: string; + + constructor(npmInfo: string, abcFileName: string, buildPath: string) { + this.npmInfo = npmInfo; + this.abcFileName = abcFileName; + this.buildPath = buildPath; } } @@ -152,6 +167,98 @@ export class GenAbcPlugin { } } +function getEntryInfo(tempFilePath: string, packageJsonPath: string) { + let npmInfoPath = path.resolve(packageJsonPath, ".."); + let fakeEntryPath = path.resolve(npmInfoPath, "fake.js"); + let tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); + let buildFakeEntryPath = genBuilldPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); + npmInfoPath = toUnixPath(path.resolve(tempFakeEntryPath, "..")); + let buildNpmInfoPath = toUnixPath(path.resolve(buildFakeEntryPath, "..")); + if (entryInfos.has(npmInfoPath)) { + return ; + } + + let npmInfoPaths = npmInfoPath.split("node_modules"); + let npmInfo = ["node_modules", npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); + npmInfo = toUnixPath(npmInfo); + let abcFileName = genAbcFileName(tempFilePath); + let abcFilePaths = abcFileName.split("node_modules"); + abcFileName = ["node_modules", abcFilePaths[abcFilePaths.length - 1]].join(path.sep); + abcFileName = toUnixPath(abcFileName); + let entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath); + entryInfos.set(npmInfoPath, entryInfo); +} + +function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { + if (process.env.processTs && process.env.processTs === 'true') { + tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.ts'); + } else { + tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.js'); + } + const abcFilePath = genAbcFileName(tempFilePath); + if (tempFilePath.indexOf("node_modules") !== -1) { + getEntryInfo(tempFilePath, module.resourceResolveData.descriptionFilePath); + let descriptionFileData = module.resourceResolveData.descriptionFileData + if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } else { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } + } else { + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } +} + +function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { + if (process.env.processTs && process.env.processTs === 'false') { + tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); + buildFilePath = buildFilePath.replace(/\.ts$/, '.ts.js'); + } + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); +} + +function processJsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { + const parent: string = path.join(tempFilePath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + if (filePath.endsWith('mjs') || filePath.endsWith('cjs')) { + fs.copyFileSync(filePath, tempFilePath); + } + if (tempFilePath.indexOf("node_modules") !== -1) { + getEntryInfo(tempFilePath, module.resourceResolveData.descriptionFilePath); + const abcFilePath = genAbcFileName(tempFilePath); + let descriptionFileData = module.resourceResolveData.descriptionFileData + if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } else if (filePath.endsWith('mjs')) { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } else { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } + } else { + const abcFilePath = genAbcFileName(tempFilePath); + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } +} + function handleFinishModules(modules, callback) { let nodeModulesFile = new Array(); modules.forEach(module => { @@ -165,39 +272,11 @@ function handleFinishModules(modules, callback) { return ; } if (filePath.endsWith('ets')) { - if (process.env.processTs && process.env.processTs === 'true') { - tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.ts'); - } else { - tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.js'); - } - const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); + processEtsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else if (filePath.endsWith('ts')) { - if (process.env.processTs && process.env.processTs === 'false') { - tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); - buildFilePath = buildFilePath.replace(/\.ts$/, '.ts.js'); - } - const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); - } else if (filePath.endsWith('js')) { - const parent: string = path.join(tempFilePath, '..'); - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent); - } - if (tempFilePath.indexOf("node_modules") !== -1) { - const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } else { - const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); - moduleInfos.push(tempModuleInfo); - } + processTsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); + } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { + processJsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else { console.error("ETS error: Cannot find resolve this path: ", filePath); } @@ -205,6 +284,30 @@ function handleFinishModules(modules, callback) { }); invokeWorkersModuleToGenAbc(moduleInfos); + processEntryToGenAbc(entryInfos); +} + +function processEntryToGenAbc(entryInfos: Map) { + let url2abc: string = path.join(arkDir, 'build', 'bin', 'url2abc'); + if (isWin) { + url2abc = path.join(arkDir, 'build-win', 'bin', 'url2abc'); + } else if (isMac) { + url2abc = path.join(arkDir, 'build-mac', 'bin', 'url2abc'); + } + + for(let [key, value] of entryInfos) { + let tempAbcFilePath = value.npmInfo + '.abc'; + let buildAbcFilePath = value.buildPath + '.abc'; + let args = []; + args.push(value.npmInfo); + args.push(value.abcFileName); + //child_process.execFileSync(url2abc, args); + fs.writeFileSync(tempAbcFilePath, "1234"); + if (!fs.existsSync(buildAbcFilePath)) { + mkDir(value.buildPath); + } + fs.copyFileSync(tempAbcFilePath, buildAbcFilePath); + } } function processToAbcFile(nodeModulesFile: Array) { @@ -287,11 +390,22 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { } function invokeWorkersModuleToGenAbc(moduleInfos: Array) { - let param: string = ''; - if (isDebug) { - param += ' --debug'; + if (fs.existsSync(buildPathInfo)) { + fs.rmdirSync(buildPathInfo, { recursive : true}); } + filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); + filterModuleInfos.forEach(moduleInfo => { + if (moduleInfo.isCommonJs) { + commonJsModuleInfos.push(moduleInfo); + } else { + ESMModuleInfos.push(moduleInfo); + } + }); + invokeCluterModuleToAbc(); +} + +function initAbcEnv() : string[] { let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); if (isWin) { js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); @@ -299,14 +413,19 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); } - if (fs.existsSync(buildPathInfo)) { - fs.rmdirSync(buildPathInfo, { recursive : true}); + const args: string[] = [ + '--expose-gc', + js2abc + ]; + if (isDebug) { + args.push('--debug'); } - filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); - const maxWorkerNumber = 3; - const splitedBundles = splitModuleBySize(filterModuleInfos, maxWorkerNumber); - const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; - const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; + + return args; +} + +function invokeCluterModuleToAbc() { + let abcArgs = initAbcEnv(); const clusterNewApiVersion = 16; const currentNodeVersion = parseInt(process.version.split('.')[0]); @@ -323,10 +442,19 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { }); } - for (let i = 0; i < workerNumber; ++i) { + if (commonJsModuleInfos.length > 0) { + let commonJsCmdPrefix = `${nodeJs} ${abcArgs.join(" ")}`; const workerData = { - 'inputs': JSON.stringify(splitedBundles[i]), - 'cmd': cmdPrefix + 'inputs': JSON.stringify(commonJsModuleInfos), + 'cmd': commonJsCmdPrefix + }; + cluster.fork(workerData); + } + if (ESMModuleInfos.length > 0) { + let ESMCmdPrefix = `${nodeJs} ${abcArgs.join(" ")}`; + const workerData = { + 'inputs': JSON.stringify(ESMModuleInfos), + 'cmd': ESMCmdPrefix }; cluster.fork(workerData); } diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index f6a067e..a3adb41 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -242,6 +242,12 @@ export function writeFileSync(filePath: string, content: string): void { export function genTemporaryPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); + if (filePath.endsWith("mjs")) { + filePath = filePath.replace(/\.mjs$/, '.js') + } + if (filePath.endsWith("cjs")) { + filePath = filePath.replace(/\.cjs$/, '.js') + } projectPath = toUnixPath(projectPath); let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); let tempFilePath = filePath.replace(hapPath, ""); @@ -251,10 +257,10 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat let output:string = ""; if (filePath.indexOf(hapPath) === -1) { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, "temprary", 'main', 'node_modules', sufStr); + output = path.join(buildPath, "temprary", 'node_modules', 'main', sufStr); } else { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, "temprary", 'auxiliary', 'node_modules', sufStr); + output = path.join(buildPath, "temprary", 'node_modules', 'auxiliary', sufStr); } return output; } @@ -270,6 +276,12 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat export function genBuilldPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); + if (filePath.endsWith("mjs")) { + filePath = filePath.replace(/\.mjs$/, '.js') + } + if (filePath.endsWith("cjs")) { + filePath = filePath.replace(/\.cjs$/, '.js') + } projectPath = toUnixPath(projectPath); let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); let tempFilePath = filePath.replace(hapPath, ""); @@ -279,10 +291,10 @@ export function genBuilldPath(filePath: string, projectPath: string, buildPath: let output:string = ""; if (filePath.indexOf(hapPath) === -1) { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, 'main', 'node_modules', sufStr); + output = path.join(buildPath, 'node_modules', 'main', sufStr); } else { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, 'auxiliary', 'node_modules', sufStr); + output = path.join(buildPath, 'node_modules', 'auxiliary', sufStr); } return output; } @@ -414,4 +426,4 @@ export function genSourceMapFileName(temporaryFile: string): string { abcFile = temporaryFile.replace(/\.js$/, '.sourcemap'); } return abcFile; -} +} \ No newline at end of file From 6af865700d6722bbabd5f6fd5432c3e3da23310e Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Wed, 18 May 2022 15:18:06 +0800 Subject: [PATCH 22/34] modify entry of node modules Signed-off-by: zhangrengao Change-Id: I23803951fe6679d075a5b2cdba7da02b4e60b6cd --- compiler/src/gen_abc_plugin.ts | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 49e7843..1e76cdf 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -81,11 +81,13 @@ class EntryInfo { npmInfo: string; abcFileName: string; buildPath: string; + entry: string; - constructor(npmInfo: string, abcFileName: string, buildPath: string) { + constructor(npmInfo: string, abcFileName: string, buildPath: string, entry: string) { this.npmInfo = npmInfo; this.abcFileName = abcFileName; this.buildPath = buildPath; + this.entry = entry; } } @@ -167,7 +169,12 @@ export class GenAbcPlugin { } } -function getEntryInfo(tempFilePath: string, packageJsonPath: string) { +function getEntryInfo(tempFilePath: string, resourceResolveData: any) { + if (!resourceResolveData.descriptionFilePath) { + return ; + } + let packageName = resourceResolveData.descriptionFileData['name']; + let packageJsonPath = resourceResolveData.descriptionFilePath; let npmInfoPath = path.resolve(packageJsonPath, ".."); let fakeEntryPath = path.resolve(npmInfoPath, "fake.js"); let tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); @@ -185,7 +192,11 @@ function getEntryInfo(tempFilePath: string, packageJsonPath: string) { let abcFilePaths = abcFileName.split("node_modules"); abcFileName = ["node_modules", abcFilePaths[abcFilePaths.length - 1]].join(path.sep); abcFileName = toUnixPath(abcFileName); - let entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath); + // let entry = resourceResolveData.descriptionFileData['main'] ?? ""; + let packagePaths = tempFilePath.split('node_modules'); + let entryPaths = packagePaths[packagePaths.length-1].split(packageName); + let entry = toUnixPath(entryPaths[entryPaths.length - 1]); + let entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); entryInfos.set(npmInfoPath, entryInfo); } @@ -199,7 +210,7 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: } const abcFilePath = genAbcFileName(tempFilePath); if (tempFilePath.indexOf("node_modules") !== -1) { - getEntryInfo(tempFilePath, module.resourceResolveData.descriptionFilePath); + getEntryInfo(tempFilePath, module.resourceResolveData); let descriptionFileData = module.resourceResolveData.descriptionFileData if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); @@ -236,7 +247,7 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: fs.copyFileSync(filePath, tempFilePath); } if (tempFilePath.indexOf("node_modules") !== -1) { - getEntryInfo(tempFilePath, module.resourceResolveData.descriptionFilePath); + getEntryInfo(tempFilePath, module.resourceResolveData); const abcFilePath = genAbcFileName(tempFilePath); let descriptionFileData = module.resourceResolveData.descriptionFileData if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { @@ -288,26 +299,15 @@ function handleFinishModules(modules, callback) { } function processEntryToGenAbc(entryInfos: Map) { - let url2abc: string = path.join(arkDir, 'build', 'bin', 'url2abc'); - if (isWin) { - url2abc = path.join(arkDir, 'build-win', 'bin', 'url2abc'); - } else if (isMac) { - url2abc = path.join(arkDir, 'build-mac', 'bin', 'url2abc'); - } - for(let [key, value] of entryInfos) { - let tempAbcFilePath = value.npmInfo + '.abc'; - let buildAbcFilePath = value.buildPath + '.abc'; - let args = []; - args.push(value.npmInfo); - args.push(value.abcFileName); - //child_process.execFileSync(url2abc, args); - fs.writeFileSync(tempAbcFilePath, "1234"); + let tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry')); + let buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry')); + fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { mkDir(value.buildPath); } fs.copyFileSync(tempAbcFilePath, buildAbcFilePath); - } + } } function processToAbcFile(nodeModulesFile: Array) { From e3e250b089bf3a8f16f588ddd41a14eae139f873 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 19 May 2022 10:18:58 +0800 Subject: [PATCH 23/34] switch of bundleless Signed-off-by: zhangrengao Change-Id: I070d30d2164cf9025e145ee4ce8201faa3a6c0db --- compiler/src/gen_abc_plugin.ts | 91 ++++++++++++++++++---------------- compiler/src/utils.ts | 12 +++++ compiler/webpack.config.js | 2 +- 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 1e76cdf..6a05d72 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -27,7 +27,8 @@ import { genBuilldPath, genAbcFileName, mkdirsSync, - genSourceMapFileName} from './utils'; + genSourceMapFileName, + checkNodeModulesFile} from './utils'; import { Compilation } from 'webpack'; import { projectConfig } from '../main'; @@ -113,7 +114,7 @@ export class GenAbcPlugin { } compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { - if (process.env.moduleAbc === 'false') { + if (process.env.bundleless === 'false') { return ; } buildPathInfo = output; @@ -122,7 +123,7 @@ export class GenAbcPlugin { compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { - if (process.env.moduleAbc === 'false') { + if (process.env.bundleless === 'false') { return ; } modules.forEach(module => { @@ -133,7 +134,7 @@ export class GenAbcPlugin { }); compilation.hooks.processAssets.tap("processAssets", (assets) => { - if (process.env.moduleAbc === 'false') { + if (process.env.bundleless === 'false') { return ; } Object.keys(compilation.assets).forEach(key => { @@ -144,7 +145,7 @@ export class GenAbcPlugin { }) compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { - if (process.env.moduleAbc === 'true') { + if (process.env.bundleless === 'true') { return ; } Object.keys(compilation.assets).forEach(key => { @@ -158,7 +159,7 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { - if (process.env.moduleAbc === 'true') { + if (process.env.bundleless === 'true') { return ; } buildPathInfo = output; @@ -200,6 +201,26 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { entryInfos.set(npmInfoPath, entryInfo); } +function processNodeModulesFile(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, nodeModulesFile: Array, module: any) { + getEntryInfo(tempFilePath, module.resourceResolveData); + let descriptionFileData = module.resourceResolveData.descriptionFileData + if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } else if (filePath.endsWith('mjs')) { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } else { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); + moduleInfos.push(tempModuleInfo); + nodeModulesFile.push(tempFilePath); + } + + return ; +} + function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { if (process.env.processTs && process.env.processTs === 'true') { tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); @@ -209,20 +230,9 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.js'); } const abcFilePath = genAbcFileName(tempFilePath); - if (tempFilePath.indexOf("node_modules") !== -1) { - getEntryInfo(tempFilePath, module.resourceResolveData); - let descriptionFileData = module.resourceResolveData.descriptionFileData - if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - const abcFilePath = genAbcFileName(tempFilePath); let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } @@ -234,8 +244,12 @@ function processTsModule(filePath: string, tempFilePath: string, buildFilePath: buildFilePath = buildFilePath.replace(/\.ts$/, '.ts.js'); } const abcFilePath = genAbcFileName(tempFilePath); - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); + } else { + let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + moduleInfos.push(tempModuleInfo); + } } function processJsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { @@ -246,25 +260,10 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: if (filePath.endsWith('mjs') || filePath.endsWith('cjs')) { fs.copyFileSync(filePath, tempFilePath); } - if (tempFilePath.indexOf("node_modules") !== -1) { - getEntryInfo(tempFilePath, module.resourceResolveData); - const abcFilePath = genAbcFileName(tempFilePath); - let descriptionFileData = module.resourceResolveData.descriptionFileData - if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } else if (filePath.endsWith('mjs')) { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); - moduleInfos.push(tempModuleInfo); - nodeModulesFile.push(tempFilePath); - } + const abcFilePath = genAbcFileName(tempFilePath); + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - const abcFilePath = genAbcFileName(tempFilePath); let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } @@ -289,7 +288,7 @@ function handleFinishModules(modules, callback) { } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { processJsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else { - console.error("ETS error: Cannot find resolve this path: ", filePath); + console.error("ETS error: Cannot find resolve this file path: ", filePath); } } }); @@ -300,8 +299,8 @@ function handleFinishModules(modules, callback) { function processEntryToGenAbc(entryInfos: Map) { for(let [key, value] of entryInfos) { - let tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry')); - let buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry')); + let tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry.txt')); + let buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry.txt')); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { mkDir(value.buildPath); @@ -443,7 +442,9 @@ function invokeCluterModuleToAbc() { } if (commonJsModuleInfos.length > 0) { - let commonJsCmdPrefix = `${nodeJs} ${abcArgs.join(" ")}`; + let tempAbcArgs = abcArgs.slice(0); + tempAbcArgs.push("-c"); + let commonJsCmdPrefix = `${nodeJs} ${tempAbcArgs.join(" ")}`; const workerData = { 'inputs': JSON.stringify(commonJsModuleInfos), 'cmd': commonJsCmdPrefix @@ -451,7 +452,9 @@ function invokeCluterModuleToAbc() { cluster.fork(workerData); } if (ESMModuleInfos.length > 0) { - let ESMCmdPrefix = `${nodeJs} ${abcArgs.join(" ")}`; + let tempAbcArgs = abcArgs.slice(0); + tempAbcArgs.push("-m"); + let ESMCmdPrefix = `${nodeJs} ${tempAbcArgs.join(" ")}`; const workerData = { 'inputs': JSON.stringify(ESMModuleInfos), 'cmd': ESMCmdPrefix diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index a3adb41..d7aa4e5 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -308,6 +308,18 @@ export function genBuilldPath(filePath: string, projectPath: string, buildPath: return ""; } + +export function checkNodeModulesFile(filePath: string, projectPath: string) { + projectPath = toUnixPath(projectPath); + let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let tempFilePath = filePath.replace(hapPath, ""); + if (tempFilePath.indexOf('node_modules') !== -1 && filePath.indexOf(projectPath) === -1) { + return true; + } + + return false; +} + export function mkdirsSync(dirname: string): boolean { if (fs.existsSync(dirname)) { return true; diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index f1bd168..ba05ff5 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -313,7 +313,7 @@ module.exports = (env, argv) => { setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); - process.env.moduleAbc = false; + process.env.bundleless = false; process.env.processTs = false; process.env.buildMode = env.buildMode; From d32c0b62068f90c63538d876994ce42f4c477a81 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 22 May 2022 02:37:43 +0800 Subject: [PATCH 24/34] fix bugs of bundleLess Signed-off-by: zhangrengao Change-Id: I254424e9b5ffa0ceeac44ac63a83b8b91194a3d1 --- compiler/main.js | 14 ++++++++ compiler/src/gen_abc_plugin.ts | 59 +++++++++++++++++++------------ compiler/src/gen_module_abc.ts | 1 - compiler/src/process_js_ast.ts | 3 +- compiler/src/process_js_file.ts | 5 +-- compiler/src/process_ui_syntax.ts | 9 +++-- compiler/src/utils.ts | 58 +++++++++++++++++------------- compiler/webpack.config.js | 10 +++--- 8 files changed, 100 insertions(+), 59 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 8e9d8cb..6efcbda 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -337,6 +337,19 @@ function hashProjectPath(projectPath) { return process.env.hashProjectPath; } +function loadModuleInfo(projectConfig, envArgs) { + if (projectConfig.aceBuildJson && fs.existsSync(projectConfig.aceBuildJson)) { + const buildJsonInfo = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); + projectConfig.bundleLess = buildJsonInfo.bundleLess; + projectConfig.projectRootPath = buildJsonInfo.projectRootPath; + projectConfig.modulePathMap = buildJsonInfo.modulePathMap; + projectConfig.processTs = false; + projectConfig.buildArkMode = envArgs.buildMode; + projectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; + } + return ; +} + const globalProgram = { program: null, watchProgram: null @@ -350,3 +363,4 @@ exports.resources = resources; exports.loadWorker = loadWorker; exports.abilityConfig = abilityConfig; exports.readWorkerFile = readWorkerFile; +exports.loadModuleInfo = loadModuleInfo; diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 6a05d72..5bb45a7 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -24,7 +24,7 @@ import { toUnixPath, toHashData, genTemporaryPath, - genBuilldPath, + genBuildPath, genAbcFileName, mkdirsSync, genSourceMapFileName, @@ -114,7 +114,7 @@ export class GenAbcPlugin { } compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { - if (process.env.bundleless === 'false') { + if (projectConfig.bundleLess === false) { return ; } buildPathInfo = output; @@ -123,7 +123,7 @@ export class GenAbcPlugin { compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { - if (process.env.bundleless === 'false') { + if (projectConfig.bundleLess === false) { return ; } modules.forEach(module => { @@ -134,18 +134,18 @@ export class GenAbcPlugin { }); compilation.hooks.processAssets.tap("processAssets", (assets) => { - if (process.env.bundleless === 'false') { + if (projectConfig.bundleLess === false) { return ; } Object.keys(compilation.assets).forEach(key => { delete assets[key]; }) - }) + }); }) compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { - if (process.env.bundleless === 'true') { + if (projectConfig.bundleLess === true) { return ; } Object.keys(compilation.assets).forEach(key => { @@ -159,7 +159,7 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { - if (process.env.bundleless === 'true') { + if (projectConfig.bundleLess === true) { return ; } buildPathInfo = output; @@ -179,7 +179,7 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { let npmInfoPath = path.resolve(packageJsonPath, ".."); let fakeEntryPath = path.resolve(npmInfoPath, "fake.js"); let tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); - let buildFakeEntryPath = genBuilldPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); + let buildFakeEntryPath = genBuildPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); npmInfoPath = toUnixPath(path.resolve(tempFakeEntryPath, "..")); let buildNpmInfoPath = toUnixPath(path.resolve(buildFakeEntryPath, "..")); if (entryInfos.has(npmInfoPath)) { @@ -197,6 +197,9 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { let packagePaths = tempFilePath.split('node_modules'); let entryPaths = packagePaths[packagePaths.length-1].split(packageName); let entry = toUnixPath(entryPaths[entryPaths.length - 1]); + if (entry.startsWith("/")) { + entry = entry.slice(1, entry.length); + } let entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); entryInfos.set(npmInfoPath, entryInfo); } @@ -222,12 +225,12 @@ function processNodeModulesFile(filePath: string, tempFilePath: string, buildFil } function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { - if (process.env.processTs && process.env.processTs === 'true') { - tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.ts'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.ts'); + if (projectConfig.processTs === true) { + tempFilePath = tempFilePath.replace(/\.ets$/, '.ts'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.ts'); } else { - tempFilePath = tempFilePath.replace(/\.ets$/, '.ets.js'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.ets.js'); + tempFilePath = tempFilePath.replace(/\.ets$/, '.js'); + buildFilePath = buildFilePath.replace(/\.ets$/, '.js'); } const abcFilePath = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { @@ -238,10 +241,14 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: } } +function processDtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { + return ; +} + function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { - if (process.env.processTs && process.env.processTs === 'false') { - tempFilePath = tempFilePath.replace(/\.ts$/, '.ts.js'); - buildFilePath = buildFilePath.replace(/\.ts$/, '.ts.js'); + if (projectConfig.processTs === false) { + tempFilePath = tempFilePath.replace(/\.ts$/, '.js'); + buildFilePath = buildFilePath.replace(/\.ts$/, '.js'); } const abcFilePath = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { @@ -275,14 +282,16 @@ function handleFinishModules(modules, callback) { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, process.env.cachePath); - let buildFilePath = genBuilldPath(filePath, projectConfig.projectPath, projectConfig.buildPath); - tempFilePath = toUnixPath(tempFilePath); - buildFilePath = toUnixPath(buildFilePath); if (tempFilePath.length === 0) { return ; } + let buildFilePath = genBuildPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + tempFilePath = toUnixPath(tempFilePath); + buildFilePath = toUnixPath(buildFilePath); if (filePath.endsWith('ets')) { processEtsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); + } else if (filePath.endsWith('d.ts')) { + processDtsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else if (filePath.endsWith('ts')) { processTsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { @@ -303,7 +312,10 @@ function processEntryToGenAbc(entryInfos: Map) { let buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry.txt')); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { - mkDir(value.buildPath); + const parent: string = path.join(buildAbcFilePath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } } fs.copyFileSync(tempAbcFilePath, buildAbcFilePath); } @@ -392,6 +404,9 @@ function invokeWorkersModuleToGenAbc(moduleInfos: Array) { if (fs.existsSync(buildPathInfo)) { fs.rmdirSync(buildPathInfo, { recursive : true}); } + if (fs.existsSync(projectConfig.nodeModulesPath)) { + fs.rmdirSync(projectConfig.nodeModulesPath, { recursive : true}); + } filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); filterModuleInfos.forEach(moduleInfo => { if (moduleInfo.isCommonJs) { @@ -572,7 +587,7 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra mkdirsSync(path.dirname(moduleInfos[i].buildFilePath)); fs.copyFileSync(moduleInfos[i].tempFilePath, moduleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(moduleInfos[i].tempFilePath), genAbcFileName(moduleInfos[i].buildFilePath)); - if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + if (projectConfig.buildArkMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); } } else { @@ -602,7 +617,7 @@ function writeModuleHashJson() { mkdirsSync(path.dirname(filterModuleInfos[i].buildFilePath)); fs.copyFileSync(filterModuleInfos[i].tempFilePath, filterModuleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(filterModuleInfos[i].tempFilePath), genAbcFileName(filterModuleInfos[i].buildFilePath)); - if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + if (projectConfig.buildArkMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); } } diff --git a/compiler/src/gen_module_abc.ts b/compiler/src/gen_module_abc.ts index 601cca8..ad4ba7f 100644 --- a/compiler/src/gen_module_abc.ts +++ b/compiler/src/gen_module_abc.ts @@ -33,7 +33,6 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { const singleCmd = `${cmd} ${inputsStr}`; logger.debug('gen abc cmd is: ', singleCmd); try { - console.error(singleCmd); childProcess.execSync(singleCmd); } catch (e) { logger.error(red, `ETS:ERROR Failed to convert file ${inputsStr} to abc `, reset); diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts index b46290f..5ebfd1d 100644 --- a/compiler/src/process_js_ast.ts +++ b/compiler/src/process_js_ast.ts @@ -17,12 +17,13 @@ import ts from 'typescript'; import path from 'path'; import { BUILD_ON } from './pre_define'; import { writeFileSyncByNode } from './utils'; +import { projectConfig } from '../main'; export function processJs(program: ts.Program, ut = false): Function { return (context: ts.TransformationContext) => { return (node: ts.SourceFile) => { if (process.env.compiler === BUILD_ON) { - if (process.env.processTs && process.env.processTs === 'false') { + if (projectConfig.bundleLess === true && projectConfig.processTs === false) { writeFileSyncByNode(node, false); } return node; diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts index 1a1b1a8..3571e71 100644 --- a/compiler/src/process_js_file.ts +++ b/compiler/src/process_js_file.ts @@ -1,7 +1,8 @@ import { writeFileSyncByString } from './utils'; - +import { projectConfig } from '../main'; module.exports = function processjs2file(source: string): string { - if (process.env.compilerType && process.env.compilerType === 'ark'){ + if (projectConfig.bundleLess === true + && process.env.compilerType && process.env.compilerType === 'ark'){ writeFileSyncByString(this.resourcePath, source, false); } return source; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index b3c8ef2..340dc4c 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -68,7 +68,10 @@ import { localStorageLinkCollection, localStoragePropCollection } from './validate_ui_syntax'; -import { projectConfig, resources } from '../main'; +import { + resources, + projectConfig +} from '../main'; import { createCustomComponentNewExpression, createViewCreate } from './process_component_member'; export const transformLog: FileLog = new FileLog(); @@ -83,7 +86,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { if (process.env.compiler === BUILD_ON) { if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) { node = ts.visitEachChild(node, processResourceNode, context); - if (process.env.processTs && process.env.processTs === 'true') { + if (projectConfig.bundleLess === true && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; @@ -101,7 +104,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); - if (process.env.processTs && process.env.processTs === 'true') { + if (projectConfig.bundleLess === true && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index d7aa4e5..17f3d4f 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -249,13 +249,14 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat filePath = filePath.replace(/\.cjs$/, '.js') } projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let hapPath = toUnixPath(projectConfig.projectRootPath); let tempFilePath = filePath.replace(hapPath, ""); - if (tempFilePath.indexOf('node_modules') !== -1) { + if (checkNodeModulesFile(filePath, projectPath)) { + let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); const dataTmps = tempFilePath.split('node_modules'); let output:string = ""; - if (filePath.indexOf(hapPath) === -1) { + if (filePath.indexOf(fakeNodeModulesPath) === -1) { const sufStr = dataTmps[dataTmps.length-1]; output = path.join(buildPath, "temprary", 'node_modules', 'main', sufStr); } else { @@ -274,7 +275,7 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat return ""; } -export function genBuilldPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { +export function genBuildPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); if (filePath.endsWith("mjs")) { filePath = filePath.replace(/\.mjs$/, '.js') @@ -283,18 +284,20 @@ export function genBuilldPath(filePath: string, projectPath: string, buildPath: filePath = filePath.replace(/\.cjs$/, '.js') } projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let hapPath = toUnixPath(projectConfig.projectRootPath); let tempFilePath = filePath.replace(hapPath, ""); - if (tempFilePath.indexOf('node_modules') !== -1) { + if (checkNodeModulesFile(filePath, projectPath)) { + filePath = toUnixPath(filePath); + let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); const dataTmps = tempFilePath.split('node_modules'); let output:string = ""; - if (filePath.indexOf(hapPath) === -1) { + if (filePath.indexOf(fakeNodeModulesPath) === -1) { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, 'node_modules', 'main', sufStr); + output = path.join(projectConfig.nodeModulesPath, '0', sufStr); } else { const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, 'node_modules', 'auxiliary', sufStr); + output = path.join(projectConfig.nodeModulesPath, '1', sufStr); } return output; } @@ -310,11 +313,24 @@ export function genBuilldPath(filePath: string, projectPath: string, buildPath: export function checkNodeModulesFile(filePath: string, projectPath: string) { + filePath = toUnixPath(filePath); projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(path.resolve(projectPath, "../../../../../")); + let hapPath = toUnixPath(projectConfig.projectRootPath); let tempFilePath = filePath.replace(hapPath, ""); - if (tempFilePath.indexOf('node_modules') !== -1 && filePath.indexOf(projectPath) === -1) { - return true; + if (tempFilePath.indexOf('node_modules') !== -1) { + let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); + if (filePath.indexOf(fakeNodeModulesPath) !== -1) { + return true; + } + if (projectConfig.modulePathMap) { + for (let key in projectConfig.modulePathMap) { + let value = projectConfig.modulePathMap[key]; + let fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, "node_modules")); + if (filePath.indexOf(fakeModuleNodeModulesPath) !== -1) { + return true; + } + } + } } return false; @@ -333,9 +349,6 @@ export function mkdirsSync(dirname: string): boolean { } export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { - if (process.env.moduleAbc === 'false') { - return ; - } let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return ; @@ -345,9 +358,6 @@ export function writeFileSyncByString(sourcePath: string, sourceCode: string, to } export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { - if (process.env.moduleAbc === 'false') { - return ; - } if (toTsFile) { const newStatements = []; const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); @@ -402,20 +412,20 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { let temporarySourceMapFile: string = ""; if (temporaryFile.endsWith("ets")) { if (toTsFile) { - temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.ts'); + temporaryFile = temporaryFile.replace(/\.ets$/, '.ts'); } else { - temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.js'); + temporaryFile = temporaryFile.replace(/\.ets$/, '.js'); } temporarySourceMapFile = genSourceMapFileName(temporaryFile); } else { if (!toTsFile) { - temporaryFile = temporaryFile.replace(/\.ts$/, '.ts.js'); + temporaryFile = temporaryFile.replace(/\.ts$/, '.js'); temporarySourceMapFile = genSourceMapFileName(temporaryFile); } } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, content); - if (temporarySourceMapFile.length > 0 && process.env.buildMode === "debug") { + if (temporarySourceMapFile.length > 0 && projectConfig.buildArkMode === "debug") { fs.writeFileSync(temporarySourceMapFile, sourceMapContent); } } @@ -433,9 +443,9 @@ export function genAbcFileName(temporaryFile: string): string { export function genSourceMapFileName(temporaryFile: string): string { let abcFile: string = temporaryFile; if (temporaryFile.endsWith('ts')) { - abcFile = temporaryFile.replace(/\.ts$/, '.sourcemap'); + abcFile = temporaryFile.replace(/\.ts$/, '.map'); } else { - abcFile = temporaryFile.replace(/\.js$/, '.sourcemap'); + abcFile = temporaryFile.replace(/\.js$/, '.map'); } return abcFile; } \ No newline at end of file diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index ba05ff5..160ccc6 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -28,7 +28,8 @@ const { resources, loadWorker, abilityConfig, - readWorkerFile + readWorkerFile, + loadModuleInfo } = require('./main'); const { ResultStates } = require('./lib/compile_info'); const { processUISyntax } = require('./lib/process_ui_syntax'); @@ -307,16 +308,13 @@ module.exports = (env, argv) => { const config = {}; setProjectConfig(env); loadEntryObj(projectConfig); + loadModuleInfo(projectConfig, env); setTsConfigFile(); initConfig(config); const workerFile = readWorkerFile(); setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); - process.env.bundleless = false; - process.env.processTs = false; - process.env.buildMode = env.buildMode; - if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile); if (env.compilerType && env.compilerType === 'ark') { @@ -367,4 +365,4 @@ module.exports = (env, argv) => { } config.output.library = projectConfig.hashProjectPath; return config; -} \ No newline at end of file +} From 2ca90163df09fd8846d574a720c0d2d73bdbbc3f Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Mon, 30 May 2022 14:23:57 +0800 Subject: [PATCH 25/34] code check Signed-off-by: zhangrengao Change-Id: Ic52c099b193e5b2e729a01cc4e12da6e33eb5a1d --- compiler/main.js | 7 +- compiler/src/gen_abc_plugin.ts | 199 +++++++++++++----------------- compiler/src/gen_module_abc.ts | 7 +- compiler/src/process_js_ast.ts | 24 ++-- compiler/src/process_js_file.ts | 12 +- compiler/src/process_ui_syntax.ts | 4 +- compiler/src/utils.ts | 123 +++++++++--------- 7 files changed, 173 insertions(+), 203 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 6efcbda..eb9c1bc 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -340,14 +340,15 @@ function hashProjectPath(projectPath) { function loadModuleInfo(projectConfig, envArgs) { if (projectConfig.aceBuildJson && fs.existsSync(projectConfig.aceBuildJson)) { const buildJsonInfo = JSON.parse(fs.readFileSync(projectConfig.aceBuildJson).toString()); - projectConfig.bundleLess = buildJsonInfo.bundleLess; + projectConfig.compileMode = buildJsonInfo.compileMode; projectConfig.projectRootPath = buildJsonInfo.projectRootPath; projectConfig.modulePathMap = buildJsonInfo.modulePathMap; projectConfig.processTs = false; projectConfig.buildArkMode = envArgs.buildMode; - projectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; + if (buildJsonInfo.compileMode === 'esmodle') { + projectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; + } } - return ; } const globalProgram = { diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 5bb45a7..dcbfdac 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -17,19 +17,17 @@ import * as fs from 'fs'; import * as path from 'path'; import cluster from 'cluster'; import process from 'process'; -import * as child_process from 'child_process' import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; -import { +import { toUnixPath, toHashData, - genTemporaryPath, + genTemporaryPath, genBuildPath, - genAbcFileName, + genAbcFileName, mkdirsSync, genSourceMapFileName, checkNodeModulesFile} from './utils'; -import { Compilation } from 'webpack'; import { projectConfig } from '../main'; const firstFileEXT: string = '_.js'; @@ -48,11 +46,11 @@ interface File { } const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; -let moduleInfos = new Array(); -let filterModuleInfos = new Array(); -let commonJsModuleInfos = new Array(); -let ESMModuleInfos = new Array(); -let entryInfos = new Map(); +const moduleInfos: Array = [];; +let filterModuleInfos: Array = []; +const commonJsModuleInfos: Array = []; +const ESMModuleInfos: Array = []; +const entryInfos = new Map(); let hashJsonObject = {}; let moduleHashJsonObject = {}; let buildPathInfo = ''; @@ -70,7 +68,7 @@ class ModuleInfo { isCommonJs: boolean; constructor(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, isCommonJs: boolean) { - this.filePath = filePath; + this.filePath = filePath; this.tempFilePath = tempFilePath; this.buildFilePath = buildFilePath; this.abcFilePath = abcFilePath; @@ -113,40 +111,39 @@ export class GenAbcPlugin { } } - compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { - if (projectConfig.bundleLess === false) { - return ; + compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { + if (projectConfig.compileMode === 'jsbundle') { + return; } buildPathInfo = output; - compilation.hooks.finishModules.tap("finishModules", handleFinishModules.bind(this)); + compilation.hooks.finishModules.tap('finishModules', handleFinishModules.bind(this)); }); - compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { - compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { - if (projectConfig.bundleLess === false) { - return ; + compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { + compilation.hooks.afterOptimizeTree.tap('afterOptimizeModules', (chunks, modules) => { + if (projectConfig.compileMode === 'jsbundle') { + return; } modules.forEach(module => { - if (module != undefined && module.resourceResolveData != undefined) { - let filePath: string = module.resourceResolveData.path; - } + if (module !== undefined && module.resourceResolveData !== undefined) { + const filePath: string = module.resourceResolveData.path; + } + }); + }); + + compilation.hooks.processAssets.tap('processAssets', (assets) => { + if (projectConfig.compileMode === 'jsbundle') { + return; + } + Object.keys(compilation.assets).forEach(key => { + delete assets[key]; + }); }); }); - compilation.hooks.processAssets.tap("processAssets", (assets) => { - if (projectConfig.bundleLess === false) { - return ; - } - Object.keys(compilation.assets).forEach(key => { - delete assets[key]; - }) - }); - - }) - compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { - if (projectConfig.bundleLess === true) { - return ; + if (projectConfig.compileMode === 'esmodule') { + return; } Object.keys(compilation.assets).forEach(key => { // choose *.js @@ -159,69 +156,67 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { - if (projectConfig.bundleLess === true) { - return ; + if (projectConfig.compileMode === 'esmodule') { + return; } buildPathInfo = output; invokeWorkersToGenAbc(); }); - - } } function getEntryInfo(tempFilePath: string, resourceResolveData: any) { if (!resourceResolveData.descriptionFilePath) { - return ; + return; } - let packageName = resourceResolveData.descriptionFileData['name']; - let packageJsonPath = resourceResolveData.descriptionFilePath; - let npmInfoPath = path.resolve(packageJsonPath, ".."); - let fakeEntryPath = path.resolve(npmInfoPath, "fake.js"); - let tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); - let buildFakeEntryPath = genBuildPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); - npmInfoPath = toUnixPath(path.resolve(tempFakeEntryPath, "..")); - let buildNpmInfoPath = toUnixPath(path.resolve(buildFakeEntryPath, "..")); + const packageName = resourceResolveData.descriptionFileData['name']; + const packageJsonPath = resourceResolveData.descriptionFilePath; + let npmInfoPath = path.resolve(packageJsonPath, '..'); + const fakeEntryPath = path.resolve(npmInfoPath, 'fake.js'); + const tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); + const buildFakeEntryPath = genBuildPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); + npmInfoPath = toUnixPath(path.resolve(tempFakeEntryPath, '..')); + const buildNpmInfoPath = toUnixPath(path.resolve(buildFakeEntryPath, '..')); if (entryInfos.has(npmInfoPath)) { - return ; + return; } - let npmInfoPaths = npmInfoPath.split("node_modules"); - let npmInfo = ["node_modules", npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); + const npmInfoPaths = npmInfoPath.split('node_modules'); + let npmInfo = ['node_modules', npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); npmInfo = toUnixPath(npmInfo); let abcFileName = genAbcFileName(tempFilePath); - let abcFilePaths = abcFileName.split("node_modules"); - abcFileName = ["node_modules", abcFilePaths[abcFilePaths.length - 1]].join(path.sep); + const abcFilePaths = abcFileName.split('node_modules'); + abcFileName = ['node_modules', abcFilePaths[abcFilePaths.length - 1]].join(path.sep); abcFileName = toUnixPath(abcFileName); // let entry = resourceResolveData.descriptionFileData['main'] ?? ""; - let packagePaths = tempFilePath.split('node_modules'); - let entryPaths = packagePaths[packagePaths.length-1].split(packageName); + const packagePaths = tempFilePath.split('node_modules'); + const entryPaths = packagePaths[packagePaths.length - 1].split(packageName); let entry = toUnixPath(entryPaths[entryPaths.length - 1]); - if (entry.startsWith("/")) { + if (entry.startsWith('/')) { entry = entry.slice(1, entry.length); } - let entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); + const entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); entryInfos.set(npmInfoPath, entryInfo); } function processNodeModulesFile(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, nodeModulesFile: Array, module: any) { getEntryInfo(tempFilePath, module.resourceResolveData); - let descriptionFileData = module.resourceResolveData.descriptionFileData - if (descriptionFileData && descriptionFileData["type"] && descriptionFileData["type"] == "module") { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const descriptionFileData = module.resourceResolveData.descriptionFileData; + if (descriptionFileData && descriptionFileData['type'] && descriptionFileData['type'] === 'module') { + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); } else if (filePath.endsWith('mjs')) { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); } - return ; + return; } function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { @@ -233,16 +228,16 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: buildFilePath = buildFilePath.replace(/\.ets$/, '.js'); } const abcFilePath = genAbcFileName(tempFilePath); - if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } function processDtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { - return ; + return; } function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { @@ -251,10 +246,10 @@ function processTsModule(filePath: string, tempFilePath: string, buildFilePath: buildFilePath = buildFilePath.replace(/\.ts$/, '.js'); } const abcFilePath = genAbcFileName(tempFilePath); - if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } @@ -268,22 +263,22 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: fs.copyFileSync(filePath, tempFilePath); } const abcFilePath = genAbcFileName(tempFilePath); - if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { + if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - let tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } function handleFinishModules(modules, callback) { - let nodeModulesFile = new Array(); + const nodeModulesFile: Array = []; modules.forEach(module => { - if (module != undefined && module.resourceResolveData != undefined) { - let filePath: string = module.resourceResolveData.path; + if (module !== undefined && module.resourceResolveData !== undefined) { + const filePath: string = module.resourceResolveData.path; let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, process.env.cachePath); if (tempFilePath.length === 0) { - return ; + return; } let buildFilePath = genBuildPath(filePath, projectConfig.projectPath, projectConfig.buildPath); tempFilePath = toUnixPath(tempFilePath); @@ -297,7 +292,7 @@ function handleFinishModules(modules, callback) { } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { processJsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else { - console.error("ETS error: Cannot find resolve this file path: ", filePath); + console.error('ETS error: Cannot find resolve this file path: ', filePath); } } }); @@ -307,9 +302,9 @@ function handleFinishModules(modules, callback) { } function processEntryToGenAbc(entryInfos: Map) { - for(let [key, value] of entryInfos) { - let tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry.txt')); - let buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry.txt')); + for (const [key, value] of entryInfos) { + const tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry.txt')); + const buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry.txt')); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { const parent: string = path.join(buildAbcFilePath, '..'); @@ -321,28 +316,6 @@ function processEntryToGenAbc(entryInfos: Map) { } } -function processToAbcFile(nodeModulesFile: Array) { - let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); - if (isWin) { - js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); - } else if (isMac) { - js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); - } - - const args: string[] = [ - '--expose-gc', - js2abc - ]; - if (isDebug) { - args.push('--debug'); - } - nodeModulesFile.forEach(ele => { - args.push(ele); - }); - - child_process.execFileSync(nodeJs, args); -} - function writeFileSync(inputString: string, output: string, jsBundleFile: string): void { const parent: string = path.join(output, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { @@ -402,10 +375,10 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { function invokeWorkersModuleToGenAbc(moduleInfos: Array) { if (fs.existsSync(buildPathInfo)) { - fs.rmdirSync(buildPathInfo, { recursive : true}); + fs.rmdirSync(buildPathInfo, { recursive: true}); } if (fs.existsSync(projectConfig.nodeModulesPath)) { - fs.rmdirSync(projectConfig.nodeModulesPath, { recursive : true}); + fs.rmdirSync(projectConfig.nodeModulesPath, { recursive: true}); } filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); filterModuleInfos.forEach(moduleInfo => { @@ -439,7 +412,7 @@ function initAbcEnv() : string[] { } function invokeCluterModuleToAbc() { - let abcArgs = initAbcEnv(); + const abcArgs = initAbcEnv(); const clusterNewApiVersion = 16; const currentNodeVersion = parseInt(process.version.split('.')[0]); @@ -457,9 +430,9 @@ function invokeCluterModuleToAbc() { } if (commonJsModuleInfos.length > 0) { - let tempAbcArgs = abcArgs.slice(0); - tempAbcArgs.push("-c"); - let commonJsCmdPrefix = `${nodeJs} ${tempAbcArgs.join(" ")}`; + const tempAbcArgs = abcArgs.slice(0); + tempAbcArgs.push('-c'); + const commonJsCmdPrefix = `${nodeJs} ${tempAbcArgs.join(' ')}`; const workerData = { 'inputs': JSON.stringify(commonJsModuleInfos), 'cmd': commonJsCmdPrefix @@ -467,9 +440,9 @@ function invokeCluterModuleToAbc() { cluster.fork(workerData); } if (ESMModuleInfos.length > 0) { - let tempAbcArgs = abcArgs.slice(0); - tempAbcArgs.push("-m"); - let ESMCmdPrefix = `${nodeJs} ${tempAbcArgs.join(" ")}`; + const tempAbcArgs = abcArgs.slice(0); + tempAbcArgs.push('-m'); + const ESMCmdPrefix = `${nodeJs} ${tempAbcArgs.join(' ')}`; const workerData = { 'inputs': JSON.stringify(ESMModuleInfos), 'cmd': ESMCmdPrefix @@ -497,8 +470,8 @@ function splitModuleBySize(moduleInfos: Array, groupNumber: number) for (let i = 0; i < groupNumber; ++i) { result.push([]); } - for (let i =0;i { const inputPaths = JSON.parse(jsonInput); - let inputs = []; + const inputs = []; for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].tempFilePath; inputs.push('"' + input + '"'); } - let inputsStr = inputs.join(' '); + const inputsStr = inputs.join(' '); const singleCmd = `${cmd} ${inputsStr}`; logger.debug('gen abc cmd is: ', singleCmd); try { @@ -39,7 +38,7 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { return; } - return ; + return; } logger.debug('worker data is: ', JSON.stringify(process.env)); diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts index 5ebfd1d..afab6bf 100644 --- a/compiler/src/process_js_ast.ts +++ b/compiler/src/process_js_ast.ts @@ -14,23 +14,21 @@ */ import ts from 'typescript'; -import path from 'path'; import { BUILD_ON } from './pre_define'; import { writeFileSyncByNode } from './utils'; import { projectConfig } from '../main'; export function processJs(program: ts.Program, ut = false): Function { - return (context: ts.TransformationContext) => { - return (node: ts.SourceFile) => { - if (process.env.compiler === BUILD_ON) { - if (projectConfig.bundleLess === true && projectConfig.processTs === false) { - writeFileSyncByNode(node, false); - } - return node; - } else { - return node; + return (context: ts.TransformationContext) => { + return (node: ts.SourceFile) => { + if (process.env.compiler === BUILD_ON) { + if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === false) { + writeFileSyncByNode(node, false); } - + return node; + } else { + return node; } - } -} \ No newline at end of file + }; + }; +} diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts index 3571e71..5ad312b 100644 --- a/compiler/src/process_js_file.ts +++ b/compiler/src/process_js_file.ts @@ -1,9 +1,9 @@ import { writeFileSyncByString } from './utils'; import { projectConfig } from '../main'; module.exports = function processjs2file(source: string): string { - if (projectConfig.bundleLess === true - && process.env.compilerType && process.env.compilerType === 'ark'){ - writeFileSyncByString(this.resourcePath, source, false); - } - return source; - } \ No newline at end of file + if (projectConfig.compileMode === 'esmodule' + && process.env.compilerType && process.env.compilerType === 'ark') { + writeFileSyncByString(this.resourcePath, source, false); + } + return source; +}; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 340dc4c..0ec5d69 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -86,7 +86,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { if (process.env.compiler === BUILD_ON) { if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) { node = ts.visitEachChild(node, processResourceNode, context); - if (projectConfig.bundleLess === true && projectConfig.processTs === true) { + if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; @@ -104,7 +104,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); - if (projectConfig.bundleLess === true && projectConfig.processTs === true) { + if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 17f3d4f..96db597 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -242,90 +242,89 @@ export function writeFileSync(filePath: string, content: string): void { export function genTemporaryPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); - if (filePath.endsWith("mjs")) { - filePath = filePath.replace(/\.mjs$/, '.js') + if (filePath.endsWith('mjs')) { + filePath = filePath.replace(/\.mjs$/, '.js'); } - if (filePath.endsWith("cjs")) { - filePath = filePath.replace(/\.cjs$/, '.js') + if (filePath.endsWith('cjs')) { + filePath = filePath.replace(/\.cjs$/, '.js'); } projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(projectConfig.projectRootPath); - let tempFilePath = filePath.replace(hapPath, ""); + const hapPath = toUnixPath(projectConfig.projectRootPath); + const tempFilePath = filePath.replace(hapPath, ''); if (checkNodeModulesFile(filePath, projectPath)) { - let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); const dataTmps = tempFilePath.split('node_modules'); - let output:string = ""; + let output:string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { - const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, "temprary", 'node_modules', 'main', sufStr); + const sufStr = dataTmps[dataTmps.length - 1]; + output = path.join(buildPath, 'temprary', 'node_modules', 'main', sufStr); } else { - const sufStr = dataTmps[dataTmps.length-1]; - output = path.join(buildPath, "temprary", 'node_modules', 'auxiliary', sufStr); + const sufStr = dataTmps[dataTmps.length - 1]; + output = path.join(buildPath, 'temprary', 'node_modules', 'auxiliary', sufStr); } return output; } if (filePath.indexOf(projectPath) !== -1) { - const sufStr = filePath.replace(projectPath, ""); - const output: string = path.join(buildPath, "temprary", sufStr); + const sufStr = filePath.replace(projectPath, ''); + const output: string = path.join(buildPath, 'temprary', sufStr); return output; } - return ""; + return ''; } export function genBuildPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); - if (filePath.endsWith("mjs")) { - filePath = filePath.replace(/\.mjs$/, '.js') + if (filePath.endsWith('mjs')) { + filePath = filePath.replace(/\.mjs$/, '.js'); } - if (filePath.endsWith("cjs")) { - filePath = filePath.replace(/\.cjs$/, '.js') + if (filePath.endsWith('cjs')) { + filePath = filePath.replace(/\.cjs$/, '.js'); } projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(projectConfig.projectRootPath); - let tempFilePath = filePath.replace(hapPath, ""); + const hapPath = toUnixPath(projectConfig.projectRootPath); + const tempFilePath = filePath.replace(hapPath, ''); if (checkNodeModulesFile(filePath, projectPath)) { filePath = toUnixPath(filePath); - let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); const dataTmps = tempFilePath.split('node_modules'); - let output:string = ""; + let output:string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { - const sufStr = dataTmps[dataTmps.length-1]; + const sufStr = dataTmps[dataTmps.length - 1]; output = path.join(projectConfig.nodeModulesPath, '0', sufStr); } else { - const sufStr = dataTmps[dataTmps.length-1]; + const sufStr = dataTmps[dataTmps.length - 1]; output = path.join(projectConfig.nodeModulesPath, '1', sufStr); } return output; } if (filePath.indexOf(projectPath) !== -1) { - const sufStr = filePath.replace(projectPath, ""); + const sufStr = filePath.replace(projectPath, ''); const output: string = path.join(buildPath, sufStr); return output; } - return ""; + return ''; } - export function checkNodeModulesFile(filePath: string, projectPath: string) { filePath = toUnixPath(filePath); projectPath = toUnixPath(projectPath); - let hapPath = toUnixPath(projectConfig.projectRootPath); - let tempFilePath = filePath.replace(hapPath, ""); + const hapPath = toUnixPath(projectConfig.projectRootPath); + const tempFilePath = filePath.replace(hapPath, ''); if (tempFilePath.indexOf('node_modules') !== -1) { - let fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, "node_modules")); + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); if (filePath.indexOf(fakeNodeModulesPath) !== -1) { return true; } if (projectConfig.modulePathMap) { - for (let key in projectConfig.modulePathMap) { - let value = projectConfig.modulePathMap[key]; - let fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, "node_modules")); + for (const key in projectConfig.modulePathMap) { + const value = projectConfig.modulePathMap[key]; + const fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, 'node_modules')); if (filePath.indexOf(fakeModuleNodeModulesPath) !== -1) { return true; } @@ -349,9 +348,9 @@ export function mkdirsSync(dirname: string): boolean { } export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { - let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); + const temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { - return ; + return; } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, sourceCode); @@ -365,38 +364,38 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { if (node.statements && node.statements.length) { newStatements.push(...node.statements); } - - node = ts.factory.updateSourceFile(node, newStatements); + + node = ts.factory.updateSourceFile(node, newStatements); } const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); - let options : ts.CompilerOptions = { - sourceMap : true + const options : ts.CompilerOptions = { + sourceMap: true }; - let mapOpions = { - sourceMap : true, - inlineSourceMap : false, - inlineSources : false, - sourceRoot : "", - mapRoot : "", - extendedDiagnostics : false - } - let host = ts.createCompilerHost(options); - let fileName = node.fileName; + const mapOpions = { + sourceMap: true, + inlineSourceMap: false, + inlineSources: false, + sourceRoot: '', + mapRoot: '', + extendedDiagnostics: false + }; + const host = ts.createCompilerHost(options); + const fileName = node.fileName; // @ts-ignore - let sourceMapGenerator = ts.createSourceMapGenerator( + const sourceMapGenerator = ts.createSourceMapGenerator( host, // @ts-ignore ts.getBaseFileName(fileName), - "", - "", + '', + '', mapOpions ); // @ts-ignore - let writer = ts.createTextWriter( + const writer = ts.createTextWriter( // @ts-ignore - ts.getNewLineCharacter({newLine : ts.NewLineKind.LineFeed, removeComments : false})); - printer["writeFile"](node, writer, sourceMapGenerator); - let sourceMapJson = sourceMapGenerator.toJSON(); + ts.getNewLineCharacter({newLine: ts.NewLineKind.LineFeed, removeComments: false})); + printer['writeFile'](node, writer, sourceMapGenerator); + const sourceMapJson = sourceMapGenerator.toJSON(); sourceMapJson['sources'] = [fileName]; const result: string = writer.getText(); let content: string = result; @@ -407,10 +406,10 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { const sourceMapContent = JSON.stringify(sourceMapJson); let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { - return ; + return; } - let temporarySourceMapFile: string = ""; - if (temporaryFile.endsWith("ets")) { + let temporarySourceMapFile: string = ''; + if (temporaryFile.endsWith('ets')) { if (toTsFile) { temporaryFile = temporaryFile.replace(/\.ets$/, '.ts'); } else { @@ -425,7 +424,7 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, content); - if (temporarySourceMapFile.length > 0 && projectConfig.buildArkMode === "debug") { + if (temporarySourceMapFile.length > 0 && projectConfig.buildArkMode === 'debug') { fs.writeFileSync(temporarySourceMapFile, sourceMapContent); } } @@ -448,4 +447,4 @@ export function genSourceMapFileName(temporaryFile: string): string { abcFile = temporaryFile.replace(/\.js$/, '.map'); } return abcFile; -} \ No newline at end of file +} From 6919258a3492dd8562c0fdc4cee5f86a1feb5e4d Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 31 May 2022 21:06:56 +0800 Subject: [PATCH 26/34] validate compile mode Signed-off-by: zhangrengao Change-Id: I7c1ec5f8e946b0edc7f5e82d31085802fe628bbd --- compiler/src/gen_abc_plugin.ts | 38 +++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index dcbfdac..c361cb0 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -59,6 +59,10 @@ const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; const hashFile = 'gen_hash.json'; const ARK = '/ark/'; +const ESMODULE = 'esmodule'; +const JSBUNDLE = 'jsbundle'; +const NODE_MODULES = 'node_modules'; +const ENTRY_TXT = 'entry.txt'; class ModuleInfo { filePath: string; @@ -111,8 +115,12 @@ export class GenAbcPlugin { } } + if (projectConfig.compileMode === undefined || projectConfig.compileMode !== JSBUNDLE || projectConfig.compileMode !== ESMODULE) { + logger.error(red, `ETS:ERROR Compile Module is warong `, reset); + } + compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { - if (projectConfig.compileMode === 'jsbundle') { + if (projectConfig.compileMode === JSBUNDLE) { return; } buildPathInfo = output; @@ -121,7 +129,7 @@ export class GenAbcPlugin { compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { compilation.hooks.afterOptimizeTree.tap('afterOptimizeModules', (chunks, modules) => { - if (projectConfig.compileMode === 'jsbundle') { + if (projectConfig.compileMode === JSBUNDLE) { return; } modules.forEach(module => { @@ -132,17 +140,19 @@ export class GenAbcPlugin { }); compilation.hooks.processAssets.tap('processAssets', (assets) => { - if (projectConfig.compileMode === 'jsbundle') { + if (projectConfig.compileMode === JSBUNDLE) { return; } Object.keys(compilation.assets).forEach(key => { - delete assets[key]; + if (path.extname(key) === '.js') { + delete assets[key]; + } }); }); }); compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { - if (projectConfig.compileMode === 'esmodule') { + if (projectConfig.compileMode === ESMODULE) { return; } Object.keys(compilation.assets).forEach(key => { @@ -156,7 +166,7 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { - if (projectConfig.compileMode === 'esmodule') { + if (projectConfig.compileMode === ESMODULE) { return; } buildPathInfo = output; @@ -181,15 +191,15 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { return; } - const npmInfoPaths = npmInfoPath.split('node_modules'); - let npmInfo = ['node_modules', npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); + const npmInfoPaths = npmInfoPath.split(NODE_MODULES); + let npmInfo = [NODE_MODULES, npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); npmInfo = toUnixPath(npmInfo); let abcFileName = genAbcFileName(tempFilePath); - const abcFilePaths = abcFileName.split('node_modules'); - abcFileName = ['node_modules', abcFilePaths[abcFilePaths.length - 1]].join(path.sep); + const abcFilePaths = abcFileName.split(NODE_MODULES); + abcFileName = [NODE_MODULES, abcFilePaths[abcFilePaths.length - 1]].join(path.sep); abcFileName = toUnixPath(abcFileName); // let entry = resourceResolveData.descriptionFileData['main'] ?? ""; - const packagePaths = tempFilePath.split('node_modules'); + const packagePaths = tempFilePath.split(NODE_MODULES); const entryPaths = packagePaths[packagePaths.length - 1].split(packageName); let entry = toUnixPath(entryPaths[entryPaths.length - 1]); if (entry.startsWith('/')) { @@ -292,7 +302,7 @@ function handleFinishModules(modules, callback) { } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { processJsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else { - console.error('ETS error: Cannot find resolve this file path: ', filePath); + logger.error(red, `ETS:ERROR Cannot find resolve this file path: ${filePath}`, reset); } } }); @@ -303,8 +313,8 @@ function handleFinishModules(modules, callback) { function processEntryToGenAbc(entryInfos: Map) { for (const [key, value] of entryInfos) { - const tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, 'entry.txt')); - const buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, 'entry.txt')); + const tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, ENTRY_TXT)); + const buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, ENTRY_TXT)); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { const parent: string = path.join(buildAbcFilePath, '..'); From 3b11a23dc4e346f406dddb39e71fc5fda4ef0fe1 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 31 May 2022 21:45:19 +0800 Subject: [PATCH 27/34] add const info of string Signed-off-by: zhangrengao Change-Id: Ib7e49febb127d935bccdb2def14ceebf2dac9b3d --- compiler/src/gen_abc_plugin.ts | 47 ++++++++++-------- compiler/src/pre_define.ts | 17 +++++++ compiler/src/process_js_file.ts | 11 +++-- compiler/src/process_ui_syntax.ts | 7 +-- compiler/src/utils.ts | 81 +++++++++++++++++++------------ 5 files changed, 107 insertions(+), 56 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index c361cb0..366d575 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -29,6 +29,19 @@ import { genSourceMapFileName, checkNodeModulesFile} from './utils'; import { projectConfig } from '../main'; +import { + ESMODULE, + JSBUNDLE, + NODE_MODULES, + ENTRY_TXT, + EXTNAME_ETS, + EXTNAME_JS, + EXTNAME_TS, + EXTNAME_MJS, + EXTNAME_CJS, + EXTNAME_D_TS, + EXTNAME_ABC +} from './pre_define' const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; @@ -59,10 +72,6 @@ const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; const hashFile = 'gen_hash.json'; const ARK = '/ark/'; -const ESMODULE = 'esmodule'; -const JSBUNDLE = 'jsbundle'; -const NODE_MODULES = 'node_modules'; -const ENTRY_TXT = 'entry.txt'; class ModuleInfo { filePath: string; @@ -144,7 +153,7 @@ export class GenAbcPlugin { return; } Object.keys(compilation.assets).forEach(key => { - if (path.extname(key) === '.js') { + if (path.extname(key) === EXTNAME_JS) { delete assets[key]; } }); @@ -157,7 +166,7 @@ export class GenAbcPlugin { } Object.keys(compilation.assets).forEach(key => { // choose *.js - if (output && path.extname(key) === '.js') { + if (output && path.extname(key) === EXTNAME_JS) { const newContent: string = compilation.assets[key].source(); const keyPath: string = key.replace(/\.js$/, firstFileEXT); writeFileSync(newContent, path.resolve(output, keyPath), key); @@ -231,11 +240,11 @@ function processNodeModulesFile(filePath: string, tempFilePath: string, buildFil function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { if (projectConfig.processTs === true) { - tempFilePath = tempFilePath.replace(/\.ets$/, '.ts'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.ts'); + tempFilePath = tempFilePath.replace(/\.ets$/, EXTNAME_TS); + buildFilePath = buildFilePath.replace(/\.ets$/, EXTNAME_TS); } else { - tempFilePath = tempFilePath.replace(/\.ets$/, '.js'); - buildFilePath = buildFilePath.replace(/\.ets$/, '.js'); + tempFilePath = tempFilePath.replace(/\.ets$/, EXTNAME_JS); + buildFilePath = buildFilePath.replace(/\.ets$/, EXTNAME_JS); } const abcFilePath = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { @@ -252,8 +261,8 @@ function processDtsModule(filePath: string, tempFilePath: string, buildFilePath: function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { if (projectConfig.processTs === false) { - tempFilePath = tempFilePath.replace(/\.ts$/, '.js'); - buildFilePath = buildFilePath.replace(/\.ts$/, '.js'); + tempFilePath = tempFilePath.replace(/\.ts$/, EXTNAME_JS); + buildFilePath = buildFilePath.replace(/\.ts$/, EXTNAME_JS); } const abcFilePath = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { @@ -269,7 +278,7 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { mkDir(parent); } - if (filePath.endsWith('mjs') || filePath.endsWith('cjs')) { + if (filePath.endsWith(EXTNAME_MJS) || filePath.endsWith(EXTNAME_CJS)) { fs.copyFileSync(filePath, tempFilePath); } const abcFilePath = genAbcFileName(tempFilePath); @@ -293,13 +302,13 @@ function handleFinishModules(modules, callback) { let buildFilePath = genBuildPath(filePath, projectConfig.projectPath, projectConfig.buildPath); tempFilePath = toUnixPath(tempFilePath); buildFilePath = toUnixPath(buildFilePath); - if (filePath.endsWith('ets')) { + if (filePath.endsWith(EXTNAME_ETS)) { processEtsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); - } else if (filePath.endsWith('d.ts')) { + } else if (filePath.endsWith(EXTNAME_D_TS)) { processDtsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); - } else if (filePath.endsWith('ts')) { + } else if (filePath.endsWith(EXTNAME_TS)) { processTsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); - } else if (filePath.endsWith('js') || filePath.endsWith('mjs') || filePath.endsWith('cjs')) { + } else if (filePath.endsWith(EXTNAME_JS) || filePath.endsWith(EXTNAME_MJS) || filePath.endsWith(EXTNAME_CJS)) { processJsModule(filePath, tempFilePath, buildFilePath, nodeModulesFile, module); } else { logger.error(red, `ETS:ERROR Cannot find resolve this file path: ${filePath}`, reset); @@ -628,7 +637,7 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil fileterIntermediateJsBundle = []; for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].path; - const abcPath = input.replace(/_.js$/, '.abc'); + const abcPath = input.replace(/_.js$/, EXTNAME_ABC); if (!fs.existsSync(input)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; @@ -655,7 +664,7 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil function writeHashJson() { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { const input = fileterIntermediateJsBundle[i].path; - const abcPath = input.replace(/_.js$/, '.abc'); + const abcPath = input.replace(/_.js$/, EXTNAME_ABC); if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 1449851..5e5c527 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -235,3 +235,20 @@ export const COMPONENT_TOGGLE: string = 'Toggle'; export const TTOGGLE_CHECKBOX: string = 'Checkbox'; export const TOGGLE_SWITCH: string = 'Switch'; +export const ESMODULE: string = 'esmodule'; +export const JSBUNDLE: string = 'jsbundle'; +export const ENTRY_TXT: string = 'entry.txt'; +export const ARK: string = 'ark'; +export const TEMPRARY: string = 'temprary'; +export const MAIN: string = 'main'; +export const AUXILIARY: string = 'auxiliary'; +export const ZERO: string = '0'; +export const ONE: string = '1'; +export const EXTNAME_JS: string = '.js'; +export const EXTNAME_TS: string = '.ts'; +export const EXTNAME_JS_MAP: string = '.js.map'; +export const EXTNAME_TS_MAP: string = '.ts.map'; +export const EXTNAME_MJS: string = '.mjs'; +export const EXTNAME_CJS: string = '.cjs'; +export const EXTNAME_D_TS: string = '.d.ts'; +export const EXTNAME_ABC: string = '.abc'; \ No newline at end of file diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts index 5ad312b..08a7d16 100644 --- a/compiler/src/process_js_file.ts +++ b/compiler/src/process_js_file.ts @@ -1,9 +1,14 @@ import { writeFileSyncByString } from './utils'; import { projectConfig } from '../main'; +import { + ESMODULE, + ARK +} from './pre_define' + module.exports = function processjs2file(source: string): string { - if (projectConfig.compileMode === 'esmodule' - && process.env.compilerType && process.env.compilerType === 'ark') { + if (projectConfig.compileMode === ESMODULE + && process.env.compilerType && process.env.compilerType === ARK) { writeFileSyncByString(this.resourcePath, source, false); } return source; -}; +}; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 0ec5d69..2f54ebd 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -42,7 +42,8 @@ import { SET_CONTROLLER_CTR_TYPE, SET_CONTROLLER_METHOD, JS_DIALOG, - CUSTOM_DIALOG_CONTROLLER_BUILDER + CUSTOM_DIALOG_CONTROLLER_BUILDER, + ESMODULE } from './pre_define'; import { componentInfo, @@ -86,7 +87,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { if (process.env.compiler === BUILD_ON) { if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) { node = ts.visitEachChild(node, processResourceNode, context); - if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === true) { + if (projectConfig.compileMode === 'ESMODULE' && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; @@ -104,7 +105,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); - if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === true) { + if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === true) { writeFileSyncByNode(node, true); } return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 96db597..d180aa0 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -19,6 +19,25 @@ import fs from 'fs'; import { projectConfig } from '../main'; import { createHash } from 'crypto'; import { processSystemApi } from './validate_ui_syntax'; +import { + ESMODULE, + JSBUNDLE, + NODE_MODULES, + ENTRY_TXT, + TEMPRARY, + MAIN, + AUXILIARY, + ZERO, + ONE, + EXTNAME_JS, + EXTNAME_TS, + EXTNAME_MJS, + EXTNAME_CJS, + EXTNAME_ABC, + EXTNAME_ETS, + EXTNAME_TS_MAP, + EXTNAME_JS_MAP +} from './pre_define' export enum LogType { ERROR = 'ERROR', @@ -242,33 +261,33 @@ export function writeFileSync(filePath: string, content: string): void { export function genTemporaryPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); - if (filePath.endsWith('mjs')) { - filePath = filePath.replace(/\.mjs$/, '.js'); + if (filePath.endsWith(EXTNAME_MJS)) { + filePath = filePath.replace(/\.mjs$/, EXTNAME_JS); } - if (filePath.endsWith('cjs')) { - filePath = filePath.replace(/\.cjs$/, '.js'); + if (filePath.endsWith(EXTNAME_CJS)) { + filePath = filePath.replace(/\.cjs$/, EXTNAME_JS); } projectPath = toUnixPath(projectPath); const hapPath = toUnixPath(projectConfig.projectRootPath); const tempFilePath = filePath.replace(hapPath, ''); if (checkNodeModulesFile(filePath, projectPath)) { - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); - const dataTmps = tempFilePath.split('node_modules'); + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); + const dataTmps = tempFilePath.split(NODE_MODULES); let output:string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { const sufStr = dataTmps[dataTmps.length - 1]; - output = path.join(buildPath, 'temprary', 'node_modules', 'main', sufStr); + output = path.join(buildPath, TEMPRARY, NODE_MODULES, MAIN, sufStr); } else { const sufStr = dataTmps[dataTmps.length - 1]; - output = path.join(buildPath, 'temprary', 'node_modules', 'auxiliary', sufStr); + output = path.join(buildPath, TEMPRARY, NODE_MODULES, AUXILIARY, sufStr); } return output; } if (filePath.indexOf(projectPath) !== -1) { const sufStr = filePath.replace(projectPath, ''); - const output: string = path.join(buildPath, 'temprary', sufStr); + const output: string = path.join(buildPath, TEMPRARY, sufStr); return output; } @@ -277,11 +296,11 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat export function genBuildPath(filePath: string, projectPath: string, buildPath: string, toTsFile: boolean = true): string { filePath = toUnixPath(filePath); - if (filePath.endsWith('mjs')) { - filePath = filePath.replace(/\.mjs$/, '.js'); + if (filePath.endsWith(EXTNAME_MJS)) { + filePath = filePath.replace(/\.mjs$/, EXTNAME_JS); } - if (filePath.endsWith('cjs')) { - filePath = filePath.replace(/\.cjs$/, '.js'); + if (filePath.endsWith(EXTNAME_CJS)) { + filePath = filePath.replace(/\.cjs$/, EXTNAME_JS); } projectPath = toUnixPath(projectPath); const hapPath = toUnixPath(projectConfig.projectRootPath); @@ -289,15 +308,15 @@ export function genBuildPath(filePath: string, projectPath: string, buildPath: s if (checkNodeModulesFile(filePath, projectPath)) { filePath = toUnixPath(filePath); - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); - const dataTmps = tempFilePath.split('node_modules'); + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); + const dataTmps = tempFilePath.split(NODE_MODULES); let output:string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { const sufStr = dataTmps[dataTmps.length - 1]; - output = path.join(projectConfig.nodeModulesPath, '0', sufStr); + output = path.join(projectConfig.nodeModulesPath, ZERO, sufStr); } else { const sufStr = dataTmps[dataTmps.length - 1]; - output = path.join(projectConfig.nodeModulesPath, '1', sufStr); + output = path.join(projectConfig.nodeModulesPath, ONE, sufStr); } return output; } @@ -316,15 +335,15 @@ export function checkNodeModulesFile(filePath: string, projectPath: string) { projectPath = toUnixPath(projectPath); const hapPath = toUnixPath(projectConfig.projectRootPath); const tempFilePath = filePath.replace(hapPath, ''); - if (tempFilePath.indexOf('node_modules') !== -1) { - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, 'node_modules')); + if (tempFilePath.indexOf(NODE_MODULES) !== -1) { + const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); if (filePath.indexOf(fakeNodeModulesPath) !== -1) { return true; } if (projectConfig.modulePathMap) { for (const key in projectConfig.modulePathMap) { const value = projectConfig.modulePathMap[key]; - const fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, 'node_modules')); + const fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, NODE_MODULES)); if (filePath.indexOf(fakeModuleNodeModulesPath) !== -1) { return true; } @@ -409,16 +428,16 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { return; } let temporarySourceMapFile: string = ''; - if (temporaryFile.endsWith('ets')) { + if (temporaryFile.endsWith(EXTNAME_ETS)) { if (toTsFile) { - temporaryFile = temporaryFile.replace(/\.ets$/, '.ts'); + temporaryFile = temporaryFile.replace(/\.ets$/, EXTNAME_TS); } else { - temporaryFile = temporaryFile.replace(/\.ets$/, '.js'); + temporaryFile = temporaryFile.replace(/\.ets$/, EXTNAME_JS); } temporarySourceMapFile = genSourceMapFileName(temporaryFile); } else { if (!toTsFile) { - temporaryFile = temporaryFile.replace(/\.ts$/, '.js'); + temporaryFile = temporaryFile.replace(/\.ts$/, EXTNAME_JS); temporarySourceMapFile = genSourceMapFileName(temporaryFile); } } @@ -431,20 +450,20 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { export function genAbcFileName(temporaryFile: string): string { let abcFile: string = temporaryFile; - if (temporaryFile.endsWith('ts')) { - abcFile = temporaryFile.replace(/\.ts$/, '.abc'); + if (temporaryFile.endsWith(EXTNAME_TS)) { + abcFile = temporaryFile.replace(/\.ts$/, EXTNAME_ABC); } else { - abcFile = temporaryFile.replace(/\.js$/, '.abc'); + abcFile = temporaryFile.replace(/\.js$/, EXTNAME_ABC); } return abcFile; } export function genSourceMapFileName(temporaryFile: string): string { let abcFile: string = temporaryFile; - if (temporaryFile.endsWith('ts')) { - abcFile = temporaryFile.replace(/\.ts$/, '.map'); + if (temporaryFile.endsWith(EXTNAME_TS)) { + abcFile = temporaryFile.replace(/\.ts$/, EXTNAME_TS_MAP); } else { - abcFile = temporaryFile.replace(/\.js$/, '.map'); + abcFile = temporaryFile.replace(/\.js$/, EXTNAME_JS_MAP); } return abcFile; -} +} From bc5d70bf18db3f21ec7ec733ed8daaaabac88ab5 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Wed, 1 Jun 2022 09:44:51 +0800 Subject: [PATCH 28/34] fix mode bug Signed-off-by: zhangrengao Change-Id: I71ee388effbba83b8ed804b7a6ee21de0465bb20 --- compiler/src/gen_abc_plugin.ts | 10 +++------- compiler/src/process_js_ast.ts | 7 +++++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 366d575..9e36e89 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -124,12 +124,8 @@ export class GenAbcPlugin { } } - if (projectConfig.compileMode === undefined || projectConfig.compileMode !== JSBUNDLE || projectConfig.compileMode !== ESMODULE) { - logger.error(red, `ETS:ERROR Compile Module is warong `, reset); - } - compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { - if (projectConfig.compileMode === JSBUNDLE) { + if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; } buildPathInfo = output; @@ -138,7 +134,7 @@ export class GenAbcPlugin { compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { compilation.hooks.afterOptimizeTree.tap('afterOptimizeModules', (chunks, modules) => { - if (projectConfig.compileMode === JSBUNDLE) { + if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; } modules.forEach(module => { @@ -149,7 +145,7 @@ export class GenAbcPlugin { }); compilation.hooks.processAssets.tap('processAssets', (assets) => { - if (projectConfig.compileMode === JSBUNDLE) { + if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; } Object.keys(compilation.assets).forEach(key => { diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts index afab6bf..ecd3972 100644 --- a/compiler/src/process_js_ast.ts +++ b/compiler/src/process_js_ast.ts @@ -14,7 +14,10 @@ */ import ts from 'typescript'; -import { BUILD_ON } from './pre_define'; +import { + BUILD_ON, + ESMODULE + } from './pre_define'; import { writeFileSyncByNode } from './utils'; import { projectConfig } from '../main'; @@ -22,7 +25,7 @@ export function processJs(program: ts.Program, ut = false): Function { return (context: ts.TransformationContext) => { return (node: ts.SourceFile) => { if (process.env.compiler === BUILD_ON) { - if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === false) { + if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === false) { writeFileSyncByNode(node, false); } return node; From 6a69114ba5dff8f994ea3f464b9eac737fa4b1c0 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 2 Jun 2022 09:55:31 +0800 Subject: [PATCH 29/34] code check Signed-off-by: zhangrengao Change-Id: Id9e95266e0887d669602fb4078c28244f0f16f7b --- compiler/src/gen_abc_plugin.ts | 39 ++++----------------------------- compiler/src/pre_define.ts | 2 +- compiler/src/process_js_ast.ts | 2 +- compiler/src/process_js_file.ts | 2 +- compiler/src/utils.ts | 5 +---- 5 files changed, 8 insertions(+), 42 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 9e36e89..8b9d21f 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -41,7 +41,7 @@ import { EXTNAME_CJS, EXTNAME_D_TS, EXTNAME_ABC -} from './pre_define' +} from './pre_define'; const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; @@ -59,7 +59,7 @@ interface File { } const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; -const moduleInfos: Array = [];; +const moduleInfos: Array = []; let filterModuleInfos: Array = []; const commonJsModuleInfos: Array = []; const ESMModuleInfos: Array = []; @@ -133,17 +133,6 @@ export class GenAbcPlugin { }); compiler.hooks.compilation.tap('GenAbcPlugin', (compilation) => { - compilation.hooks.afterOptimizeTree.tap('afterOptimizeModules', (chunks, modules) => { - if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { - return; - } - modules.forEach(module => { - if (module !== undefined && module.resourceResolveData !== undefined) { - const filePath: string = module.resourceResolveData.path; - } - }); - }); - compilation.hooks.processAssets.tap('processAssets', (assets) => { if (projectConfig.compileMode === JSBUNDLE || projectConfig.compileMode === undefined) { return; @@ -196,14 +185,11 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { return; } - const npmInfoPaths = npmInfoPath.split(NODE_MODULES); - let npmInfo = [NODE_MODULES, npmInfoPaths[npmInfoPaths.length - 1]].join(path.sep); - npmInfo = toUnixPath(npmInfo); let abcFileName = genAbcFileName(tempFilePath); const abcFilePaths = abcFileName.split(NODE_MODULES); abcFileName = [NODE_MODULES, abcFilePaths[abcFilePaths.length - 1]].join(path.sep); abcFileName = toUnixPath(abcFileName); - // let entry = resourceResolveData.descriptionFileData['main'] ?? ""; + const packagePaths = tempFilePath.split(NODE_MODULES); const entryPaths = packagePaths[packagePaths.length - 1].split(packageName); let entry = toUnixPath(entryPaths[entryPaths.length - 1]); @@ -317,7 +303,7 @@ function handleFinishModules(modules, callback) { } function processEntryToGenAbc(entryInfos: Map) { - for (const [key, value] of entryInfos) { + for (const value of entryInfos.values()) { const tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, ENTRY_TXT)); const buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, ENTRY_TXT)); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); @@ -475,23 +461,6 @@ function invokeCluterModuleToAbc() { } } -function splitModuleBySize(moduleInfos: Array, groupNumber: number) { - const result = []; - if (moduleInfos.length < groupNumber) { - result.push(moduleInfos); - return result; - } - - for (let i = 0; i < groupNumber; ++i) { - result.push([]); - } - for (let i = 0; i < moduleInfos.length; i++) { - const pos = i % groupNumber; - result[pos].push(moduleInfos[i]); - } - return result; -} - function invokeWorkersToGenAbc() { let param: string = ''; if (isDebug) { diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 5e5c527..1740949 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -251,4 +251,4 @@ export const EXTNAME_TS_MAP: string = '.ts.map'; export const EXTNAME_MJS: string = '.mjs'; export const EXTNAME_CJS: string = '.cjs'; export const EXTNAME_D_TS: string = '.d.ts'; -export const EXTNAME_ABC: string = '.abc'; \ No newline at end of file +export const EXTNAME_ABC: string = '.abc'; diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts index ecd3972..acc8219 100644 --- a/compiler/src/process_js_ast.ts +++ b/compiler/src/process_js_ast.ts @@ -17,7 +17,7 @@ import ts from 'typescript'; import { BUILD_ON, ESMODULE - } from './pre_define'; +} from './pre_define'; import { writeFileSyncByNode } from './utils'; import { projectConfig } from '../main'; diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts index 08a7d16..5b8d700 100644 --- a/compiler/src/process_js_file.ts +++ b/compiler/src/process_js_file.ts @@ -3,7 +3,7 @@ import { projectConfig } from '../main'; import { ESMODULE, ARK -} from './pre_define' +} from './pre_define'; module.exports = function processjs2file(source: string): string { if (projectConfig.compileMode === ESMODULE diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index d180aa0..3e07e9a 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -20,10 +20,7 @@ import { projectConfig } from '../main'; import { createHash } from 'crypto'; import { processSystemApi } from './validate_ui_syntax'; import { - ESMODULE, - JSBUNDLE, NODE_MODULES, - ENTRY_TXT, TEMPRARY, MAIN, AUXILIARY, @@ -37,7 +34,7 @@ import { EXTNAME_ETS, EXTNAME_TS_MAP, EXTNAME_JS_MAP -} from './pre_define' +} from './pre_define'; export enum LogType { ERROR = 'ERROR', From 8d8303383972a700c7830db72e9c4c6866d2e76d Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Mon, 6 Jun 2022 10:43:41 +0800 Subject: [PATCH 30/34] fix pr issue Signed-off-by: zhangrengao Change-Id: I0815b0f0c28aeeebfad97b4fc6ced1e607163512 --- compiler/main.js | 2 +- compiler/src/gen_abc.ts | 6 +- compiler/src/gen_abc_plugin.ts | 171 +++++++++++++++--------------- compiler/src/gen_module_abc.ts | 10 +- compiler/src/process_js_ast.ts | 8 +- compiler/src/process_js_file.ts | 2 +- compiler/src/process_ui_syntax.ts | 9 +- compiler/src/utils.ts | 149 ++++++++++++++------------ compiler/webpack.config.js | 10 +- 9 files changed, 191 insertions(+), 176 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index eb9c1bc..367a3b9 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -345,7 +345,7 @@ function loadModuleInfo(projectConfig, envArgs) { projectConfig.modulePathMap = buildJsonInfo.modulePathMap; projectConfig.processTs = false; projectConfig.buildArkMode = envArgs.buildMode; - if (buildJsonInfo.compileMode === 'esmodle') { + if (buildJsonInfo.compileMode === 'esmodule') { projectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; } } diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 2c4be9d..055fa13 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -23,10 +23,10 @@ const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; function js2abcByWorkers(jsonInput: string, cmd: string): Promise { - const inputPaths = JSON.parse(jsonInput); + const inputPaths: any = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].path; - const singleCmd = `${cmd} "${input}"`; + const input: string = inputPaths[i].path; + const singleCmd: any = `${cmd} "${input}"`; logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { childProcess.execSync(singleCmd); diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 8b9d21f..dbe63b1 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -27,7 +27,8 @@ import { genAbcFileName, mkdirsSync, genSourceMapFileName, - checkNodeModulesFile} from './utils'; + checkNodeModulesFile +} from './utils'; import { projectConfig } from '../main'; import { ESMODULE, @@ -44,8 +45,8 @@ import { } from './pre_define'; const firstFileEXT: string = '_.js'; -const genAbcScript = 'gen_abc.js'; -const genModuleAbcScript = 'gen_module_abc.js'; +const genAbcScript: string = 'gen_abc.js'; +const genModuleAbcScript: string = 'gen_module_abc.js'; let output: string; let isWin: boolean = false; let isMac: boolean = false; @@ -63,15 +64,15 @@ const moduleInfos: Array = []; let filterModuleInfos: Array = []; const commonJsModuleInfos: Array = []; const ESMModuleInfos: Array = []; -const entryInfos = new Map(); +const entryInfos: Map = new Map(); let hashJsonObject = {}; let moduleHashJsonObject = {}; -let buildPathInfo = ''; +let buildPathInfo: string = ''; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; -const hashFile = 'gen_hash.json'; -const ARK = '/ark/'; +const hashFile: string = 'gen_hash.json'; +const ARK: string = '/ark/'; class ModuleInfo { filePath: string; @@ -173,46 +174,46 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { if (!resourceResolveData.descriptionFilePath) { return; } - const packageName = resourceResolveData.descriptionFileData['name']; - const packageJsonPath = resourceResolveData.descriptionFilePath; - let npmInfoPath = path.resolve(packageJsonPath, '..'); - const fakeEntryPath = path.resolve(npmInfoPath, 'fake.js'); - const tempFakeEntryPath = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); - const buildFakeEntryPath = genBuildPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); + const packageName: string = resourceResolveData.descriptionFileData['name']; + const packageJsonPath: string = resourceResolveData.descriptionFilePath; + let npmInfoPath: string = path.resolve(packageJsonPath, '..'); + const fakeEntryPath: string = path.resolve(npmInfoPath, 'fake.js'); + const tempFakeEntryPath: string = genTemporaryPath(fakeEntryPath, projectConfig.projectPath, process.env.cachePath); + const buildFakeEntryPath: string = genBuildPath(fakeEntryPath, projectConfig.projectPath, projectConfig.buildPath); npmInfoPath = toUnixPath(path.resolve(tempFakeEntryPath, '..')); - const buildNpmInfoPath = toUnixPath(path.resolve(buildFakeEntryPath, '..')); + const buildNpmInfoPath: string = toUnixPath(path.resolve(buildFakeEntryPath, '..')); if (entryInfos.has(npmInfoPath)) { return; } - let abcFileName = genAbcFileName(tempFilePath); - const abcFilePaths = abcFileName.split(NODE_MODULES); + let abcFileName: string = genAbcFileName(tempFilePath); + const abcFilePaths: string[] = abcFileName.split(NODE_MODULES); abcFileName = [NODE_MODULES, abcFilePaths[abcFilePaths.length - 1]].join(path.sep); abcFileName = toUnixPath(abcFileName); - const packagePaths = tempFilePath.split(NODE_MODULES); - const entryPaths = packagePaths[packagePaths.length - 1].split(packageName); - let entry = toUnixPath(entryPaths[entryPaths.length - 1]); + const packagePaths: string[] = tempFilePath.split(NODE_MODULES); + const entryPaths: string[] = packagePaths[packagePaths.length - 1].split(packageName); + let entry: string = toUnixPath(entryPaths[entryPaths.length - 1]); if (entry.startsWith('/')) { entry = entry.slice(1, entry.length); } - const entryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); + const entryInfo: EntryInfo = new EntryInfo(npmInfoPath, abcFileName, buildNpmInfoPath, entry); entryInfos.set(npmInfoPath, entryInfo); } function processNodeModulesFile(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, nodeModulesFile: Array, module: any) { getEntryInfo(tempFilePath, module.resourceResolveData); - const descriptionFileData = module.resourceResolveData.descriptionFileData; + const descriptionFileData: any = module.resourceResolveData.descriptionFileData; if (descriptionFileData && descriptionFileData['type'] && descriptionFileData['type'] === 'module') { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); - } else if (filePath.endsWith('mjs')) { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + } else if (filePath.endsWith(EXTNAME_MJS)) { + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); } else { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, true); moduleInfos.push(tempModuleInfo); nodeModulesFile.push(tempFilePath); } @@ -228,11 +229,11 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: tempFilePath = tempFilePath.replace(/\.ets$/, EXTNAME_JS); buildFilePath = buildFilePath.replace(/\.ets$/, EXTNAME_JS); } - const abcFilePath = genAbcFileName(tempFilePath); + const abcFilePath: string = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } @@ -246,11 +247,11 @@ function processTsModule(filePath: string, tempFilePath: string, buildFilePath: tempFilePath = tempFilePath.replace(/\.ts$/, EXTNAME_JS); buildFilePath = buildFilePath.replace(/\.ts$/, EXTNAME_JS); } - const abcFilePath = genAbcFileName(tempFilePath); + const abcFilePath: string = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } @@ -263,11 +264,11 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: if (filePath.endsWith(EXTNAME_MJS) || filePath.endsWith(EXTNAME_CJS)) { fs.copyFileSync(filePath, tempFilePath); } - const abcFilePath = genAbcFileName(tempFilePath); + const abcFilePath: string = genAbcFileName(tempFilePath); if (checkNodeModulesFile(filePath, projectConfig.projectPath)) { processNodeModulesFile(filePath, tempFilePath, buildFilePath, abcFilePath, nodeModulesFile, module); } else { - const tempModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); + const tempModuleInfo: ModuleInfo = new ModuleInfo(filePath, tempFilePath, buildFilePath, abcFilePath, false); moduleInfos.push(tempModuleInfo); } } @@ -281,7 +282,7 @@ function handleFinishModules(modules, callback) { if (tempFilePath.length === 0) { return; } - let buildFilePath = genBuildPath(filePath, projectConfig.projectPath, projectConfig.buildPath); + let buildFilePath: string = genBuildPath(filePath, projectConfig.projectPath, projectConfig.buildPath); tempFilePath = toUnixPath(tempFilePath); buildFilePath = toUnixPath(buildFilePath); if (filePath.endsWith(EXTNAME_ETS)) { @@ -304,8 +305,8 @@ function handleFinishModules(modules, callback) { function processEntryToGenAbc(entryInfos: Map) { for (const value of entryInfos.values()) { - const tempAbcFilePath = toUnixPath(path.resolve(value.npmInfo, ENTRY_TXT)); - const buildAbcFilePath = toUnixPath(path.resolve(value.buildPath, ENTRY_TXT)); + const tempAbcFilePath: string = toUnixPath(path.resolve(value.npmInfo, ENTRY_TXT)); + const buildAbcFilePath: string = toUnixPath(path.resolve(value.buildPath, ENTRY_TXT)); fs.writeFileSync(tempAbcFilePath, value.entry, 'utf-8'); if (!fs.existsSync(buildAbcFilePath)) { const parent: string = path.join(buildAbcFilePath, '..'); @@ -324,7 +325,7 @@ function writeFileSync(inputString: string, output: string, jsBundleFile: string } fs.writeFileSync(output, inputString); if (fs.existsSync(output)) { - const fileSize = fs.statSync(output).size; + const fileSize: any = fs.statSync(output).size; intermediateJsBundle.push({path: output, size: fileSize}); } else { logger.error(red, `ETS:ERROR Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, reset); @@ -340,7 +341,7 @@ function mkDir(path_: string): void { } function getSmallestSizeGroup(groupSize: Map) { - const groupSizeArray = Array.from(groupSize); + const groupSizeArray: any = Array.from(groupSize); groupSizeArray.sort(function(g1, g2) { return g1[1] - g2[1]; // sort by size }); @@ -348,7 +349,7 @@ function getSmallestSizeGroup(groupSize: Map) { } function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { - const result = []; + const result: any = []; if (bundleArray.length < groupNumber) { result.push(bundleArray); return result; @@ -357,7 +358,7 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { bundleArray.sort(function(f1: File, f2: File) { return f2.size - f1.size; }); - const groupFileSize = new Map(); + const groupFileSize: any = new Map(); for (let i = 0; i < groupNumber; ++i) { result.push([]); groupFileSize.set(i, 0); @@ -365,9 +366,9 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { let index = 0; while (index < bundleArray.length) { - const smallestGroup = getSmallestSizeGroup(groupFileSize); + const smallestGroup: any = getSmallestSizeGroup(groupFileSize); result[smallestGroup].push(bundleArray[index]); - const sizeUpdate = groupFileSize.get(smallestGroup) + bundleArray[index].size; + const sizeUpdate: any = groupFileSize.get(smallestGroup) + bundleArray[index].size; groupFileSize.set(smallestGroup, sizeUpdate); index++; } @@ -413,11 +414,11 @@ function initAbcEnv() : string[] { } function invokeCluterModuleToAbc() { - const abcArgs = initAbcEnv(); + const abcArgs: string[] = initAbcEnv(); - const clusterNewApiVersion = 16; - const currentNodeVersion = parseInt(process.version.split('.')[0]); - const useNewApi = currentNodeVersion >= clusterNewApiVersion; + const clusterNewApiVersion: number = 16; + const currentNodeVersion: number = parseInt(process.version.split('.')[0]); + const useNewApi: boolean = currentNodeVersion >= clusterNewApiVersion; if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { if (useNewApi) { @@ -431,20 +432,20 @@ function invokeCluterModuleToAbc() { } if (commonJsModuleInfos.length > 0) { - const tempAbcArgs = abcArgs.slice(0); + const tempAbcArgs: string[] = abcArgs.slice(0); tempAbcArgs.push('-c'); - const commonJsCmdPrefix = `${nodeJs} ${tempAbcArgs.join(' ')}`; - const workerData = { + const commonJsCmdPrefix: any = `${nodeJs} ${tempAbcArgs.join(' ')}`; + const workerData: any = { 'inputs': JSON.stringify(commonJsModuleInfos), 'cmd': commonJsCmdPrefix }; cluster.fork(workerData); } if (ESMModuleInfos.length > 0) { - const tempAbcArgs = abcArgs.slice(0); + const tempAbcArgs: string[] = abcArgs.slice(0); tempAbcArgs.push('-m'); - const ESMCmdPrefix = `${nodeJs} ${tempAbcArgs.join(' ')}`; - const workerData = { + const ESMCmdPrefix: any = `${nodeJs} ${tempAbcArgs.join(' ')}`; + const workerData: any = { 'inputs': JSON.stringify(ESMModuleInfos), 'cmd': ESMCmdPrefix }; @@ -475,14 +476,14 @@ function invokeWorkersToGenAbc() { } filterIntermediateJsBundleByHashJson(buildPathInfo, intermediateJsBundle); - const maxWorkerNumber = 3; - const splitedBundles = splitJsBundlesBySize(fileterIntermediateJsBundle, maxWorkerNumber); - const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; + const maxWorkerNumber: number = 3; + const splitedBundles: any[] = splitJsBundlesBySize(fileterIntermediateJsBundle, maxWorkerNumber); + const workerNumber: number = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; - const clusterNewApiVersion = 16; - const currentNodeVersion = parseInt(process.version.split('.')[0]); - const useNewApi = currentNodeVersion >= clusterNewApiVersion; + const clusterNewApiVersion: number = 16; + const currentNodeVersion: number = parseInt(process.version.split('.')[0]); + const useNewApi: boolean = currentNodeVersion >= clusterNewApiVersion; if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { if (useNewApi) { @@ -496,7 +497,7 @@ function invokeWorkersToGenAbc() { } for (let i = 0; i < workerNumber; ++i) { - const workerData = { + const workerData: any = { 'inputs': JSON.stringify(splitedBundles[i]), 'cmd': cmdPrefix }; @@ -517,27 +518,27 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra for (let i = 0; i < moduleInfos.length; ++i) { filterModuleInfos.push(moduleInfos[i]); } - const hashFilePath = genHashJsonPath(buildPath); + const hashFilePath: string = genHashJsonPath(buildPath); if (hashFilePath.length === 0) { return; } - const updateJsonObject = {}; - let jsonObject = {}; - let jsonFile = ''; + const updateJsonObject: any = {}; + let jsonObject: any = {}; + let jsonFile: string = ''; if (fs.existsSync(hashFilePath)) { jsonFile = fs.readFileSync(hashFilePath).toString(); jsonObject = JSON.parse(jsonFile); filterModuleInfos = []; for (let i = 0; i < moduleInfos.length; ++i) { - const input = moduleInfos[i].tempFilePath; - const abcPath = moduleInfos[i].abcFilePath; + const input: string = moduleInfos[i].tempFilePath; + const abcPath: string = moduleInfos[i].abcFilePath; if (!fs.existsSync(input)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; } if (fs.existsSync(abcPath)) { - const hashInputContentData = toHashData(input); - const hashAbcContentData = toHashData(abcPath); + const hashInputContentData: any = toHashData(input); + const hashAbcContentData: any = toHashData(abcPath); if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; @@ -561,14 +562,14 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra function writeModuleHashJson() { for (let i = 0; i < filterModuleInfos.length; ++i) { - const input = filterModuleInfos[i].tempFilePath; - const abcPath = filterModuleInfos[i].abcFilePath; + const input: string = filterModuleInfos[i].tempFilePath; + const abcPath: string = filterModuleInfos[i].abcFilePath; if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; } - const hashInputContentData = toHashData(input); - const hashAbcContentData = toHashData(abcPath); + const hashInputContentData: any = toHashData(input); + const hashAbcContentData: any = toHashData(abcPath); moduleHashJsonObject[input] = hashInputContentData; moduleHashJsonObject[abcPath] = hashAbcContentData; mkdirsSync(path.dirname(filterModuleInfos[i].buildFilePath)); @@ -578,7 +579,7 @@ function writeModuleHashJson() { fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); } } - const hashFilePath = genHashJsonPath(buildPathInfo); + const hashFilePath: string = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { return; } @@ -589,27 +590,27 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil for (let i = 0; i < inputPaths.length; ++i) { fileterIntermediateJsBundle.push(inputPaths[i]); } - const hashFilePath = genHashJsonPath(buildPath); + const hashFilePath: string = genHashJsonPath(buildPath); if (hashFilePath.length === 0) { return; } - const updateJsonObject = {}; - let jsonObject = {}; - let jsonFile = ''; + const updateJsonObject: any = {}; + let jsonObject: any = {}; + let jsonFile: string = ''; if (fs.existsSync(hashFilePath)) { jsonFile = fs.readFileSync(hashFilePath).toString(); jsonObject = JSON.parse(jsonFile); fileterIntermediateJsBundle = []; for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].path; - const abcPath = input.replace(/_.js$/, EXTNAME_ABC); + const input: string = inputPaths[i].path; + const abcPath: string = input.replace(/_.js$/, EXTNAME_ABC); if (!fs.existsSync(input)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; } if (fs.existsSync(abcPath)) { - const hashInputContentData = toHashData(input); - const hashAbcContentData = toHashData(abcPath); + const hashInputContentData: any = toHashData(input); + const hashAbcContentData: any = toHashData(abcPath); if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; @@ -628,19 +629,19 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil function writeHashJson() { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { - const input = fileterIntermediateJsBundle[i].path; - const abcPath = input.replace(/_.js$/, EXTNAME_ABC); + const input:string = fileterIntermediateJsBundle[i].path; + const abcPath: string = input.replace(/_.js$/, EXTNAME_ABC); if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; } - const hashInputContentData = toHashData(input); - const hashAbcContentData = toHashData(abcPath); + const hashInputContentData: any = toHashData(input); + const hashAbcContentData: any = toHashData(abcPath); hashJsonObject[input] = hashInputContentData; hashJsonObject[abcPath] = hashAbcContentData; fs.unlinkSync(input); } - const hashFilePath = genHashJsonPath(buildPathInfo); + const hashFilePath: string = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { return; } @@ -656,8 +657,8 @@ function genHashJsonPath(buildPath: string) { } return path.join(process.env.cachePath, hashFile); } else if (buildPath.indexOf(ARK) >= 0) { - const dataTmps = buildPath.split(ARK); - const hashPath = path.join(dataTmps[0], ARK); + const dataTmps: string[] = buildPath.split(ARK); + const hashPath: string = path.join(dataTmps[0], ARK); if (!fs.existsSync(hashPath) || !fs.statSync(hashPath).isDirectory()) { logger.error(red, `ETS:ERROR hash path does not exist`, reset); return ''; diff --git a/compiler/src/gen_module_abc.ts b/compiler/src/gen_module_abc.ts index b80bc4e..ecc84b7 100644 --- a/compiler/src/gen_module_abc.ts +++ b/compiler/src/gen_module_abc.ts @@ -22,14 +22,14 @@ const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; function js2abcByWorkers(jsonInput: string, cmd: string): Promise { - const inputPaths = JSON.parse(jsonInput); - const inputs = []; + const inputPaths: any = JSON.parse(jsonInput); + const inputs: string[] = []; for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].tempFilePath; + const input: string = inputPaths[i].tempFilePath; inputs.push('"' + input + '"'); } - const inputsStr = inputs.join(' '); - const singleCmd = `${cmd} ${inputsStr}`; + const inputsStr: string = inputs.join(' '); + const singleCmd: any = `${cmd} ${inputsStr}`; logger.debug('gen abc cmd is: ', singleCmd); try { childProcess.execSync(singleCmd); diff --git a/compiler/src/process_js_ast.ts b/compiler/src/process_js_ast.ts index acc8219..8b94e5c 100644 --- a/compiler/src/process_js_ast.ts +++ b/compiler/src/process_js_ast.ts @@ -15,19 +15,15 @@ import ts from 'typescript'; import { - BUILD_ON, - ESMODULE + BUILD_ON } from './pre_define'; import { writeFileSyncByNode } from './utils'; -import { projectConfig } from '../main'; export function processJs(program: ts.Program, ut = false): Function { return (context: ts.TransformationContext) => { return (node: ts.SourceFile) => { if (process.env.compiler === BUILD_ON) { - if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === false) { - writeFileSyncByNode(node, false); - } + writeFileSyncByNode(node, false); return node; } else { return node; diff --git a/compiler/src/process_js_file.ts b/compiler/src/process_js_file.ts index 5b8d700..1eaec24 100644 --- a/compiler/src/process_js_file.ts +++ b/compiler/src/process_js_file.ts @@ -6,7 +6,7 @@ import { } from './pre_define'; module.exports = function processjs2file(source: string): string { - if (projectConfig.compileMode === ESMODULE + if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === false && process.env.compilerType && process.env.compilerType === ARK) { writeFileSyncByString(this.resourcePath, source, false); } diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 2f54ebd..e2dce1e 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -43,7 +43,8 @@ import { SET_CONTROLLER_METHOD, JS_DIALOG, CUSTOM_DIALOG_CONTROLLER_BUILDER, - ESMODULE + ESMODULE, + ARK } from './pre_define'; import { componentInfo, @@ -87,7 +88,8 @@ export function processUISyntax(program: ts.Program, ut = false): Function { if (process.env.compiler === BUILD_ON) { if (!ut && (path.basename(node.fileName) === 'app.ets' || /\.ts$/.test(node.fileName))) { node = ts.visitEachChild(node, processResourceNode, context); - if (projectConfig.compileMode === 'ESMODULE' && projectConfig.processTs === true) { + if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === true + && process.env.compilerType && process.env.compilerType === ARK) { writeFileSyncByNode(node, true); } return node; @@ -105,7 +107,8 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); - if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === true) { + if (projectConfig.compileMode === ESMODULE && projectConfig.processTs === true + && process.env.compilerType && process.env.compilerType === ARK) { writeFileSyncByNode(node, true); } return node; diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 3e07e9a..6212e6f 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -239,9 +239,9 @@ export function toUnixPath(data: string): string { return data; } -export function toHashData(path: string) { - const content = fs.readFileSync(path); - const hash = createHash('sha256'); +export function toHashData(path: string): any { + const content: string = fs.readFileSync(path).toString(); + const hash: any = createHash('sha256'); hash.update(content); return hash.digest('hex'); } @@ -265,25 +265,25 @@ export function genTemporaryPath(filePath: string, projectPath: string, buildPat filePath = filePath.replace(/\.cjs$/, EXTNAME_JS); } projectPath = toUnixPath(projectPath); - const hapPath = toUnixPath(projectConfig.projectRootPath); - const tempFilePath = filePath.replace(hapPath, ''); + const hapPath: string = toUnixPath(projectConfig.projectRootPath); + const tempFilePath: string = filePath.replace(hapPath, ''); if (checkNodeModulesFile(filePath, projectPath)) { - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); - const dataTmps = tempFilePath.split(NODE_MODULES); - let output:string = ''; + const fakeNodeModulesPath: string = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); + const dataTmps: string[] = tempFilePath.split(NODE_MODULES); + let output: string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { - const sufStr = dataTmps[dataTmps.length - 1]; + const sufStr: string = dataTmps[dataTmps.length - 1]; output = path.join(buildPath, TEMPRARY, NODE_MODULES, MAIN, sufStr); } else { - const sufStr = dataTmps[dataTmps.length - 1]; + const sufStr: string = dataTmps[dataTmps.length - 1]; output = path.join(buildPath, TEMPRARY, NODE_MODULES, AUXILIARY, sufStr); } return output; } if (filePath.indexOf(projectPath) !== -1) { - const sufStr = filePath.replace(projectPath, ''); + const sufStr: string = filePath.replace(projectPath, ''); const output: string = path.join(buildPath, TEMPRARY, sufStr); return output; } @@ -300,26 +300,26 @@ export function genBuildPath(filePath: string, projectPath: string, buildPath: s filePath = filePath.replace(/\.cjs$/, EXTNAME_JS); } projectPath = toUnixPath(projectPath); - const hapPath = toUnixPath(projectConfig.projectRootPath); - const tempFilePath = filePath.replace(hapPath, ''); + const hapPath: string = toUnixPath(projectConfig.projectRootPath); + const tempFilePath: string = filePath.replace(hapPath, ''); if (checkNodeModulesFile(filePath, projectPath)) { filePath = toUnixPath(filePath); - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); - const dataTmps = tempFilePath.split(NODE_MODULES); - let output:string = ''; + const fakeNodeModulesPath: string = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); + const dataTmps: string[] = tempFilePath.split(NODE_MODULES); + let output: string = ''; if (filePath.indexOf(fakeNodeModulesPath) === -1) { - const sufStr = dataTmps[dataTmps.length - 1]; + const sufStr: string = dataTmps[dataTmps.length - 1]; output = path.join(projectConfig.nodeModulesPath, ZERO, sufStr); } else { - const sufStr = dataTmps[dataTmps.length - 1]; + const sufStr: string = dataTmps[dataTmps.length - 1]; output = path.join(projectConfig.nodeModulesPath, ONE, sufStr); } return output; } if (filePath.indexOf(projectPath) !== -1) { - const sufStr = filePath.replace(projectPath, ''); + const sufStr: string = filePath.replace(projectPath, ''); const output: string = path.join(buildPath, sufStr); return output; } @@ -330,17 +330,17 @@ export function genBuildPath(filePath: string, projectPath: string, buildPath: s export function checkNodeModulesFile(filePath: string, projectPath: string) { filePath = toUnixPath(filePath); projectPath = toUnixPath(projectPath); - const hapPath = toUnixPath(projectConfig.projectRootPath); - const tempFilePath = filePath.replace(hapPath, ''); + const hapPath: string = toUnixPath(projectConfig.projectRootPath); + const tempFilePath: string = filePath.replace(hapPath, ''); if (tempFilePath.indexOf(NODE_MODULES) !== -1) { - const fakeNodeModulesPath = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); + const fakeNodeModulesPath: string = toUnixPath(path.resolve(projectConfig.projectRootPath, NODE_MODULES)); if (filePath.indexOf(fakeNodeModulesPath) !== -1) { return true; } if (projectConfig.modulePathMap) { for (const key in projectConfig.modulePathMap) { - const value = projectConfig.modulePathMap[key]; - const fakeModuleNodeModulesPath = toUnixPath(path.resolve(value, NODE_MODULES)); + const value: string = projectConfig.modulePathMap[key]; + const fakeModuleNodeModulesPath: string = toUnixPath(path.resolve(value, NODE_MODULES)); if (filePath.indexOf(fakeModuleNodeModulesPath) !== -1) { return true; } @@ -354,27 +354,27 @@ export function checkNodeModulesFile(filePath: string, projectPath: string) { export function mkdirsSync(dirname: string): boolean { if (fs.existsSync(dirname)) { return true; - } else { - if (mkdirsSync(path.dirname(dirname))) { - fs.mkdirSync(dirname); - return true; - } + } else if (mkdirsSync(path.dirname(dirname))) { + fs.mkdirSync(dirname); + return true; } + return false; } -export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { +export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean): void { const temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return; } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, sourceCode); + return; } export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { if (toTsFile) { - const newStatements = []; + const newStatements: ts.Node[] = []; const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); newStatements.push(tsIgnoreNode); if (node.statements && node.statements.length) { @@ -383,43 +383,7 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { node = ts.factory.updateSourceFile(node, newStatements); } - const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); - const options : ts.CompilerOptions = { - sourceMap: true - }; - const mapOpions = { - sourceMap: true, - inlineSourceMap: false, - inlineSources: false, - sourceRoot: '', - mapRoot: '', - extendedDiagnostics: false - }; - const host = ts.createCompilerHost(options); - const fileName = node.fileName; - // @ts-ignore - const sourceMapGenerator = ts.createSourceMapGenerator( - host, - // @ts-ignore - ts.getBaseFileName(fileName), - '', - '', - mapOpions - ); - // @ts-ignore - const writer = ts.createTextWriter( - // @ts-ignore - ts.getNewLineCharacter({newLine: ts.NewLineKind.LineFeed, removeComments: false})); - printer['writeFile'](node, writer, sourceMapGenerator); - const sourceMapJson = sourceMapGenerator.toJSON(); - sourceMapJson['sources'] = [fileName]; - const result: string = writer.getText(); - let content: string = result; - content = processSystemApi(content, true); - if (toTsFile) { - content = result.replace(`${TS_NOCHECK};`, TS_NOCHECK); - } - const sourceMapContent = JSON.stringify(sourceMapJson); + const mixedInfo: {content: string, sourceMapContent: string} = genContentAndSourceMapInfo(node, toTsFile); let temporaryFile: string = genTemporaryPath(node.fileName, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return; @@ -439,12 +403,57 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } } mkdirsSync(path.dirname(temporaryFile)); - fs.writeFileSync(temporaryFile, content); + fs.writeFileSync(temporaryFile, mixedInfo.content); if (temporarySourceMapFile.length > 0 && projectConfig.buildArkMode === 'debug') { - fs.writeFileSync(temporarySourceMapFile, sourceMapContent); + fs.writeFileSync(temporarySourceMapFile, mixedInfo.sourceMapContent); } } +function genContentAndSourceMapInfo(node: ts.SourceFile, toTsFile: boolean): {content: string, sourceMapContent: string} { + const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); + const options: ts.CompilerOptions = { + sourceMap: true + }; + const mapOpions: any = { + sourceMap: true, + inlineSourceMap: false, + inlineSources: false, + sourceRoot: '', + mapRoot: '', + extendedDiagnostics: false + }; + const host: ts.CompilerHost = ts.createCompilerHost(options); + const fileName: string = node.fileName; + // @ts-ignore + const sourceMapGenerator: any = ts.createSourceMapGenerator( + host, + // @ts-ignore + ts.getBaseFileName(fileName), + '', + '', + mapOpions + ); + // @ts-ignore + const writer: any = ts.createTextWriter( + // @ts-ignore + ts.getNewLineCharacter({newLine: ts.NewLineKind.LineFeed, removeComments: false})); + printer['writeFile'](node, writer, sourceMapGenerator); + const sourceMapJson: any = sourceMapGenerator.toJSON(); + sourceMapJson['sources'] = [fileName]; + const result: string = writer.getText(); + let content: string = result; + content = processSystemApi(content, true); + if (toTsFile) { + content = result.replace(`${TS_NOCHECK};`, TS_NOCHECK); + } + const sourceMapContent: string = JSON.stringify(sourceMapJson); + + return { + content: content, + sourceMapContent: sourceMapContent + }; +} + export function genAbcFileName(temporaryFile: string): string { let abcFile: string = temporaryFile; if (temporaryFile.endsWith(EXTNAME_TS)) { diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 160ccc6..4c259f5 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -79,10 +79,16 @@ function initConfig(config) { transpileOnly: true, configFile: path.resolve(__dirname, 'tsconfig.json'), getCustomTransformers(program) { - return { + let transformerOperation = { before: [processUISyntax(program)], - after: [processJs(program)] + after: [] }; + if (projectConfig.compileMode === 'esmodule' && projectConfig.processTs === false + && process.env.compilerType && process.env.compilerType === 'ark') { + transformerOperation.after.push(processJs(program)); + } + + return transformerOperation; }, ignoreDiagnostics: IGNORE_ERROR_CODE } From ba6c35aa077896230373d056b908b4fa29ac0e26 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Mon, 6 Jun 2022 16:36:13 +0800 Subject: [PATCH 31/34] split by number Signed-off-by: zhangrengao Change-Id: I50898fabc61f525f166314a9f46c6003b4082222 --- compiler/src/gen_abc_plugin.ts | 39 +++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index dbe63b1..a1d74cc 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -435,21 +435,31 @@ function invokeCluterModuleToAbc() { const tempAbcArgs: string[] = abcArgs.slice(0); tempAbcArgs.push('-c'); const commonJsCmdPrefix: any = `${nodeJs} ${tempAbcArgs.join(' ')}`; - const workerData: any = { - 'inputs': JSON.stringify(commonJsModuleInfos), - 'cmd': commonJsCmdPrefix - }; - cluster.fork(workerData); + const chunkSize: number = 50; + const splitedModules: any[] = splitModulesByNumber(commonJsModuleInfos, chunkSize); + const workerNumber: number = splitedModules.length; + for (let i= 0; i < workerNumber;i++) { + const workerData: any = { + 'inputs': JSON.stringify(splitedModules[i]), + 'cmd': commonJsCmdPrefix + }; + cluster.fork(workerData); + } } if (ESMModuleInfos.length > 0) { const tempAbcArgs: string[] = abcArgs.slice(0); tempAbcArgs.push('-m'); const ESMCmdPrefix: any = `${nodeJs} ${tempAbcArgs.join(' ')}`; - const workerData: any = { - 'inputs': JSON.stringify(ESMModuleInfos), - 'cmd': ESMCmdPrefix - }; - cluster.fork(workerData); + const chunkSize: number = 50; + const splitedModules: any[] = splitModulesByNumber(ESMModuleInfos, chunkSize); + const workerNumber: number = splitedModules.length; + for (let i=0;i { @@ -462,6 +472,15 @@ function invokeCluterModuleToAbc() { } } +function splitModulesByNumber(moduleInfos: Array, chunkSize: number): any[] { + const result: any[] = []; + for(let i = 0; i < moduleInfos.length; i += chunkSize) { + result.push(moduleInfos.slice(i, i + chunkSize)); + } + + return result; +} + function invokeWorkersToGenAbc() { let param: string = ''; if (isDebug) { From 05c39a13c3c80de20decc9d190797377d6272d65 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 7 Jun 2022 11:01:11 +0800 Subject: [PATCH 32/34] add function signature Signed-off-by: zhangrengao Change-Id: I7362c73097106234ca5a09053e9f8c0ce33b5263 --- compiler/src/gen_abc_plugin.ts | 42 +++++++++++++++++----------------- compiler/src/utils.ts | 6 ++--- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index a1d74cc..b2458bb 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -170,7 +170,7 @@ export class GenAbcPlugin { } } -function getEntryInfo(tempFilePath: string, resourceResolveData: any) { +function getEntryInfo(tempFilePath: string, resourceResolveData: any): void { if (!resourceResolveData.descriptionFilePath) { return; } @@ -201,7 +201,7 @@ function getEntryInfo(tempFilePath: string, resourceResolveData: any) { entryInfos.set(npmInfoPath, entryInfo); } -function processNodeModulesFile(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, nodeModulesFile: Array, module: any) { +function processNodeModulesFile(filePath: string, tempFilePath: string, buildFilePath: string, abcFilePath: string, nodeModulesFile: Array, module: any): void { getEntryInfo(tempFilePath, module.resourceResolveData); const descriptionFileData: any = module.resourceResolveData.descriptionFileData; if (descriptionFileData && descriptionFileData['type'] && descriptionFileData['type'] === 'module') { @@ -221,7 +221,7 @@ function processNodeModulesFile(filePath: string, tempFilePath: string, buildFil return; } -function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { +function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any): void { if (projectConfig.processTs === true) { tempFilePath = tempFilePath.replace(/\.ets$/, EXTNAME_TS); buildFilePath = buildFilePath.replace(/\.ets$/, EXTNAME_TS); @@ -238,11 +238,11 @@ function processEtsModule(filePath: string, tempFilePath: string, buildFilePath: } } -function processDtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { +function processDtsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any): void { return; } -function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { +function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any): void { if (projectConfig.processTs === false) { tempFilePath = tempFilePath.replace(/\.ts$/, EXTNAME_JS); buildFilePath = buildFilePath.replace(/\.ts$/, EXTNAME_JS); @@ -256,7 +256,7 @@ function processTsModule(filePath: string, tempFilePath: string, buildFilePath: } } -function processJsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any) { +function processJsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array, module: any): void { const parent: string = path.join(tempFilePath, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { mkDir(parent); @@ -273,7 +273,7 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: } } -function handleFinishModules(modules, callback) { +function handleFinishModules(modules, callback): void { const nodeModulesFile: Array = []; modules.forEach(module => { if (module !== undefined && module.resourceResolveData !== undefined) { @@ -303,7 +303,7 @@ function handleFinishModules(modules, callback) { processEntryToGenAbc(entryInfos); } -function processEntryToGenAbc(entryInfos: Map) { +function processEntryToGenAbc(entryInfos: Map): void { for (const value of entryInfos.values()) { const tempAbcFilePath: string = toUnixPath(path.resolve(value.npmInfo, ENTRY_TXT)); const buildAbcFilePath: string = toUnixPath(path.resolve(value.buildPath, ENTRY_TXT)); @@ -340,7 +340,7 @@ function mkDir(path_: string): void { fs.mkdirSync(path_); } -function getSmallestSizeGroup(groupSize: Map) { +function getSmallestSizeGroup(groupSize: Map): any { const groupSizeArray: any = Array.from(groupSize); groupSizeArray.sort(function(g1, g2) { return g1[1] - g2[1]; // sort by size @@ -348,7 +348,7 @@ function getSmallestSizeGroup(groupSize: Map) { return groupSizeArray[0][0]; } -function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { +function splitJsBundlesBySize(bundleArray: Array, groupNumber: number): any { const result: any = []; if (bundleArray.length < groupNumber) { result.push(bundleArray); @@ -375,7 +375,7 @@ function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { return result; } -function invokeWorkersModuleToGenAbc(moduleInfos: Array) { +function invokeWorkersModuleToGenAbc(moduleInfos: Array): void { if (fs.existsSync(buildPathInfo)) { fs.rmdirSync(buildPathInfo, { recursive: true}); } @@ -413,7 +413,7 @@ function initAbcEnv() : string[] { return args; } -function invokeCluterModuleToAbc() { +function invokeCluterModuleToAbc(): void { const abcArgs: string[] = initAbcEnv(); const clusterNewApiVersion: number = 16; @@ -438,7 +438,7 @@ function invokeCluterModuleToAbc() { const chunkSize: number = 50; const splitedModules: any[] = splitModulesByNumber(commonJsModuleInfos, chunkSize); const workerNumber: number = splitedModules.length; - for (let i= 0; i < workerNumber;i++) { + for (let i = 0; i < workerNumber; i++) { const workerData: any = { 'inputs': JSON.stringify(splitedModules[i]), 'cmd': commonJsCmdPrefix @@ -453,7 +453,7 @@ function invokeCluterModuleToAbc() { const chunkSize: number = 50; const splitedModules: any[] = splitModulesByNumber(ESMModuleInfos, chunkSize); const workerNumber: number = splitedModules.length; - for (let i=0;i, chunkSize: number): any[] { const result: any[] = []; - for(let i = 0; i < moduleInfos.length; i += chunkSize) { + for (let i = 0; i < moduleInfos.length; i += chunkSize) { result.push(moduleInfos.slice(i, i + chunkSize)); } return result; } -function invokeWorkersToGenAbc() { +function invokeWorkersToGenAbc(): void { let param: string = ''; if (isDebug) { param += ' --debug'; @@ -533,7 +533,7 @@ function invokeWorkersToGenAbc() { } } -function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Array) { +function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Array): void { for (let i = 0; i < moduleInfos.length; ++i) { filterModuleInfos.push(moduleInfos[i]); } @@ -579,7 +579,7 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra moduleHashJsonObject = updateJsonObject; } -function writeModuleHashJson() { +function writeModuleHashJson(): void { for (let i = 0; i < filterModuleInfos.length; ++i) { const input: string = filterModuleInfos[i].tempFilePath; const abcPath: string = filterModuleInfos[i].abcFilePath; @@ -605,7 +605,7 @@ function writeModuleHashJson() { fs.writeFileSync(hashFilePath, JSON.stringify(moduleHashJsonObject)); } -function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: File[]) { +function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: File[]): void { for (let i = 0; i < inputPaths.length; ++i) { fileterIntermediateJsBundle.push(inputPaths[i]); } @@ -646,7 +646,7 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil hashJsonObject = updateJsonObject; } -function writeHashJson() { +function writeHashJson(): void { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { const input:string = fileterIntermediateJsBundle[i].path; const abcPath: string = input.replace(/_.js$/, EXTNAME_ABC); @@ -667,7 +667,7 @@ function writeHashJson() { fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); } -function genHashJsonPath(buildPath: string) { +function genHashJsonPath(buildPath: string): void { buildPath = toUnixPath(buildPath); if (process.env.cachePath) { if (!fs.existsSync(process.env.cachePath) || !fs.statSync(process.env.cachePath).isDirectory()) { diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 6212e6f..c208272 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -327,7 +327,7 @@ export function genBuildPath(filePath: string, projectPath: string, buildPath: s return ''; } -export function checkNodeModulesFile(filePath: string, projectPath: string) { +export function checkNodeModulesFile(filePath: string, projectPath: string): boolean { filePath = toUnixPath(filePath); projectPath = toUnixPath(projectPath); const hapPath: string = toUnixPath(projectConfig.projectRootPath); @@ -372,7 +372,7 @@ export function writeFileSyncByString(sourcePath: string, sourceCode: string, to return; } -export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { +export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean): void { if (toTsFile) { const newStatements: ts.Node[] = []; const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); @@ -409,7 +409,7 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } } -function genContentAndSourceMapInfo(node: ts.SourceFile, toTsFile: boolean): {content: string, sourceMapContent: string} { +function genContentAndSourceMapInfo(node: ts.SourceFile, toTsFile: boolean): any { const printer: ts.Printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed }); const options: ts.CompilerOptions = { sourceMap: true From f5dda62b79a00f020e89dc877f48d8c396e757b4 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 7 Jun 2022 15:35:22 +0800 Subject: [PATCH 33/34] fix type of string Signed-off-by: zhangrengao Change-Id: Ife13b935b0e613d75728050d7f5fe250b5174926 --- compiler/src/gen_abc_plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index b2458bb..448ed00 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -273,7 +273,7 @@ function processJsModule(filePath: string, tempFilePath: string, buildFilePath: } } -function handleFinishModules(modules, callback): void { +function handleFinishModules(modules, callback): any { const nodeModulesFile: Array = []; modules.forEach(module => { if (module !== undefined && module.resourceResolveData !== undefined) { @@ -667,7 +667,7 @@ function writeHashJson(): void { fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); } -function genHashJsonPath(buildPath: string): void { +function genHashJsonPath(buildPath: string): string { buildPath = toUnixPath(buildPath); if (process.env.cachePath) { if (!fs.existsSync(process.env.cachePath) || !fs.statSync(process.env.cachePath).isDirectory()) { From cdcd71ab494610256448b3a3eed515118b2a6e1d Mon Sep 17 00:00:00 2001 From: hufeng Date: Tue, 7 Jun 2022 16:50:25 +0800 Subject: [PATCH 34/34] Fix some scenario of importing Lib's ohmUrl Signed-off-by: hufeng Change-Id: I2fcd6832a8b470b2cb11d9b8a16d655c7cead903 --- compiler/src/validate_ui_syntax.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index f209c01..3591592 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -884,7 +884,8 @@ function replaceOhmStartsWithBundle(url: string, item: string, importValue: stri if (urlResult) { const moduleKind: string = urlResult[3]; if (moduleKind === 'lib') { - item = replaceLibSo(importValue, moduleRequest, sourcePath); + const libSoKey: string = urlResult[4]; + item = replaceLibSo(importValue, libSoKey, sourcePath); } } return item; @@ -898,7 +899,7 @@ function replaceOhmStartsWithModule(url: string, item: string, importValue: stri const modulePath: string = urlResult[3]; const bundleName: string = getPackageInfo(projectConfig.aceModuleJsonPath)[0]; moduleRequest = `@bundle:${bundleName}/${moduleName}/${moduleKind}/${modulePath}`; - item = moduleKind === 'lib' ? replaceLibSo(importValue, moduleRequest, sourcePath) : + item = moduleKind === 'lib' ? replaceLibSo(importValue, modulePath, sourcePath) : item.replace(/['"](\S+)['"]/, '\"' + moduleRequest + '\"'); } return item;