diff --git a/package.json b/package.json index df9d4597d..193f1dacd 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "lerna": "^3.13.2", "prettier": "^1.16.4", "react": "^16.8.6", + "regenerator-runtime": "^0.13.2", "rollup": "^1.9.3", "rollup-plugin-babel": "^4.3.2", "rollup-plugin-commonjs": "^9.3.4", diff --git a/packages/server/src/ChunkExtractor.js b/packages/server/src/ChunkExtractor.js index 582930095..46b547720 100644 --- a/packages/server/src/ChunkExtractor.js +++ b/packages/server/src/ChunkExtractor.js @@ -51,9 +51,9 @@ function assetToScriptElement(asset, extraProps) { ) } -function assetToStyleString(asset) { +function assetToStyleString(asset, { inputFileSystem }) { return new Promise((resolve, reject) => { - fs.readFile(asset.path, 'utf8', (err, data) => { + inputFileSystem.readFile(asset.path, 'utf8', (err, data) => { if (err) { reject(err) return @@ -69,9 +69,9 @@ function assetToStyleTag(asset, extraProps) { }"${extraPropsToString(asset, extraProps)}>` } -function assetToStyleTagInline(asset, extraProps) { +function assetToStyleTagInline(asset, extraProps, { inputFileSystem }) { return new Promise((resolve, reject) => { - fs.readFile(asset.path, 'utf8', (err, data) => { + inputFileSystem.readFile(asset.path, 'utf8', (err, data) => { if (err) { reject(err) return @@ -100,9 +100,9 @@ function assetToStyleElement(asset, extraProps) { ) } -function assetToStyleElementInline(asset, extraProps) { +function assetToStyleElementInline(asset, extraProps, { inputFileSystem }) { return new Promise((resolve, reject) => { - fs.readFile(asset.path, 'utf8', (err, data) => { + inputFileSystem.readFile(asset.path, 'utf8', (err, data) => { if (err) { reject(err) return @@ -162,6 +162,7 @@ class ChunkExtractor { namespace = '', outputPath, publicPath, + inputFileSystem = fs, } = {}) { this.namespace = namespace this.stats = stats || smartRequire(statsFile) @@ -170,6 +171,7 @@ class ChunkExtractor { this.statsFile = statsFile this.entrypoints = Array.isArray(entrypoints) ? entrypoints : [entrypoints] this.chunks = [] + this.inputFileSystem = inputFileSystem } resolvePublicUrl(filename) { @@ -342,7 +344,7 @@ class ChunkExtractor { getCssString() { const mainAssets = this.getMainAssets('style') const promises = mainAssets.map(asset => - assetToStyleString(asset).then(data => data), + assetToStyleString(asset, this).then(data => data), ) return Promise.all(promises).then(results => joinTags(results)) } @@ -355,7 +357,7 @@ class ChunkExtractor { getInlineStyleTags(extraProps = {}) { const mainAssets = this.getMainAssets('style') const promises = mainAssets.map(asset => - assetToStyleTagInline(asset, extraProps).then(data => data), + assetToStyleTagInline(asset, extraProps, this).then(data => data), ) return Promise.all(promises).then(results => joinTags(results)) } @@ -368,7 +370,7 @@ class ChunkExtractor { getInlineStyleElements(extraProps = {}) { const mainAssets = this.getMainAssets('style') const promises = mainAssets.map(asset => - assetToStyleElementInline(asset, extraProps).then(data => data), + assetToStyleElementInline(asset, extraProps, this).then(data => data), ) return Promise.all(promises).then(results => results) } diff --git a/packages/server/src/ChunkExtractor.test.js b/packages/server/src/ChunkExtractor.test.js index 8953f79d6..331a76f04 100644 --- a/packages/server/src/ChunkExtractor.test.js +++ b/packages/server/src/ChunkExtractor.test.js @@ -1,3 +1,5 @@ +/* eslint-disable import/no-extraneous-dependencies */ +import 'regenerator-runtime/runtime' import path from 'path' import stats from '../__fixtures__/stats.json' import ChunkExtractor from './ChunkExtractor' @@ -63,9 +65,9 @@ describe('ChunkExtrator', () => { describe('#getScriptTags', () => { it('should return main script tag without chunk', () => { expect(extractor.getScriptTags()).toMatchInlineSnapshot(` -" -" -`) + " + " + `) }) it('should return main script tag without chunk with namespaced required chunks id', () => { @@ -75,37 +77,37 @@ describe('ChunkExtrator', () => { outputPath: path.resolve(__dirname, '../__fixtures__'), }) expect(extractor.getScriptTags()).toMatchInlineSnapshot(` -" -" -`) + " + " + `) }) it('should return other chunks if referenced', () => { extractor.addChunk('letters-A') expect(extractor.getScriptTags()).toMatchInlineSnapshot(` -" - -" -`) + " + + " + `) }) it('should allow for query params in chunk names', () => { extractor.addChunk('letters-E') expect(extractor.getScriptTags()).toMatchInlineSnapshot(` -" - -" -`) + " + + " + `) }) it('should add extra props if specified - object argument', () => { extractor.addChunk('letters-A') expect(extractor.getScriptTags({ nonce: 'testnonce' })) .toMatchInlineSnapshot(` -" - -" -`) + " + + " + `) }) it('should add extra props if specified - function argument', () => { @@ -115,10 +117,10 @@ describe('ChunkExtrator', () => { return { nonce: asset ? asset.chunk : 'anonymous' } }), ).toMatchInlineSnapshot(` -" - -" -`) + " + + " + `) }) }) @@ -130,129 +132,129 @@ describe('ChunkExtrator', () => { outputPath: path.resolve(__dirname, '../__fixtures__'), }) expect(extractor.getScriptElements()).toMatchInlineSnapshot(` -Array [ -