1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const path = require('path'); 17const fs = require('fs'); 18const webpack = require('webpack'); 19const TerserPlugin = require('terser-webpack-plugin'); 20const SnapshotPlugin = require('./lib/lite/lite-snapshot-plugin'); 21const ImageCoverterPlugin = require('./lib/lite/lite-image-coverter-plugin'); 22const ReturnExportsPlugin = require('./lib/lite/lite-return-exports-plugin'); 23const ResourcePlugin = require('./lib/resource-plugin'); 24const ResultStates = require('./lib/compile-plugin'); 25const { checkFilePath } = require('./lib/lite/lite-customize'); 26const CopyPlugin = require("copy-webpack-plugin"); 27 28process.env.DEVICE_LEVEL = 'lite'; 29process.env.DEVICE_TYPE = 'smartVision'; 30 31const watchMode = (process.env.watchMode && process.env.watchMode === 'true') || false; 32const pictrueSwitch = process.env.img2bin !== 'false'; 33const util = require('./lib/util'); 34const { PLATFORM }= require('./lib/lite/lite-enum'); 35const { deleteFolderRecursive, readManifest, loadEntryObj } = require('./main.product'); 36 37const webpackConfig = { 38 target: ['web', 'es5'], 39 cache: { 40 type: 'filesystem' 41 }, 42 watch: watchMode, 43 watchOptions: { 44 aggregateTimeout: 10, 45 poll: false, 46 ignored: /node_modules/, 47 }, 48 output: { 49 filename: '[name].js', 50 devtoolModuleFilenameTemplate: 'webpack:///[absolute-resource-path]' 51 }, 52 devtool: 'nosources-source-map', 53 mode: 'development', 54 module: { 55 rules: [ 56 { 57 test: /(\.hml|app\.js)(\?[^?]+)?$/, 58 use: [{ 59 loader: path.resolve(__dirname, './index.js') 60 }] 61 }, 62 { 63 test: /\.png$/, 64 use: [{ 65 loader: 'file-loader', 66 options: { 67 name: '[name].[ext]', outputPath: 'common' 68 } 69 }] 70 }, 71 { 72 test: /\.css$/, 73 use: [{ 74 loader: 'css-loader' 75 }] 76 }, 77 { 78 test: /\.less$/, 79 use: [{ 80 loader: 'less-loader' 81 }] 82 }, 83 { 84 test: /\.(scss|sass)$/, 85 use: [{ 86 loader: 'style-loader!css-loader!sass-loader' 87 }] 88 }, 89 { 90 test: /\.jsx?$/, 91 use: [ 92 { 93 loader: path.resolve(__dirname, 'lib/module-script.js'), 94 }, 95 { 96 loader: util.loadBabelModule('babel-loader'), 97 options: { 98 presets: [util.loadBabelModule('@babel/preset-env')], 99 plugins: [util.loadBabelModule('@babel/plugin-transform-modules-commonjs'), 100 util.loadBabelModule('@babel/plugin-proposal-class-properties')], 101 }, 102 }, 103 ] 104 } 105 ] 106 }, 107 node: { 108 global: false, 109 }, 110 stats: 'none', 111}; 112 113function setConfigs(env) { 114 process.env.error = env.error === undefined ? true : env.error 115 process.env.warning = env.warning === undefined ? true : env.warning 116 process.env.note = env.note === undefined ? true : env.note 117 process.env.buildMode = env.buildMode || 'debug' 118 process.env.logLevel = env.logLevel || '1' 119 process.env.projectPath = env.aceModuleRoot || process.env.aceModuleRoot || process.cwd(); 120 process.env.buildPath = env.aceModuleBuild || process.env.aceModuleBuild || path.resolve(process.env.projectPath, 'build'); 121 process.env.cachePath = env.cachePath || process.env.cachePath || path.resolve(__dirname, 'node_modules/.cache'); 122 process.env.aceManifestPath = process.env.aceManifestPath || path.resolve(process.env.projectPath, 'manifest.json'); 123 process.env.watchCSSFiles = process.env.watchCSSFiles || path.resolve(process.env.buildPath, 'preview_css.json'); 124 process.env.abilityType = 'page'; 125 const manifest = readManifest(process.env.aceManifestPath) 126 process.env.PLATFORM_VERSION = PLATFORM.VERSION6; 127 const version = parseInt(manifest.minPlatformVersion); 128 if (version == 5) { 129 process.env.PLATFORM_VERSION = PLATFORM.VERSION5; 130 } 131 if (version <= 4) { 132 process.env.PLATFORM_VERSION = PLATFORM.VERSION3; 133 } 134 process.env.PLATFORM_VERSION_VERSION = version; 135} 136 137module.exports = (env) => { 138 setConfigs(env) 139 deleteFolderRecursive(process.env.buildPath); 140 webpackConfig.cache.cacheDirectory = path.resolve(process.env.cachePath, '.lite_cache'); 141 webpackConfig.entry = loadEntryObj(process.env.projectPath, process.env.DEVICE_LEVEL, 142 process.env.abilityType, process.env.aceManifestPath) 143 webpackConfig.output.path = path.resolve(__dirname, process.env.buildPath) 144 webpackConfig.plugins = [ 145 new ResourcePlugin(process.env.projectPath, process.env.buildPath, 146 process.env.aceManifestPath, process.env.watchCSSFiles), 147 new ResultStates({ 148 build: process.env.buildPath 149 }), 150 new ReturnExportsPlugin(), 151 new webpack.DefinePlugin({ 152 STANDARD: JSON.stringify(false), 153 LITE: JSON.stringify(true) 154 }) 155 ] 156 webpackConfig.resolve = { 157 modules: [ 158 process.env.projectPath, 159 path.join(process.env.projectPath, '../../../../../'), 160 path.join(__dirname, 'node_modules'), 161 './node_modules' 162 ] 163 } 164 if (fs.existsSync(path.resolve(process.env.projectPath, 'i18n'))) { 165 webpackConfig.plugins.push(new CopyPlugin({ 166 patterns: [ 167 { 168 from: path.resolve(process.env.projectPath, 'i18n'), 169 to: path.resolve(process.env.buildPath, 'i18n'), 170 noErrorOnMissing: true 171 } 172 ] 173 })) 174 } 175 176 if (process.env.hapMode && process.env.hapMode === 'true') { 177 webpackConfig.optimization = { 178 minimize: true, 179 minimizer: [new TerserPlugin({ 180 terserOptions: { 181 compress: false, 182 mangle: true, 183 }, 184 }, 185 )], 186 }; 187 } 188 189 if (pictrueSwitch) { 190 webpackConfig.plugins.push( 191 new ImageCoverterPlugin({ build: process.env.buildPath }), 192 ); 193 } 194 195 if (env.deviceType) { 196 const deviceArr = env.deviceType.split(/,/); 197 if (deviceArr.includes('liteWearable')) { 198 process.env.DEVICE_TYPE = 'liteWearable'; 199 } 200 } 201 202 if (env.sourceMap === 'none') { 203 webpackConfig.devtool = false 204 } 205 if (env.buildMode === 'release') { 206 webpackConfig.devtool = 'source-map' 207 webpackConfig.mode = 'production'; 208 webpackConfig.plugins.push( 209 new SnapshotPlugin({ build: process.env.buildPath }) 210 ); 211 webpackConfig.output.sourceMapFilename = '_releaseMap/[name].js.map' 212 } 213 checkFilePath(); 214 return webpackConfig; 215}; 216