-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack-build-common.js
160 lines (159 loc) · 6.61 KB
/
webpack-build-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
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
var path = require('path');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');
var atl = require('awesome-typescript-loader');
var base_href_webpack_1 = require('@angular-cli/base-href-webpack');
var find_lazy_modules_1 = require('./find-lazy-modules');
var HtmlIncluderWebpackPlugin = require('html-includer-webpack-plugin').default
function getWebpackCommonConfig(projectRoot, environment, appConfig, baseHref) {
var appRoot = path.resolve(projectRoot, appConfig.root);
var appMain = path.resolve(appRoot, appConfig.main);
var styles = appConfig.styles
? appConfig.styles.map(function (style) { return path.resolve(appRoot, style); })
: [];
var scripts = appConfig.scripts
? appConfig.scripts.map(function (script) { return path.resolve(appRoot, script); })
: [];
var lazyModules = find_lazy_modules_1.findLazyModules(appRoot);
var entry = {
main: [appMain]
};
// Only add styles/scripts if there's actually entries there
if (appConfig.styles.length > 0) {
entry['styles'] = styles;
}
if (appConfig.scripts.length > 0) {
entry['scripts'] = scripts;
}
return {
devtool: 'source-map',
resolve: {
extensions: ['', '.ts', '.js'],
root: appRoot
},
context: path.resolve(__dirname, './'),
entry: entry,
output: {
path: path.resolve(projectRoot, appConfig.outDir),
filename: '[name].bundle.js'
},
module: {
preLoaders: [
{
test: /\.js$/,
loader: 'source-map-loader',
exclude: [
/node_modules/
]
}
],
loaders: [
{
test: /\.ts$/,
loaders: [
{
loader: 'awesome-typescript-loader',
query: {
useForkChecker: true,
tsconfig: path.resolve(appRoot, appConfig.tsconfig)
}
}, {
loader: 'angular2-template-loader'
}
],
exclude: [/\.(spec|e2e)\.ts$/]
},
// in main, load css as raw text
{
exclude: styles,
test: /\.css$/,
loaders: ['raw-loader', 'postcss-loader']
}, {
exclude: styles,
test: /\.styl$/,
loaders: ['raw-loader', 'postcss-loader', 'stylus-loader'] },
{
exclude: styles,
test: /\.less$/,
loaders: ['raw-loader', 'postcss-loader', 'less-loader']
}, {
exclude: styles,
test: /\.scss$|\.sass$/,
loaders: ['raw-loader', 'postcss-loader', 'sass-loader']
},
// outside of main, load it via style-loader
{
include: styles,
test: /\.css$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader']
}, {
include: styles,
test: /\.styl$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader', 'stylus-loader']
}, {
include: styles,
test: /\.less$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader']
}, {
include: styles,
test: /\.scss$|\.sass$/,
loaders: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader']
},
// load global scripts using script-loader
{ include: scripts, test: /\.js$/, loader: 'script-loader' },
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.(jpg|png|gif)$/, loader: 'url-loader?limit=10000' },
{ test: /\.html$/, loader: 'raw-loader' },
{ test: /\.(otf|woff|ttf|svg)$/, loader: 'url?limit=10000' },
{ test: /\.woff2$/, loader: 'url?limit=10000&mimetype=font/woff2' },
{ test: /\.eot$/, loader: 'file' }
]
},
plugins: [
new webpack.ContextReplacementPlugin(/.*/, appRoot, lazyModules),
new atl.ForkCheckerPlugin(),
new HtmlWebpackPlugin({
template: path.resolve(appRoot, appConfig.index),
chunksSortMode: 'dependency'
}),
new HtmlIncluderWebpackPlugin(),
new base_href_webpack_1.BaseHrefWebpackPlugin({
baseHref: baseHref
}),
new webpack.NormalModuleReplacementPlugin(
// This plugin is responsible for swapping the environment files.
// Since it takes a RegExp as first parameter, we need to escape the path.
// See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
new RegExp(path.resolve(appRoot, appConfig.environments['source'])
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')), path.resolve(appRoot, appConfig.environments[environment])),
new webpack.optimize.CommonsChunkPlugin({
// Optimizing ensures loading order in index.html
name: ['styles', 'scripts', 'main'].reverse()
}),
new webpack.optimize.CommonsChunkPlugin({
minChunks: Infinity,
name: 'inline',
filename: 'inline.js',
sourceMapFilename: 'inline.map'
}),
new CopyWebpackPlugin([{
context: path.resolve(appRoot, appConfig.assets),
from: { glob: '**/*', dot: true },
ignore: ['.gitkeep'],
to: path.resolve(projectRoot, appConfig.outDir, appConfig.assets)
}])
],
node: {
fs: 'empty',
global: 'window',
crypto: 'empty',
module: false,
clearImmediate: false,
setImmediate: false
}
};
}
exports.getWebpackCommonConfig = getWebpackCommonConfig;
//# sourceMappingURL=/Users/hans/Sources/angular-cli/packages/angular-cli/angular-cli/models/webpack-build-common.js.map