forked from FredKSchott/snowpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
376 lines (348 loc) · 11.8 KB
/
plugin.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
const crypto = require('crypto');
const fs = require('fs');
const glob = require('glob');
const path = require('path');
const url = require('url');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const jsdom = require('jsdom');
const {JSDOM} = jsdom;
const cwd = process.cwd();
const minify = require('html-minifier').minify;
function insertBefore(newNode, existingNode) {
existingNode.parentNode.insertBefore(newNode, existingNode);
}
function parseHTMLFiles({buildDirectory}) {
// Get all html files from the output folder
const pattern = buildDirectory + '/**/*.html';
const htmlFiles = glob.sync(pattern).map((htmlPath) => path.relative(buildDirectory, htmlPath));
const doms = {};
const jsEntries = {};
for (const htmlFile of htmlFiles) {
const dom = new JSDOM(fs.readFileSync(path.join(buildDirectory, htmlFile)));
//Find all local script, use it as the entrypoint
const scripts = Array.from(dom.window.document.querySelectorAll('script'))
.filter((el) => el.type.trim().toLowerCase() === 'module')
.filter((el) => !/^[a-zA-Z]+:\/\//.test(el.src));
for (const el of scripts) {
const src = el.src.trim();
const parsedPath = path.parse(src);
const name = parsedPath.name;
if (!(name in jsEntries)) {
jsEntries[name] = {
path: path.join(buildDirectory, src),
occurrences: [],
};
}
jsEntries[name].occurrences.push({script: el, dom});
}
doms[htmlFile] = dom;
}
return {doms, jsEntries};
}
function emitHTMLFiles({doms, jsEntries, stats, baseUrl, buildDirectory, htmlMinifierOptions}) {
const entrypoints = stats.toJson({assets: false, hash: true}).entrypoints;
//Now that webpack is done, modify the html files to point to the newly compiled resources
Object.keys(jsEntries).forEach((name) => {
if (entrypoints[name] !== undefined && entrypoints[name]) {
const assetFiles = entrypoints[name].assets || [];
const jsFiles = assetFiles.filter((d) => d.endsWith('.js'));
const cssFiles = assetFiles.filter((d) => d.endsWith('.css'));
for (const occurrence of jsEntries[name].occurrences) {
const originalScriptEl = occurrence.script;
const dom = occurrence.dom;
const head = dom.window.document.querySelector('head');
for (const jsFile of jsFiles) {
const scriptEl = dom.window.document.createElement('script');
scriptEl.src = url.parse(baseUrl).protocol
? url.resolve(baseUrl, jsFile)
: path.posix.join(baseUrl, jsFile);
// insert _before_ so the relative order of these scripts is maintained
insertBefore(scriptEl, originalScriptEl);
}
for (const cssFile of cssFiles) {
const linkEl = dom.window.document.createElement('link');
linkEl.setAttribute('rel', 'stylesheet');
linkEl.href = path.posix.join(baseUrl, cssFile);
head.append(linkEl);
}
originalScriptEl.remove();
}
}
});
//And write our modified html files out to the destination
for (const [htmlFile, dom] of Object.entries(doms)) {
const html = htmlMinifierOptions
? minify(dom.serialize(), htmlMinifierOptions)
: dom.serialize();
fs.writeFileSync(path.join(buildDirectory, htmlFile), html);
}
}
function getSplitChunksConfig({numEntries}) {
/**
* Implements a version of granular chunking, as described at https://web.dev/granular-chunking-nextjs/.
*/
return {
chunks: 'all',
maxInitialRequests: 25,
minSize: 20000,
cacheGroups: {
default: false,
vendors: false,
// NPM libraries larger than 150KB are pulled into their own chunk
lib: {
test(module) {
return module.size() > 150000 && /node_modules[/\\]/.test(module.identifier());
},
priority: 30,
minChunks: 1,
reuseExistingChunk: true,
},
// modules used by all entrypoints end up in commons
commons: {
name: 'commons',
minChunks: numEntries,
priority: 20,
},
// modules used by multiple chunks can be pulled into shared chunks
shared: {
name(module, chunks) {
const hash = crypto
.createHash(`sha1`)
.update(chunks.reduce((acc, chunk) => acc + chunk.name, ``))
.digest(`hex`);
return hash;
},
priority: 10,
minChunks: 2,
reuseExistingChunk: true,
},
},
};
}
module.exports = function plugin(config, args = {}) {
// Deprecated: args.mode
if (args.mode && args.mode !== 'production') {
throw new Error('args.mode support has been removed.');
}
// Validate: args.outputPattern
args.outputPattern = args.outputPattern || {};
const jsOutputPattern = args.outputPattern.js || 'js/[name].[contenthash].js';
const cssOutputPattern = args.outputPattern.css || 'css/[name].[contenthash].css';
const assetsOutputPattern = args.outputPattern.assets || 'assets/[name]-[hash].[ext]';
if (!jsOutputPattern.endsWith('.js')) {
throw new Error('Output Pattern for JS must end in .js');
}
if (!cssOutputPattern.endsWith('.css')) {
throw new Error('Output Pattern for CSS must end in .css');
}
// Default options for HTMLMinifier
// https://github.com/kangax/html-minifier#options-quick-reference
const defaultHtmlMinifierOptions = {
collapseWhitespace: true,
removeComments: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
};
const htmlMinifierOptions =
args.htmlMinifierOptions === false
? false
: Object.assign({}, defaultHtmlMinifierOptions, args.htmlMinifierOptions);
const manifest =
typeof args.manifest === 'string'
? args.manifest
: !!args.manifest
? './asset-manifest.json'
: undefined;
// Webpack handles minification for us, so its safe to always
// disable Snowpack's default minifier.
config.buildOptions.minify = false;
// Webpack creates unique file hashes for all generated bundles,
// so we clean the build directory before building to remove outdated
// build artifacts.
config.buildOptions.clean = true;
return {
name: '@snowpack/plugin-webpack',
async optimize({buildDirectory, log}) {
// config.homepage is legacy, remove in future version
const buildOptions = config.buildOptions || {};
let baseUrl = buildOptions.baseUrl || config.homepage || '/';
const tempBuildManifest = JSON.parse(
await fs.readFileSync(path.join(cwd, 'package.json'), {
encoding: 'utf-8',
}),
);
const presetEnvTargets =
tempBuildManifest.browserslist || '>0.75%, not ie 11, not UCAndroid >0, not OperaMini all';
let extendConfig = (cfg) => cfg;
if (typeof args.extendConfig === 'function') {
extendConfig = args.extendConfig;
} else if (typeof args.extendConfig === 'object') {
extendConfig = (cfg) => ({...cfg, ...args.extendConfig});
}
const {doms, jsEntries} = parseHTMLFiles({buildDirectory});
if (Object.keys(jsEntries).length === 0) {
throw new Error("Can't bundle without script tag in html");
}
//Compile files using webpack
let webpackConfig = {
context: buildDirectory,
resolve: {
alias: {
'/__snowpack__': path.join(buildDirectory, '__snowpack__'),
'/web_modules': path.join(buildDirectory, 'web_modules'),
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
cwd: buildDirectory,
configFile: false,
babelrc: false,
compact: true,
presets: [
[
'@babel/preset-env',
{
targets: presetEnvTargets,
bugfixes: true,
modules: false,
useBuiltIns: 'usage',
corejs: 3,
},
],
],
},
},
{
loader: require.resolve('./plugins/import-meta-fix.js'),
},
{
loader: require.resolve('./plugins/proxy-import-resolve.js'),
},
],
},
{
test: /\.css$/,
exclude: /\.module\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
},
],
},
{
test: /\.module\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
{
loader: 'css-loader',
options: {
modules: true,
},
},
],
},
{
test: /.*/,
exclude: [/\.js?$/, /\.json?$/, /\.css$/],
use: [
{
loader: 'file-loader',
options: {
name: assetsOutputPattern,
},
},
],
},
],
},
mode: 'production',
devtool: args.sourceMap ? 'source-map' : undefined,
optimization: {
// extract webpack runtime to its own chunk: https://webpack.js.org/concepts/manifest/#runtime
runtimeChunk: {
name: `webpack-runtime`,
},
splitChunks: getSplitChunksConfig({numEntries: jsEntries.length}),
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},
};
const plugins = [
//Extract a css file from imported css files
new MiniCssExtractPlugin({
filename: cssOutputPattern,
}),
];
if (manifest) {
plugins.push(new ManifestPlugin({fileName: manifest}));
}
let entry = {};
for (name in jsEntries) {
entry[name] = jsEntries[name].path;
}
const extendedConfig = extendConfig({
...webpackConfig,
plugins,
entry,
output: {
path: buildDirectory,
publicPath: baseUrl,
filename: jsOutputPattern,
},
});
const compiler = webpack(extendedConfig);
const stats = await new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
reject(err);
return;
}
if (stats.hasErrors()) {
const info = stats.toJson(extendedConfig.stats);
console.error(info.warnings.join('\n-----\n'));
console.error(info.errors.join('\n-----\n'));
}
resolve(stats);
});
});
if (extendedConfig.stats !== 'none') {
console.log(
stats.toString(
extendedConfig.stats
? extendedConfig.stats
: {
colors: true,
all: false,
assets: true,
},
),
);
}
emitHTMLFiles({
doms,
jsEntries,
stats,
baseUrl,
buildDirectory,
htmlMinifierOptions,
});
},
};
};