From 3b11a23dc4e346f406dddb39e71fc5fda4ef0fe1 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 31 May 2022 21:45:19 +0800 Subject: [PATCH] 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; -} +}