Skip to content

Commit 78e7b28

Browse files
committed
fix(server): disable common chunks optim
1 parent cfcdb17 commit 78e7b28

File tree

5 files changed

+89
-19
lines changed

5 files changed

+89
-19
lines changed

packages/babel-plugin/src/__snapshots__/index.test.js.snap

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ exports[`plugin aggressive import without "webpackChunkName" should add it 1`] =
126126
exports[`plugin loadable.lib should be transpiled too 1`] = `
127127
"loadable.lib({
128128
chunkName() {
129-
return \\"moment\\";
129+
return \\"loadable-moment\\";
130130
},
131131
132132
isReady(props) {
@@ -138,7 +138,7 @@ exports[`plugin loadable.lib should be transpiled too 1`] = `
138138
},
139139
140140
requireAsync: () => import(
141-
/* webpackChunkName: \\"moment\\" */
141+
/* webpackChunkName: \\"loadable-moment\\" */
142142
'moment'),
143143
144144
requireSync(props) {
@@ -165,7 +165,7 @@ exports[`plugin loadable.lib should be transpiled too 1`] = `
165165
exports[`plugin simple import in a complex promise should work 1`] = `
166166
"loadable({
167167
chunkName() {
168-
return \\"ModA\\";
168+
return \\"loadable-ModA\\";
169169
},
170170
171171
isReady(props) {
@@ -177,7 +177,7 @@ exports[`plugin simple import in a complex promise should work 1`] = `
177177
},
178178
179179
requireAsync: () => timeout(import(
180-
/* webpackChunkName: \\"ModA\\" */
180+
/* webpackChunkName: \\"loadable-ModA\\" */
181181
'./ModA'), 2000),
182182
183183
requireSync(props) {
@@ -204,7 +204,7 @@ exports[`plugin simple import in a complex promise should work 1`] = `
204204
exports[`plugin simple import should transform path into "chunk-friendly" name 1`] = `
205205
"loadable({
206206
chunkName() {
207-
return \\"foo-bar\\";
207+
return \\"loadable-foo-bar\\";
208208
},
209209
210210
isReady(props) {
@@ -216,7 +216,7 @@ exports[`plugin simple import should transform path into "chunk-friendly" name 1
216216
},
217217
218218
requireAsync: () => import(
219-
/* webpackChunkName: \\"foo-bar\\" */
219+
/* webpackChunkName: \\"loadable-foo-bar\\" */
220220
'../foo/bar'),
221221
222222
requireSync(props) {
@@ -279,10 +279,49 @@ exports[`plugin simple import should work with template literal 1`] = `
279279
});"
280280
`;
281281
282+
exports[`plugin simple import with "webpackChunkName" comment should not put prefix if already there 1`] = `
283+
"loadable({
284+
chunkName() {
285+
return \\"loadable-ChunkA\\";
286+
},
287+
288+
isReady(props) {
289+
if (typeof __webpack_modules__ !== 'undefined') {
290+
return !!__webpack_modules__[this.resolve(props)];
291+
}
292+
293+
return false;
294+
},
295+
296+
requireAsync: () => import(
297+
/* webpackChunkName: \\"loadable-ChunkA\\" */
298+
'./ModA'),
299+
300+
requireSync(props) {
301+
const id = this.resolve(props);
302+
303+
if (typeof __webpack_require__ !== 'undefined') {
304+
return __webpack_require__(id);
305+
}
306+
307+
return eval('module.require')(id);
308+
},
309+
310+
resolve() {
311+
if (require.resolveWeak) {
312+
return require.resolveWeak(\\"./ModA\\");
313+
}
314+
315+
return require('path').resolve(__dirname, \\"./ModA\\");
316+
}
317+
318+
});"
319+
`;
320+
282321
exports[`plugin simple import with "webpackChunkName" comment should use it 1`] = `
283322
"loadable({
284323
chunkName() {
285-
return \\"ChunkA\\";
324+
return \\"loadable-ChunkA\\";
286325
},
287326
288327
isReady(props) {
@@ -294,7 +333,7 @@ exports[`plugin simple import with "webpackChunkName" comment should use it 1`]
294333
},
295334
296335
requireAsync: () => import(
297-
/* webpackChunkName: \\"ChunkA\\" */
336+
/* webpackChunkName: \\"loadable-ChunkA\\" */
298337
'./ModA'),
299338
300339
requireSync(props) {
@@ -321,7 +360,7 @@ exports[`plugin simple import with "webpackChunkName" comment should use it 1`]
321360
exports[`plugin simple import without "webpackChunkName" comment should add it 1`] = `
322361
"loadable({
323362
chunkName() {
324-
return \\"ModA\\";
363+
return \\"loadable-ModA\\";
325364
},
326365
327366
isReady(props) {
@@ -333,7 +372,7 @@ exports[`plugin simple import without "webpackChunkName" comment should add it 1
333372
},
334373
335374
requireAsync: () => import(
336-
/* webpackChunkName: \\"ModA\\" */
375+
/* webpackChunkName: \\"loadable-ModA\\" */
337376
'./ModA'),
338377
339378
requireSync(props) {

packages/babel-plugin/src/index.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ describe('plugin', () => {
3737

3838
expect(result).toMatchSnapshot()
3939
})
40+
41+
it('should not put prefix if already there', () => {
42+
const result = testPlugin(`
43+
loadable(() => import(/* webpackChunkName: "loadable-ChunkA" */ './ModA'))
44+
`)
45+
46+
expect(result).toMatchSnapshot()
47+
})
4048
})
4149

4250
describe('without "webpackChunkName" comment', () => {

packages/babel-plugin/src/properties/chunkName.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ export default function chunkNameProperty({ types: t }) {
6565
importArg.node.expressions,
6666
)
6767
}
68-
return t.stringLiteral(moduleToChunk(importArg.node.value))
68+
return t.stringLiteral(`loadable-${moduleToChunk(importArg.node.value)}`)
6969
}
7070

7171
function getExistingChunkName(callPath) {
7272
const importArg = getImportArg(callPath)
7373
const chunkName = getRawChunkNameFromCommments(importArg)
7474
if (!chunkName) return null
75-
return t.stringLiteral(chunkName)
75+
const loadableChunkName = chunkName.startsWith('loadable-')
76+
? chunkName
77+
: `loadable-${chunkName}`
78+
return t.stringLiteral(loadableChunkName)
7679
}
7780

7881
function isAgressiveImport(callPath) {
@@ -82,7 +85,7 @@ export default function chunkNameProperty({ types: t }) {
8285
)
8386
}
8487

85-
function addChunkNameComment(callPath, chunkName) {
88+
function addOrReplaceChunkNameComment(callPath, chunkName) {
8689
const importArg = getImportArg(callPath)
8790
const chunkNameComment = getChunkNameComment(importArg)
8891
if (chunkNameComment) {
@@ -104,14 +107,17 @@ export default function chunkNameProperty({ types: t }) {
104107
return ({ callPath, funcPath }) => {
105108
let chunkName
106109
const agressiveImport = isAgressiveImport(callPath)
110+
107111
if (!agressiveImport) {
108112
chunkName = getExistingChunkName(callPath)
109113
}
114+
110115
if (!chunkName) {
111116
chunkName = generateChunkName(callPath)
112-
addChunkNameComment(callPath, chunkName)
113117
}
114118

119+
addOrReplaceChunkNameComment(callPath, chunkName)
120+
115121
return t.objectMethod(
116122
'method',
117123
t.identifier('chunkName'),

packages/server/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This table compares the two modes.
1717
| Mode | `@loadable/babel-plugin` | `@loadable/webpack-plugin` | `loadComponents()` | Perf |
1818
| ------- | ------------------------ | -------------------------- | ------------------ | ---- |
1919
| Static | Required | Required | Not required | ++ |
20-
| Dynamic | Required | No required | Required | + |
20+
| Dynamic | Required | Required | Required | + |
2121

2222
### Static Mode (recommended)
2323

@@ -39,8 +39,8 @@ This table compares the two modes.
3939
const LoadablePlugin = require('@loadable/webpack-plugin')
4040

4141
module.exports = {
42-
/* Your webpack config... */
43-
plugins: [new HtmlWebpackPlugin()],
42+
// ...
43+
plugins: [new LoadablePlugin()],
4444
}
4545
```
4646

@@ -75,7 +75,20 @@ const scriptTags = loadableState.getScriptTags() // or loadableState.getScriptEl
7575
}
7676
```
7777

78-
#### 2. Setup `LoadableState` server-side (without manifest)
78+
#### 2. Install `@loadable/webpack-plugin`
79+
80+
**webpack.config.js**
81+
82+
```js
83+
const LoadablePlugin = require('@loadable/webpack-plugin')
84+
85+
module.exports = {
86+
// ...
87+
plugins: [new LoadablePlugin()],
88+
}
89+
```
90+
91+
#### 3. Setup `LoadableState` server-side (without manifest)
7992

8093
```js
8194
import path from 'path'
@@ -92,7 +105,7 @@ const html = renderToString(
92105
const scriptTags = loadableState.getScriptTags() // or loadableState.getScriptElements();
93106
```
94107

95-
#### 3. Use `loadComponents()` client-side
108+
#### 4. Use `loadComponents()` client-side
96109

97110
```js
98111
import { hydrate } from 'react-dom'

packages/webpack-plugin/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class LoadablePlugin {
66
}
77

88
apply(compiler) {
9+
// Disable splitChunks for loadable chunks
10+
compiler.options.optimization.splitChunks.chunks = chunk =>
11+
!chunk.canBeInitial() && !chunk.name.startsWith('loadable-')
12+
913
compiler.hooks.emit.tap('@loadable/webpack-plugin', hookCompiler => {
1014
const { assetsByChunkName, publicPath } = hookCompiler.getStats().toJson({
1115
hash: true,

0 commit comments

Comments
 (0)