forked from foundryvtt/pf2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
137 lines (132 loc) · 4.46 KB
/
webpack.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import * as process from 'process';
import { Configuration, DefinePlugin } from 'webpack';
import copyWebpackPlugin from 'copy-webpack-plugin';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import TerserPlugin from 'terser-webpack-plugin';
import WebpackBar from 'webpackbar';
const buildMode = process.argv[3] == 'production' ? 'production' : 'development';
const isProductionBuild = buildMode === 'production';
const pf2eSystemPath = (() => {
const configPath = path.resolve(process.cwd(), 'foundryconfig.json');
const configData = fs.existsSync(configPath) ? fs.readJSONSync(configPath) : undefined;
return configData !== undefined ? path.join(configData.dataPath, 'Data', 'systems', configData.systemName) : null;
})();
const outDir = pf2eSystemPath ?? path.join(__dirname, 'dist/');
type Optimization = Configuration['optimization'];
const optimization: Optimization = isProductionBuild
? {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false,
},
}),
new CssMinimizerPlugin(),
],
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
name: 'main',
test: 'src/pf2e.ts',
},
vendor: {
name: 'vendor',
test: /node_modules/,
},
},
},
}
: undefined;
const config: Configuration = {
context: __dirname,
mode: buildMode,
entry: './src/pf2e.ts',
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'ts-loader',
options: {
configFile: path.resolve(__dirname, 'tsconfig.json'),
experimentalWatchApi: !isProductionBuild,
happyPackMode: true,
transpileOnly: true,
compilerOptions: {
noEmit: false,
},
},
},
],
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
sourceMap: true,
},
},
{
loader: 'sass-loader',
options: { sourceMap: true },
},
],
},
{
loader: 'thread-loader',
options: {
workers: os.cpus().length + 1,
poolRespawn: false,
poolTimeout: isProductionBuild ? 500 : Infinity,
},
},
],
},
optimization: optimization,
devtool: isProductionBuild ? undefined : 'inline-source-map',
bail: isProductionBuild,
watch: !isProductionBuild,
plugins: [
new ForkTsCheckerWebpackPlugin(),
new DefinePlugin({
BUILD_MODE: JSON.stringify(buildMode)
}),
new copyWebpackPlugin({
patterns: [{ from: 'static/' }, { from: 'system.json' }],
}),
new MiniCssExtractPlugin({
filename: 'styles/pf2e.css',
insert: 'head',
}),
new WebpackBar({}),
],
resolve: {
alias: {
'@actor': path.resolve(__dirname, 'src/module/actor'),
'@item': path.resolve(__dirname, 'src/module/item'),
'@module': path.resolve(__dirname, 'src/module'),
'@scripts': path.resolve(__dirname, 'src/scripts'),
'@system': path.resolve(__dirname, 'src/module/system'),
'@utils': path.resolve(__dirname, 'src/module/utils.ts'),
},
extensions: ['.ts'],
},
output: {
clean: true,
path: outDir,
filename: '[name].bundle.js',
},
};
export default config;