forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.ts
136 lines (128 loc) · 4.98 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
import webpack from "webpack"
import "webpack-dev-server" // just imported for type magic
import path from "path"
/* eslint-disable @typescript-eslint/no-var-requires */
const MiniCssExtractPlugin = require("mini-css-extract-plugin")
const { WebpackManifestPlugin } = require("webpack-manifest-plugin")
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
const DotenvWebpackPlugin = require("dotenv-webpack")
/* eslint-enable @typescript-eslint/no-var-requires */
const config = (env: any, argv: any): webpack.Configuration => {
const isProduction = argv.mode === "production"
// baseDir is necessary to make webpack.config.ts use the correct path both in TS as well as in
// transpiled JS form
const baseDir =
path.basename(__dirname) === "itsJustJavascript"
? path.resolve(__dirname, "..")
: __dirname
const javascriptDir = path.resolve(baseDir, "itsJustJavascript")
return {
cache: {
type: "filesystem",
buildDependencies: {
config: [__filename],
},
},
context: javascriptDir,
entry: {
admin: "./adminSiteClient/admin.entry.js",
owid: "./site/owid.entry.js",
},
optimization: {
splitChunks: {
cacheGroups: {
// The bundle created through this cache group contains all the dependencies
// that are _both_ used by owid.entry.js and admin.entry.js.
vendors: {
test: (module: webpack.NormalModule) =>
!module.type?.startsWith("css") && // no need to split CSS, since there's very little vendor css anyway
/[\\/]node_modules[\\/]/.test(module.resource),
name: "vendors",
chunks: "all",
minChunks: 2,
priority: 1, // needs to be higher than for "commons"
},
// The bundle created through this cache group contains all the code
// that is _both_ used by owid.entry.js and admin.entry.js.
commons: {
name: "commons",
chunks: "all",
minChunks: 2,
priority: 0,
},
},
},
minimize: isProduction,
minimizer: ["...", new CssMinimizerPlugin()],
},
output: {
path: path.join(javascriptDir, "webpack"),
publicPath: "",
filename: "[name].js",
},
resolve: {
extensions: [".js", ".css"],
modules: ["node_modules", javascriptDir, baseDir], // baseDir is required for resolving *.scss files
fallback: {
// don't polyfill these Node modules
fs: false,
os: false,
path: false,
},
},
module: {
rules: [
{
test: /\.js$/,
enforce: "pre",
use: ["source-map-loader"],
exclude: /node_modules/,
},
{
test: /\.s?css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
url: false,
},
},
"sass-loader",
],
},
{
test: /\.(jpe?g|gif|png|eot|woff|ttf|svg|woff2)$/,
use: "asset",
},
],
},
devtool: "source-map", // add source maps in both production and dev
plugins: [
// This plugin extracts css files required in the entry points
// into a separate CSS bundle for download
new MiniCssExtractPlugin({ filename: "[name].css" }),
// Writes manifest.json which production code reads to know paths to asset files
new WebpackManifestPlugin(),
// Provide client-side settings from .env
new DotenvWebpackPlugin(),
// Ensure serverSettings.ts never end up in a webpack build by accident
new webpack.IgnorePlugin({
resourceRegExp: /settings\/serverSettings/,
}),
],
devServer: {
host: "localhost",
port: 8090,
allowedHosts: "all",
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":
"GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers":
"X-Requested-With, content-type, Authorization",
},
},
}
}
export default config