mirror of
https://github.com/openharmony/developtools_ace-js2bundle.git
synced 2026-07-19 18:23:34 -04:00
Signed-off-by: yangbo <1442420648@qq.com> Change-Id: Ia1b25c3b03d3bcd8e102312112a4651d1337deda
This commit is contained in:
+38
-55
@@ -15,6 +15,7 @@
|
||||
|
||||
var path = require('path')
|
||||
var fs = require('fs')
|
||||
var shell = require('shelljs')
|
||||
|
||||
const red = '\u001b[31m';
|
||||
const reset = '\u001b[39m';
|
||||
@@ -41,66 +42,14 @@ function readManifest(manifestFilePath) {
|
||||
if (fs.existsSync(manifestFilePath)) {
|
||||
const jsonString = fs.readFileSync(manifestFilePath).toString();
|
||||
manifest = JSON.parse(jsonString);
|
||||
} else if (process.env.aceConfigPath && fs.existsSync(process.env.aceConfigPath)) {
|
||||
buildManifest(manifest, process.env.aceConfigPath);
|
||||
} else {
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or config.json is lost.' + '\u001b[39m')
|
||||
.message;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or config.json file format is invalid.' +
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json file format is invalid.' +
|
||||
'\u001b[39m').message;
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
|
||||
function buildManifest(manifest, aceConfigPath) {
|
||||
try {
|
||||
const configJson = JSON.parse(fs.readFileSync(aceConfigPath).toString());
|
||||
const srcPath = process.env.srcPath;
|
||||
manifest.type = process.env.abilityType;
|
||||
manifest.pages = []
|
||||
if (manifest.type === 'form') {
|
||||
if (configJson.module && configJson.module.abilities) {
|
||||
manifest.pages = getForms(configJson, srcPath);
|
||||
} else {
|
||||
throw Error('\u001b[31m'+
|
||||
'EERROR: the config.json file miss keyword module || module[abilities].' +
|
||||
'\u001b[39m').message;
|
||||
}
|
||||
manifest.minPlatformVersion = configJson.app.apiVersion.compatible;
|
||||
}
|
||||
} catch (e) {
|
||||
throw Error('\u001b[31m' + 'ERROR: the config.json file is lost or format is invalid.' +
|
||||
'\u001b[39m').message;
|
||||
}
|
||||
}
|
||||
|
||||
function getForms(configJson, srcPath) {
|
||||
const pages = [];
|
||||
for (const ability of configJson.module.abilities){
|
||||
if (ability.srcPath === srcPath) {
|
||||
readForms(ability, pages);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
function readForms(ability, pages) {
|
||||
if (ability.forms) {
|
||||
for (const form of ability.forms){
|
||||
if (form.src) {
|
||||
pages.push(form.src);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw Error('\u001b[31m' +`ERROR: the ${ability} in config.json file miss forms.' +
|
||||
'\u001b[39m`).message;
|
||||
}
|
||||
}
|
||||
|
||||
function loadEntryObj(projectPath, device_level, abilityType, manifestFilePath) {
|
||||
let entryObj = {};
|
||||
switch (abilityType) {
|
||||
@@ -151,8 +100,42 @@ function addPageEntryObj(manifest, projectPath) {
|
||||
return entryObj;
|
||||
}
|
||||
|
||||
function compileCardModule(env) {
|
||||
if (process.env.aceModuleJsonPath && fs.existsSync(process.env.aceModuleJsonPath)) {
|
||||
const moduleJsonConfig = JSON.parse(fs.readFileSync(process.env.aceModuleJsonPath).toString());
|
||||
if (moduleJsonConfig.module && moduleJsonConfig.module.uiSyntax === 'ets') {
|
||||
process.env.DEVICE_LEVEL = 'card';
|
||||
} else if (validateCardModule(moduleJsonConfig) && !process.env.compileCardModule) {
|
||||
process.env.compileCardModule = true;
|
||||
const cmd = `webpack --config webpack.rich.config.js --env compilerType=${env.compilerType} ` +
|
||||
`DEVICE_LEVEL=card aceModuleRoot=${process.env.projectPath} ` +
|
||||
`aceModuleJsonPath=${process.env.aceModuleJsonPath} aceProfilePath=${process.env.aceProfilePath} ` +
|
||||
`watchMode=${process.env.watchMode} cachePath=${process.env.cachePath} ` +
|
||||
`aceModuleBuild=${process.env.buildPath}`;
|
||||
shell.exec(cmd, (err) => {
|
||||
if (err) {
|
||||
throw Error(err).message;
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function validateCardModule(moduleJsonConfig) {
|
||||
if (moduleJsonConfig.module && moduleJsonConfig.module.extensionAbilities) {
|
||||
for (let i = 0; i < moduleJsonConfig.module.extensionAbilities.length; i++) {
|
||||
const extensionAbility = moduleJsonConfig.module.extensionAbilities[i];
|
||||
if (extensionAbility.type && extensionAbility.type === 'form') {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
deleteFolderRecursive: deleteFolderRecursive,
|
||||
readManifest: readManifest,
|
||||
loadEntryObj: loadEntryObj
|
||||
loadEntryObj: loadEntryObj,
|
||||
compileCardModule: compileCardModule
|
||||
};
|
||||
Generated
+25
@@ -5430,6 +5430,31 @@
|
||||
"resolved": "https://registry.nlark.com/shebang-regex/download/shebang-regex-3.0.0.tgz?cache=0&sync_timestamp=1628896299850&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fshebang-regex%2Fdownload%2Fshebang-regex-3.0.0.tgz",
|
||||
"integrity": "sha1-rhbxZE2HPsrYQ7AwexQzYtTEIXI="
|
||||
},
|
||||
"shelljs": {
|
||||
"version": "0.8.5",
|
||||
"resolved": "https://registry.npmmirror.com/shelljs/download/shelljs-0.8.5.tgz",
|
||||
"integrity": "sha512-TiwcRcrkhHvbrZbnRcFYMLl30Dfov3HKqzp5tO5b4pt6G/SezKcYhmDg15zXVBswHmctSAQKznqNW2LO5tTDow==",
|
||||
"requires": {
|
||||
"glob": "^7.0.0",
|
||||
"interpret": "^1.0.0",
|
||||
"rechoir": "^0.6.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"interpret": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npm.taobao.org/interpret/download/interpret-1.4.0.tgz",
|
||||
"integrity": "sha1-Zlq4vE2iendKQFhOgS4+D6RbGh4="
|
||||
},
|
||||
"rechoir": {
|
||||
"version": "0.6.2",
|
||||
"resolved": "https://registry.nlark.com/rechoir/download/rechoir-0.6.2.tgz",
|
||||
"integrity": "sha1-hSBLVNuoLVdC4oyWdW70OvUOM4Q=",
|
||||
"requires": {
|
||||
"resolve": "^1.1.6"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"side-channel": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.nlark.com/side-channel/download/side-channel-1.0.4.tgz",
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
"resolve-bin": "^0.4.0",
|
||||
"sass": "^1.26.8",
|
||||
"sass-loader": "^7.3.1",
|
||||
"shelljs": "^0.8.5",
|
||||
"source-map": "^0.7.3",
|
||||
"uglify-es": "^3.3.9",
|
||||
"webpack": "5.48.0",
|
||||
|
||||
@@ -21,7 +21,7 @@ const CUSTOM_THEME_PROP_GROUPS = require('./theme/customThemeStyles');
|
||||
const OHOS_THEME_PROP_GROUPS = require('./theme/ohosStyles');
|
||||
import { mkDir } from './util';
|
||||
|
||||
const FILE_EXT_NAME = ['.js', '.css', '.jsx', '.less', '.sass', '.scss', '.md', '.DS_Store', '.hml'];
|
||||
const FILE_EXT_NAME = ['.js', '.css', '.jsx', '.less', '.sass', '.scss', '.md', '.DS_Store', '.hml', '.json'];
|
||||
const red = '\u001b[31m';
|
||||
const reset = '\u001b[39m';
|
||||
let input = '';
|
||||
@@ -110,10 +110,11 @@ class ResourcePlugin {
|
||||
circularFile(input, output, '../share');
|
||||
});
|
||||
compiler.hooks.normalModuleFactory.tap('OtherEntryOptionPlugin', () => {
|
||||
if (process.env.DEVICE_LEVEL === 'card') {
|
||||
if (process.env.DEVICE_LEVEL === 'card' && process.env.compileMode !== 'moduleJson') {
|
||||
return;
|
||||
}
|
||||
addPageEntryObj();
|
||||
entryObj = Object.assign(entryObj, abilityEntryObj);
|
||||
for (const key in entryObj) {
|
||||
if (!compiler.options.entry[key]) {
|
||||
const singleEntry = new SingleEntryPlugin('', entryObj[key], key);
|
||||
@@ -168,66 +169,119 @@ function addPageEntryObj() {
|
||||
return entryObj;
|
||||
}
|
||||
|
||||
let abilityEntryObj = {};
|
||||
function addAbilityEntryObj(moduleJson) {
|
||||
abilityEntryObj = {};
|
||||
const entranceFiles = readAbilityEntrance(moduleJson);
|
||||
entranceFiles.forEach(filePath => {
|
||||
const key = filePath.replace(/^\.\/js\//, './').replace(/\.js$/, '');
|
||||
abilityEntryObj[key] = path.resolve(proces.env.projectPath, '../', filePath);
|
||||
});
|
||||
return abilityEntryObj;
|
||||
}
|
||||
|
||||
function readAbilityEntrance(moduleJson) {
|
||||
const entranceFiles = [];
|
||||
if (moduleJson.module) {
|
||||
if (moduleJson.module.srcEntrance) {
|
||||
entranceFiles.push(moduleJson.module.srcEntrance);
|
||||
}
|
||||
if (moduleJson.module.abilities && moduleJson.module.abilities.length) {
|
||||
readEntrances(moduleJson.module.abilities, entranceFiles);
|
||||
}
|
||||
if (moduleJson.module.extensionAbilities && moduleJson.module.extensionAbilities.length) {
|
||||
readEntrances(moduleJson.module.extensionAbilities, entranceFiles);
|
||||
}
|
||||
}
|
||||
return entranceFiles;
|
||||
}
|
||||
|
||||
function readEntrances(abilities, entranceFiles) {
|
||||
abilities.forEach(ability => {
|
||||
if ((!ability.type || ability.type !== 'form') && ability.srcEntrance) {
|
||||
entranceFiles.push(ability.srcEntrance);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function readManifest(manifestFilePath) {
|
||||
let manifest = {};
|
||||
try {
|
||||
if (fs.existsSync(manifestFilePath)) {
|
||||
const jsonString = fs.readFileSync(manifestFilePath).toString();
|
||||
manifest = JSON.parse(jsonString);
|
||||
} else if (process.env.aceConfigPath && fs.existsSync(process.env.aceConfigPath)) {
|
||||
buildManifest(manifest, process.env.aceConfigPath);
|
||||
} else if (process.env.aceModuleJsonPath && fs.existsSync(process.env.aceModuleJsonPath)) {
|
||||
buildManifest(manifest);
|
||||
} else {
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or config.json is lost.' +
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or module.json is lost.' +
|
||||
'\u001b[39m').message;
|
||||
}
|
||||
} catch (e) {
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or config.json file format is invalid.' +
|
||||
throw Error('\u001b[31m' + 'ERROR: the manifest.json or module.json file format is invalid.' +
|
||||
'\u001b[39m').message;
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
function buildManifest(manifest, aceConfigPath) {
|
||||
function readModulePages(moduleJson) {
|
||||
if (moduleJson.module.uiSyntax === 'hml' && moduleJson.module.pages) {
|
||||
const modulePagePath = path.resolve(process.env.aceProfilePath,
|
||||
`${moduleJson.module.pages.replace(/\@profile\:/, '')}.json`);
|
||||
if (fs.existsSync(modulePagePath)) {
|
||||
const pagesConfig = JSON.parse(fs.readFileSync(modulePagePath, 'utf-8'));
|
||||
return pagesConfig.src;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function readFormPages(moduleJson) {
|
||||
const pages = [];
|
||||
if (moduleJson.module.extensionAbilities && moduleJson.module.extensionAbilities.length) {
|
||||
moduleJson.module.extensionAbilities.forEach(extensionAbility => {
|
||||
if (extensionAbility.type && extensionAbility.type === 'form' && extensionAbility.metadate &&
|
||||
extensionAbility.metadate.length) {
|
||||
extensionAbility.metadate.forEach(item => {
|
||||
if (item.resource && /\@profile\:/.test(item.resource)) {
|
||||
parseFormConfig(item.resource, pages);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function parseFormConfig(resource, pages) {
|
||||
const resourceFile = path.resolve(process.env.aceProfilePath,
|
||||
`${resource.replace(/\@profile\:/, '')}.json`);
|
||||
if (fs.existsSync(resourceFile)) {
|
||||
const pagesConfig = JSON.parse(fs.readFileSync(resourceFile, 'utf-8'));
|
||||
if (pagesConfig.forms && pagesConfig.forms.length) {
|
||||
pagesConfig.forms.forEach(form => {
|
||||
if (form.src) {
|
||||
pages.push(form.src);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function buildManifest(manifest) {
|
||||
try {
|
||||
const configJson = JSON.parse(fs.readFileSync(aceConfigPath).toString());
|
||||
const srcPath = process.env.srcPath;
|
||||
manifest.type = process.env.abilityType;
|
||||
if (configJson.module && configJson.module.abilities) {
|
||||
manifest.pages = getPages(configJson, srcPath);
|
||||
} else {
|
||||
throw Error('\u001b[31m'+
|
||||
'EERROR: the config.json file miss keyword module || module[abilities].' +
|
||||
'\u001b[39m').message;
|
||||
const moduleJson = JSON.parse(fs.readFileSync(process.env.aceModuleJsonPath).toString());
|
||||
if (process.env.DEVICE_LEVEL === 'rich') {
|
||||
manifest.pages = readModulePages(moduleJson);
|
||||
manifest.abilityEntryObj = addAbilityEntryObj(moduleJson);
|
||||
}
|
||||
if (process.env.DEVICE_LEVEL === 'card') {
|
||||
manifest.pages = readFormPages(moduleJson);
|
||||
}
|
||||
manifest.minPlatformVersion = configJson.app.apiVersion.compatible;
|
||||
} catch (e) {
|
||||
throw Error("\x1B[31m" + 'ERROR: the config.json file is lost or format is invalid.' +
|
||||
throw Error("\x1B[31m" + 'ERROR: the module.json file is lost or format is invalid.' +
|
||||
"\x1B[39m").message;
|
||||
}
|
||||
}
|
||||
|
||||
function getPages(configJson, srcPath) {
|
||||
let pages = []
|
||||
for (const ability of configJson.module.abilities){
|
||||
if (ability.srcPath === srcPath) {
|
||||
readPages(ability, pages, configJson)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
function readPages(ability, pages, configJson) {
|
||||
for (const js of configJson.module.js){
|
||||
if (ability.name === js.name) {
|
||||
js.pages.forEach(page => {
|
||||
pages.push(page)
|
||||
})
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function loadWorker(entryObj) {
|
||||
const workerPath = path.resolve(input, 'workers');
|
||||
if (fs.existsSync(workerPath)) {
|
||||
|
||||
@@ -27,9 +27,13 @@ const { PLATFORM }= require('./lib/lite/lite-enum')
|
||||
const util = require('./lib/util')
|
||||
const TerserPlugin = require('terser-webpack-plugin')
|
||||
const CopyPlugin = require("copy-webpack-plugin")
|
||||
const watchMode = (process.env.watchMode && process.env.watchMode === 'true') || false
|
||||
|
||||
const { deleteFolderRecursive, readManifest, loadEntryObj } = require('./main.product')
|
||||
let watchMode = (process.env.watchMode && process.env.watchMode === 'true') || false
|
||||
const {
|
||||
deleteFolderRecursive,
|
||||
readManifest,
|
||||
loadEntryObj,
|
||||
compileCardModule
|
||||
} = require('./main.product')
|
||||
|
||||
const richModule = {
|
||||
rules: [
|
||||
@@ -174,6 +178,9 @@ let config = {
|
||||
}
|
||||
|
||||
function setConfigs(env) {
|
||||
if (process.env.aceModuleJsonPath || env.aceModuleJsonPath) {
|
||||
process.env.compileMode = 'moduleJson';
|
||||
}
|
||||
process.env.error = env.error === undefined ? true : env.error
|
||||
process.env.warning = env.warning === undefined ? true : env.warning
|
||||
process.env.note = env.note === undefined ? true : env.note
|
||||
@@ -185,11 +192,14 @@ function setConfigs(env) {
|
||||
path.resolve(process.env.projectPath, 'build');
|
||||
process.env.cachePath = env.cachePath || process.env.cachePath || path.resolve(__dirname, 'node_modules/.cache');
|
||||
process.env.aceManifestPath = process.env.aceManifestPath || path.resolve(process.env.projectPath, 'manifest.json');
|
||||
process.env.abilityType = process.env.abilityType || 'page'
|
||||
process.env.DEVICE_LEVEL = process.env.DEVICE_LEVEL || 'rich'
|
||||
process.env.abilityType = process.env.abilityType || 'page';
|
||||
process.env.DEVICE_LEVEL = env.DEVICE_LEVEL || process.env.DEVICE_LEVEL || 'rich';
|
||||
process.env.aceModuleJsonPath = env.aceModuleJsonPath || process.env.aceModuleJsonPath;
|
||||
process.env.aceProfilePath = env.aceProfilePath || process.env.aceProfilePath;
|
||||
watchMode = (process.env.watchMode && process.env.watchMode === 'true') ||
|
||||
(env.watchMode && env.watchMode === 'true') || false;
|
||||
if (process.env.abilityType === 'page' || process.env.abilityType === 'form') {
|
||||
const manifest = readManifest(process.env.aceManifestPath)
|
||||
process.env.DEVICE_LEVEL = manifest.type === 'form' ? 'card' : 'rich'
|
||||
process.env.PLATFORM_VERSION = PLATFORM.VERSION6;
|
||||
const version = parseInt(manifest.minPlatformVersion);
|
||||
if (version == 5) {
|
||||
@@ -226,11 +236,16 @@ function notPreview(env) {
|
||||
}
|
||||
|
||||
module.exports = (env) => {
|
||||
setConfigs(env)
|
||||
deleteFolderRecursive(process.env.buildPath);
|
||||
setConfigs(env);
|
||||
if (process.env.compileMode === 'moduleJson') {
|
||||
compileCardModule(env);
|
||||
config.entry = {};
|
||||
} else {
|
||||
deleteFolderRecursive(process.env.buildPath);
|
||||
config.entry = loadEntryObj(process.env.projectPath, process.env.DEVICE_LEVEL,
|
||||
process.env.abilityType, process.env.aceManifestPath);
|
||||
}
|
||||
config.cache.cacheDirectory = path.resolve(process.env.cachePath, '.rich_cache');
|
||||
config.entry = loadEntryObj(process.env.projectPath, process.env.DEVICE_LEVEL,
|
||||
process.env.abilityType, process.env.aceManifestPath)
|
||||
config.output.path = path.resolve(__dirname, process.env.buildPath)
|
||||
config.plugins = [
|
||||
new ResourcePlugin(process.env.projectPath, process.env.buildPath, process.env.aceManifestPath),
|
||||
|
||||
Reference in New Issue
Block a user