From c120766ac7dca531892fd0e3cfd824212b625ca5 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Thu, 5 May 2022 09:12:15 +0800 Subject: [PATCH 01/17] 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 02/17] 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 03/17] 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 04/17] 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 05/17] 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 06/17] 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 07/17] 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 08/17] 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 09/17] 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 10/17] 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 11/17] 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 12/17] 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 13/17] 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 14/17] 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 15/17] 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 16/17] 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 17/17] 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