加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
webpack.config.js 6.83 KB
一键复制 编辑 原始数据 按行查看 历史
内容可能含有违规信息 提交于 2021-12-11 15:48 . 修改
/* eslint-disable */
const path = require('path');
const webpack = require('webpack');
const open = require('open');
var fs = require('fs');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const GlobalDefinePlugin = require('global-define-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
function getEntryAndHtmlPlugin() {
var siteArr = [{
entry: "./index.tsx", name: 'index', fileName: 'index.html', template: './index.html'
},
];
var re = { entry: {}, htmlplugins: [] };
for (var i = 0, j = siteArr.length; i < j; i++) {
var site = siteArr[i];
re.entry[site.name] = [site.entry];//js多入口字典对象
re.htmlplugins.push(new HtmlWebpackPlugin({
filename: site.fileName, //打包出来的html名字
template: site.template, //模版路径
inject: 'body',
chunks: [site.name],//js注入的名字
hash: true
}));
}
return re;
}
var rmdirSync = (function () {
function iterator(url, dirs) {
var stat = fs.statSync(url);
if (stat.isDirectory()) {
dirs.unshift(url); //收集目录
inner(url, dirs);
} else if (stat.isFile()) {
fs.unlinkSync(url); //直接删除文件
}
}
function inner(path, dirs) {
var arr = fs.readdirSync(path);
for (var i = 0,
el; el = arr[i++];) {
iterator(path + "/" + el, dirs);
}
}
return function (dir, cb) {
cb = cb ||
function () { };
var dirs = [];
try {
iterator(dir, dirs);
for (var i = 0,
el; el = dirs[i++];) {
fs.rmdirSync(el); //一次性删除所有收集到的目录
}
cb()
} catch (e) { //如果文件或目录本来就不存在,fs.statSync会报错,不过我们还是当成没有异常发生
e.code === "ENOENT" ? cb() : cb(e);
}
}
})();
module.exports = function () {
const env = process.env;
const action = env.action || 'start';
var entryAndHtmlPlugin = getEntryAndHtmlPlugin();
const nodeEnv = env.env || 'dev';
const isBuild = action === 'build';
let report = (env.report || '')==='true';
var entry = entryAndHtmlPlugin.entry;
var plugins = [
new webpack.LoaderOptionsPlugin({
minimize: true
}),
new GlobalDefinePlugin({
output: {
inject: true,
injectEnv: nodeEnv,
filename: `SEveConfig.js?${Date.now()}`
},
env: {
dev: {
ENV: 'dev',
$$isBuild:JSON.stringify(isBuild)
},
uat: {
ENV: 'uat',
$$isBuild:JSON.stringify(isBuild)
},
test: {
ENV: 'test',
$$isBuild:JSON.stringify(isBuild)
},
'xk-prod': {
ENV: 'pro',
$$isBuild:JSON.stringify(isBuild)
},
},
})
];
plugins = plugins.concat(entryAndHtmlPlugin.htmlplugins);
const devServer = {
// open:!isBuild,
};
if (isBuild) {
rmdirSync('./dist');
if(report){
plugins.push(new BundleAnalyzerPlugin());
}
plugins.push(new UglifyJsPlugin({ extractComments: true}));
// 打包的时候才使用样式抽离
plugins.push(new MiniCssExtractPlugin({
filename: 'css/css-[contenthash:8].css',
ignoreOrder: true
}));
} else {
// 开发环境用style-loader 因为webpack5 有一个BUG 修改样式不更新
const openurl = env.openurl || '';
if (openurl.length > 0) {
open(openurl);
}
}
const config= {
cache:isBuild?false: {
type: 'filesystem',
allowCollectingMemory: true,
},
mode:isBuild?'production':'development',
context: path.resolve(__dirname, 'app'),
entry: entry,
output: {
filename: '[name].[hash:8].js',
chunkFilename:isBuild ? '[name].[chunkhash:8].min.js':'[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: isBuild ? './' : '/',
},
devtool: isBuild ? 'cheap-module-source-map' : 'eval-cheap-module-source-map',
target: ['web', 'es5'],
devServer,
resolve: {
mainFiles: ["index.tsx", 'index.ts', 'index.js', 'index'],
modules: [path.resolve(__dirname, "src"), "node_modules"],
extensions: [`.${nodeEnv}.ts`, '.tsx', '.ts', '.js'],
alias: {
'current-app':path.resolve(__dirname, "./app"),
},
},
module: {
rules: [
{
test: /(?<!\.d)\.tsx?$/,
use: [{
loader: "babel-loader"
}, {
loader: "ts-loader",
},],
exclude: /node_modules/,
},
{
test: /\.d\.ts$/,
loader: 'ignore-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
use: [
{
loader: 'babel-loader',
},
{
loader: '@svgr/webpack',
options: {
babel: false,
icon: true,
},
},
],
},
{
test: /\.(png|jpg|jpeg|gif|woff)$/,
loader: 'url-loader',
options: {
limit: 6144,
name : 'imgs/[path][name].[ext]'
}
},
{
test: /\.(eot|ttf|woff|woff2)$/,
loader: 'file-loader',
options: {
name : 'fonts/[name].[ext]'
}
},
{
test: /\.css$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader",
}]
},
{
test: /\.md$/,
use: [
{
loader: "raw-loader"
}
]
},
{
test: /\.less$/,
use: [{
loader: "style-loader"
}, {
loader: "css-loader",
},
{
loader: "postcss-loader",
options: {
ident: 'postcss',
plugins: (loader) => [
require('postcss-import')({ root: loader.resourcePath }),
require('autoprefixer')(),
require('postcss-pxtorem')({
rootValue: 50,
propList: ['*'],
exclude:function(file){
return (file.indexOf("\\app\\pc")>0 || file.indexOf("\\node_modules\\antd")>0);
}
})
]
}
},
{
loader: "less-loader",
options: {
lessOptions: {
javascriptEnabled: true,
}
},
}]
}
],
},
plugins: plugins,
};
if (isBuild) {
config.optimization={
minimize: true,
minimizer: [
new CssMinimizerPlugin(),
],
};
}
return config;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化