Skip to content

Commit

Permalink
Added getEntrypointPath to ChunkExtractor and reading json from fs (#951
Browse files Browse the repository at this point in the history
)

* Added getEntrypointPath to ChunkExtractor and reading json from inputFilesystem instead of require

* Adds getAllScriptAssetsPaths to ChunkExtractor
  • Loading branch information
fivethreeo committed Feb 3, 2023
1 parent 82c7019 commit 2d1645d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/server/src/ChunkExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import flatMap from 'lodash/flatMap'
import React from 'react'
import { invariant, getRequiredChunkKey } from './sharedInternals'
import ChunkExtractorManager from './ChunkExtractorManager'
import { smartRequire, joinURLPath } from './util'
import { smartRequire, joinURLPath, readJsonFileSync } from './util'

const EXTENSION_SCRIPT_TYPES = {
'.js': 'script',
Expand Down Expand Up @@ -205,7 +205,7 @@ class ChunkExtractor {
inputFileSystem = fs,
} = {}) {
this.namespace = namespace
this.stats = stats || smartRequire(statsFile)
this.stats = stats || readJsonFileSync(inputFileSystem, statsFile)
this.publicPath = publicPath || this.stats.publicPath
this.outputPath = outputPath || this.stats.outputPath
this.statsFile = statsFile
Expand Down Expand Up @@ -383,18 +383,30 @@ class ChunkExtractor {
// Utilities

requireEntrypoint(entrypoint) {
const entrypointPath = this.getEntrypointPath(this.entrypoint)

this.getAllScriptAssetsPaths()
.forEach((assetPath) => {
smartRequire(assetPath)
})

return smartRequire(entrypointPath)
}

getEntrypointPath(entrypoint) {
entrypoint = entrypoint || this.entrypoints[0]
const assets = this.getChunkAssets(entrypoint)
const mainAsset = assets.find(asset => asset.scriptType === 'script')
invariant(mainAsset, 'asset not found')
return cleanFileName(mainAsset.path)
}

this.stats.assets
getAllScriptAssetsPaths() {
return this.stats.assets
.filter(({ name }) => isScriptFile(name))
.forEach(({ name }) => {
smartRequire(path.join(this.outputPath, cleanFileName(name)))
.map(({ name }) => {
return path.join(this.outputPath, cleanFileName(name))
})

return smartRequire(cleanFileName(mainAsset.path))
}

// Main assets
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ export const joinURLPath = (publicPath, filename) => {

return `${publicPath}/${filename}`
}

export const readJsonFileSync = (inputFileSystem, jsonFilePath) => {
return JSON.parse(inputFileSystem.readFileSync(jsonFilePath))
}

0 comments on commit 2d1645d

Please sign in to comment.