This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
199 lines (174 loc) · 4.4 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
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
196
197
198
199
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const packageJSON = require('./package.json');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const moduleName = 'flights.search.widget';
// For DEV mode prepend "NODE_ENV=dev" before "webpack" command.
// terminal: NODE_ENV=dev webpack
/* global process */
const isDevMode = process.env.NODE_ENV === 'development';
// Streaming compiled styles to the separate ".css" file.
const extractSass = new ExtractTextPlugin({
filename: `${moduleName}.min.css`
});
const extractNemoSass = new ExtractTextPlugin({
filename: `nemo-${moduleName}.min.css`
});
const config = {
// Root folder for Webpack.
context: __dirname,
entry: {
[moduleName]: ['whatwg-fetch', './src/main.tsx'],
demo: './src/demo.tsx'
},
// Watch for changes in file.
watch: isDevMode,
mode: isDevMode ? 'development' : 'production',
watchOptions: {
// Do not watch for changes in "node_modules".
ignored: /node_modules/,
// Webpack will wait for 300ms before building bundles.
aggregateTimeout: 300
},
output: {
// Folder to store generated files.
path: path.resolve(__dirname, 'dist'),
// Path for loading assets.
publicPath: '/dist/',
// Output file name.
filename: '[name].min.js',
library: 'FlightsSearchWidget',
libraryTarget: 'umd'
},
performance: {
hints: false
},
optimization: {
minimizer: !isDevMode ? [new UglifyJsPlugin({
uglifyOptions: {
compress: {
reduce_funcs: false
}
}
})] : []
},
resolve: {
// Where to look for modules.
modules: [
'node_modules',
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'dist')
],
extensions: ['.ts', '.js', '.json', '.jsx', '.tsx', '.css', '.scss']
},
module: {
// List of loaders ("handlers") for different types of files.
rules: [
// Handling ".tsx" and ".ts" files in "/src" folder.
{
test: /\.tsx?$/,
loader: 'ts-loader',
include: [
path.resolve(__dirname, 'src')
],
// Do not parse these folders.
exclude: [
path.resolve(__dirname, 'node_modules')
]
},
// Handling ".scss" files, converting them to ".css", appending vendor prefixes.
{
test: /\.scss$/,
include: [
path.resolve(__dirname, 'src/css')
],
exclude: [
path.resolve(__dirname, 'src/css/nemo')
],
use: extractSass.extract({
use: [
// Allows to import CSS through JavaScript.
{
loader: 'css-loader',
options: {
minimize: !isDevMode
}
},
'resolve-url-loader', // Resolving relative URL in CSS code.
'postcss-loader', // Using autoprefixe plugin.
'sass-loader' // Compiles Sass to CSS.
],
fallback: 'style-loader',
publicPath: path.resolve(__dirname, 'dist')
})
},
// Handling ".scss" files for nemo default theme
{
test: /\.scss$/,
include: [
path.resolve(__dirname, 'src/css/nemo')
],
use: extractNemoSass.extract({
use: [
// Allows to import CSS through JavaScript.
{
loader: 'css-loader',
options: {
minimize: !isDevMode
}
},
'resolve-url-loader', // Resolving relative URL in CSS code.
'postcss-loader', // Using autoprefixe plugin.
'sass-loader' // Compiles Sass to CSS.
],
fallback: 'style-loader',
publicPath: path.resolve(__dirname, 'dist')
})
},
// Handling fonts and converting them to base64 format.
{
test: /\.woff$/,
loader: 'url-loader',
options: {
limit: 50000,
mimetype: 'application/font-woff'
},
include: [
path.resolve(__dirname, 'src/css/fonts')
]
},
// Handling SVG images.
{
test: /\.svg$/,
loader: 'url-loader',
include: [
path.resolve(__dirname, 'src/css/images'),
path.resolve(__dirname, 'src/css/nemo/images')
],
options: {
publicPath: '',
outputPath: '',
name: '/[name].[ext]'
}
}
]
},
plugins: [
extractSass,
extractNemoSass,
new webpack.ContextReplacementPlugin(/moment[/\\]locale$/, /ru|en|de|ro/)
]
};
if (!isDevMode) {
config.plugins.push(new webpack.optimize.OccurrenceOrderPlugin());
}
config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(isDevMode ? 'development' : 'production'),
VERSION: JSON.stringify(packageJSON.version)
}
})
);
module.exports = config;