-
Notifications
You must be signed in to change notification settings - Fork 120
/
webpack.config.examples.cjs
151 lines (145 loc) · 4.02 KB
/
webpack.config.examples.cjs
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
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {join} = require('path');
/** Get the list of examples from the examples directory.
*
* @param {string} dirName Name of the directory to read.
* @param {Function} callback Function to execute for each example.
*
* @return {Object} Entries.
*/
function getExamples(dirName, callback) {
const example_files = fs.readdirSync(dirName);
const entries = {};
// iterate through the list of files in the directory.
for (const filename of example_files) {
// ooo, javascript file!
if (filename.endsWith('.js')) {
// trim the entry name down to the file without the extension.
const entry_name = filename.split('.')[0];
callback(entry_name, path.join(dirName, filename));
}
}
return entries;
}
/** Creates an object with the entry names and file names
* to be transformed.
*
* @param {string} dirName Name of the directory to read.
*
* @return {Object} with webpack entry points.
*/
function getEntries(dirName) {
const entries = {};
getExamples(dirName, (entryName, filename) => {
entries[entryName] = filename;
});
return entries;
}
/** Each example needs a dedicated HTML file.
* This will create a "plugin" that outputs HTML from a template.
*
* @param {string} dirName Name of the directory to read.
*
* @return {Array} specifying webpack plugins.
*/
function getHtmlTemplates(dirName) {
const html_conf = [];
// create the array of HTML plugins.
const template = path.join(dirName, '_template.html');
getExamples(dirName, (entryName, filename) => {
html_conf.push(
new HtmlWebpackPlugin({
title: entryName,
// ensure each output has a unique filename
filename: entryName + '.html',
template,
// without specifying chunks, all chunks are
// included with the file.
chunks: ['common', entryName],
})
);
});
return html_conf;
}
module.exports = (env, argv) => {
const prod = argv.mode === 'production';
return {
context: __dirname,
target: 'web',
mode: prod ? 'production' : 'development',
entry: getEntries(path.resolve(path.join(__dirname, 'examples'))),
optimization: {
runtimeChunk: {
name: 'common',
},
splitChunks: {
name: 'common',
chunks: 'initial',
minChunks: 2,
},
},
output: {
filename: '[name].js',
path: path.join(__dirname, 'dist', 'examples'),
publicPath: 'auto',
},
resolve: {
alias: {
'ol-mapbox-style': path.join(__dirname, 'src'),
},
fallback: {
'assert': path.join(__dirname, 'node_modules', 'nanoassert'),
},
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.css$/,
use: [
prod ? MiniCssExtractPlugin.loader : 'style-loader',
'css-loader',
],
},
{
test: /\.js$/,
enforce: 'pre',
use: ['remove-flow-types-loader'],
include: join(
__dirname,
'node_modules',
'@mapbox',
'mapbox-gl-style-spec'
),
},
],
},
plugins: [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: '[name].css',
chunkFilename: '[id].css',
}),
// ensure the data is copied over.
// currently the index.html is manually created.
// @ts-ignore
new CopyWebpackPlugin({
patterns: [
{
from: path.resolve(__dirname, './examples/data'),
to: 'data',
},
{
from: path.resolve(__dirname, './examples/index.html'),
to: 'index.html',
},
],
}),
].concat(getHtmlTemplates('./examples')),
};
};