diff --git a/ace-loader/main.product.js b/ace-loader/main.product.js index 36b123d5248febb00c0623d5394de64adaa88ba1..1c651aa98fee5df1cc250c299da9fb649114a56f 100644 --- a/ace-loader/main.product.js +++ b/ace-loader/main.product.js @@ -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,64 +42,12 @@ 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.' + - '\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.' + + throw Error('\u001b[31m' + 'ERROR: the manifest.json file 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; - } + return manifest; } function loadEntryObj(projectPath, device_level, abilityType, manifestFilePath) { @@ -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 = `cross-env DEVICE_LEVEL=card projectPath=${process.env.projectPath} ` + + `aceModuleJsonPath=${process.env.aceModuleJsonPath} aceProfilePath=${process.env.aceProfilePath} ` + + `watchMode=${process.env.watchMode} cachePath=${process.env.cachePath} ` + + `aceModuleBuild=${process.env.buildPath} ` + + `webpack --config webpack.rich.config.js --env compilerType=${env.compilerType}`; + 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 }; \ No newline at end of file diff --git a/ace-loader/package-lock.json b/ace-loader/package-lock.json index bd192d0d5e1278f9ae8881aec6fb67ca7f6f728b..bec341c7e2249208409b15804f0fb87305ac598f 100644 --- a/ace-loader/package-lock.json +++ b/ace-loader/package-lock.json @@ -2474,6 +2474,14 @@ "integrity": "sha1-pgQtNjTCsn6TKPg3uWX6yDgI24U=", "optional": true }, + "cross-env": { + "version": "7.0.3", + "resolved": "https://registry.nlark.com/cross-env/download/cross-env-7.0.3.tgz", + "integrity": "sha1-hlJkspZ33AFbqEGJGJZd0jL8VM8=", + "requires": { + "cross-spawn": "^7.0.1" + } + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.nlark.com/cross-spawn/download/cross-spawn-7.0.3.tgz", @@ -5430,6 +5438,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", diff --git a/ace-loader/package.json b/ace-loader/package.json index 8f8c52a82e48acd5207b8f68e8a4249dd976acdc..c6bb0c203a1b1a640a648cea94b7a80d2637843e 100644 --- a/ace-loader/package.json +++ b/ace-loader/package.json @@ -36,6 +36,7 @@ "babel-loader": "^8.0.6", "babel-plugin-require-context-hook": "^1.0.0", "copy-webpack-plugin": "^8.1.0", + "cross-env": "^7.0.3", "css": "^3.0.0", "css-loader": "^3.4.2", "deccjsunit": "^1.0.6", @@ -51,6 +52,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", diff --git a/ace-loader/src/resource-plugin.js b/ace-loader/src/resource-plugin.js index 58fccb07893a1261001c04fa50d95aad9b945078..c78a6d5391033d841c7775898bda2af84eb7f658 100644 --- a/ace-loader/src/resource-plugin.js +++ b/ace-loader/src/resource-plugin.js @@ -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,8 @@ class ResourcePlugin { circularFile(input, output, '../share'); }); compiler.hooks.normalModuleFactory.tap('OtherEntryOptionPlugin', () => { - if (process.env.DEVICE_LEVEL === 'card') { - 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,63 +166,116 @@ 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) { - 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; +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; } - manifest.minPlatformVersion = configJson.app.apiVersion.compatible; - } catch (e) { - throw Error("\x1B[31m" + 'ERROR: the config.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; +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); + } + }); } } - 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 buildManifest(manifest) { + try { + 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 module.json file is lost or format is invalid.' + + "\x1B[39m").message; } } diff --git a/ace-loader/webpack.rich.config.js b/ace-loader/webpack.rich.config.js index ecc9cc2f7614667f9d20a1aa029afa999db1d0d1..1620711f8d517cdbc66636e62eda5b50e87f7ab0 100644 --- a/ace-loader/webpack.rich.config.js +++ b/ace-loader/webpack.rich.config.js @@ -29,7 +29,12 @@ 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') +const { + deleteFolderRecursive, + readManifest, + loadEntryObj, + compileCardModule +} = require('./main.product') const richModule = { rules: [ @@ -189,7 +194,7 @@ function setConfigs(env) { process.env.DEVICE_LEVEL = process.env.DEVICE_LEVEL || 'rich' 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.DEVICE_LEVEL = manifest.type === 'form' ? 'card' : 'rich' process.env.PLATFORM_VERSION = PLATFORM.VERSION6; const version = parseInt(manifest.minPlatformVersion); if (version == 5) { @@ -226,11 +231,18 @@ function notPreview(env) { } module.exports = (env) => { - setConfigs(env) - deleteFolderRecursive(process.env.buildPath); + setConfigs(env); + if (process.env.aceModuleJsonPath) { + compileCardModule(env); + } 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) + if (fs.existsSync(process.env.aceManifestPath) && !process.env.compileCardModule) { + deleteFolderRecursive(process.env.buildPath); + config.entry = loadEntryObj(process.env.projectPath, process.env.DEVICE_LEVEL, + process.env.abilityType, process.env.aceManifestPath); + } else { + config.entry = {}; + } config.output.path = path.resolve(__dirname, process.env.buildPath) config.plugins = [ new ResourcePlugin(process.env.projectPath, process.env.buildPath, process.env.aceManifestPath),