Skip to content

Commit 9731e9c

Browse files
brkalowgregberge
authored andcommitted
feat(ChunkExtractor): support publicPath override (#292)
1 parent c172324 commit 9731e9c

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

packages/server/src/ChunkExtractor.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,23 @@ function isValidChunkAsset(chunkAsset) {
153153
}
154154

155155
class ChunkExtractor {
156-
constructor({ statsFile, stats, entrypoints = ['main'], outputPath } = []) {
156+
constructor({
157+
statsFile,
158+
stats,
159+
entrypoints = ['main'],
160+
outputPath,
161+
publicPath,
162+
} = {}) {
157163
this.stats = stats || smartRequire(statsFile)
164+
this.publicPath = publicPath || this.stats.publicPath
158165
this.outputPath = outputPath || this.stats.outputPath
159166
this.statsFile = statsFile
160167
this.entrypoints = Array.isArray(entrypoints) ? entrypoints : [entrypoints]
161168
this.chunks = []
162169
}
163170

164171
resolvePublicUrl(filename) {
165-
const { publicPath } = this.stats
166-
return joinURLPath(publicPath, filename)
172+
return joinURLPath(this.publicPath, filename)
167173
}
168174

169175
getChunkGroup(chunk) {

packages/server/src/ChunkExtractor.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ describe('ChunkExtrator', () => {
1212
})
1313
})
1414

15+
describe('#resolvePublicUrl', () => {
16+
it('should default to using stats.publicPath', () => {
17+
expect(extractor.resolvePublicUrl('main.js')).toEqual(
18+
'/dist/node/main.js',
19+
)
20+
})
21+
22+
it('should use publicPath from ChunkExtractor options', () => {
23+
extractor = new ChunkExtractor({
24+
stats,
25+
publicPath: 'https://cdn.example.org/v1.1.0/',
26+
outputPath: path.resolve(__dirname, '../__fixtures__'),
27+
})
28+
29+
expect(extractor.resolvePublicUrl('main.js')).toEqual(
30+
'https://cdn.example.org/v1.1.0/main.js',
31+
)
32+
})
33+
})
34+
1535
describe('#stats', () => {
1636
it('should load stats from file', () => {
1737
extractor = new ChunkExtractor({
@@ -228,6 +248,33 @@ Array [
228248
src="/dist/node/letters-A.js"
229249
/>,
230250
]
251+
`)
252+
})
253+
254+
it('should use publicPath from options', () => {
255+
extractor = new ChunkExtractor({
256+
stats,
257+
publicPath: 'https://cdn.example.org/v1.1.0/',
258+
outputPath: path.resolve(__dirname, '../__fixtures__'),
259+
})
260+
261+
expect(extractor.getScriptElements()).toMatchInlineSnapshot(`
262+
Array [
263+
<script
264+
dangerouslySetInnerHTML={
265+
Object {
266+
"__html": "[]",
267+
}
268+
}
269+
id="__LOADABLE_REQUIRED_CHUNKS__"
270+
type="application/json"
271+
/>,
272+
<script
273+
async={true}
274+
data-chunk="main"
275+
src="https://cdn.example.org/v1.1.0/main.js"
276+
/>,
277+
]
231278
`)
232279
})
233280
})

website/src/pages/docs/server-side-rendering.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,18 @@ import loadable from '@loadable/component'
199199
// This dynamic import will not be processed server-side
200200
const Other = loadable(() => import('./Other'), { ssr: false })
201201
```
202+
203+
## Override `stats.publicPath` at runtime
204+
205+
To override `stats.publicPath` at runtime, pass in a custom `publicPath` to the `ChunkExtractor` constructor:
206+
207+
```js
208+
import { ChunkExtractor } from '@loadable/server'
209+
210+
const statsFile = path.resolve('../dist/loadable-stats.json')
211+
212+
const extractor = new ChunkExtractor({
213+
statsFile,
214+
publicPath: 'https://cdn.example.org/v1.1.0/',
215+
})
216+
```

0 commit comments

Comments
 (0)