@sanpjs/bundler-webpack
Version:
@sanpjs/bundler-webpack
106 lines • 5 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_extra_1 = __importDefault(require("fs-extra"));
const lodash_compact_1 = __importDefault(require("lodash.compact"));
const webpackbar_1 = __importDefault(require("webpackbar"));
const mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin"));
// import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
const NoEmitOnErrorsPlugin_1 = __importDefault(require("webpack/lib/NoEmitOnErrorsPlugin"));
const plugin_1 = __importDefault(require("san-loader/lib/plugin"));
const case_sensitive_paths_webpack_plugin_1 = __importDefault(require("case-sensitive-paths-webpack-plugin"));
const DefinePlugin_1 = __importDefault(require("webpack/lib/DefinePlugin"));
const html_webpack_harddisk_plugin_1 = __importDefault(require("html-webpack-harddisk-plugin"));
const utils_1 = require("../utils");
const html_1 = require("../utils/html");
const rules = __importStar(require("../rules"));
exports.default = (context, args) => {
const { mode, root, pages, server } = context.config;
const { publicPath, outDir, hash, assetDir, style, clean, copy } = context.config.build;
const { resolve } = context.resolver;
const cssFilename = (0, utils_1.getAssetPath)(assetDir, `css/[name]${hash ? '.[contenthash:8]' : ''}.css`);
const modules = ['node_modules', (0, path_1.join)(root, 'node_modules'), (0, path_1.join)(__dirname, 'node_modules')];
const webpackConfig = {
mode,
context: root,
entry: pages.reduce((webpackEntry, entry) => {
webpackEntry[entry.name] = [entry.filepath];
return webpackEntry;
}, {}),
output: {
path: (0, path_1.join)(root, outDir),
filename: `[name]${hash ? '.[contenthash:8]' : ''}.js`,
publicPath,
pathinfo: false
},
module: {
rules: Object.values(rules).map(rule => rule(context))
},
resolve: {
// 不需要那么多
extensions: ['.js', '.ts', '.d.ts', '.san'],
symlinks: false,
mainFields: ['browser', 'module', 'main'],
alias: {
// warn 这里还可以扩充alias:components 这类
'@': root,
'regenerator-runtime': (0, path_1.dirname)(resolve('regenerator-runtime')),
'core-js': (0, path_1.dirname)(resolve('core-js'))
},
modules
},
resolveLoader: {
modules
},
plugins: [
new plugin_1.default(),
new DefinePlugin_1.default((0, utils_1.defineVar)(mode)),
new webpackbar_1.default({ name: args._[0] }),
new case_sensitive_paths_webpack_plugin_1.default(),
...(0, html_1.getHtmlWebpackPlugin)(context),
server?.devMiddleware?.writeToDisk && new html_webpack_harddisk_plugin_1.default(),
style.extract &&
new mini_css_extract_plugin_1.default({
filename: cssFilename,
chunkFilename: cssFilename,
...(style.extract && typeof style.extract === 'object' ? style.extract : {})
}),
// webpac-dev-server默认添加, 使用其他server可重新添加
// context.dev && new HotModuleReplacementPlugin(),
context.dev && new NoEmitOnErrorsPlugin_1.default()
],
optimization: {}
};
webpackConfig.plugins = (0, lodash_compact_1.default)(webpackConfig.plugins);
// TODO: 这种跟配置无关的操作不应该放到这里?
if (clean) {
// 删掉目录
fs_extra_1.default.removeSync(webpackConfig.output.path);
}
// 拷贝静态和copy指定目录
(0, utils_1.copyFiles)(['public', ...(copy || [])], root, webpackConfig.output.path);
return webpackConfig;
};
//# sourceMappingURL=base.js.map