-
Notifications
You must be signed in to change notification settings - Fork 11
/
webpack.common.js
147 lines (131 loc) · 4.56 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
142
143
144
145
146
147
/* eslint-disable no-var, strict, prefer-arrow-callback */
'use strict';
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const packageJson = require('./package.json');
const vendorDependencies = Object.keys(packageJson['dependencies']);
const dotenv = require('dotenv');
const loadEnv = envPath => {
try {
dotenv.config({ path: envPath });
} catch (err) {
// only ignore error if file is not found
if (err.toString().indexOf('ENOENT') < 0) {
throw err;
}
}
}
// Contrary to what you'd expect, the first env loaded is the one whose values are kept
loadEnv(path.join(__dirname, './.env.local'));
loadEnv(path.join(__dirname, './.env'));
function getEngineLocation() {
switch (process.env.ENGINE_LOCATION) {
case 'local':
return path.resolve(__dirname, './src/eterna/folding/engines');
case 'package':
return 'eternajs-folding-engines/engines';
default:
throw new Error('Invalid engine location');
}
}
module.exports = {
devtool: "inline-source-map",
entry: {
main: ['babel-polyfill', "./src/eterna/index.ts"],
vendor: vendorDependencies
},
output: {
filename: '[name].[chunkhash].js',
chunkFilename: 'bundles/[chunkhash].js',
assetModuleFilename: 'assets/[name].[hash].[ext]'
},
resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"],
alias: {
assets: path.resolve(__dirname, 'assets/'),
signals: path.resolve(__dirname, 'src/signals'),
flashbang: path.resolve(__dirname, 'src/flashbang'),
eterna: path.resolve(__dirname, 'src/eterna'),
'engines-bin': getEngineLocation()
},
fallback: {
// Our emscripten modules have code intended for non-web environments which import
// node libraries, which webpack tries to import even though they're not available in
// the web environment. More info: https://stackoverflow.com/a/59488387/5557208
fs: false,
crypto: false,
path: false
}
},
module: {
rules: [
{
// Include ts, tsx, and js files.
test: /\.(tsx?)|(js)$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
cacheDirectory: true
}
},
{
test: /\.(png|jpg|gif|mp3|ttf)$/,
type: 'asset/resource',
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
],
},
// 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.
externals: {
// "react": "React",
// "react-dom": "ReactDOM"
},
cache: {
// Cache to disk to make future builds faster
// (especially due to the folding engines, which are large)
type: 'filesystem',
buildDependencies: {
config: [
__filename,
path.join(__dirname, 'package-lock.json'),
path.join(__dirname, '.env'),
path.join(__dirname, '.env.local'),
path.join(__dirname, 'tsconfig.json'),
path.join(__dirname, '.babelrc'),
]
}
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
plugins: [
new webpack.EnvironmentPlugin(Object.keys(process.env)),
// Generate an index.html that includes our webpack bundles
new HtmlWebpackPlugin({
template: 'src/index.html.tmpl',
inject: false,
scriptLoading: 'blocking',
process: {
env: {
...process.env
}
}
}),
// Generate a manifest.json file containing our entry point file names:
// https://github.com/danethurber/webpack-manifest-plugin#hooks-options
new WebpackManifestPlugin({
filter: (item) => item.isInitial
}),
]
};