-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack.config.js
134 lines (127 loc) · 3.88 KB
/
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
const path = require('path')
const { NODE_ENV, dest, codap, noGlobals, noMap } = process.env;
const CopyPlugin = require('copy-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const ReplaceInFileWebpackPlugin = require('replace-in-file-webpack-plugin')
const Dotenv = require('dotenv-webpack')
const { assets, replacementStrings } = require('./build-support/build-opts')
const isProduction = NODE_ENV === 'production'
const srcDir = path.resolve(__dirname, 'src')
const destDir = path.resolve(__dirname, dest || './dist')
// Base configuration shared between configurations for each entry point.
// Note that the env passed in to these configuration functions is the webpack
// environment, as controlled via --env command-line arguments, which we are not
// currently making use of. By convention, the node.js environment (i.e. process.env)
// is used for configuration purposes instead.
const baseConfig = (env) => ({
mode: isProduction ? 'production' : 'development',
performance: { hints: false },
devtool: noMap
? false
: 'source-map',
context: srcDir,
output: {
filename: '[name]',
path: destDir
},
module: {
rules: [
{
test: /(\.tsx?|\.jsx?)$/,
use: 'ts-loader',
exclude: [/node_modules/,/\.test\./]
},
{
test: /\.styl$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader', // translates CSS into CommonJS
options: {
url: false
}
},
{
loader: 'stylus-loader' // compiles Stylus to CSS
}
]
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.jsx', '.json', '.styl']
},
plugins: [
new Dotenv(),
new MiniCssExtractPlugin({
filename: (pathData) =>
`${pathData.chunk.name.replace(/js\//, 'css/').replace(/\.js$/, '.css')}`
}),
new CopyPlugin({
patterns: assets.map(name => ({
from: path.resolve(__dirname, `./src/assets/${name}`),
to: `${destDir}/${name}`
}))
}),
new ReplaceInFileWebpackPlugin([
{
dir: destDir,
test: /\.html$/,
rules: replacementStrings.html
},
{
dir: destDir,
test: /\.css$/,
rules: replacementStrings.css
},
{
dir: destDir,
test: /\.js$/,
rules: replacementStrings.js
}
])
]
})
//
const appConfig = (env) => ({
// for now we use simple destructuring to merge configurations
// webpack-merge is available if merge becomes more complicated
// https://www.npmjs.com/package/webpack-merge
...baseConfig(env),
entry: {
'js/app.js': './code/app.tsx'
// 'app.js': './code/app.tsx' to put at top level rather than in js subdir
},
// These third-party libraries are bundled separately in the globals bundle and then
// accessed as global variables. This configures webpack to replace imports of these
// libraries with references to the corresponding global variables. See globals.ts
// for the code that loads the libraries and defines the global variables.
externals : {
'create-react-class': 'createReactClass',
'jquery' : '$',
'lodash' : '_',
'react': 'React',
'react-dom': 'ReactDOM',
'react-dom-factories': 'ReactDOMFactories'
}
})
const globalsConfig = (env) => ({
...baseConfig(env),
entry: {
'js/globals.js': './code/globals.ts'
// 'globals.js': './code/globals.ts' to put at top level rather than in js subdir
}
})
const autolaunchConfig = (env) => ({
...baseConfig(env),
entry: {
'autolaunch/autolaunch.js': './code/autolaunch/autolaunch.ts',
}
})
module.exports = (env) => {
const _appConfig = appConfig(env)
const _globalsConfig = codap || noGlobals ? [] : [globalsConfig(env)]
return codap
? _appConfig
: [autolaunchConfig(env), ..._globalsConfig, _appConfig]
}