Skip to content

Commit 2d1645d

Browse files
authored
Added getEntrypointPath to ChunkExtractor and reading json from fs (#951)
* Added getEntrypointPath to ChunkExtractor and reading json from inputFilesystem instead of require * Adds getAllScriptAssetsPaths to ChunkExtractor
1 parent 82c7019 commit 2d1645d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

packages/server/src/ChunkExtractor.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import flatMap from 'lodash/flatMap'
77
import React from 'react'
88
import { invariant, getRequiredChunkKey } from './sharedInternals'
99
import ChunkExtractorManager from './ChunkExtractorManager'
10-
import { smartRequire, joinURLPath } from './util'
10+
import { smartRequire, joinURLPath, readJsonFileSync } from './util'
1111

1212
const EXTENSION_SCRIPT_TYPES = {
1313
'.js': 'script',
@@ -205,7 +205,7 @@ class ChunkExtractor {
205205
inputFileSystem = fs,
206206
} = {}) {
207207
this.namespace = namespace
208-
this.stats = stats || smartRequire(statsFile)
208+
this.stats = stats || readJsonFileSync(inputFileSystem, statsFile)
209209
this.publicPath = publicPath || this.stats.publicPath
210210
this.outputPath = outputPath || this.stats.outputPath
211211
this.statsFile = statsFile
@@ -383,18 +383,30 @@ class ChunkExtractor {
383383
// Utilities
384384

385385
requireEntrypoint(entrypoint) {
386+
const entrypointPath = this.getEntrypointPath(this.entrypoint)
387+
388+
this.getAllScriptAssetsPaths()
389+
.forEach((assetPath) => {
390+
smartRequire(assetPath)
391+
})
392+
393+
return smartRequire(entrypointPath)
394+
}
395+
396+
getEntrypointPath(entrypoint) {
386397
entrypoint = entrypoint || this.entrypoints[0]
387398
const assets = this.getChunkAssets(entrypoint)
388399
const mainAsset = assets.find(asset => asset.scriptType === 'script')
389400
invariant(mainAsset, 'asset not found')
401+
return cleanFileName(mainAsset.path)
402+
}
390403

391-
this.stats.assets
404+
getAllScriptAssetsPaths() {
405+
return this.stats.assets
392406
.filter(({ name }) => isScriptFile(name))
393-
.forEach(({ name }) => {
394-
smartRequire(path.join(this.outputPath, cleanFileName(name)))
407+
.map(({ name }) => {
408+
return path.join(this.outputPath, cleanFileName(name))
395409
})
396-
397-
return smartRequire(cleanFileName(mainAsset.path))
398410
}
399411

400412
// Main assets

packages/server/src/util.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ export const joinURLPath = (publicPath, filename) => {
4141

4242
return `${publicPath}/${filename}`
4343
}
44+
45+
export const readJsonFileSync = (inputFileSystem, jsonFilePath) => {
46+
return JSON.parse(inputFileSystem.readFileSync(jsonFilePath))
47+
}

0 commit comments

Comments
 (0)