-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
148 lines (141 loc) · 3.73 KB
/
webpack.config.js
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
138
139
140
141
142
143
144
145
146
147
148
import { fileURLToPath } from 'url'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import CopyWebpackPlugin from 'copy-webpack-plugin'
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'
import developmentOptions from './fvtt.config.js'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import TerserPlugin from 'terser-webpack-plugin'
import WebpackBar from 'webpackbar'
const rootFolder = path.dirname(fileURLToPath(import.meta.url))
/** Set the run mode for @constant bundleScript */
const buildMode = process.argv[3] === 'production' ? 'production' : 'development'
/**
* Get the user data path for Foundry VTT, based on what is configured on key
* userDataPath inside fvtt.config.js
*/
function buildDestination () {
try {
const { userDataPath } = developmentOptions
let check = ''
if (fs.existsSync(check = rootFolder + '/module/module.json')) {
const json = JSON.parse(fs.readFileSync(check, 'utf8'))
if (typeof json.id !== 'undefined') {
if (fs.existsSync(userDataPath)) {
return path.join(userDataPath, 'Data', 'modules', json.id)
}
}
}
} catch (e) {
//
}
return path.join(rootFolder, 'build/')
}
function copyList () {
const list = []
if (fs.existsSync('module/assets/')) {
list.push({ from: 'module/assets/', to: 'assets/' })
}
if (fs.existsSync('module/lang/')) {
list.push({ from: 'module/lang/', to: 'lang/' })
}
if (fs.existsSync('module/packs/')) {
list.push({ from: 'module/packs/', to: 'packs/' })
}
if (fs.existsSync('module/templates/')) {
list.push({ from: 'module/templates/', to: 'templates/' })
}
if (fs.existsSync('module/README.md')) {
list.push({ from: 'module/README.md', to: 'README.md' })
}
// Licence is required
list.push({ from: 'module/LICENSE', to: 'LICENSE', toType: 'file' })
list.push({ from: 'module/module.json', to: 'module.json' })
return list
}
/** Set optimization options for when @constant buildMode is `production` */
const optimization =
buildMode === 'production'
? {
minimize: true,
minimizer: [
new TerserPlugin({
terserOptions: {
mangle: false
}
}),
new CssMinimizerPlugin()
],
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
name: 'main',
test: 'module/coc7.js'
}
}
}
}
: undefined
/**
* The nerve center. Here are all the settings for compiling bundles:
* production and development
*/
const bundleScript = {
bail: buildMode === 'production',
context: rootFolder,
entry: './module/src/coc7.js',
devtool: 'inline-source-map',
mode: buildMode,
module: {
rules: [
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false,
sourceMap: true
}
},
{
loader: 'less-loader',
options: { sourceMap: true }
}
]
},
{
loader: 'thread-loader',
options: {
workers: os.cpus().length + 1,
poolRespawn: false,
poolTimeout: buildMode === 'production' ? 500 : Infinity
}
}
]
},
optimization,
output: {
clean: true,
path: buildDestination(),
filename: 'module.js'
},
plugins: [
new CopyWebpackPlugin({
patterns: copyList()
}),
new MiniCssExtractPlugin({
filename: 'module.css',
insert: 'head'
}),
new WebpackBar({})
],
resolve: {
extensions: ['.js']
},
watch: buildMode === 'development'
}
export default bundleScript