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 205 206 207 208
|
const path = require("path"); require("babel-polyfill"); const fs = require("fs"); const CompressionWebpackPlugin = require("compression-webpack-plugin"); const argv = require("yargs").argv;
function resolve(dir) { return path.join(__dirname, dir); } const PRODUCTION = process.env.NODE_ENV === "production";
const serverType = PRODUCTION ? (argv["test_server"] ? 2 : 1) : 0;
const APP_VERSION = (process.env.VUE_APP_VERSION = process.env.npm_package_appVersion);
const H5_VERSION = (process.env.VUE_APP_H5_VERSION = process.env.npm_package_version);
process.env.VUE_APP_NAME = ""; process.env.VUE_APP_VERSION_CODE = "";
process.env.VUE_APP_HOST = serverType == 1 ? process.env.VUE_APP_CDN_HOST : process.env.VUE_APP_OSS_HOST;
let lessVars = require("./src/assets/config/less"); lessVars["TEST_SERVICE"] = process.env.VUE_APP_TEST_SERVICE = serverType;
console.log(">>>>>>>> 运行环境 ", ["开发环境", "正式环境", "测试服务器"][serverType]);
const getPages = (arr = [], toName) => { const dirs = fs.readdirSync("./src/views"); let pages = {}; for (const name of dirs) { if (arr.length > 0 && !arr.includes(name)) continue; let temPath = `templates/${name}.html`; let entryPath = `src/views/${name}/main.js`; let newName = name; if (arr.length == 1 && toName) { newName = toName; } pages[newName] = { entry: entryPath, template: fs.existsSync(temPath) ? temPath : "public/index.html", filename: PRODUCTION ? `index.html` : `${newName}.html`, title: newName, chunks: ["chunk-vendors", "chunk-common", newName] }; } return pages; }; let proPathName = PRODUCTION ? argv["test_server"] || process.argv[3] : ""; let proNewName = (argv["test_server"] ? process.argv[5] : process.argv[4]) || proPathName;
console.log(process.argv); let devPathName = ""; let page = {}; if (!PRODUCTION) { if (argv["project"] && argv["project"] !== true) { devPathName = String(argv["project"]).trim().split(","); console.log(">>>>>>>> 运行项目 ", devPathName); page = getPages(devPathName); if (devPathName.length == 1) { process.env.VUE_APP_NAME = devPathName[0]; process.env.VUE_APP_VERSION_CODE = require(`./src/views/${devPathName[0]}/assets/config/index`).versionCode; } } else { console.log(">>>>>>>> 运行项目 ", "全部"); page = getPages(); } } else if (proPathName) { proPathName = proPathName.replace(/[^a-zA-Z|_\d]/gi, ""); proNewName = proNewName.replace(/[^a-zA-Z|_\d]/gi, ""); console.log(">>>>>>>> 打包项目 ", proPathName, proNewName); process.env.VUE_APP_NAME = proNewName; process.env.VUE_APP_VERSION_CODE = require(`./src/views/${proPathName}/assets/config/index`).versionCode; page = getPages([proPathName], proNewName); } else { throw new Error("未写项目包名"); }
let cssOptions = PRODUCTION ? { extract: { filename: `css/[name]-${H5_VERSION}-[hash:8].css`, chunkFilename: `css/[name]-${H5_VERSION}-[hash:8].css` } } : {}; module.exports = { publicPath: !PRODUCTION ? "/m" : "./", outputDir: "./dist/" + proNewName, filenameHashing: true, pages: page, productionSourceMap: false, devServer: { openPage: `${proNewName || ""}.html`, open: true, host: "0.0.0.0", port: 8082, https: false, hotOnly: false, proxy: { "/api": { target: "http://10.20.39.122:8082", changeOrigin: true, pathRewrite: { "^/api": "" } } } }, css: { loaderOptions: { less: { globalVars: lessVars } }, ...cssOptions }, configureWebpack: (config) => { if (PRODUCTION && proPathName) { config.entry[proNewName] = ["babel-polyfill", ...config.entry[proNewName]]; config.output.filename = `js/[name]-${H5_VERSION}-[hash:8].js`; config.output.chunkFilename = `js/[name]-${H5_VERSION}-[hash:8].js`; }
let plugins = []; config.plugins.push( new CompressionWebpackPlugin({ test: /\.(js|css|svg|woff|ttf|json|html)$/, threshold: 10240 }) ); if (serverType == 1) { console.log(`>>>>>>>> 移除console和debugger`); config.plugins = [...config.plugins, ...plugins]; config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true; config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true; } }, chainWebpack: (config) => { config.externals({ "animate.css": "animate.css", vue: "Vue", "vue-router": "VueRouter", vuex: "Vuex", axios: "axios", moment: "moment", vant: "vant", "vue-clipboard2": "vue-clipboard2" });
config.resolve.alias .set("@", resolve("src")) .set("@assets", resolve("src/assets")) .set("@views", resolve("src/views")) .set("@components", resolve("src/components")) .set("@phjtWebTools", resolve("src/phjtWebTools")) .set("@utils", resolve("src/assets/utils"));
config.module .rule("svg") .use("file-loader") .loader("file-loader") .options({ name: `img/[name]-${H5_VERSION}-[hash:8].[ext]` });
config.module .rule("images") .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/) .use("url-loader") .loader("url-loader") .tap((options) => { options.fallback.options.name = `img/[name]-${H5_VERSION}-[hash:8].[ext]`; options.limit = 5120; return options; });
config.module .rule("vue") .use("vue-loader") .loader("vue-loader") .tap((options) => { options.compilerOptions.preserveWhitespace = true; return options; }); } };
|