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 +}