forked from fgpv-vpgf/fgpv-vpgf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common.js
195 lines (172 loc) · 6.59 KB
/
webpack.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const webpack = require('webpack');
const path = require('path');
const fs = require('fs');
const glob = require("glob");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TranslationPlugin = require('./scripts/webpack/translations_plugin.js');
const SchemaValidatorPlugin = require('./scripts/webpack/schema_validation_plugin.js');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const VersionPlugin = require('./scripts/webpack/version_plugin.js');
const WrapperPlugin = require('wrapper-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const WebpackShellPlugin = require('webpack-shell-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const babelPresets = {
presets: ['env', 'stage-2'],
cacheDirectory: true
}
module.exports = function (env) {
const geoPath = env.geoLocal ?
env.geoLocal.length > 0 ?
env.geoLocal :
path.resolve(__dirname, '../', 'geoApi') :
path.resolve(__dirname, 'node_modules/geoApi');
const config = {
entry: {
'rv-main': path.resolve(__dirname, 'src/app/app-loader.js')
},
output: {
path: path.resolve(__dirname, 'build'),
filename: '[name].js'
},
module: {
rules: [
{
test: /\.js$/,
include: [path.resolve(__dirname, 'src/app'), path.resolve(__dirname, 'src/plugins'), geoPath],
use: [{
loader: 'ng-annotate-loader'
}, {
loader: 'babel-loader',
options: babelPresets
}, {
loader: 'eslint-loader'
}]
},
{
test: /\.ts$/,
include: [path.resolve(__dirname, 'intention'), path.resolve(__dirname, 'api'), path.resolve(__dirname, 'src/app')],
use: [{
loader: 'babel-loader',
options: babelPresets
}, {
loader: 'ts-loader'
}]
},
{
test: /\.s?[ac]ss$/,
use: [
env.hmr ? 'style-loader' : MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
{
test: /\.html$/,
use: ['ngtemplate-loader?relativeTo=' + (path.resolve(__dirname, './src/app')), 'html-loader?minimize=false']
},
{
test: /\.(png|svg)$/,
use: 'url-loader'
},
{
test: /\.xsl$/,
use: 'raw-loader'
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "rv-styles.css"
}),
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale$/,
/en|fr/
),
new CopyWebpackPlugin([{
context: 'src/content/samples',
from: '**/*.+(json|js|css|html)',
to: 'samples'
},{
from: 'src/locales/about',
to: 'samples/about'
},{
from: 'src/locales/help',
to: 'samples/help'
},
{
from: 'src/polyfill/ie-polyfills.js',
to: ''
}]),
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
new TranslationPlugin('./src/locales/translations.csv'),
new WrapperPlugin({
header: fileName => /^rv-main\.js$/.test(fileName) ? fs.readFileSync('./scripts/webpack/header.js', 'utf8') : '',
footer: fileName => /^rv-main\.js$/.test(fileName) ? fs.readFileSync('./scripts/webpack/footer.js', 'utf8') : ''
}),
new VersionPlugin(),
new CleanWebpackPlugin(['build']),
new SchemaValidatorPlugin()
],
resolve: {
modules: [path.resolve(__dirname, 'node_modules'), path.resolve(geoPath, 'node_modules'), path.resolve(__dirname, 'intention/node_modules')],
alias: {
XSLT: path.resolve(__dirname, 'src/content/metadata/'),
jquery: 'jquery/src/jquery', // so webpack builds from src and not dist - optional but good to have
api: path.resolve(__dirname, 'api/src/'),
src: path.resolve(__dirname, 'src/'),
app: path.resolve(__dirname, 'src/app/'),
intention: path.resolve(__dirname, 'intention/')
},
extensions: ['.ts', '.js', 'css', 'scss']
},
watchOptions: {
aggregateTimeout: 300,
poll: 1000,
ignored: /node_modules/
},
devServer: {
host: '0.0.0.0',
publicPath: '/',
historyApiFallback: {
index: '/samples/webpack-note.html',
verbose: true
},
disableHostCheck: true,
contentBase: false,
port: 6001,
stats: { colors: true },
compress: true
}
};
const files = glob.sync("samples/**/*", {cwd: './src/content/', nodir: true});
config.plugins.push(...files.map(file => {
if (/\.tpl$/.test(file)) {
const filePath = file.split('/');
const fileName = filePath.pop();
return new HtmlWebpackPlugin({
inject: false,
filename: `${filePath.join('/')}/${fileName.replace(/\.[^/.]+$/, '.html')}`,
template: `src/content/${file}`,
excludeChunks: ['ie-polyfills']
});
}
}).filter(x => x)
);
// not supported while doing hmr - causes memory leaks and slows build time by ~40%
if (!env.hmr && !env.inspect) {
config.plugins.push(new webpack.optimize.ModuleConcatenationPlugin());
}
if (env.inspect) {
config.plugins.push(new BundleAnalyzerPlugin({openAnalyzer: false, generateStatsFile: true}));
}
if (env.geoLocal) {
config.resolve.alias['geoApi$'] = geoPath;
}
return config;
}