Skip to content

Commit 50bcc4c

Browse files
committed
chore: use cache-loader and splitChunks in example
1 parent 6017ed5 commit 50bcc4c

File tree

5 files changed

+113
-18
lines changed

5 files changed

+113
-18
lines changed

Diff for: example/index.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<link rel="stylesheet" href="/dist/main.css">
22
<div id="app"></div>
3-
<script src="/dist/bundle.js"></script>
3+
<script src="/dist/vendors.js"></script>
4+
<script src="/dist/main.js"></script>

Diff for: example/webpack.config.js

+33-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
const fs = require('fs')
12
const path = require('path')
3+
const hash = require('hash-sum')
24
const VueLoaderPlugin = require('../dist/plugin')
35
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
46

@@ -13,7 +15,7 @@ module.exports = (env = {}) => {
1315
devtool: isProd ? 'source-map' : 'cheap-module-eval-source-map',
1416
output: {
1517
path: path.resolve(__dirname, 'dist'),
16-
filename: 'bundle.js',
18+
filename: '[name].js',
1719
publicPath: '/dist/'
1820
},
1921
module: {
@@ -47,16 +49,27 @@ module.exports = (env = {}) => {
4749
},
4850
{
4951
test: /\.js$/,
50-
use: babel
51-
? {
52-
loader: 'babel-loader',
53-
options: {
54-
presets: ['@babel/preset-env']
55-
}
52+
use: [
53+
{
54+
loader: 'cache-loader',
55+
options: {
56+
cacheIdentifier: hash(
57+
fs.readFileSync(path.resolve(__dirname, '../package.json')) +
58+
JSON.stringify(env)
59+
)
5660
}
57-
: [
58-
/* skip babel */
59-
]
61+
},
62+
...(babel
63+
? [
64+
{
65+
loader: 'babel-loader',
66+
options: {
67+
presets: ['@babel/preset-env']
68+
}
69+
}
70+
]
71+
: [])
72+
]
6073
},
6174
// target <docs> custom blocks
6275
{
@@ -72,7 +85,16 @@ module.exports = (env = {}) => {
7285
})
7386
],
7487
optimization: {
75-
minimize
88+
minimize,
89+
splitChunks: {
90+
cacheGroups: {
91+
commons: {
92+
test: /esm-bundler/,
93+
name: 'vendors',
94+
chunks: 'all'
95+
}
96+
}
97+
}
7698
},
7799
devServer: {
78100
stats: 'minimal',

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@types/loader-utils": "^1.1.3",
4646
"@types/webpack": "^4.41.0",
4747
"babel-loader": "^8.0.6",
48+
"cache-loader": "^4.1.0",
4849
"css-loader": "^3.3.2",
4950
"file-loader": "^5.0.2",
5051
"jest": "^24.9.0",

Diff for: src/pitcher.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,21 @@ function genProxyModule(
9191
loaders: Loader[],
9292
context: webpack.loader.LoaderContext
9393
) {
94+
const request = genRequest(loaders, context)
95+
// return a proxy module which simply re-exports everything from the
96+
// actual request.
97+
return `export { default } from ${request}; export * from ${request}`
98+
}
99+
100+
function genRequest(loaders: Loader[], context: webpack.loader.LoaderContext) {
94101
const loaderStrings = loaders.map(loader => {
95102
return typeof loader === 'string' ? loader : loader.request
96103
})
97104
const resource = context.resourcePath + context.resourceQuery
98-
const request = loaderUtils.stringifyRequest(
105+
return loaderUtils.stringifyRequest(
99106
context,
100107
'-!' + [...loaderStrings, resource].join('!')
101108
)
102-
// return a proxy module which simply re-exports everything from the
103-
// actual request.
104-
return `export { default } from ${request}; export * from ${request}`
105109
}
106110

107111
function shouldIgnoreCustomBlock(loaders: Loader[]) {

Diff for: yarn.lock

+69-2
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,11 @@ buffer-indexof@^1.0.0:
17691769
resolved "https://registry.yarnpkg.com/buffer-indexof/-/buffer-indexof-1.1.1.tgz#52fabcc6a606d1a00302802648ef68f639da268c"
17701770
integrity sha512-4/rOEg86jivtPTeOUUT61jJO1Ya1TrR/OkqCSZDyq84WJh3LuuiphBYJN+fm5xufIk4XAFcEwte/8WzC8If/1g==
17711771

1772+
buffer-json@^2.0.0:
1773+
version "2.0.0"
1774+
resolved "https://registry.yarnpkg.com/buffer-json/-/buffer-json-2.0.0.tgz#f73e13b1e42f196fe2fd67d001c7d7107edd7c23"
1775+
integrity sha512-+jjPFVqyfF1esi9fvfUs3NqM0pH1ziZ36VP4hmA/y/Ssfo/5w5xHKfTw9BwQjoJ1w/oVtpLomqwUHKdefGyuHw==
1776+
17721777
buffer-xor@^1.0.3:
17731778
version "1.0.3"
17741779
resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9"
@@ -1834,6 +1839,18 @@ cache-base@^1.0.1:
18341839
union-value "^1.0.0"
18351840
unset-value "^1.0.0"
18361841

1842+
cache-loader@^4.1.0:
1843+
version "4.1.0"
1844+
resolved "https://registry.yarnpkg.com/cache-loader/-/cache-loader-4.1.0.tgz#9948cae353aec0a1fcb1eafda2300816ec85387e"
1845+
integrity sha512-ftOayxve0PwKzBF/GLsZNC9fJBXl8lkZE3TOsjkboHfVHVkL39iUEs1FO07A33mizmci5Dudt38UZrrYXDtbhw==
1846+
dependencies:
1847+
buffer-json "^2.0.0"
1848+
find-cache-dir "^3.0.0"
1849+
loader-utils "^1.2.3"
1850+
mkdirp "^0.5.1"
1851+
neo-async "^2.6.1"
1852+
schema-utils "^2.0.0"
1853+
18371854
caller-callsite@^2.0.0:
18381855
version "2.0.0"
18391856
resolved "https://registry.yarnpkg.com/caller-callsite/-/caller-callsite-2.0.0.tgz#847e0fce0a223750a9a027c54b33731ad3154134"
@@ -3141,13 +3158,30 @@ find-cache-dir@^2.0.0, find-cache-dir@^2.1.0:
31413158
make-dir "^2.0.0"
31423159
pkg-dir "^3.0.0"
31433160

3161+
find-cache-dir@^3.0.0:
3162+
version "3.2.0"
3163+
resolved "https://registry.yarnpkg.com/find-cache-dir/-/find-cache-dir-3.2.0.tgz#e7fe44c1abc1299f516146e563108fd1006c1874"
3164+
integrity sha512-1JKclkYYsf1q9WIJKLZa9S9muC+08RIjzAlLrK4QcYLJMS6mk9yombQ9qf+zJ7H9LS800k0s44L4sDq9VYzqyg==
3165+
dependencies:
3166+
commondir "^1.0.1"
3167+
make-dir "^3.0.0"
3168+
pkg-dir "^4.1.0"
3169+
31443170
find-up@^3.0.0:
31453171
version "3.0.0"
31463172
resolved "https://registry.yarnpkg.com/find-up/-/find-up-3.0.0.tgz#49169f1d7993430646da61ecc5ae355c21c97b73"
31473173
integrity sha512-1yD6RmLI1XBfxugvORwlck6f75tYL+iR0jqwsOrOxMZyGYqUuDhJ0l4AXdO1iX/FTs9cBAMEk1gWSEx1kSbylg==
31483174
dependencies:
31493175
locate-path "^3.0.0"
31503176

3177+
find-up@^4.0.0:
3178+
version "4.1.0"
3179+
resolved "https://registry.yarnpkg.com/find-up/-/find-up-4.1.0.tgz#97afe7d6cdc0bc5928584b7c8d7b16e8a9aa5d19"
3180+
integrity sha512-PpOwAdQ/YlXQ2vj8a3h8IipDuYRi3wceVQQGYWxNINccq40Anw7BlsEXCMbt1Zt+OLA6Fq9suIpIWD0OsnISlw==
3181+
dependencies:
3182+
locate-path "^5.0.0"
3183+
path-exists "^4.0.0"
3184+
31513185
31523186
version "3.0.0"
31533187
resolved "https://registry.yarnpkg.com/findup-sync/-/findup-sync-3.0.0.tgz#17b108f9ee512dfb7a5c7f3c8b27ea9e1a9c08d1"
@@ -4785,6 +4819,13 @@ locate-path@^3.0.0:
47854819
p-locate "^3.0.0"
47864820
path-exists "^3.0.0"
47874821

4822+
locate-path@^5.0.0:
4823+
version "5.0.0"
4824+
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-5.0.0.tgz#1afba396afd676a6d42504d0a67a3a7eb9f62aa0"
4825+
integrity sha512-t7hw9pI+WvuwNJXwk5zVHpyhIqzg2qTlklJOf0mVxGSbe3Fp2VieZcduNYjaLDoy6p9uGpQEGWG87WpMKlNq8g==
4826+
dependencies:
4827+
p-locate "^4.1.0"
4828+
47884829
lodash.clonedeep@^4.5.0:
47894830
version "4.5.0"
47904831
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
@@ -4868,6 +4909,13 @@ make-dir@^2.0.0, make-dir@^2.1.0:
48684909
pify "^4.0.1"
48694910
semver "^5.6.0"
48704911

4912+
make-dir@^3.0.0:
4913+
version "3.0.0"
4914+
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.0.0.tgz#1b5f39f6b9270ed33f9f054c5c0f84304989f801"
4915+
integrity sha512-grNJDhb8b1Jm1qeqW5R/O63wUo4UXo2v2HMic6YT9i/HBlF93S8jkMgH7yugvY9ABDShH4VZMn8I+U8+fCNegw==
4916+
dependencies:
4917+
semver "^6.0.0"
4918+
48714919
48724920
version "1.3.5"
48734921
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"
@@ -5590,7 +5638,7 @@ p-is-promise@^2.0.0:
55905638
resolved "https://registry.yarnpkg.com/p-is-promise/-/p-is-promise-2.1.0.tgz#918cebaea248a62cf7ffab8e3bca8c5f882fc42e"
55915639
integrity sha512-Y3W0wlRPK8ZMRbNq97l4M5otioeA5lm1z7bkNkxCka8HSPjR0xRWmpCmc9utiaLP9Jb1eD8BgeIxTW4AIF45Pg==
55925640

5593-
p-limit@^2.0.0:
5641+
p-limit@^2.0.0, p-limit@^2.2.0:
55945642
version "2.2.1"
55955643
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-2.2.1.tgz#aa07a788cc3151c939b5131f63570f0dd2009537"
55965644
integrity sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==
@@ -5604,6 +5652,13 @@ p-locate@^3.0.0:
56045652
dependencies:
56055653
p-limit "^2.0.0"
56065654

5655+
p-locate@^4.1.0:
5656+
version "4.1.0"
5657+
resolved "https://registry.yarnpkg.com/p-locate/-/p-locate-4.1.0.tgz#a3428bb7088b3a60292f66919278b7c297ad4f07"
5658+
integrity sha512-R79ZZ/0wAxKGu3oYMlz8jy/kbhsNrS7SKZ7PxEHBgJ5+F2mtFW2fK2cOtBh1cHYkQsbzFV7I+EoRKe6Yt0oK7A==
5659+
dependencies:
5660+
p-limit "^2.2.0"
5661+
56075662
p-map@^2.0.0:
56085663
version "2.1.0"
56095664
resolved "https://registry.yarnpkg.com/p-map/-/p-map-2.1.0.tgz#310928feef9c9ecc65b68b17693018a665cea175"
@@ -5702,6 +5757,11 @@ path-exists@^3.0.0:
57025757
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-3.0.0.tgz#ce0ebeaa5f78cb18925ea7d810d7b59b010fd515"
57035758
integrity sha1-zg6+ql94yxiSXqfYENe1mwEP1RU=
57045759

5760+
path-exists@^4.0.0:
5761+
version "4.0.0"
5762+
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-4.0.0.tgz#513bdbe2d3b95d7762e8c1137efa195c6c61b5b3"
5763+
integrity sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==
5764+
57055765
path-is-absolute@^1.0.0:
57065766
version "1.0.1"
57075767
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
@@ -5806,6 +5866,13 @@ pkg-dir@^3.0.0:
58065866
dependencies:
58075867
find-up "^3.0.0"
58085868

5869+
pkg-dir@^4.1.0:
5870+
version "4.2.0"
5871+
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-4.2.0.tgz#f099133df7ede422e81d1d8448270eeb3e4261f3"
5872+
integrity sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==
5873+
dependencies:
5874+
find-up "^4.0.0"
5875+
58095876
please-upgrade-node@^3.1.1:
58105877
version "3.2.0"
58115878
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942"
@@ -6572,7 +6639,7 @@ schema-utils@^1.0.0:
65726639
ajv-errors "^1.0.0"
65736640
ajv-keywords "^3.1.0"
65746641

6575-
schema-utils@^2.5.0, schema-utils@^2.6.0:
6642+
schema-utils@^2.0.0, schema-utils@^2.5.0, schema-utils@^2.6.0:
65766643
version "2.6.1"
65776644
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-2.6.1.tgz#eb78f0b945c7bcfa2082b3565e8db3548011dc4f"
65786645
integrity sha512-0WXHDs1VDJyo+Zqs9TKLKyD/h7yDpHUhEFsM2CzkICFdoX1av+GBq/J2xRTFfsQO5kBfhZzANf2VcIm84jqDbg==

0 commit comments

Comments
 (0)