-
Notifications
You must be signed in to change notification settings - Fork 13
/
webpack.config.ts
103 lines (97 loc) · 2.89 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
/**
* webpack.config.ts
*
* To build in 'production' mode, run `npx webpack --env production`.
* Alternatively, use 'script/build` for development, and 'script/cibuild' for
* production.
*
* The config's output target is assets/js/primer_spec_plugin.min.js.
*/
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import * as webpack from 'webpack';
import * as webpackDevServer from 'webpack-dev-server';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
interface Configuration extends webpack.Configuration {
devServer?: webpackDevServer.Configuration;
}
const VERSION_RAW = fs.readFileSync(
path.resolve(__dirname, 'VERSION'),
'utf-8',
);
// Starting with v1.2, every minor version's assets will be hosted in a
// separate directory (so that specs that use older versions of Primer Spec
// will still render correctly).
const semver_version = semver.coerce(VERSION_RAW);
if (semver_version == null) {
throw new Error('Unexpectedly null semver version');
}
const VERSION_MINOR_STR = `v${semver_version.major}.${semver_version.minor}`;
function getBuildMode(env: NodeJS.ProcessEnv) {
return env && env.production ? 'production' : 'development';
}
export default function (env: NodeJS.ProcessEnv): Configuration {
return {
mode: getBuildMode(env),
context: path.resolve(__dirname, 'src_js/'),
entry: './main.tsx',
output: {
path: path.join(__dirname, `/assets/${VERSION_MINOR_STR}/js/`),
filename: 'primer_spec_plugin.min.js',
},
module: {
rules: [
// JavaScript loader
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /(node_modules|bower_components)/,
options: {
babelrc: false,
presets: [['@babel/preset-env', { modules: false }]],
},
},
// TypeScript loader
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
// When importing files, no need to mention these file-extensions
resolve: {
extensions: ['.js', '.ts', '.tsx'],
},
plugins: [
// These variables become available in any file
new webpack.DefinePlugin({
'process.env.VERSION_RAW': JSON.stringify(VERSION_RAW),
'process.env.VERSION_MINOR_STR': JSON.stringify(VERSION_MINOR_STR),
'process.env.BUILD_MODE': JSON.stringify(getBuildMode(env)),
}),
new BundleAnalyzerPlugin({
analyzerMode: 'static',
openAnalyzer: false,
reportFilename: '../../../report.html',
}),
],
optimization: {
usedExports: true,
},
// Generate sourcemaps
devtool: 'source-map',
// Minimize output
stats: { preset: 'minimal' },
devServer: {
stats: {
hash: false,
version: false,
timings: false,
assets: false,
chunks: false,
},
},
};
}