webpack-typescript-builder
Version:
Webpack config builder for typescript, styles (css and sass), fonts and images.
88 lines (87 loc) • 3.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin"));
const optimize_css_assets_webpack_plugin_1 = __importDefault(require("optimize-css-assets-webpack-plugin"));
const terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
const tsconfig_paths_webpack_plugin_1 = __importDefault(require("tsconfig-paths-webpack-plugin"));
const webpack = __importStar(require("webpack"));
const rules_1 = require("./rules");
function createWebConfig(tsConfigLocation, entry, outputPath, isProd) {
const plugins = [
new mini_css_extract_plugin_1.default({
filename: isProd ? "[name].[contenthash:6].css" : "[name].css",
}),
];
if (!isProd) {
plugins.push(new webpack.HotModuleReplacementPlugin({ quiet: true }));
}
else {
plugins.push(new optimize_css_assets_webpack_plugin_1.default({
assetNameRegExp: /\.css$/g,
cssProcessor: require("cssnano"),
cssProcessorOptions: {
preset: ["default", { discardComments: { removeAll: true } }],
},
}));
}
return {
devtool: isProd ? undefined : "cheap-module-eval-source-map",
entry,
mode: isProd ? "production" : "development",
module: { rules: rules_1.clientRules(isProd) },
name: "client",
optimization: {
minimizer: [
new terser_webpack_plugin_1.default({
terserOptions: {
output: {
comments: false,
},
},
}),
],
noEmitOnErrors: true,
splitChunks: {
cacheGroups: {
styles: {
chunks: "all",
enforce: true,
name: "styles",
test: /\.css$/,
},
vendors: {
chunks: "all",
name: "vendors",
test: /[\\/]node_modules[\\/]/,
},
},
},
},
output: {
filename: isProd ? "[name].[contenthash:6].js" : "[name].js",
library: "[name]",
libraryTarget: "umd",
path: outputPath,
pathinfo: false,
publicPath: "/",
},
plugins,
resolve: {
alias: {},
extensions: [".js", ".jsx", ".ts", ".tsx"],
plugins: [new tsconfig_paths_webpack_plugin_1.default({ configFile: tsConfigLocation })],
},
stats: true,
};
}
exports.createWebConfig = createWebConfig;