1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 3× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× | const path = require('path'); const webpack = require('webpack'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const {getBabelPath, getExcludeBabelPath, getGlobalCheckWhiteList, getFreePort} = require('./utils'); var UglifyJsPlugin = require('uglifyjs-webpack-plugin') var OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin') const ChameleonWebpackPlugin = require('chameleon-webpack-plugin') const WebpackCheckPlugin = require('webpack-check-plugin') const config = require('./config.js'); const ChameleonErrorsWebpackPlugin = require('chameleon-errors-webpack-plugin'); module.exports = function (options) { let { type, media, root } = options; function getstaticPath(filetype) { return `static/${filetype}/[name]_[hash:7].[ext]` } let webServerPort = getFreePort().webServerPort; let publicPath; let defaultPublichPathMap = { 'wx': '/', 'alipay': '/', 'baidu': `http://${config.ip}:${webServerPort}/baidu/`, // baidu小程序的publicPath不能设置能/ 所以在启动dev服务的时候 也将dist作为静态资源 'web': `http://${config.ip}:${webServerPort}/`, 'weex': `http://${config.ip}:${webServerPort}/weex/` } publicPath = options.publicPath || defaultPublichPathMap[type]; let commonConfig = { stats: cml.logLevel === 'debug' ? 'verbose' : 'none', output: { publicPath: publicPath }, resolve: { symlinks: false, extensions: ['.cml', '.interface', '.vue', '.js'], alias: { '$CMLPROJECT': path.resolve(cml.root), '$PROJECT': path.resolve(root), '$ROUTER': path.resolve(root, 'node_modules/chameleon-runtime/.temp/router.js'), '$ROUTER_CONFIG': path.resolve(root, './src/router.config.json') }, modules: [ 'node_modules', path.join(cml.root, '/node_modules') ] }, resolveLoader: { modules: [ 'node_modules', path.join(cml.root, '/node_modules') ] }, module: { rules: [{ test: path.resolve(root, 'node_modules/chameleon-runtime/.temp/router.js'), loader: path.join(__dirname, 'routerLoader.js') }, { test: /\.js$/, exclude: getExcludeBabelPath(), // 不能babel babel-runtime include: getBabelPath(), use: [{ loader: 'babel-loader', options: { 'filename': path.join(cml.root, 'chameleon.js') } } ] }, { test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, loader: 'chameleon-url-loader', options: { limit: false, // 不做limit的base64转换,需要添加?inline参数 name: getstaticPath('img') } }, { test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, loader: 'file-loader', options: { name: getstaticPath('media') } }, { test: /\.(woff|woff2?|eot|ttf|otf)(\?.*)?$/, loader: 'file-loader', options: { name: getstaticPath('fonts') } }, { test: /\.interface$/, // exclude: /(node_modules|bower_components)/, // 不能babel babel-runtime include: getBabelPath(), use: [{ loader: 'babel-loader', options: { 'filename': path.join(cml.root, 'chameleon.js') } }, { loader: 'interface-loader', options: { cmlType: type, media, check: cml.config.get().check } } ] } ] }, plugins: [ new webpack.DefinePlugin({ 'process.env.platform': JSON.stringify(type) }), new ChameleonErrorsWebpackPlugin({ cmlType: type }) ] } Iif (cml.config.get().enableGlobalCheck === true) { commonConfig.plugins.push( new WebpackCheckPlugin({ cmlType: type, whiteListFile: getGlobalCheckWhiteList() }) ) } Eif (options.definePlugin) { commonConfig.plugins.push(new webpack.DefinePlugin(options.definePlugin)) } Iif (options.analysis) { commonConfig.plugins.push(new BundleAnalyzerPlugin()) } let devApiPrefix = `http://${config.ip}:${webServerPort}` // 兼容旧版api let apiPrefix = options.apiPrefix || devApiPrefix; // 新版api 优先读取domainMap let domainMap = (cml.config.get().domainMap && cml.config.get().domainMap[cml.media]) || { apiPrefix }; let defaultDomainKey = cml.config.get().defaultDomainKey || 'apiPrefix'; Eif (options.media === 'dev') { // dev模式默认apiPrefix commonConfig.plugins.push(new webpack.DefinePlugin({ 'process.env.devApiPrefix': JSON.stringify(devApiPrefix) })) } // 兼容旧版api commonConfig.plugins.push(new webpack.DefinePlugin({ 'process.env.cmlApiPrefix': JSON.stringify(apiPrefix) })) commonConfig.plugins.push(new webpack.DefinePlugin({ 'process.env.domainMap': JSON.stringify(domainMap) })) commonConfig.plugins.push(new webpack.DefinePlugin({ 'process.env.defaultDomainKey': JSON.stringify(defaultDomainKey) })) commonConfig.plugins.push(new webpack.DefinePlugin({ 'process.env.media': JSON.stringify(options.media) })) Iif (options.minimize) { commonConfig.plugins = commonConfig.plugins.concat([ new OptimizeCSSPlugin({ assetNameRegExp: /\.css$/, cssProcessorOptions: { safe: true, discardComments: { removeAll: true } } }), new UglifyJsPlugin({}) ]) } let moduleIdType = options.moduleIdType let moduleIdMap = { hash: new webpack.HashedModuleIdsPlugin(), name: new webpack.NamedModulesPlugin(), chameleon: new ChameleonWebpackPlugin({openModuleHash: true, openChunkHash: true}) } Eif (moduleIdType && moduleIdMap[moduleIdType]) { commonConfig.plugins.push(moduleIdMap[moduleIdType]) } return commonConfig; } |