mirror of
https://github.com/openharmony/developtools_ace-ets2bundle.git
synced 2026-07-21 03:55:23 -04:00
!986 fix clean webpack of incre compile
Merge pull request !986 from zrg/fix_clean_webpack_release
This commit is contained in:
@@ -26,7 +26,9 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise<void> {
|
||||
const inputPaths = JSON.parse(jsonInput);
|
||||
for (let i = 0; i < inputPaths.length; ++i) {
|
||||
const input = inputPaths[i].path;
|
||||
const singleCmd = `${cmd} "${input}"`;
|
||||
const cacheOutputPath: string = inputPaths[i].cacheOutputPath;
|
||||
const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, ".abc");
|
||||
const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${input}"`;
|
||||
logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte');
|
||||
try {
|
||||
childProcess.execSync(singleCmd);
|
||||
@@ -34,14 +36,6 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise<void> {
|
||||
logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset);
|
||||
return;
|
||||
}
|
||||
|
||||
const abcFile: string = input.replace(/\.js$/, '.abc');
|
||||
if (fs.existsSync(abcFile)) {
|
||||
const abcFileNew: string = abcFile.replace(/_.abc$/, '.abc');
|
||||
fs.renameSync(abcFile, abcFileNew);
|
||||
} else {
|
||||
logger.error(red, `ETS:ERROR ${abcFile} is lost`, reset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,13 @@ import cluster from 'cluster';
|
||||
import process from 'process';
|
||||
import Compiler from 'webpack/lib/Compiler';
|
||||
import { logger } from './compile_info';
|
||||
import { toUnixPath, toHashData } from './utils';
|
||||
import {
|
||||
toUnixPath,
|
||||
toHashData,
|
||||
compareNodeVersion,
|
||||
removeDir
|
||||
} from './utils';
|
||||
import { NODE_MODULES } from './pre_define';
|
||||
|
||||
const firstFileEXT: string = '_.js';
|
||||
const genAbcScript = 'gen_abc.js';
|
||||
@@ -32,7 +38,8 @@ let nodeJs: string;
|
||||
|
||||
interface File {
|
||||
path: string,
|
||||
size: number
|
||||
size: number,
|
||||
cacheOutputPath: string
|
||||
}
|
||||
const intermediateJsBundle: Array<File> = [];
|
||||
let fileterIntermediateJsBundle: Array<File> = [];
|
||||
@@ -65,7 +72,14 @@ export class GenAbcPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
if (!checkNodeModules()) {
|
||||
return;
|
||||
}
|
||||
|
||||
removeDir(output);
|
||||
|
||||
compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => {
|
||||
buildPathInfo = output;
|
||||
Object.keys(compilation.assets).forEach(key => {
|
||||
// choose *.js
|
||||
if (output && path.extname(key) === '.js') {
|
||||
@@ -84,14 +98,25 @@ export class GenAbcPlugin {
|
||||
}
|
||||
|
||||
function writeFileSync(inputString: string, output: string, jsBundleFile: string): void {
|
||||
const parent: string = path.join(output, '..');
|
||||
let parent: string = path.join(output, '..');
|
||||
if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) {
|
||||
mkDir(parent);
|
||||
}
|
||||
fs.writeFileSync(output, inputString);
|
||||
if (fs.existsSync(output)) {
|
||||
const fileSize = fs.statSync(output).size;
|
||||
intermediateJsBundle.push({path: output, size: fileSize});
|
||||
const sufStr: string = output.replace(buildPathInfo, '');
|
||||
let cacheOutputPath: string = "";
|
||||
if (process.env.cachePath) {
|
||||
cacheOutputPath = path.join(process.env.cachePath, "temporary", sufStr);
|
||||
} else {
|
||||
cacheOutputPath = output;
|
||||
}
|
||||
parent = path.join(cacheOutputPath, '..');
|
||||
if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) {
|
||||
mkDir(parent);
|
||||
}
|
||||
fs.writeFileSync(cacheOutputPath, inputString);
|
||||
if (fs.existsSync(cacheOutputPath)) {
|
||||
const fileSize: any = fs.statSync(cacheOutputPath).size;
|
||||
intermediateJsBundle.push({path: output, size: fileSize, cacheOutputPath: cacheOutputPath});
|
||||
} else {
|
||||
logger.error(red, `ETS:ERROR Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, reset);
|
||||
}
|
||||
@@ -160,8 +185,7 @@ function invokeWorkersToGenAbc() {
|
||||
const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `;
|
||||
|
||||
const clusterNewApiVersion = 16;
|
||||
const currentNodeVersion = parseInt(process.version.split('.')[0]);
|
||||
const useNewApi = currentNodeVersion >= clusterNewApiVersion;
|
||||
const useNewApi = compareNodeVersion(clusterNewApiVersion);
|
||||
|
||||
if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) {
|
||||
if (useNewApi) {
|
||||
@@ -210,17 +234,23 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil
|
||||
for (let i = 0; i < inputPaths.length; ++i) {
|
||||
const input = inputPaths[i].path;
|
||||
const abcPath = input.replace(/_.js$/, '.abc');
|
||||
if (!fs.existsSync(input)) {
|
||||
logger.error(red, `ETS:ERROR ${input} is lost`, reset);
|
||||
const cacheOutputPath: string = inputPaths[i].cacheOutputPath;
|
||||
const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc');
|
||||
if (!fs.existsSync(cacheOutputPath)) {
|
||||
logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset);
|
||||
continue;
|
||||
}
|
||||
if (fs.existsSync(abcPath)) {
|
||||
const hashInputContentData = toHashData(input);
|
||||
const hashAbcContentData = toHashData(abcPath);
|
||||
if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) {
|
||||
updateJsonObject[input] = hashInputContentData;
|
||||
updateJsonObject[abcPath] = hashAbcContentData;
|
||||
fs.unlinkSync(input);
|
||||
if (fs.existsSync(cacheAbcFilePath)) {
|
||||
const hashInputContentData: any = toHashData(cacheOutputPath);
|
||||
const hashAbcContentData: any = toHashData(cacheAbcFilePath);
|
||||
if (jsonObject[cacheOutputPath] === hashInputContentData && jsonObject[cacheAbcFilePath] === hashAbcContentData) {
|
||||
updateJsonObject[cacheOutputPath] = hashInputContentData;
|
||||
updateJsonObject[cacheAbcFilePath] = hashAbcContentData;
|
||||
let parent: string = path.join(abcPath, '..');
|
||||
if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) {
|
||||
mkDir(parent);
|
||||
}
|
||||
fs.copyFileSync(cacheAbcFilePath, abcPath);
|
||||
} else {
|
||||
fileterIntermediateJsBundle.push(inputPaths[i]);
|
||||
}
|
||||
@@ -237,15 +267,29 @@ function writeHashJson() {
|
||||
for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) {
|
||||
const input = fileterIntermediateJsBundle[i].path;
|
||||
const abcPath = input.replace(/_.js$/, '.abc');
|
||||
if (!fs.existsSync(input) || !fs.existsSync(abcPath)) {
|
||||
logger.error(red, `ETS:ERROR ${input} is lost`, reset);
|
||||
const cacheOutputPath: string = fileterIntermediateJsBundle[i].cacheOutputPath;
|
||||
const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc');
|
||||
if (!fs.existsSync(cacheOutputPath) || !fs.existsSync(cacheAbcFilePath)) {
|
||||
logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset);
|
||||
continue;
|
||||
}
|
||||
const hashInputContentData = toHashData(input);
|
||||
const hashAbcContentData = toHashData(abcPath);
|
||||
hashJsonObject[input] = hashInputContentData;
|
||||
hashJsonObject[abcPath] = hashAbcContentData;
|
||||
fs.unlinkSync(input);
|
||||
const hashInputContentData: any = toHashData(cacheOutputPath);
|
||||
const hashAbcContentData: any = toHashData(cacheAbcFilePath);
|
||||
hashJsonObject[cacheOutputPath] = hashInputContentData;
|
||||
hashJsonObject[cacheAbcFilePath] = hashAbcContentData;
|
||||
}
|
||||
for (let i = 0; i < intermediateJsBundle.length; ++i) {
|
||||
const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc");
|
||||
const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc");
|
||||
if (!fs.existsSync(cacheAbcFilePath)) {
|
||||
logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset);
|
||||
break;
|
||||
}
|
||||
let parent: string = path.join(abcFile, '..');
|
||||
if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) {
|
||||
mkDir(parent);
|
||||
}
|
||||
fs.copyFileSync(cacheAbcFilePath, abcFile);
|
||||
}
|
||||
const hashFilePath = genHashJsonPath(buildPathInfo);
|
||||
if (hashFilePath.length === 0) {
|
||||
@@ -275,3 +319,20 @@ function genHashJsonPath(buildPath: string) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function checkNodeModules() {
|
||||
let arkEntryPath: string = path.join(arkDir, 'build');
|
||||
if (isWin) {
|
||||
arkEntryPath = path.join(arkDir, 'build-win');
|
||||
} else if (isMac) {
|
||||
arkEntryPath = path.join(arkDir, 'build-mac');
|
||||
}
|
||||
let nodeModulesPath: string = path.join(arkEntryPath, NODE_MODULES);
|
||||
if (!(fs.existsSync(nodeModulesPath) && fs.statSync(nodeModulesPath).isDirectory())) {
|
||||
logger.error(red, `ERROR: node_modules for ark compiler not found.
|
||||
Please make sure switch to non-root user before runing "npm install" for safity requirements and try re-run "npm install" under ${arkEntryPath}`, reset);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -234,3 +234,21 @@ export function writeFileSync(filePath: string, content: string): void {
|
||||
fs.writeFileSync(filePath, content);
|
||||
}
|
||||
|
||||
export function compareNodeVersion(nodeVersion: number = 16): boolean {
|
||||
const currentNodeVersion: number = parseInt(process.versions.node.split('.')[0]);
|
||||
if (currentNodeVersion >= nodeVersion) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function removeDir(dirName: string): void {
|
||||
if (fs.existsSync(dirName)) {
|
||||
if (compareNodeVersion()) {
|
||||
fs.rmSync(dirName, { recursive: true});
|
||||
} else {
|
||||
fs.rmdirSync(dirName, { recursive: true});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user