-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
executable file
·120 lines (103 loc) · 3.64 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
const path = require('path');
const webpack = require('webpack');
// load the web server settings from package.json
const {
devServer
} = require('./package.json');
// used to copy content from the src folder to the dist folder
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
// configure the environment object for development mode
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
//const ENV = process.env.ENV = process.env.NODE_ENV = 'production';
const MINIFY = false;
// configure source and distribution folder paths
const srcFolder = 'src';
const srcFolderPath = path.join(__dirname, srcFolder);
const tsFolderPath = path.join(srcFolderPath, 'ts');
// export webpack configuration
const webpackConfig = {
// root folder for entry point files
context: tsFolderPath,
// entry points for the three bundles, order does not matter
entry: {
app: [ 'whatwg-fetch', './app.tsx' ],
},
// allows us to require modules using
// import { someExport } from './my-module';
// instead of
// import { someExport } from './my-module.ts';
// with the extensions in the list, it can be omitted from the import
// root is an absolute path to the folder containing our application modules
resolve: {
extensions: [ '.js', '.ts', '.tsx', '.json' ], // order matters, resolves left to right
modules: [ tsFolderPath, path.join(__dirname, 'node_modules') ],
},
module: {
rules: [
// process all JavaScript files through the Babel preprocessor
// this enables support for ES2017 and earlier including modules
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [ 'ts-loader' ],
},
// transpiles global SASS stylesheets
// loader order is executed right to left
{
test: /\.s?css$/,
exclude: [path.join(__dirname, srcFolder, 'ts')],
use: [
'style-loader',
'css-loader',
'postcss-loader',
'sass-loader',
],
},
],
},
plugins: [
// copy image files, and the index.html file directly when they are changed
new CopyWebpackPlugin([{
from: path.join(srcFolderPath, 'images'),
to: 'images',
}, ]),
// configure the file to have the bundle script elements injected
// this is almost always the main html for the initial loading of
// the site
new HtmlWebpackPlugin({
template: path.join(srcFolderPath, 'index.html')
}),
// setup environment variables
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(ENV),
},
}),
],
// out file settings
// path points to web server content folder where the web server will serve the files from
// publicPath is the path to the files from the perspective of the web browser requesting
// the files from the web server, this is used to insert the script elements into the index.html
// file
// file name is the name of the files, where [name] is the name of each entry point
output: {
path: path.join(__dirname, devServer.contentBase),
publicPath: '/',
filename: '[name].js'
},
// use the webpack dev server to serve up the web application
devServer,
// use full source maps
// this specific setting value is required to set breakpoints in the TypeScript
// in the web browser for development
// other source map settings do not allow debugging in browser and vscode
devtool: 'source-map',
};
// only minify code when in production
if (ENV === 'production' && MINIFY) {
webpackConfig
.plugins
.push(new webpack.optimize.UglifyJsPlugin());
}
module.exports = webpackConfig;