UNPKG

@codelet/cli-service

Version:
145 lines (144 loc) 5.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDefaultConfig = void 0; const app_json_webpack_plugin_1 = __importDefault(require("@codelet/app-json-webpack-plugin")); const hmr_webpack_plugin_1 = __importDefault(require("@codelet/hmr-webpack-plugin")); const inject_chunk_webpack_plugin_1 = __importDefault(require("@codelet/inject-chunk-webpack-plugin")); const mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin")); const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin")); const webpackbar_1 = __importDefault(require("webpackbar")); const utils_1 = require("./utils"); function getDefaultConfig(options) { const { entryPath, source, pageIndex, isDev } = Object.assign({ isDev: false, pageIndex: '', entryPath: './src', source: ['app.(js|ts)', 'pages/**/*.(js|ts)', 'components/**/*.(js|ts)'], }, options); // 模式 const mode = isDev ? 'development' : 'production'; // 插件 const plugins = [ new mini_css_extract_plugin_1.default({ filename: '[name].wxss', }), new inject_chunk_webpack_plugin_1.default(), new app_json_webpack_plugin_1.default({ pageIndex, }), new webpackbar_1.default(), ]; if (isDev) { plugins.push(new hmr_webpack_plugin_1.default()); } // 优化 const optimization = { splitChunks: { chunks: 'all', minChunks: 2, minSize: 0, cacheGroups: { main: { name: 'bundle', minChunks: 2, chunks: 'all', }, }, }, }; // 生产环境 if (!isDev) { optimization.minimize = true; optimization.minimizer = [ new terser_webpack_plugin_1.default({ // 不生成 license 文件 extractComments: false, terserOptions: { format: { comments: false, // 删除注释 }, }, }), ]; } return { pageIndex, entryPath, source, webpack: { mode, devtool: false, output: { filename: '[name].js', path: (0, utils_1.resolve)('dist'), publicPath: '/', clean: true, }, resolve: { alias: { '@': (0, utils_1.resolve)('src'), }, extensions: ['.js', '.ts'], }, module: { rules: [ { oneOf: [ { test: /\.(css|wxss)$/, use: [mini_css_extract_plugin_1.default.loader, 'css-loader'], }, { test: /\.s(a|c)ss$/, use: [mini_css_extract_plugin_1.default.loader, 'css-loader', 'sass-loader'], }, { test: /\.(wxml|html)$/, loader: '@codelet/wxml-loader', options: { entryPath, }, }, { test: /\.wxs$/, loader: '@codelet/copy-loader', options: { entryPath, }, }, { test: /\.json$/, type: 'javascript/auto', loader: '@codelet/copy-loader', options: { entryPath, }, }, { test: /\.(png|jpe?g|gif|svg)$/, type: 'asset/resource', generator: { filename: 'assets/images/[name][ext]', }, }, { test: /\.(ts|js)$/, loader: 'babel-loader', options: { cacheDirectory: true, // 开启 babel 缓存 cacheCompression: false, // 关闭缓存文件压缩 }, }, ], }, ], }, plugins, optimization, }, }; } exports.getDefaultConfig = getDefaultConfig;