This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
common.webpack.config.js
182 lines (161 loc) · 4.42 KB
/
common.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*jslint node: true */
'use strict';
const logger = require('@blackbaud/skyux-logger');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin');
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
const { OutputKeepAlivePlugin } = require('../../plugin/output-keep-alive');
const skyPagesConfigUtil = require('../sky-pages/sky-pages.config');
const aliasBuilder = require('./alias-builder');
function spaPath() {
return skyPagesConfigUtil.spaPath.apply(skyPagesConfigUtil, arguments);
}
function outPath() {
return skyPagesConfigUtil.outPath.apply(skyPagesConfigUtil, arguments);
}
function getLogFormat(skyPagesConfig, argv) {
if (argv.hasOwnProperty('logFormat')) {
return argv.logFormat;
}
if (skyPagesConfig.runtime.command === 'serve' || argv.serve) {
return 'compact';
}
return 'expanded';
}
/**
* Called when loaded via require.
* @name getWebpackConfig
* @param {SkyPagesConfig} skyPagesConfig
* @returns {WebpackConfig} webpackConfig
*/
function getWebpackConfig(skyPagesConfig, argv = {}) {
const resolves = [
process.cwd(),
spaPath('node_modules'),
outPath('node_modules')
];
let alias = aliasBuilder.buildAliasList(skyPagesConfig);
const outConfigMode = skyPagesConfig && skyPagesConfig.skyux && skyPagesConfig.skyux.mode;
const logFormat = getLogFormat(skyPagesConfig, argv);
let appPath;
switch (outConfigMode) {
case 'advanced':
appPath = spaPath('src', 'main.ts');
break;
default:
appPath = outPath('src', 'main-internal.ts');
break;
}
let plugins = [
// Some properties are required on the root object passed to HtmlWebpackPlugin
new HtmlWebpackPlugin({
template: skyPagesConfig.runtime.app.template,
inject: skyPagesConfig.runtime.app.inject,
runtime: skyPagesConfig.runtime,
skyux: skyPagesConfig.skyux
}),
new CommonsChunkPlugin({
name: ['skyux', 'vendor', 'polyfills']
}),
new webpack.DefinePlugin({
'skyPagesConfig': JSON.stringify(skyPagesConfig)
}),
new LoaderOptionsPlugin({
options: {
context: __dirname,
skyPagesConfig: skyPagesConfig
}
}),
new ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)@angular/,
spaPath('src'),
{}
),
new OutputKeepAlivePlugin({
enabled: argv['output-keep-alive']
})
];
// Supporting a custom logging type of none
if (logFormat !== 'none') {
plugins.push(new SimpleProgressWebpackPlugin({
format: logFormat,
color: logger.logColor
}));
}
return {
entry: {
polyfills: [outPath('src', 'polyfills.ts')],
vendor: [outPath('src', 'vendor.ts')],
skyux: [outPath('src', 'skyux.ts')],
app: [appPath]
},
output: {
filename: '[name].js',
chunkFilename: '[id].chunk.js',
path: spaPath('dist'),
},
resolveLoader: {
modules: resolves
},
resolve: {
alias: alias,
modules: resolves,
extensions: [
'.js',
'.ts'
]
},
module: {
rules: [
{
enforce: 'pre',
test: /config\.ts$/,
loader: outPath('loader', 'sky-app-config'),
include: outPath('runtime')
},
{
enforce: 'pre',
test: [
/\.(html|s?css)$/,
/sky-pages\.module\.ts/
],
loader: outPath('loader', 'sky-assets')
},
{
enforce: 'pre',
test: /sky-pages\.module\.ts$/,
loader: outPath('loader', 'sky-pages-module')
},
{
enforce: 'pre',
loader: outPath('loader', 'sky-processor', 'preload'),
include: spaPath('src'),
exclude: /node_modules/
},
{
test: /\.s?css$/,
use: [
'raw-loader',
'sass-loader'
]
},
{
test: /\.html$/,
loader: 'raw-loader'
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
plugins
};
}
module.exports = {
getWebpackConfig: getWebpackConfig
};