TeaWeb/webpack.config.ts

143 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-03-31 13:19:53 +00:00
import * as ts from "typescript";
import * as fs from "fs";
import trtransformer from "./tools/trgen/ts_transformer";
2020-03-31 13:19:53 +00:00
2020-03-27 22:36:14 +00:00
const path = require('path');
2020-03-30 23:27:59 +00:00
const webpack = require("webpack");
2020-03-27 22:36:14 +00:00
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
2020-03-30 23:27:59 +00:00
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ManifestGenerator = require("./webpack/ManifestPlugin");
const WorkerPlugin = require('worker-plugin');
2020-03-31 13:19:53 +00:00
const TerserPlugin = require('terser-webpack-plugin');
2020-03-30 23:27:59 +00:00
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
2020-03-27 22:36:14 +00:00
export let isDevelopment = process.env.NODE_ENV === 'development';
console.log("Webpacking for %s (%s)", isDevelopment ? "development" : "production", process.env.NODE_ENV || "NODE_ENV not specified");
const generate_definitions = (target: string) => {
const git_rev = fs.readFileSync(path.join(__dirname, ".git", "HEAD")).toString();
let version;
if(git_rev.indexOf("/") === -1)
version = git_rev;
else
version = fs.readFileSync(path.join(__dirname, ".git", git_rev.substr(5).trim())).toString().substr(0, 7);
return {
"__build": {
target: JSON.stringify(target),
mode: JSON.stringify(isDevelopment ? "debug" : "release"),
version: JSON.stringify(version),
timestamp: Date.now(),
entry_chunk_name: JSON.stringify(target === "web" ? "shared-app" : "client-app")
} as BuildDefinitions
} as any;
};
export const config = (target: "web" | "client") => { return {
entry: {
"loader": "./loader/app/index.ts"
},
2020-03-31 13:19:53 +00:00
devtool: isDevelopment ? "inline-source-map" : undefined,
mode: isDevelopment ? "development" : "production",
2020-03-27 22:36:14 +00:00
plugins: [
2020-03-30 23:27:59 +00:00
new CleanWebpackPlugin(),
2020-03-27 22:36:14 +00:00
new MiniCssExtractPlugin({
filename: isDevelopment ? '[name].css' : '[name].[hash].css',
chunkFilename: isDevelopment ? '[id].css' : '[id].[hash].css'
2020-03-30 11:44:18 +00:00
}),
2020-03-30 23:27:59 +00:00
new ManifestGenerator({
file: path.join(__dirname, "dist/manifest.json"),
base: __dirname
2020-03-30 23:27:59 +00:00
}),
new WorkerPlugin(),
//new BundleAnalyzerPlugin()
2020-03-30 11:44:18 +00:00
/*
new CircularDependencyPlugin({
//exclude: /a\.js|node_modules/,
failOnError: true,
allowAsyncCycles: false,
cwd: process.cwd(),
2020-03-27 22:36:14 +00:00
})
2020-03-30 11:44:18 +00:00
*/
isDevelopment ? undefined : new webpack.optimize.AggressiveSplittingPlugin({
2020-03-31 13:19:53 +00:00
minSize: 1024 * 8,
maxSize: 1024 * 128
}),
new webpack.DefinePlugin(generate_definitions(target))
].filter(e => !!e),
2020-03-27 22:36:14 +00:00
module: {
rules: [
{
test: /\.s[ac]ss$/,
loader: [
2020-03-31 13:19:53 +00:00
isDevelopment ? 'style-loader' : MiniCssExtractPlugin.loader,
2020-03-27 22:36:14 +00:00
{
loader: 'css-loader',
options: {
modules: true,
sourceMap: isDevelopment
}
},
{
loader: 'sass-loader',
options: {
sourceMap: isDevelopment
}
}
]
},
{
test: /\.tsx?$/,
exclude: /node_modules/,
2020-03-27 22:36:57 +00:00
loader: [
{
loader: 'ts-loader',
options: {
2020-03-31 13:19:53 +00:00
transpileOnly: true,
getCustomTransformers: (prog: ts.Program) => {
return {
before: [trtransformer(prog, {
optimized: true,
target_file: path.join(__dirname, "dist", "translations.json")
})]
2020-03-31 13:19:53 +00:00
};
}
2020-03-27 22:36:57 +00:00
}
}
]
},
{
test: /\.was?t$/,
loader: [
"./webpack/WatLoader.js"
]
2020-03-30 23:27:59 +00:00
}
2020-03-27 22:36:14 +00:00
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js', ".scss"],
alias: { },
2020-03-27 22:36:14 +00:00
},
externals: [
{"tc-loader": "window loader"}
] as any[],
2020-03-27 22:36:14 +00:00
output: {
filename: (chunkData) => {
if(chunkData.chunk.name === "loader")
return "loader.js";
return '[name].js';
},
chunkFilename: "[name].js",
2020-03-27 22:36:14 +00:00
path: path.resolve(__dirname, 'dist'),
2020-03-30 23:27:59 +00:00
publicPath: "js/"
2020-03-27 22:36:14 +00:00
},
optimization: {
splitChunks: {
},
2020-03-31 13:19:53 +00:00
minimize: !isDevelopment,
minimizer: [new TerserPlugin()]
2020-03-27 22:36:14 +00:00
}
}};