/* * Copyright (c) 2020 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. */ const path = require('path'); const fs = require('fs'); const CopyPlugin = require('copy-webpack-plugin'); const Webpack = require('webpack'); const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const { GenAbcPlugin } = require('./lib/gen_abc_plugin'); const buildPipeServer = require('./server/build_pipe_server'); const { projectConfig, loadEntryObj, loadWorker, abilityConfig, readWorkerFile, loadModuleInfo, checkAppResourcePath, addSDKBuildDependencies, readPatchConfig, getCleanConfig } = require('./main'); const { ResultStates } = require('./lib/compile_info'); const { processUISyntax } = require('./lib/process_ui_syntax'); const { IGNORE_ERROR_CODE, removeDir, mkdirsSync, getResolveModules } = require('./lib/utils'); const { BUILD_SHARE_PATH, PREBUILDINFO_JSON } = require('./lib/pre_define'); process.env.watchMode = (process.env.watchMode && process.env.watchMode === 'true') || 'false'; function initConfig(config) { const projectPath = path.resolve(projectConfig.projectPath); Object.assign(config, { entry: projectConfig.entryObj, watch: process.env.watchMode === 'true', watchOptions: { aggregateTimeout: 10, poll: false, ignored: /node_modules/ }, output: { path: path.resolve(__dirname, projectConfig.buildPath), filename: '[name].js', devtoolModuleFilenameTemplate: 'webpack:///[absolute-resource-path]', globalObject: 'globalThis' }, devtool: 'nosources-source-map', mode: 'development', module: { rules: [ { test:/\.(jpg|png|gif|jpeg|mp3|mp4|svg)$/, use:{ loader:'url-loader' } }, { test: /\.d\.ts/, loader: 'ignore-loader' }, { test: /(? { return `webpack:///${info.absoluteResourcePath.replace(projectConfig.projectRootPath, '')}`; }; config.output.sourceMapFilename = '_releaseMap/[name].js.map'; config.performance = { hints: false }; } function setCopyPluginConfig(config, appResource, isPreview) { const copyPluginPattrens = []; copyPluginPattrens.push({ from: '**/*', context: path.resolve(__dirname, projectConfig.projectPath), globOptions: { ignore: [ '**/*.ets', '**/*.ts', '**/*.js', path.resolve(__dirname, projectConfig.buildPath, '**').replace(/\\/g, '/') ] }, to: path.resolve(__dirname, projectConfig.buildPath), noErrorOnMissing: true }); const sharePath = path.resolve(__dirname, projectConfig.projectPath, BUILD_SHARE_PATH); if (fs.existsSync(sharePath)) { copyPluginPattrens.push({ from: '**/*', context: path.resolve(__dirname, projectConfig.projectPath, BUILD_SHARE_PATH), to: path.resolve(__dirname, projectConfig.buildPath, BUILD_SHARE_PATH), globOptions: { ignore: [ '**/*.ets', '**/*.ts', '**/*.js', ] }, noErrorOnMissing: true }); } if (abilityConfig.abilityType === 'page') { if (fs.existsSync(projectConfig.manifestFilePath)) { copyPluginPattrens.push({ from: projectConfig.manifestFilePath, to: path.resolve(__dirname, projectConfig.buildPath) }); } else if (fs.existsSync(projectConfig.aceConfigPath)) { copyPluginPattrens.push({ from: projectConfig.aceConfigPath, to: path.resolve(__dirname, projectConfig.buildPath) }); } } if (appResource && fs.existsSync(appResource) && !projectConfig.xtsMode && isPreview === 'true') { copyPluginPattrens.push({ from: path.resolve(__dirname, appResource), to: path.resolve(__dirname, projectConfig.cachePath) }); } config.plugins.push(new CopyPlugin({ patterns: copyPluginPattrens })); } function excludeWorker(workerFile, name) { if (workerFile) { return Object.keys(workerFile).includes(name); } return /^\.\/workers\//.test(name); } function setOptimizationConfig(config, workerFile) { if ((process.env.compileMode !== 'moduleJson' || projectConfig.splitCommon) && abilityConfig.abilityType === 'page') { config.optimization = { splitChunks: { chunks(chunk) { return !excludeWorker(workerFile, chunk.name) && !/^\.\/TestAbility/.test(chunk.name); }, minSize: 0, cacheGroups: { vendors: { test: /[\\/]node_modules[\\/]/, priority: -10, name: "vendors", }, commons: { name: 'commons', priority: -20, minChunks: 2, reuseExistingChunk: true } } } } } } function setGenAbcPlugin(env, config) { process.env.compilerType = 'ark'; process.env.panda = projectConfig.pandaMode; let arkDir = path.join(__dirname, 'bin', 'ark'); if (env.arkFrontendDir) { arkDir = env.arkFrontendDir; } let nodeJs = 'node'; if (env.nodeJs) { nodeJs = env.nodeJs; } config.plugins.push(new GenAbcPlugin(projectConfig.buildPath, arkDir, nodeJs, env.buildMode === 'debug')); if (env.buildMode === 'release') { config.output.path = path.join(projectConfig.cachePath, 'releaseAssets', path.basename(projectConfig.buildPath)) } } function setCleanWebpackPlugin(workerFile, config) { config.plugins.push( new CleanWebpackPlugin({ dry: false, dangerouslyAllowCleanPatternsOutsideProject: true, cleanOnceBeforeBuildPatterns: getCleanConfig(workerFile) }) ); } function clearWebpackCacheByBuildInfo() { if (!projectConfig.cachePath || projectConfig.xtsMode) { return; } // clear&update cache dir when build info is different from last time const cachePrebuildInfoPath = path.join(projectConfig.cachePath, PREBUILDINFO_JSON); if (fs.existsSync(cachePrebuildInfoPath)) { let cachedJson = undefined; try { cachedJson = JSON.parse(fs.readFileSync(cachePrebuildInfoPath).toString()); } catch { removeDir(projectConfig.cachePath); mkdirsSync(projectConfig.cachePath); } if (projectConfig.compileMode === 'esmodule' && cachedJson && (cachedJson.buildMode && cachedJson.buildMode !== projectConfig.buildArkMode || cachedJson.bundleName && cachedJson.bundleName !== projectConfig.bundleName || cachedJson.moduleName && cachedJson.moduleName !== projectConfig.moduleName)) { removeDir(projectConfig.cachePath); mkdirsSync(projectConfig.cachePath); } // api version is 8 or 9 if (projectConfig.compileMode === 'jsbundle' && cachedJson && cachedJson.minAPIVersion && cachedJson.minAPIVersion.toString() !== process.env.minPlatformVersion) { removeDir(projectConfig.cachePath); mkdirsSync(projectConfig.cachePath); } } } module.exports = (env, argv) => { const config = {}; setProjectConfig(env); loadEntryObj(projectConfig); loadModuleInfo(projectConfig, env); clearWebpackCacheByBuildInfo(); initConfig(config); readPatchConfig(); const workerFile = readWorkerFile(); setOptimizationConfig(config, workerFile); setCleanWebpackPlugin(workerFile, config); if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile); if (env.compilerType && env.compilerType === 'ark' && !projectConfig.compileHar) { setGenAbcPlugin(env, config); } } else { projectConfig.isPreview = true; projectConfig.checkEntry = env.checkEntry; setGenAbcPlugin(env, config); let port; process.argv.forEach((val, index) => { if (val.startsWith('port=')) { port = val.split('=')[1]; } }); if (port) { buildPipeServer.init(port); } } if (projectConfig.compileHar && env.obfuscate === 'uglify') { projectConfig.obfuscateHarType = 'uglify'; } if (env.sourceMap === 'none') { config.devtool = false; } if (env.buildMode === 'release' && projectConfig.compileMode !== 'esmodule') { setReleaseConfig(config); } if (projectConfig.compileMode === 'esmodule' && projectConfig.harNameOhmMap) { config.externals = []; for (const harName in projectConfig.harNameOhmMap) { config.externals.push(RegExp('^(' + harName + ')($|\\/\\S+)')); } } const appResourcePath = env.appResource || process.env.appResource; checkAppResourcePath(appResourcePath, config); setCopyPluginConfig(config, appResourcePath, env.isPreview); addSDKBuildDependencies(config); config.output.library = projectConfig.hashProjectPath; return config; }