New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: push manifest #1680
Merged
Merged
fix: push manifest #1680
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
190e291
refactor: Move away from hard-coded filename expectations for push-ma…
rschristian 22a52fd
test: Add tests for updated push-manifest building
rschristian d98b87f
docs: Adding changeset
rschristian 86338c6
Merge branch 'master' into fix/asset-manifest
ForsakenHarmony File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'preact-cli': patch | ||
--- | ||
|
||
Fixed bug in push-manifest that would result in undefined entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,67 @@ | ||
const isESM = filename => /\.esm\.js$/.test(filename); | ||
const isMatch = (filename, condition) => isESM(filename) === condition; | ||
module.exports = (assets, namedChunkGroups) => { | ||
/** | ||
* This is a mapping of generic/pre-build filenames to their postbuild output | ||
* | ||
* bundle.js -> bundle.29bec.esm.js | ||
* route-home.css -> styles/route-home.chunk.8aeee.css | ||
* | ||
* Even if a user alters the output name, we still have keys we can expect & rely on | ||
*/ | ||
assets = JSON.parse(assets['asset-manifest.json']._value); | ||
|
||
module.exports = (assets, isESMBuild = false, namedChunkGroups) => { | ||
let mainJs, | ||
mainCss, | ||
scripts = [], | ||
styles = []; | ||
for (let filename in assets) { | ||
if (!/\.map$/.test(filename)) { | ||
if (/^route-.*\.js$/.test(filename)) { | ||
// both ESM & regular match here | ||
isMatch(filename, isESMBuild) && scripts.push(filename); | ||
} else if (/chunk\.(.+)\.css$/.test(filename)) { | ||
styles.push(filename); | ||
} else if (/^bundle(.+)\.css$/.test(filename)) { | ||
mainCss = filename; | ||
} else if (/^bundle(.+)\.js$/.test(filename)) { | ||
// both ESM & regular bundles match here | ||
if (isMatch(filename, isESMBuild)) { | ||
mainJs = filename; | ||
} | ||
} | ||
} | ||
} | ||
const mainJs = assets['bundle.js']; | ||
const mainCss = assets['bundle.css']; | ||
|
||
let defaults = { | ||
[mainCss]: { | ||
type: 'style', | ||
weight: 1, | ||
}, | ||
[mainJs]: { | ||
type: 'script', | ||
weight: 1, | ||
}, | ||
const defaults = { | ||
...(mainCss && { | ||
[mainCss]: { | ||
type: 'style', | ||
weight: 1, | ||
}, | ||
}), | ||
...(mainJs && { | ||
[mainJs]: { | ||
type: 'script', | ||
weight: 1, | ||
}, | ||
}), | ||
}, | ||
manifest = { | ||
'/': defaults, | ||
}; | ||
|
||
let path, css, obj; | ||
scripts.forEach(filename => { | ||
css = styles.find(asset => asset.startsWith(filename.replace(/\..*/, ''))); | ||
obj = Object.assign({}, defaults); | ||
obj[filename] = { type: 'script', weight: 0.9 }; | ||
if (css) obj[css] = { type: 'style', weight: 0.9 }; | ||
path = filename | ||
.replace(/route-/, '/') | ||
.replace(/\.chunk(\.\w+)?(\.esm)?\.js$/, '') | ||
.replace(/\/home/, '/'); | ||
if (namedChunkGroups) { | ||
// async files to be loaded, generated by splitChunksPlugin | ||
const asyncFiles = | ||
namedChunkGroups.get( | ||
filename.replace(/\.chunk(\.\w+)?(\.esm)?\.js$/, '') | ||
) || {}; | ||
if (asyncFiles && asyncFiles.chunks) { | ||
asyncFiles.chunks.forEach(asset => { | ||
asset.files = asset.files || []; | ||
asset.files.forEach(file => { | ||
if (/\.css$/.test(file)) { | ||
obj[file] = { type: 'style', weight: 0.9 }; | ||
} else if (/\.js$/.test(file)) { | ||
obj[file] = { type: 'script', weight: 0.9 }; | ||
} | ||
Object.keys(assets) | ||
.filter(asset => /^route-.*\.js$/.test(asset)) | ||
.map(asset => asset.replace(/\.js$/, '')) | ||
.forEach(route => { | ||
const routeManifest = Object.assign({}, defaults); | ||
|
||
const routeCss = assets[`${route}.css`]; | ||
const routeJs = assets[`${route}.js`]; | ||
|
||
routeManifest[routeJs] = { type: 'script', weight: 0.9 }; | ||
if (routeCss) routeManifest[routeCss] = { type: 'script', weight: 0.9 }; | ||
|
||
const path = route.replace(/^route-/, '/').replace(/^\/home/, '/'); | ||
|
||
if (namedChunkGroups) { | ||
// async files to be loaded, generated by splitChunksPlugin | ||
const asyncFiles = namedChunkGroups.get(route) || {}; | ||
if (asyncFiles && asyncFiles.chunks) { | ||
asyncFiles.chunks.forEach(asset => { | ||
asset.files = asset.files || []; | ||
asset.files.forEach(file => { | ||
if (/\.css$/.test(file)) { | ||
routeManifest[file] = { type: 'style', weight: 0.9 }; | ||
} else if (/\.js$/.test(file)) { | ||
routeManifest[file] = { type: 'script', weight: 0.9 }; | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
manifest[path] = obj; | ||
}); | ||
manifest[path] = routeManifest; | ||
}); | ||
|
||
return manifest; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
const webpack = require('webpack'); | ||
const createLoadManifest = require('./create-load-manifest'); | ||
|
||
module.exports = class PushManifestPlugin { | ||
constructor(env = {}) { | ||
this.isESMBuild_ = env.esm; | ||
} | ||
apply(compiler) { | ||
compiler.hooks.emit.tap('PushManifestPlugin', compilation => { | ||
const manifest = createLoadManifest( | ||
compilation.assets, | ||
this.isESMBuild_, | ||
compilation.namedChunkGroups | ||
); | ||
compiler.hooks.emit.tap( | ||
{ | ||
name: 'PushManifestPlugin', | ||
stage: webpack.Compiler.PROCESS_ASSETS_STAGE_REPORT, | ||
}, | ||
compilation => { | ||
const manifest = createLoadManifest( | ||
compilation.assets, | ||
compilation.namedChunkGroups | ||
); | ||
|
||
let output = JSON.stringify(manifest); | ||
compilation.assets['push-manifest.json'] = { | ||
source() { | ||
return output; | ||
}, | ||
size() { | ||
return output.length; | ||
}, | ||
}; | ||
let output = JSON.stringify(manifest); | ||
compilation.assets['push-manifest.json'] = { | ||
source() { | ||
return output; | ||
}, | ||
size() { | ||
return output.length; | ||
}, | ||
}; | ||
|
||
return compilation; | ||
return compilation; | ||
|
||
// callback(); | ||
}); | ||
// callback(); | ||
} | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case this raises an eyebrow, the asset manifest will always map to ESM output if enabled, CJS if it's not. We don't need to deal with that ourselves anymore.
There's a new test to ensure the
push-manifest.json
is correct and referring to ESM scripts when ESM is enabled.