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 }