2020-03-31 13:19:53 +00:00
|
|
|
import * as ts from "typescript";
|
2020-04-01 19:47:33 +00:00
|
|
|
import * as fs from "fs";
|
|
|
|
import trtransformer from "./tools/trgen/ts_transformer";
|
2020-04-09 17:26:56 +00:00
|
|
|
import {exec} from "child_process";
|
|
|
|
import * as util from "util";
|
2020-07-13 09:29:16 +00:00
|
|
|
import LoaderIndexGenerator = require("./loader/IndexGenerator");
|
2020-07-20 17:08:13 +00:00
|
|
|
import {Configuration} from "webpack";
|
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
|
|
|
|
2020-04-01 19:47:33 +00:00
|
|
|
export let isDevelopment = process.env.NODE_ENV === 'development';
|
2020-04-02 15:40:09 +00:00
|
|
|
console.log("Webpacking for %s (%s)", isDevelopment ? "development" : "production", process.env.NODE_ENV || "NODE_ENV not specified");
|
2020-04-09 17:26:56 +00:00
|
|
|
const generate_definitions = async (target: string) => {
|
2020-04-01 19:47:33 +00:00
|
|
|
const git_rev = fs.readFileSync(path.join(__dirname, ".git", "HEAD")).toString();
|
|
|
|
let version;
|
|
|
|
if(git_rev.indexOf("/") === -1)
|
2020-04-09 21:24:50 +00:00
|
|
|
version = (git_rev || "0000000").substr(0, 7);
|
2020-04-01 19:47:33 +00:00
|
|
|
else
|
|
|
|
version = fs.readFileSync(path.join(__dirname, ".git", git_rev.substr(5).trim())).toString().substr(0, 7);
|
|
|
|
|
2020-04-09 17:26:56 +00:00
|
|
|
let timestamp;
|
|
|
|
try {
|
|
|
|
const { stdout } = await util.promisify(exec)("git show -s --format=%ct");
|
|
|
|
timestamp = parseInt(stdout.toString());
|
|
|
|
if(isNaN(timestamp)) throw "failed to parse timestamp '" + stdout.toString() + "'";
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to get commit timestamp: %o", error);
|
|
|
|
throw "failed to get commit timestamp";
|
|
|
|
}
|
2020-04-01 19:47:33 +00:00
|
|
|
return {
|
|
|
|
"__build": {
|
2020-04-02 15:40:09 +00:00
|
|
|
target: JSON.stringify(target),
|
2020-04-01 19:47:33 +00:00
|
|
|
mode: JSON.stringify(isDevelopment ? "debug" : "release"),
|
|
|
|
version: JSON.stringify(version),
|
2020-04-09 17:26:56 +00:00
|
|
|
timestamp: timestamp,
|
2020-04-02 15:40:09 +00:00
|
|
|
entry_chunk_name: JSON.stringify(target === "web" ? "shared-app" : "client-app")
|
2020-04-01 19:47:33 +00:00
|
|
|
} as BuildDefinitions
|
|
|
|
} as any;
|
|
|
|
};
|
|
|
|
|
2020-07-13 09:29:16 +00:00
|
|
|
const isLoaderFile = (file: string) => {
|
|
|
|
if(file.startsWith(__dirname)) {
|
|
|
|
const path = file.substr(__dirname.length).replace(/\\/g, "/");
|
|
|
|
if(path.startsWith("/loader/")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-08-01 12:54:44 +00:00
|
|
|
export const config = async (target: "web" | "client"): Promise<Configuration> => { return {
|
2020-04-01 19:47:33 +00:00
|
|
|
entry: {
|
2020-07-20 17:08:13 +00:00
|
|
|
"loader": "./loader/app/index.ts",
|
|
|
|
"modal-external": "./shared/js/ui/react-elements/external-modal/PopoutEntrypoint.ts",
|
|
|
|
"devel-main": "./shared/js/devel_main.ts"
|
2020-04-01 19:47:33 +00:00
|
|
|
},
|
2020-04-01 13:36:37 +00:00
|
|
|
|
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({
|
2020-04-01 19:47:33 +00:00
|
|
|
file: path.join(__dirname, "dist/manifest.json"),
|
|
|
|
base: __dirname
|
2020-03-30 23:27:59 +00:00
|
|
|
}),
|
|
|
|
new WorkerPlugin(),
|
2020-08-01 12:54:44 +00:00
|
|
|
//new BundleAnalyzerPlugin(),
|
2020-04-01 13:36:37 +00:00
|
|
|
isDevelopment ? undefined : new webpack.optimize.AggressiveSplittingPlugin({
|
2020-03-31 13:19:53 +00:00
|
|
|
minSize: 1024 * 8,
|
|
|
|
maxSize: 1024 * 128
|
2020-04-01 19:47:33 +00:00
|
|
|
}),
|
2020-04-09 21:09:59 +00:00
|
|
|
new webpack.DefinePlugin(await generate_definitions(target)),
|
|
|
|
|
2020-07-13 09:29:16 +00:00
|
|
|
new LoaderIndexGenerator({
|
|
|
|
buildTarget: target,
|
2020-04-09 21:09:59 +00:00
|
|
|
output: path.join(__dirname, "dist/index.html"),
|
2020-07-13 09:29:16 +00:00
|
|
|
isDevelopment: isDevelopment
|
2020-04-09 21:09:59 +00:00
|
|
|
})
|
2020-04-01 13:36:37 +00:00
|
|
|
].filter(e => !!e),
|
2020-08-05 16:25:08 +00:00
|
|
|
|
2020-03-27 22:36:14 +00:00
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2020-06-10 16:13:56 +00:00
|
|
|
test: /\.(s[ac]|c)ss$/,
|
2020-03-27 22:36:14 +00:00
|
|
|
loader: [
|
2020-04-09 14:14:54 +00:00
|
|
|
'style-loader',
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
loader: MiniCssExtractPlugin.loader,
|
|
|
|
options: {
|
|
|
|
esModule: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
*/
|
2020-03-27 22:36:14 +00:00
|
|
|
{
|
|
|
|
loader: 'css-loader',
|
|
|
|
options: {
|
2020-04-18 17:37:30 +00:00
|
|
|
modules: {
|
|
|
|
mode: "local",
|
2020-04-18 20:45:18 +00:00
|
|
|
localIdentName: isDevelopment ? "[path][name]__[local]--[hash:base64:5]" : "[hash:base64]",
|
2020-04-18 17:37:30 +00:00
|
|
|
},
|
2020-03-27 22:36:14 +00:00
|
|
|
sourceMap: isDevelopment
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: 'sass-loader',
|
|
|
|
options: {
|
|
|
|
sourceMap: isDevelopment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2020-07-13 09:29:16 +00:00
|
|
|
test: (module: string) => module.match(/\.tsx?$/) && !isLoaderFile(module),
|
2020-03-27 22:36:14 +00:00
|
|
|
exclude: /node_modules/,
|
2020-03-27 22:36:57 +00:00
|
|
|
|
|
|
|
loader: [
|
|
|
|
{
|
|
|
|
loader: 'ts-loader',
|
|
|
|
options: {
|
2020-07-19 15:12:41 +00:00
|
|
|
context: __dirname,
|
|
|
|
colors: true,
|
2020-08-01 12:54:44 +00:00
|
|
|
getCustomTransformers: (prog: ts.Program) => {
|
2020-03-31 13:19:53 +00:00
|
|
|
return {
|
2020-04-01 19:47:33 +00:00
|
|
|
before: [trtransformer(prog, {
|
2020-06-10 16:13:56 +00:00
|
|
|
optimized: false,
|
|
|
|
verbose: true,
|
2020-04-03 13:04:31 +00:00
|
|
|
target_file: path.join(__dirname, "dist", "translations.json")
|
2020-04-01 19:47:33 +00:00
|
|
|
})]
|
2020-03-31 13:19:53 +00:00
|
|
|
};
|
2020-07-20 17:08:13 +00:00
|
|
|
},
|
|
|
|
transpileOnly: isDevelopment
|
2020-03-27 22:36:57 +00:00
|
|
|
}
|
2020-04-18 17:37:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: "./webpack/DevelBlocks.js",
|
|
|
|
options: {
|
|
|
|
enabled: true
|
|
|
|
}
|
2020-03-27 22:36:57 +00:00
|
|
|
}
|
|
|
|
]
|
2020-03-31 11:44:16 +00:00
|
|
|
},
|
2020-07-13 09:29:16 +00:00
|
|
|
{
|
|
|
|
test: (module: string) => module.match(/\.tsx?$/) && isLoaderFile(module),
|
|
|
|
exclude: /(node_modules|bower_components)/,
|
|
|
|
|
|
|
|
loader: [
|
|
|
|
{
|
|
|
|
loader: "babel-loader",
|
|
|
|
options: {
|
|
|
|
presets: ["@babel/preset-env"] //Preset used for env setup
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
loader: "ts-loader",
|
|
|
|
options: {
|
|
|
|
transpileOnly: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2020-03-31 11:44:16 +00:00
|
|
|
{
|
|
|
|
test: /\.was?t$/,
|
|
|
|
loader: [
|
|
|
|
"./webpack/WatLoader.js"
|
|
|
|
]
|
2020-06-10 16:13:56 +00:00
|
|
|
},
|
2020-08-05 16:25:08 +00:00
|
|
|
{
|
|
|
|
test: /\.wasm$/,
|
|
|
|
type: 'javascript/auto',
|
|
|
|
loader: 'file-loader',
|
|
|
|
options: {
|
|
|
|
/* the public path will already be set by emscripten base path */
|
|
|
|
publicPath: './'
|
|
|
|
}
|
|
|
|
},
|
2020-06-10 16:13:56 +00:00
|
|
|
{
|
|
|
|
test: /\.svg$/,
|
|
|
|
loader: 'svg-inline-loader'
|
2020-03-30 23:27:59 +00:00
|
|
|
}
|
2020-03-27 22:36:14 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
resolve: {
|
2020-07-12 14:31:57 +00:00
|
|
|
extensions: ['.tsx', '.ts', '.js', ".scss", ".css"],
|
2020-07-17 21:56:20 +00:00
|
|
|
alias: {
|
|
|
|
"vendor/xbbcode": path.resolve(__dirname, "vendor/xbbcode/src")
|
|
|
|
},
|
2020-03-27 22:36:14 +00:00
|
|
|
},
|
2020-04-01 19:47:33 +00:00
|
|
|
externals: [
|
|
|
|
{"tc-loader": "window loader"}
|
2020-08-01 12:54:44 +00:00
|
|
|
],
|
2020-03-27 22:36:14 +00:00
|
|
|
output: {
|
2020-04-09 21:09:59 +00:00
|
|
|
filename: isDevelopment ? "[name].[contenthash].js" : "[contenthash].js",
|
|
|
|
chunkFilename: isDevelopment ? "[name].[contenthash].js" : "[contenthash].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: {
|
2020-04-01 19:47:33 +00:00
|
|
|
splitChunks: {
|
2020-08-05 16:29:33 +00:00
|
|
|
chunks: "all"
|
2020-04-01 19:47:33 +00:00
|
|
|
},
|
2020-03-31 13:19:53 +00:00
|
|
|
minimize: !isDevelopment,
|
|
|
|
minimizer: [new TerserPlugin()]
|
2020-03-27 22:36:14 +00:00
|
|
|
}
|
2020-04-01 19:47:33 +00:00
|
|
|
}};
|