const TerserPlugin = require("terser-webpack-plugin"), { VueLoaderPlugin } = require("vue-loader"), path = require("path"), fs = require("fs-extra"); const ZenAppPlugin = require("@zen/app-plugin"); const MiddleWare = require("@zen/app-plugin/middleware"); function optimization(mode) { if (mode === "production") { return { splitChunks: false, minimize: true, minimizer: [ new TerserPlugin({ terserOptions: { ecma: undefined, parse: {}, compress: {}, mangle: true, // Note `mangle.properties` is `false` by default. module: false, // Deprecated output: null, format: null, toplevel: false, nameCache: null, ie8: false, keep_classnames: undefined, keep_fnames: false, safari10: false, }, }), ], }; } return {}; } var webpackConfig = { entry: {}, output: { filename: "[name].js", }, watchOptions: { // , "**/*.json" ignored: ["node_modules", "dist", "debug"], poll: 300, }, externals: { vue: "Vue", "router-view": "VueRouter", "element-plus": "ElementPlus", }, plugins: [new VueLoaderPlugin()], resolve: { alias: { "@util": path.resolve(__dirname, "src/util"), "@store": path.resolve( __dirname, "src/design/engine/store/index.js" ), }, extensions: [".js", ".vue", ".jsx", ".json"], }, module: { noParse: /Vue|jquery/, rules: [ { test: /\.jsx?$/, exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader", options: { cacheDirectory: true, presets: [ [ "@vue/app", { modules: "commonjs", loose: true, useBuiltIns: "entry", }, ], ], plugins: ["@babel/plugin-syntax-jsx"], }, }, }, { test: /\.vue$/, loader: "vue-loader", }, { test: /\.css$/, use: ["style-loader", "css-loader"], }, { test: /\.scss$/, use: [ "style-loader", "css-loader", // translates CSS into CommonJS { loader: "sass-loader", options: { implementation: require("sass"), }, }, ], }, ], }, }; let basename, file, i, len, sourceList, vuePath = path.resolve(__dirname); sourceList = fs.readdirSync(path.resolve(vuePath, "src")); for (i = 0, len = sourceList.length; i < len; i++) { file = sourceList[i]; if (file.indexOf(".js") === -1) { continue; } basename = path.basename(file); basename = basename.substr(0, basename.length - 3); webpackConfig.entry[basename] = path.resolve(vuePath, "src", file); } module.exports = (env, argv) => { let mode = argv.mode; webpackConfig.mode = mode; webpackConfig.optimization = optimization(mode); if (mode === "development") { webpackConfig.cache = { type: "memory" }; webpackConfig.devServer = { open: true, hot: false, client: false, host: "127.0.0.1", allowedHosts: "all", historyApiFallback: true, onAfterSetupMiddleware: MiddleWare, static: { directory: path.join(vuePath, "public"), }, headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "*", }, }; webpackConfig.plugins.push(new ZenAppPlugin()); } else { webpackConfig.output.clean = true; webpackConfig.output.path = path.join(vuePath, "dist"); } return webpackConfig; };