code check

Signed-off-by: zhangrengao <zhangrengao1@huawei.com>
Change-Id: Ic52c099b193e5b2e729a01cc4e12da6e33eb5a1d
This commit is contained in:
zhangrengao
2022-05-30 14:23:57 +08:00
parent d32c0b6206
commit 2ca90163df
7 changed files with 173 additions and 203 deletions
+4 -3
View File
@@ -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 = {
+86 -113
View File
@@ -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<File> = [];
let fileterIntermediateJsBundle: Array<File> = [];
let moduleInfos = new Array<ModuleInfo>();
let filterModuleInfos = new Array<ModuleInfo>();
let commonJsModuleInfos = new Array<ModuleInfo>();
let ESMModuleInfos = new Array<ModuleInfo>();
let entryInfos = new Map<string, EntryInfo>();
const moduleInfos: Array<ModuleInfo> = [];;
let filterModuleInfos: Array<ModuleInfo> = [];
const commonJsModuleInfos: Array<ModuleInfo> = [];
const ESMModuleInfos: Array<ModuleInfo> = [];
const entryInfos = new Map<string, EntryInfo>();
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<string>, 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<string>, 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<string>, module: any) {
return ;
return;
}
function processTsModule(filePath: string, tempFilePath: string, buildFilePath: string, nodeModulesFile: Array<string>, 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<string>();
const nodeModulesFile: Array<string> = [];
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<string, EntryInfo>) {
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<string, EntryInfo>) {
}
}
function processToAbcFile(nodeModulesFile: Array<string>) {
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<File>, groupNumber: number) {
function invokeWorkersModuleToGenAbc(moduleInfos: Array<ModuleInfo>) {
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<ModuleInfo>, groupNumber: number)
for (let i = 0; i < groupNumber; ++i) {
result.push([]);
}
for (let i =0;i<moduleInfos.length;i++) {
let pos = i%groupNumber;
for (let i = 0; i < moduleInfos.length; i++) {
const pos = i % groupNumber;
result[pos].push(moduleInfos[i]);
}
return result;
@@ -587,7 +560,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 (projectConfig.buildArkMode == "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 {
@@ -617,7 +590,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 (projectConfig.buildArkMode == "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));
}
}
+3 -4
View File
@@ -15,7 +15,6 @@
import * as childProcess from 'child_process';
import * as process from 'process';
import * as fs from 'fs';
import cluster from 'cluster';
import { logger } from './compile_info';
@@ -24,12 +23,12 @@ const reset: string = '\u001b[39m';
function js2abcByWorkers(jsonInput: string, cmd: string): Promise<void> {
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<void> {
return;
}
return ;
return;
}
logger.debug('worker data is: ', JSON.stringify(process.env));
+11 -13
View File
@@ -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;
}
}
}
};
};
}
+6 -6
View File
@@ -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;
}
if (projectConfig.compileMode === 'esmodule'
&& process.env.compilerType && process.env.compilerType === 'ark') {
writeFileSyncByString(this.resourcePath, source, false);
}
return source;
};
+2 -2
View File
@@ -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;
+61 -62
View File
@@ -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;
}
}