diff --git a/compiler/main.js b/compiler/main.js index b63f46599cfce43107c7625f8f063b24ef185f1d..18f2b710d9ffe7fe90081250e44dda7cb58defb6 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -31,6 +31,10 @@ configure({ const logger = getLogger('ETS'); const staticPreviewPage = process.env.aceStaticPreview; +const abilityConfig = { + abilityType: process.env.abilityType || 'page', + abilityEntryFile: null +}; const projectConfig = {}; const resources = { app: {}, @@ -49,24 +53,18 @@ function initProjectConfig(projectConfig) { function loadEntryObj(projectConfig) { initProjectConfig(projectConfig); - const appEtsPath = path.join(projectConfig.projectPath, 'app.ets'); - let manifest = {}; - try { - const jsonString = fs.readFileSync(projectConfig.manifestFilePath).toString(); - manifest = JSON.parse(jsonString); - } catch (error) { - throw Error('\u001b[31m ERROR: the manifest file is lost or format is invalid. \u001b[39m').message; - } - - if (!fs.existsSync(appEtsPath)) { - throw Error('\u001b[31m ERROR: missing app.ets. \u001b[39m').message; - } - projectConfig.entryObj['./app'] = projectConfig.projectPath + '/app.ets?entry'; - + setEntryFile(projectConfig); if(staticPreviewPage) { projectConfig.entryObj['./' + staticPreviewPage] = projectConfig.projectPath + path.sep + staticPreviewPage + '.ets?entry'; - } else { + } else if (abilityConfig.abilityType === 'page') { + let manifest = {}; + try { + const jsonString = fs.readFileSync(projectConfig.manifestFilePath).toString(); + manifest = JSON.parse(jsonString); + } catch (error) { + throw Error(`\u001b[31m ERROR: the manifest file '${projectConfig.manifestFilePath}' is lost or format is invalid. \u001b[39m`).message; + } if (manifest.pages) { const pages = manifest.pages; pages.forEach((element) => { @@ -78,11 +76,22 @@ function loadEntryObj(projectConfig) { } }); } else { - throw Error('\u001b[31m ERROR: missing pages attribute in manifest.json. \u001b[39m').message; + throw Error(`\u001b[31m ERROR: missing pages attribute in '${projectConfig.manifestFilePath}'. \u001b[39m`).message; } } } +function setEntryFile(projectConfig) { + const entryFileName = abilityConfig.abilityType === 'page' ? 'app' : abilityConfig.abilityType; + const extendFile = entryFileName === 'app' ? '.ets' : '.ts'; + abilityConfig.abilityEntryFile = entryFileName + extendFile; + const entryFilePath = path.join(projectConfig.projectPath, abilityConfig.abilityEntryFile); + if (!fs.existsSync(entryFilePath)) { + throw Error(`\u001b[31m ERROR: missing ${entryFilePath}. \u001b[39m`).message; + } + projectConfig.entryObj[`./${entryFileName}`] = projectConfig.projectPath + `/${abilityConfig.abilityEntryFile}?entry`; +} + function loadWorker(projectConfig) { const workerPath = path.resolve(projectConfig.projectPath, WORKERS_DIR); if (fs.existsSync(workerPath)) { @@ -141,3 +150,4 @@ exports.loadEntryObj = loadEntryObj; exports.readAppResource = readAppResource; exports.resources = resources; exports.loadWorker = loadWorker; +exports.abilityConfig = abilityConfig; diff --git a/compiler/src/process_ability_entry_file.ts b/compiler/src/process_ability_entry_file.ts new file mode 100644 index 0000000000000000000000000000000000000000..00b67886ac60a30a2f8e250c4e94d3560a8edcc5 --- /dev/null +++ b/compiler/src/process_ability_entry_file.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import path from 'path'; + +import { abilityConfig } from '../main'; + +module.exports = function processAbilityEntryFile(source: string): string { + if (path.basename(this.resourcePath) === abilityConfig.abilityEntryFile) { + source = source.replace(/exports\.default/, 'globalThis.exports.default'); + } + return source; +} \ No newline at end of file diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 9be4d648eb22c197cef8fb82e824f2de5d33ef08..1a267d47ccaf547bb18afcc1514cff75b861007e 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -24,7 +24,8 @@ const { loadEntryObj, readAppResource, resources, - loadWorker + loadWorker, + abilityConfig } = require('./main'); const { ResultStates } = require('./lib/compile_info'); const { processUISyntax } = require('./lib/process_ui_syntax'); @@ -77,6 +78,7 @@ function initConfig(config) { test: /\.ts$/, exclude: /node_modules/, use: [ + { loader: path.resolve(__dirname, 'lib/process_ability_entry_file.js') }, { loader: 'ts-loader', options: { @@ -116,24 +118,6 @@ function initConfig(config) { /\.d\.ts$/ ]}), new CleanWebpackPlugin(), - new CopyPlugin({ - patterns: [ - { - from: '**/*', - context: path.resolve(__dirname, projectConfig.projectPath), - globOptions: { - ignore: [ - '**/*.ets', - '**/*.ts', - '**/*.js', - path.resolve(__dirname, projectConfig.buildPath, '**').replace(/\\/g, '/') - ] - }, - noErrorOnMissing: true - }, - { from: projectConfig.manifestFilePath } - ] - }), new ResultStates() ] }) @@ -155,27 +139,8 @@ function setProjectConfig(envArgs) { } } -module.exports = (env, argv) => { - const config = {}; - setProjectConfig(env); - loadEntryObj(projectConfig); - initConfig(config); - - if (env.isPreview !== "true") { - loadWorker(projectConfig); - if (env.compilerType && env.compilerType === 'ark') { - config.plugins.push(new GenAbcPlugin(projectConfig.buildPath, path.join(__dirname, 'bin'), env.buildMode === 'debug')); - } - } else { - projectConfig.isPreview = true; - } - - if (env.sourceMap === 'none') { - config.devtool = false; - } - - if (env.buildMode === 'release') { - const TerserPlugin = require('terser-webpack-plugin'); +function setReleaseConfig(config) { + const TerserPlugin = require('terser-webpack-plugin'); config.mode = 'production'; config.optimization = { emitOnErrors: true, @@ -201,6 +166,51 @@ module.exports = (env, argv) => { ] }; config.output.sourceMapFilename = '_releaseMap/[name].js.map'; +} + +function setCopyPluginConfig(config) { + const copyPluginPattrens = []; + copyPluginPattrens.push({ + from: '**/*', + context: path.resolve(__dirname, projectConfig.projectPath), + globOptions: { + ignore: [ + '**/*.ets', + '**/*.ts', + '**/*.js', + path.resolve(__dirname, projectConfig.buildPath, '**').replace(/\\/g, '/') + ] + }, + noErrorOnMissing: true + }); + if (abilityConfig.abilityType === 'page') { + copyPluginPattrens.push({ from: projectConfig.manifestFilePath }); + } + config.plugins.push(new CopyPlugin({ patterns: copyPluginPattrens })); +} + +module.exports = (env, argv) => { + const config = {}; + setProjectConfig(env); + loadEntryObj(projectConfig); + initConfig(config); + setCopyPluginConfig(config); + + if (env.isPreview !== "true") { + loadWorker(projectConfig); + if (env.compilerType && env.compilerType === 'ark') { + config.plugins.push(new GenAbcPlugin(projectConfig.buildPath, path.join(__dirname, 'bin'), env.buildMode === 'debug')); + } + } else { + projectConfig.isPreview = true; + } + + if (env.sourceMap === 'none') { + config.devtool = false; + } + + if (env.buildMode === 'release') { + setReleaseConfig(config); } const appResourcePath = env.appResource || process.env.appResource;