-
Notifications
You must be signed in to change notification settings - Fork 3
/
scala.webpack.config.js
227 lines (215 loc) · 5.88 KB
/
scala.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
const webpack = require("webpack"),
merge = require("webpack-merge"),
UglifyJsPlugin = require("uglifyjs-webpack-plugin"),
path = require("path"),
CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require('html-webpack-plugin');
const topsrcdir = path.resolve(__dirname, "examples/src/main")
const Paths = {
ProjectName: "examples",
Assets: path.resolve(topsrcdir, "assets"),
Scala: path.resolve(topsrcdir, "scala"),
Public: path.resolve(topsrcdir, "public"),
}
function libraryOutput(dest, isProd, publicPath) {
const hash = isProd && isProd === true ? "[chunkhash]" : "[hash]"
return {
name: "ExampleApp",
output: {
publicPath,
path: dest,
filename: `[name].${hash}.js`,
library: "[name]",
libraryTarget: "var",
chunkFilename: `[name].${hash}.js`,
},
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "vendors",
chunks: "initial"
}
}
}
}
};
}
const indexHTMLPlugin = [
new HtmlWebpackPlugin({
title: "scalajs-reaction Examples App",
filename: "index.html",
template: path.join(Paths.Public, "index.html"),
}),
]
const finalStyleLoaders = [
{ loader: "style-loader" },
// may want to replace with types-for-css-modules-loader
{ loader: "css-loader", options: { modules: true, importLoaders: 1 } },
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins: loader => [
require("postcss-import")({ root: loader.resourcePath }),
require("postcss-mixins")(),
require("postcss-cssnext")({
//https://github.com/MoOx/postcss-cssnext/blob/master/src/features.js
features: {
customProperties: false,
applyRule: false,
calc: false
}
}),
require("postcss-reporter")({ clearMessages: true })
]
}
}
];
// in dev mode, we create a "dist" directory
const devServer = {
//publicPath: "dist/",
historyApiFallback: {
rewrites: [
{
// match everything or our SPA
from: "/^.$/",
// server to the root
to: "/"
}
]
},
contentBase: "dist",
compress: true,
hot: true,
open: true,
port: 8080,
host: "0.0.0.0",
disableHostCheck: true,
https: true,
watchContentBase: true,
headers: {
"Access-Control-Allow-Origin": "*"
}
};
// scalapath: relative path from topdir to scala output .js file
const common = (scalapath, isProd) => ({
entry: {
Scala: scalapath
},
target: "web",
resolve: {
// If using symlinks in node_modules, you need this false so webpack does
// *not* use the symlink's resolved absolute path as the directory hierarchy.
symlinks: false,
extensions: [".ts", ".tsx", ".js", ".jsx", ".json", "*"],
alias: {
Assets: Paths.Assets,
Public: Paths.Public,
Examples: Paths.Scala,
JSExamples: Paths.Assets,
BuildSettings: isProd
? path.resolve(Paths.Assets, "config.production")
: path.resolve(Paths.Assets, "config.development")
}
},
module: {
rules: [
{
// Load css, convert to js object, also load via stylesheet.
test: /\.css(\.js)?$/,
use: finalStyleLoaders
},
{
// Order is important for this loader, run after babel but before other css loaders. Why not postcss-js?
test: /\.css\.js?$/,
use: [{ loader: "css-js-loader" }]
},
{
// babel only transpiles, run typecheck separately via npm run typecheck
test: /\.(ts|js)x?$/, // picks up pure .js and .jsx
exclude: /node_modules/,
include: [/examples\/src\/main\/assets/],
loader: require.resolve("babel-loader"),
options: {
cacheDirectory: true
}
},
{
test: /\.(png|jpg|gif)$/,
use: ["url-loader"]
},
// {
// test: /\.js$/,
// use: ["scalajs-friendly-source-map-loader"],
// //use: ["source-map-loader"],
// enforce: "pre",
// exclude: [/node_modules/]
// },
{
test: /\.md$/,
use: "raw-loader"
}
]
},
devServer: devServer
});
function copies(dest) {
return [{ from: "examples/src/main/public/*.html", to: dest, flatten: true }];
}
const dev = {
devtool: "source-map",
mode: "development"
};
const prod = {
// skip source-map
mode: "production"
};
module.exports = function (env, argv) {
const isProd = env && env.BUILD_KIND && env.BUILD_KIND === "production";
const scalapath = path.join(__dirname, "examples/target/scala-3.1.0/Scala.js");
const dist = path.join(__dirname, "dist");
const staticAssets = copies(dist);
const output = libraryOutput(dist, isProd);
const globals = nodeEnv => ({
"process.env": { NODE_ENV: JSON.stringify(nodeEnv || "development") }
});
const copyplugin = new CopyWebpackPlugin(staticAssets);
const prodCopy = new CopyWebpackPlugin([
{
from: "dist/*",
to: path.join(__dirname, "website/scalajs-reaction/static/demo"),
flatten: true
}
]);
console.log("isProd: ", isProd);
console.log("scalapath: ", scalapath);
if (isProd) {
const g = globals("production");
console.log("Production build");
console.log("globals: ", g);
return merge(output, common(scalapath, isProd), prod, {
plugins: [
...indexHTMLPlugin,
new webpack.DefinePlugin(g),
copyplugin, // must be befor prodCopy
prodCopy
]
});
} else {
const g = globals();
console.log("Development build");
console.log("globals: ", g);
return merge(output, common(scalapath, isProd), dev, {
plugins: [
...indexHTMLPlugin,
new webpack.HotModuleReplacementPlugin(),
new webpack.DefinePlugin(g),
copyplugin,
prodCopy
]
});
}
};