@best-shot/preset-web 0.8.6 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
package/{index.cjs → index.mjs}
RENAMED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import suffix from 'suffix';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const { splitChunks } = require('./lib/apply-split-chunks.cjs');
|
|
7
|
-
const { setHtml } = require('./lib/apply-set-html.cjs');
|
|
3
|
+
import { setHtml } from './lib/apply-set-html.mjs';
|
|
4
|
+
import { setOutputName } from './lib/apply-set-output-name.mjs';
|
|
5
|
+
import { splitChunks } from './lib/apply-split-chunks.mjs';
|
|
8
6
|
|
|
9
7
|
function addHash(filename) {
|
|
10
8
|
return suffix(filename, '.[contenthash:8]');
|
|
@@ -14,25 +12,12 @@ function addMin(filename) {
|
|
|
14
12
|
return suffix(filename, '.min');
|
|
15
13
|
}
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function isInstalled() {
|
|
20
|
-
try {
|
|
21
|
-
require.resolve('@road-to-rome/webpack-plugin/package.json');
|
|
22
|
-
return true;
|
|
23
|
-
} catch {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
exports.apply = function applyWeb({
|
|
29
|
-
config: { html, inject, vendors, define, sri, rtr },
|
|
30
|
-
}) {
|
|
31
|
-
return (chain) => {
|
|
15
|
+
export function apply({ config: { html, inject, vendors } }) {
|
|
16
|
+
return async (chain) => {
|
|
32
17
|
const mode = chain.get('mode');
|
|
33
|
-
const hot = chain.devServer.get('hot') || false;
|
|
34
18
|
const minimize = chain.optimization.get('minimize');
|
|
35
19
|
const serve = chain.devServer.entries() !== undefined;
|
|
20
|
+
const hot = (serve && chain.devServer.get('hot')) || false;
|
|
36
21
|
|
|
37
22
|
chain.devtool(
|
|
38
23
|
mode === 'production' ? false : serve ? 'eval-source-map' : 'source-map',
|
|
@@ -46,15 +31,15 @@ exports.apply = function applyWeb({
|
|
|
46
31
|
script: (filename) => `script/${filename}`,
|
|
47
32
|
style: (filename) => `style/${filename}`,
|
|
48
33
|
}),
|
|
49
|
-
)
|
|
50
|
-
|
|
51
|
-
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
await splitChunks({ vendors })(chain);
|
|
52
37
|
|
|
53
|
-
|
|
54
|
-
chain.plugin('road-to-rome').use('@road-to-rome/webpack-plugin', [rtr]);
|
|
55
|
-
}
|
|
38
|
+
await setHtml({ html, inject })(chain);
|
|
56
39
|
};
|
|
57
|
-
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const name = 'preset-web';
|
|
58
43
|
|
|
59
44
|
const regexpFormat = {
|
|
60
45
|
format: 'regex',
|
|
@@ -66,7 +51,7 @@ const items = {
|
|
|
66
51
|
type: 'object',
|
|
67
52
|
};
|
|
68
53
|
|
|
69
|
-
|
|
54
|
+
export const schema = {
|
|
70
55
|
html: {
|
|
71
56
|
oneOf: [
|
|
72
57
|
items,
|
|
@@ -87,6 +72,7 @@ exports.schema = {
|
|
|
87
72
|
},
|
|
88
73
|
},
|
|
89
74
|
babel: {
|
|
75
|
+
default: {},
|
|
90
76
|
type: 'object',
|
|
91
77
|
properties: {
|
|
92
78
|
polyfill: {
|
|
@@ -94,10 +80,6 @@ exports.schema = {
|
|
|
94
80
|
},
|
|
95
81
|
},
|
|
96
82
|
},
|
|
97
|
-
sri: {
|
|
98
|
-
default: true,
|
|
99
|
-
type: 'boolean',
|
|
100
|
-
},
|
|
101
83
|
vendors: {
|
|
102
84
|
additionalProperties: {
|
|
103
85
|
oneOf: [
|
|
@@ -112,5 +94,4 @@ exports.schema = {
|
|
|
112
94
|
},
|
|
113
95
|
type: 'object',
|
|
114
96
|
},
|
|
115
|
-
rtr: {},
|
|
116
97
|
};
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { relative, resolve } from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const { SubresourceIntegrityPlugin } = require('webpack-subresource-integrity');
|
|
7
|
-
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
|
8
|
-
const {
|
|
9
|
-
default: HtmlWebpackInjectPlugin,
|
|
10
|
-
} = require('html-webpack-inject-plugin');
|
|
11
|
-
const { relative } = require('path');
|
|
4
|
+
import deepmerge from 'deepmerge';
|
|
5
|
+
import extToRegexp from 'ext-to-regexp';
|
|
6
|
+
import slashToRegexp from 'slash-to-regexp';
|
|
12
7
|
|
|
13
8
|
function mergeAll(...options) {
|
|
14
9
|
return deepmerge.all(options, {
|
|
@@ -28,8 +23,8 @@ const htmlMinifier = {
|
|
|
28
23
|
useShortDoctype: true,
|
|
29
24
|
};
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
return (chain) => {
|
|
26
|
+
export function setHtml({ html = {}, inject = [] }) {
|
|
27
|
+
return async (chain) => {
|
|
33
28
|
const mode = chain.get('mode');
|
|
34
29
|
const watch = chain.get('watch');
|
|
35
30
|
const context = chain.get('context');
|
|
@@ -37,11 +32,12 @@ exports.setHtml = function setHtml({ html = {}, inject = [], define, sri }) {
|
|
|
37
32
|
|
|
38
33
|
const page = Array.isArray(html) ? html : [html];
|
|
39
34
|
|
|
35
|
+
const { default: HtmlWebpackPlugin } = await import('html-webpack-plugin');
|
|
36
|
+
|
|
40
37
|
page.forEach((options, index) => {
|
|
41
38
|
chain.plugin(`html-page-${index}`).use(HtmlWebpackPlugin, [
|
|
42
39
|
mergeAll(
|
|
43
40
|
{
|
|
44
|
-
...(define && { templateParameters: { define } }),
|
|
45
41
|
template: './src/index.html',
|
|
46
42
|
cache: watch,
|
|
47
43
|
},
|
|
@@ -53,14 +49,22 @@ exports.setHtml = function setHtml({ html = {}, inject = [], define, sri }) {
|
|
|
53
49
|
});
|
|
54
50
|
|
|
55
51
|
if (inject.length > 0) {
|
|
52
|
+
const {
|
|
53
|
+
default: { default: HtmlWebpackInjectPlugin },
|
|
54
|
+
} = await import('html-webpack-inject-plugin');
|
|
55
|
+
|
|
56
56
|
chain
|
|
57
57
|
.plugin('inject')
|
|
58
58
|
.use(HtmlWebpackInjectPlugin, [{ externals: inject }]);
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
if (mode === 'production'
|
|
61
|
+
if (mode === 'production') {
|
|
62
62
|
chain.output.crossOriginLoading('anonymous');
|
|
63
63
|
|
|
64
|
+
const { SubresourceIntegrityPlugin } = await import(
|
|
65
|
+
'webpack-subresource-integrity'
|
|
66
|
+
);
|
|
67
|
+
|
|
64
68
|
chain.plugin('subresource-integrity').use(SubresourceIntegrityPlugin);
|
|
65
69
|
}
|
|
66
70
|
|
|
@@ -70,12 +74,17 @@ exports.setHtml = function setHtml({ html = {}, inject = [], define, sri }) {
|
|
|
70
74
|
.use('micro-tpl-loader')
|
|
71
75
|
.loader('micro-tpl-loader');
|
|
72
76
|
|
|
73
|
-
chain.resolveLoader.modules.prepend(
|
|
77
|
+
chain.resolveLoader.modules.prepend(
|
|
78
|
+
relative(
|
|
79
|
+
context,
|
|
80
|
+
resolve(fileURLToPath(import.meta.url), '../../node_modules'),
|
|
81
|
+
),
|
|
82
|
+
);
|
|
74
83
|
|
|
75
|
-
if (chain.module.rules.has('babel')) {
|
|
84
|
+
if (!watch && chain.module.rules.has('babel')) {
|
|
76
85
|
chain.module
|
|
77
86
|
.rule('babel')
|
|
78
87
|
.exclude.add(slashToRegexp('/node_modules/micromustache/'));
|
|
79
88
|
}
|
|
80
89
|
};
|
|
81
|
-
}
|
|
90
|
+
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.setOutputName = function setOutputName({ script, style }) {
|
|
1
|
+
export function setOutputName({ script, style }) {
|
|
4
2
|
return (chain) => {
|
|
5
3
|
const jsFilename = chain.output.get('filename');
|
|
6
4
|
|
|
@@ -18,4 +16,4 @@ exports.setOutputName = function setOutputName({ script, style }) {
|
|
|
18
16
|
]);
|
|
19
17
|
}
|
|
20
18
|
};
|
|
21
|
-
}
|
|
19
|
+
}
|
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const slashToRegexp = require('slash-to-regexp');
|
|
4
|
-
const { MinChunkSizePlugin } = require('webpack').optimize;
|
|
5
|
-
|
|
6
|
-
const { join } = require('path');
|
|
1
|
+
import slashToRegexp from 'slash-to-regexp';
|
|
7
2
|
|
|
8
3
|
function mapValues(obj, func) {
|
|
9
4
|
const arr = Object.entries(obj);
|
|
10
|
-
return Object.fromEntries(
|
|
11
|
-
(
|
|
12
|
-
|
|
5
|
+
return Object.fromEntries(
|
|
6
|
+
arr.map(([key, value], index) => [
|
|
7
|
+
key,
|
|
8
|
+
func(value, key, index, arr.length),
|
|
9
|
+
]),
|
|
10
|
+
);
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
const force = {
|
|
@@ -17,8 +15,8 @@ const force = {
|
|
|
17
15
|
reuseExistingChunk: true,
|
|
18
16
|
};
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
return (chain) => {
|
|
18
|
+
export function splitChunks({ vendors = {} }) {
|
|
19
|
+
return async (chain) => {
|
|
22
20
|
const settings = mapValues(vendors, (value, key, index, length) => {
|
|
23
21
|
const mod = Array.isArray(value) ? `(${value.join('|')})` : value;
|
|
24
22
|
const regexp = slashToRegexp(`/node_modules/${mod}/`);
|
|
@@ -63,20 +61,18 @@ exports.splitChunks = function splitChunks({ vendors = {} }) {
|
|
|
63
61
|
const mode = chain.get('mode');
|
|
64
62
|
|
|
65
63
|
if (mode === 'production') {
|
|
64
|
+
const {
|
|
65
|
+
default: {
|
|
66
|
+
optimize: { MinChunkSizePlugin },
|
|
67
|
+
},
|
|
68
|
+
} = await import('webpack');
|
|
66
69
|
chain
|
|
67
70
|
.plugin('min-chunk-size')
|
|
68
71
|
.use(MinChunkSizePlugin, [{ minChunkSize: 1024 * 8 }]);
|
|
69
72
|
|
|
70
|
-
const
|
|
73
|
+
const { cachePath } = chain.get('x');
|
|
71
74
|
|
|
72
|
-
chain.recordsPath(
|
|
73
|
-
join(
|
|
74
|
-
process.cwd(),
|
|
75
|
-
'node_modules/.cache/best-shot/stats',
|
|
76
|
-
name,
|
|
77
|
-
'records.json',
|
|
78
|
-
),
|
|
79
|
-
);
|
|
75
|
+
chain.recordsPath(cachePath('records.json'));
|
|
80
76
|
}
|
|
81
77
|
};
|
|
82
|
-
}
|
|
78
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@best-shot/preset-web",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "A `best-shot` preset for web project",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -29,23 +29,23 @@
|
|
|
29
29
|
"bugs": {
|
|
30
30
|
"url": "https://github.com/best-shot/best-shot/issues"
|
|
31
31
|
},
|
|
32
|
-
"main": "index.
|
|
32
|
+
"main": "index.mjs",
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"deepmerge": "^4.2.2",
|
|
35
35
|
"ext-to-regexp": "^0.1.0",
|
|
36
|
-
"html-webpack-inject-plugin": "^5.
|
|
37
|
-
"html-webpack-plugin": "^5.
|
|
38
|
-
"micro-tpl-loader": "^0.0
|
|
36
|
+
"html-webpack-inject-plugin": "^5.2.3",
|
|
37
|
+
"html-webpack-plugin": "^5.5.0",
|
|
38
|
+
"micro-tpl-loader": "^0.1.0",
|
|
39
39
|
"slash-to-regexp": "^0.0.4",
|
|
40
40
|
"suffix": "^1.0.0",
|
|
41
|
-
"webpack-subresource-integrity": "5.0.0"
|
|
41
|
+
"webpack-subresource-integrity": "^5.0.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
|
-
"@best-shot/core": "^0.
|
|
45
|
-
"webpack": "^5.
|
|
44
|
+
"@best-shot/core": "^0.6.0",
|
|
45
|
+
"webpack": "^5.65.0"
|
|
46
46
|
},
|
|
47
47
|
"engines": {
|
|
48
|
-
"node": "^
|
|
48
|
+
"node": "^14.17.0 || >=16.13.0"
|
|
49
49
|
},
|
|
50
50
|
"x-readme": {
|
|
51
51
|
"logo": "https://cdn.jsdelivr.net/gh/best-shot/best-shot/packages/core/logo.svg"
|