-
Notifications
You must be signed in to change notification settings - Fork 33
/
webpack.common.js
141 lines (133 loc) · 4.64 KB
/
webpack.common.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
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const FaviconsWebpackPlugin = require('favicons-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const localConfig = path.resolve(__dirname, 'config-local.js')
const defaultConfig = path.resolve(__dirname, 'config.js')
let config
if (fs.existsSync(localConfig)) {
config = localConfig
} else if (fs.existsSync(defaultConfig)) {
config = defaultConfig
} else {
throw `The config file is missing: ${defaultConfig}`
}
// get git info from command line
const gitSHA = require('child_process').execSync('git rev-parse HEAD').toString().trim()
let package = require('./package.json')
module.exports = {
entry: path.resolve(__dirname, 'src', 'index.tsx'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.[contenthash].js',
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
extensions: ['.ts', '.tsx', '.js', '.json', '.css', '.svg'],
},
module: {
rules: [
{ test: /\.tsx?$/, loader: 'ts-loader' },
{
enforce: 'pre',
test: /\.js$/,
loader: 'source-map-loader',
},
// We use css modules for our own code, which uses .module.css as naming convention.
{
test: /\.module\.css$/,
exclude: path.resolve(__dirname, 'node_modules'),
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[path][name]__[local]',
},
},
},
],
},
// All other css files are simply processed without modules.
// We use these for some 3rd party dependencies like ol and codemirror.
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
},
// this loader inlines svg images as react components
{
test: /\.svg$/,
exclude: path.resolve(__dirname, 'src/pathDetails/img'),
use: ['@svgr/webpack'],
},
// HeightGraph.css loads svg files using url(), so we need to add them as asset modules
{
test: /\.svg$/,
include: path.resolve(__dirname, 'src/pathDetails/img'),
type: 'asset',
},
{
test: /\.png$/i,
type: 'asset',
},
{
test: /\.ttf$/i,
type: 'asset',
},
],
},
plugins: [
new HTMLWebpackPlugin({ template: path.resolve(__dirname, 'src/index.html') }),
new FaviconsWebpackPlugin({
logo: './src/logo.svg',
favicons: {
icons: { appleStartup: false, yandex: false, coast: false },
},
}),
// config.js is kept outside the bundle and simply copied to the dist folder
new CopyPlugin({
patterns: [
{
from: config,
to: 'config.js',
},
],
}),
new CopyPlugin({
patterns: [
{
from: './src/manifest.json',
to: 'manifest.json',
// see https://stackoverflow.com/a/54700817/194609
transform(content, path) {
let manifest = JSON.parse(content.toString())
manifest.version = package.version
return JSON.stringify(manifest, null, 2)
},
},
],
}),
new webpack.DefinePlugin({
GIT_SHA: JSON.stringify(gitSHA),
}),
],
externals: {
config: 'config',
},
// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
// maybe use this later, but with HTMLWebpackPlugin it is easier without
/* externals: {
"react": "React",
"react-dom": "ReactDOM"
}
*/
}