@codelet/cli-service
Version:
cli-service
198 lines (197 loc) • 7.97 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultConfig = getDefaultConfig;
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 copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
const path_1 = __importDefault(require("path"));
const webpackbar_1 = __importDefault(require("webpackbar"));
const utils_1 = require("./utils");
function getDefaultConfig(options) {
const { entryPath, source, externalSource, npmDir, pageIndex, publicDir, isDev } = Object.assign({
isDev: false,
pageIndex: '',
publicDir: 'public',
externalSource: [],
npmDir: '',
entryPath: './src',
source: [
'app.(js|ts)',
'(pages|components)/**/index.(js|ts)',
'packages/*/(pages|components)/**/index.(js|ts)',
],
}, options);
const externalFiles = (0, utils_1.resolveExternalFiles)(entryPath, externalSource);
const resolveExternalFile = (0, utils_1.createExternalRequestResolver)({
entryPath,
externalFiles,
});
const resolveNpmDirRequest = npmDir ? (0, utils_1.createNpmDirRequestResolver)(npmDir) : () => '';
// 模式
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 copy_webpack_plugin_1.default({
patterns: [
{
from: publicDir, // 源目录
to: './', // 复制到输出目录(dist)的根路径
noErrorOnMissing: true, // 若 public 目录不存在时不报错
info: {
minimized: true,
},
},
...(0, utils_1.createExternalCopyPatterns)(entryPath, externalSource),
...(0, utils_1.createNpmDirCopyPatterns)(npmDir),
],
}),
new webpackbar_1.default(),
];
if (externalSource.length > 0) {
plugins.push(new utils_1.RewriteExternalRequestPlugin());
}
if (isDev) {
plugins.push(new hmr_webpack_plugin_1.default());
}
// 优化
// 将 splitChunks 的组装逻辑放到 utils 中,保持这里专注于默认配置拼装。
const optimization = (0, utils_1.createOptimization)();
// 生产环境
if (!isDev) {
optimization.minimize = true;
optimization.minimizer = [
new terser_webpack_plugin_1.default({
// 不生成 license 文件
extractComments: false,
terserOptions: {
format: {
comments: false, // 删除注释
},
},
}),
];
}
return {
pageIndex,
entryPath,
source,
externalSource,
npmDir,
publicDir,
webpack: {
mode,
devtool: false,
...(externalSource.length > 0 || Boolean(npmDir)
? {
externalsType: 'commonjs',
externals: [
({ context, request }, callback) => {
if (!context || !request) {
callback();
return;
}
const npmRequest = resolveNpmDirRequest(request);
if (npmRequest) {
callback(null, `commonjs ${npmRequest}`);
return;
}
const targetFile = resolveExternalFile(context, request);
if (!targetFile) {
callback();
return;
}
const targetRequest = path_1.default
.relative((0, utils_1.resolve)(entryPath), targetFile)
.replace(/\\/g, '/')
.replace(/\.js$/, '');
callback(null, `commonjs ${utils_1.externalRequestPlaceholderPrefix}${targetRequest}`);
},
],
}
: {}),
output: {
filename: '[name].js',
path: (0, utils_1.resolve)('dist'),
publicPath: '/',
clean: true,
},
watchOptions: {
ignored: (0, utils_1.createWatchIgnored)([publicDir, npmDir]),
},
resolve: {
alias: {
'@': (0, utils_1.resolve)(entryPath),
},
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,
},
};
}