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, checkMultiResourceBuild } = 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 process.env.aceBuildJson = env.aceBuildJson || process.env.aceBuildJson; 136 checkMultiResourceBuild(process.env.aceBuildJson); 137} 138 139module.exports = (env) => { 140 setConfigs(env) 141 deleteFolderRecursive(process.env.buildPath); 142 webpackConfig.cache.cacheDirectory = path.resolve(process.env.cachePath, '.lite_cache'); 143 webpackConfig.entry = loadEntryObj(process.env.projectPath, process.env.DEVICE_LEVEL, 144 process.env.abilityType, process.env.aceManifestPath) 145 webpackConfig.output.path = path.resolve(__dirname, process.env.buildPath) 146 webpackConfig.plugins = [ 147 new ResourcePlugin(process.env.projectPath, process.env.buildPath, 148 process.env.aceManifestPath, process.env.watchCSSFiles), 149 new ResultStates({ 150 build: process.env.buildPath 151 }), 152 new ReturnExportsPlugin(), 153 new webpack.DefinePlugin({ 154 STANDARD: JSON.stringify(false), 155 LITE: JSON.stringify(true) 156 }) 157 ] 158 webpackConfig.resolve = { 159 modules: [ 160 process.env.projectPath, 161 path.join(process.env.projectPath, '../../../../../'), 162 path.join(__dirname, 'node_modules'), 163 './node_modules' 164 ] 165 } 166 if (fs.existsSync(path.resolve(process.env.projectPath, 'i18n'))) { 167 webpackConfig.plugins.push(new CopyPlugin({ 168 patterns: [ 169 { 170 from: path.resolve(process.env.projectPath, 'i18n'), 171 to: path.resolve(process.env.buildPath, 'i18n'), 172 noErrorOnMissing: true 173 } 174 ] 175 })) 176 } 177 178 if (process.env.hapMode && process.env.hapMode === 'true') { 179 webpackConfig.optimization = { 180 minimize: true, 181 minimizer: [new TerserPlugin({ 182 terserOptions: { 183 compress: false, 184 mangle: true, 185 }, 186 }, 187 )], 188 }; 189 } 190 191 if (pictrueSwitch) { 192 webpackConfig.plugins.push( 193 new ImageCoverterPlugin({ build: process.env.buildPath }), 194 ); 195 } 196 197 if (env.deviceType) { 198 const deviceArr = env.deviceType.split(/,/); 199 if (deviceArr.includes('liteWearable')) { 200 process.env.DEVICE_TYPE = 'liteWearable'; 201 } 202 } 203 204 if (env.sourceMap === 'none') { 205 webpackConfig.devtool = false 206 } 207 if (env.buildMode === 'release') { 208 webpackConfig.devtool = 'source-map' 209 webpackConfig.mode = 'production'; 210 webpackConfig.plugins.push( 211 new SnapshotPlugin({ build: process.env.buildPath }) 212 ); 213 webpackConfig.output.sourceMapFilename = '_releaseMap/[name].js.map' 214 } 215 checkFilePath(); 216 return webpackConfig; 217}; 218