-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwebpack.config.js
231 lines (219 loc) · 6.84 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const HTMLWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const StylelintPlugin = require('stylelint-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const sass = require('node-sass');
const TerserPlugin = require('terser-webpack-plugin');
const path = require('path');
const fs = require('fs');
const webpack = require('webpack');
const glob = require('glob');
const isProduction = process.argv[process.argv.indexOf('--mode') + 1] === 'production';
module.exports = {
entry: glob.sync('./app/**/**.js').reduce((acc, filePath) => {
let entry = filePath.replace(`/${path.basename(filePath)}`, '');
entry = (entry === './app' ? 'index' : entry.replace('./app/', ''));
if (path.basename(filePath) === 'index.js') {
acc[entry === 'index' ? entry : `${entry}/${entry}`] = filePath;
} else {
acc[`${entry}/${path.basename(filePath).replace('.js', '')}`] = filePath;
}
return acc;
}, {}),
devtool: isProduction ? 'cheap-module-source-map' : 'source-map', // try source-map for prod
mode: isProduction ? 'production' : 'development',
performance: {
hints: false
},
optimization: {
minimize: !!isProduction,
minimizer: [
new TerserPlugin({
test: /\.js(\?.*)?$/i
}),
],
},
output: {
library: '[name]-lib.js',
libraryTarget: 'umd',
libraryExport: 'default',
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
// Configure the dev server (node) with settings
devServer: {
port: 4300,
writeToDisk: true,
contentBase: path.resolve(__dirname, 'dist'),
// Server the files in app/data as a JSON "API"
// For example: http://localhost:4300/api/bikes or relative as /api/bikes
before: (app) => {
app.get('/api/:fileName', (req, res) => {
const { fileName } = req.params;
const json = fs.readFileSync(`./app/data/${fileName}.json`, 'utf8');
res.json(JSON.parse(json));
});
},
},
module: {
rules: [
{
test: /\.html$/,
loader: 'handlebars-loader'
},
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
// Options are all in babel.config.js
loader: 'babel-loader',
}
]
},
{
test: /\.scss$/,
exclude: [
/node_modules/,
path.resolve(__dirname, 'app')
],
use: [
'sass-to-string',
{
loader: 'sass-loader',
options: {
sassOptions: {
outputStyle: 'nested' // 'compressed',
},
},
},
],
},
{
test: /\.scss$/,
exclude: [
/node_modules/,
path.resolve(__dirname, 'src')
],
use: [
// Creates `style` nodes from JS strings
{
loader: 'style-loader',
options: {
attributes: {
id: 'demo-styles',
nonce: '0a59a005' // @TODO needs to match a global nonce instance
}
}
},
// Translates CSS into CommonJS
'css-loader',
// Compiles Sass to CSS
'sass-loader',
]
},
]
},
plugins: [
new webpack.LoaderOptionsPlugin({
options: {
handlebarsLoader: {}
}
}),
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: 'css/[name].min.css'
}),
// Append index "kitchen sink", rest is dynamic below
new HTMLWebpackPlugin({
template: 'app/index.html',
inject: 'body',
title: 'IDS Enterprise Web Components',
chunks: ['index']
}),
// Show Style Lint Errors in the console and fail
new StylelintPlugin({}),
// Make a Copy of the Sass Files only for standalone Css
new CopyWebpackPlugin({
patterns: [
{
from: './src/**/*.scss',
to({ absoluteFilename }) {
const baseName = path.basename(absoluteFilename);
return `${baseName.replace('.scss', '')}/${baseName.replace('scss', 'css')}`;
},
transform(content, transFormPath) {
const result = sass.renderSync({
file: transFormPath
});
let css = result.css.toString();
css = css.replace(':host {', ':root {');
return css;
}
},
{
from: './src/**/*.d.ts',
to({ absoluteFilename }) {
const baseName = path.basename(absoluteFilename);
if (absoluteFilename.indexOf('ids-base') > -1) {
return `${absoluteFilename.replace('/src/', '/dist/')}`;
}
return `${baseName.replace('.d.ts', '')}/${baseName}`;
},
}
]
})
]
};
// Fix build error on prod about favicon
if (!isProduction) {
module.exports.plugins.push(new FaviconsWebpackPlugin({
logo: 'app/assets/favicon.ico',
mode: 'auto'
}));
}
// Dynamically add all html examples
glob.sync('./app/**/*.html').reduce((acc, filePath) => {
const folderName = path.dirname(filePath).replace('./app/', '');
let folderAndFile = filePath.replace('./app/', '');
let title = `${folderName.split('-').map((word) =>
`${word.substring(0, 1).toUpperCase()}${word.substring(1)}`)
.join(' ')} ${folderAndFile.indexOf('standalone-css') > -1 ? 'Standalone Css' : 'Component'}`;
// Add a title to the component page
title = title.replace('Ids', 'IDS');
// Adjust the folder paths for layouts
if (folderName === 'layouts' || folderAndFile.indexOf('example.html') > -1 || folderAndFile === 'index.html') {
return folderName;
}
// Figure out the chunks to use
if (folderAndFile.indexOf('index.html') === -1) {
folderAndFile = folderAndFile.replace('.html', '');
}
let chunk = `${folderName}/${folderName}`;
const jsFile = path.basename(filePath).replace('.html', '.js');
if (jsFile !== 'index.js' && fs.existsSync(filePath.replace('.html', '.js'))) {
chunk = `${folderName}/${jsFile.replace('.js', '')}`;
}
const folderChunks = [chunk, 'ids-icon/ids-icon', 'ids-text/ids-text', 'ids-layout-grid/ids-layout-grid'];
// Add example.js to the page as a separate chunk
const demoFile = filePath.replace('index.html', 'example.js');
if (jsFile === 'index.js' && fs.existsSync(demoFile)) {
folderChunks.push(`${folderName}/example`);
}
// Create the entry
module.exports.plugins.push(
new HTMLWebpackPlugin({
template: filePath,
inject: 'body',
filename: folderAndFile,
title,
chunksSortMode: 'manual',
chunks: jsFile === 'standalone-css.js'
? []
: folderChunks
}),
);
return folderName;
}, {});