-
-
Notifications
You must be signed in to change notification settings - Fork 375
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
Adds brotli support for modern javascript #674
Merged
Merged
Changes from 34 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
0507885
WIP adding sw as a webpack dependency
prateekbh c5d5246
Merge branch 'babel-bug-fix' into sw-webpack
prateekbh 5a37b9f
adding sw-config
prateekbh 65a507c
Brotli support for modern javascript
prateekbh 02498fe
adding brotli plugin to webpack
prateekbh 375c545
fixing conditional plugins
prateekbh 9c34521
lint fixes
prateekbh e9e1cdc
fixing error
prateekbh 8b84b4d
bug-fix
prateekbh cc29725
WIP fix watch mode
prateekbh 1500698
Merge branch 'next' of https://github.com/developit/preact-cli into s…
prateekbh 86f0f78
fix watch mode
prateekbh 3d1ac3a
add the capability to build sw from user land
prateekbh 1369f2c
fixing comments
prateekbh 3135b3b
fixing spacing
prateekbh c1fd332
fixing in place mutation
prateekbh 8aec475
no multi compilers
prateekbh 8bc9521
Update run-webpack.js
prateekbh dcc8f41
removing unwanted files
prateekbh 434a813
Merge branch 'sw-webpack' of https://github.com/developit/preact-cli …
prateekbh 9ed7049
Update sw-plugin.js
prateekbh fb28138
Update sw-plugin.js
prateekbh 81bb193
Merge branch 'next' into sw-webpack
ForsakenHarmony dc64998
making changes for workbox v4
prateekbh 9a04494
fixes for regexp
prateekbh 95d3ccb
addressing comments
prateekbh 400fdb2
precacing only index.html
prateekbh 7f21e24
Merge branch 'next' into sw-webpack
prateekbh fdf81b4
Update run-webpack.js
prateekbh 37d9ec7
Update run-webpack.js
prateekbh 9054e9b
fixing kluer dep
prateekbh 126cd49
fixing package.json
prateekbh c95b6dc
fixing unhashed bundle bug
prateekbh 4302de4
no json parse
prateekbh 8935426
fixing comments
prateekbh b12603b
Merge branch 'master' of https://github.com/developit/preact-cli into…
prateekbh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
self.__precacheManifest = [].concat(self.__precacheManifest || []); | ||
|
||
/* global workbox */ | ||
/** We are sure brotli is enabled for browsers supporting script type=module | ||
* so we do brotli support only for them. | ||
* We can do brolti support for other browsers but there is no good way of | ||
* feature detect the same at the time of pre-caching. | ||
*/ | ||
if (process.env.ENABLE_BROTLI && process.env.ES_BUILD) { | ||
// Alter the precache manifest to precache brotli files instead of gzip files. | ||
self.__precacheManifest = self.__precacheManifest.map(asset => { | ||
if (/.*.js$/.test(asset.url)) { | ||
asset.url = asset.url.replace(/.esm.js$/, '.esm.js.br'); | ||
} | ||
return asset; | ||
}); | ||
|
||
class BrotliRedirectPlugin { | ||
// Before saving the response in cache, we need to treat the headers. | ||
async cacheWillUpdate({ response }) { | ||
const clonedResponse = response.clone(); | ||
if (/.js.br(\?.*)?$/.test(clonedResponse.url)) { | ||
const headers = new Headers(clonedResponse.headers); | ||
headers.set('content-type', 'application/javascript'); | ||
return new Response(await clonedResponse.text(), { headers }); | ||
} | ||
return response; | ||
} | ||
} | ||
workbox.precaching.addPlugins([new BrotliRedirectPlugin()]); | ||
} | ||
|
||
const precacheOptions = {}; | ||
if (process.env.ENABLE_BROTLI) { | ||
developit marked this conversation as resolved.
Show resolved
Hide resolved
|
||
precacheOptions['urlManipulation'] = ({ url }) => { | ||
if (/.esm.js$/.test(url.href)) { | ||
url.href = url.href + '.br'; | ||
} | ||
return [url]; | ||
}; | ||
} | ||
|
||
workbox.precaching.precacheAndRoute(self.__precacheManifest, precacheOptions); | ||
workbox.routing.registerNavigationRoute( | ||
workbox.precaching.getCacheKeyForURL('/index.html') | ||
); |
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,115 @@ | ||
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); | ||
const BabelEsmPlugin = require('babel-esm-plugin'); | ||
const { DefinePlugin } = require('webpack'); | ||
const fs = require('fs'); | ||
const { resolve } = require('path'); | ||
const { blue } = require('chalk'); | ||
|
||
class SWBuilderPlugin { | ||
constructor(config) { | ||
const { src, brotli, esm } = config; | ||
this.brotli_ = brotli; | ||
this.esm_ = esm; | ||
this.src_ = src; | ||
} | ||
apply(compiler) { | ||
let swSrc = resolve(__dirname, '../sw.js'); | ||
const exists = fs.existsSync(resolve(`${this.src_}/sw.js`)); | ||
if (exists) { | ||
if (exists) { | ||
console.log( | ||
blue( | ||
'⚛️ Detected custom sw.js: compiling instead of default Service Worker.' | ||
) | ||
); | ||
} else { | ||
console.log( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing here? |
||
blue('⚛️ No custom sw.js detected: compiling default Service Worker.') | ||
); | ||
} | ||
} | ||
compiler.hooks.make.tapAsync( | ||
this.constructor.name, | ||
(compilation, callback) => { | ||
const outputOptions = compiler.options; | ||
const plugins = [ | ||
new BabelEsmPlugin({ | ||
filename: '[name]-esm.js', | ||
excludedPlugins: ['BabelEsmPlugin', this.constructor.name], | ||
beforeStartExecution: plugins => { | ||
plugins.forEach(plugin => { | ||
if (plugin.constructor.name === 'DefinePlugin') { | ||
if (!plugin.definitions) | ||
throw Error( | ||
'ESM Error: DefinePlugin found without definitions.' | ||
); | ||
plugin.definitions['process.env.ES_BUILD'] = true; | ||
} | ||
}); | ||
}, | ||
}), | ||
new DefinePlugin({ | ||
'process.env.ENABLE_BROTLI': this.brotli_, | ||
'process.env.ES_BUILD': false, | ||
'process.env.NODE_ENV': 'production', | ||
}), | ||
]; | ||
|
||
/** | ||
* We are deliberatly not passing plugins in createChildCompiler. | ||
* All webpack does with plugins is to call `apply` method on them | ||
* with the childCompiler. | ||
* But by then we haven't given childCompiler a fileSystem or other options | ||
* which a few plugins might expect while execution the apply method. | ||
* We do call the `apply` method of all plugins by ourselves later in the code | ||
*/ | ||
const childCompiler = compilation.createChildCompiler( | ||
this.constructor.name | ||
); | ||
|
||
childCompiler.context = compiler.context; | ||
childCompiler.options = Object.assign({}, outputOptions); | ||
childCompiler.options.entry = { | ||
sw: swSrc, | ||
}; | ||
childCompiler.options.target = 'webworker'; | ||
childCompiler.options.output = Object.assign( | ||
{}, | ||
childCompiler.options.output, | ||
{ filename: '[name].js' } | ||
); | ||
childCompiler.options.output.filename = '[name].js'; | ||
|
||
// Call the `apply` method of all plugins by ourselves. | ||
if (Array.isArray(plugins)) { | ||
for (const plugin of plugins) { | ||
plugin.apply(childCompiler); | ||
} | ||
} | ||
|
||
childCompiler.apply( | ||
new SingleEntryPlugin(compiler.context, swSrc, 'sw') | ||
); | ||
|
||
compilation.hooks.additionalAssets.tapAsync( | ||
this.constructor.name, | ||
childProcessDone => { | ||
childCompiler.runAsChild((err, entries, childCompilation) => { | ||
if (!err) { | ||
compilation.assets = Object.assign( | ||
childCompilation.assets, | ||
compilation.assets | ||
); | ||
} | ||
err && compilation.errors.push(err); | ||
childProcessDone(); | ||
}); | ||
} | ||
); | ||
callback(); | ||
} | ||
); | ||
} | ||
} | ||
|
||
module.exports = SWBuilderPlugin; |
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.
guess this should use the util log fn