-
-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a56d904
commit fcd0375
Showing
10 changed files
with
238 additions
and
95 deletions.
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.