-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
120 lines (94 loc) · 2.9 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
/**
* Webpack configuration
*/
const {
CommonsChunkPlugin,
UglifyJsPlugin,
DedupePlugin,
} = require('webpack').optimize;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const IndexHtml = new ExtractTextPlugin('index.html');
const RootCss = new ExtractTextPlugin('root.css');
const autoprefixer = require('autoprefixer');
module.exports = (function webpackConfig() {
const config = {
production: _e(),
};
config.entry = {
main: './src/main.ts',
common: ['./src/vendor.ts', './src/index.pug', './src/root.scss'],
};
config.output = {
path: './dist/',
filename: '[name].js',
chunkFilename: '[name].chunk[id].js',
};
config.resolve = {
root: './src/',
extensions: ['', '.ts', '.js', '.pug', '.scss'],
};
config.devtool = 'source-map';
config.module = {loaders: [
// *.component.ts is passed through ng2-component loader which inlines the
// compiled pug templates and scss styles
{test: _p('ts', 'component', ''), loader: 'ts!ng2-component'},
{test: _p('ts'), loader: 'ts',
exclude: [_p('ts', 'component', ''), _p()]}, // regular ts files
// index.pug is compiled and written to dist/index.html
{test: _p('pug', 'index'), loader: IndexHtml.extract('pug-html')},
{test: _p('pug'), loader: 'pug-html',
exclude: [_p('pug', 'index')]}, // pug templates for ng2-component loader
// root.scss is compiled and written to dist/root.css
{test: _p('scss', 'root'), loader: RootCss.extract('css!postcss!sass')},
{test: _p('scss'), loader: 'raw!postcss!sass',
exclude: [_p('scss', 'root'), _p()]}, // styles for ng2-component loader
// see IMPORTANT comment in src/include/variables.scss
{test: /fonts\/.*(eot|svg|ttf|woff2?)(\?v=\d\.\d\.\d)?$/,
loader: 'file?name=fonts/[name].[ext]'},
]};
config.plugins = [
new CommonsChunkPlugin({
name: ['common'], // contains vendor files etc.
filename: '[name].js',
}),
IndexHtml, // ExtractTextPlugin instance for index.html
RootCss, // ExtractTextPlugin instance for root.css
];
if (config.production) {
config.plugins.push(
new UglifyJsPlugin({
compress: {warnings: false},
comments: false,
}),
new DedupePlugin()
);
}
config.ts = {
silent: true, // stfu ts-loader! nobody wants to know TypeScript version
};
if (config.production)
config.ts.silent = false;
config.sassLoader = {
includePaths: ['./node_modules'], // see IMPORTANT comment in src/root.scss
};
config.postcss = () => [
autoprefixer({browsers: ['last 2 versions']}),
];
config.stats = {
children: false,
};
return config;
})();
// Generates RegExps
function _p(ext, ...args) {
if (!ext)
return /node_modules/;
if (!args.length)
return new RegExp(`\\.${ext}$`);
let r = args.reduce((r, v) => v + '\\.' + r, '');
return new RegExp(`${r}${ext}$`);
}
// Checks if running in production mode
function _e(env = process.env.NODE_ENV || 'dev') {
return Boolean(String(env).match(/^prod(uction)?$/i));
}