This repository was archived by the owner on Sep 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 279
Asset list #15
Closed
Closed
Asset list #15
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,4 +1,5 @@ | ||
| var WebWorkerTemplatePlugin = require("webpack/lib/webworker/WebWorkerTemplatePlugin"); | ||
| var ConstDependency = require('webpack/lib/dependencies/ConstDependency'); | ||
| var SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin"); | ||
| var path = require("path"); | ||
|
|
||
|
|
@@ -38,16 +39,66 @@ module.exports.pitch = function(request) { | |
| compilation.cache = compilation.cache[subCache]; | ||
| } | ||
| }); | ||
|
|
||
| // If the user doesn't include the magic `__assets__` anywhere, there is no | ||
| // need to produce an asset file. | ||
| var includeAssets = false | ||
|
|
||
| // I don't know how to register this in webpack to get a hashed filename. | ||
| // This is my temporary hack to use while I try to find a good solution. | ||
| var BAD_HASH = Math.random().toString(36).substring(2, 10) | ||
| // Service workers won't update if the file hasn't changed, so it's | ||
| // important that __assets__ changes any time the list of assets has | ||
| // changed. Ideally it would not change if the list of assets hasn't | ||
| // changed. | ||
| var assetListFilename = "assets." + BAD_HASH + ".js" | ||
|
|
||
| workerCompiler.parser.plugin("expression __assets__" , function(expr) { | ||
| includeAssets = true; | ||
| // importScripts can only be used inside a worker and it's much like a `<script>` tag. | ||
| const dep = new ConstDependency("(importScripts(" + JSON.stringify(assetListFilename) + "), __asset_list)", expr.range); | ||
| dep.loc = expr.loc; | ||
| this.state.current.addDependency(dep); | ||
| return true | ||
| }) | ||
|
|
||
| // I know this is hacky, but I don't know how else to do this. In my (weak) | ||
| // defense... it is documented | ||
| // http://webpack.github.io/docs/loaders.html#_compiler | ||
| this._compiler.plugin('emit', function(compilation, callback) { | ||
|
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.
|
||
| if (includeAssets) { | ||
| var fileContent = "__asset_list = " + JSON.stringify( | ||
| Object.keys(compilation.assets).sort() | ||
| ) | ||
|
|
||
| compilation.assets[assetListFilename] = { | ||
| source: function() { | ||
| return fileContent; | ||
| }, | ||
| size: function() { | ||
| return fileContent.length; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| callback() | ||
| }); | ||
|
|
||
| workerCompiler.runAsChild(function(err, entries, compilation) { | ||
| if(err) return callback(err); | ||
| if (entries[0]) { | ||
| var workerFile = entries[0].files[0]; | ||
| var constructor = "new Worker(__webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; | ||
| if(query.inline) { | ||
| constructor = "require(" + JSON.stringify("!!" + path.join(__dirname, "createInlineWorker.js")) + ")(" + | ||
| var constructor | ||
| if(query.service) { | ||
| constructor = "navigator.serviceWorker.register(__webpack_public_path__ + " + JSON.stringify(workerFile) + ", options);" | ||
| } else { | ||
| constructor = "new Worker(__webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; | ||
| if(query.inline) { | ||
| constructor = "require(" + JSON.stringify("!!" + path.join(__dirname, "createInlineWorker.js")) + ")(" + | ||
| JSON.stringify(compilation.assets[workerFile].source()) + ", __webpack_public_path__ + " + JSON.stringify(workerFile) + ")"; | ||
| } | ||
| } | ||
| return callback(null, "module.exports = function() {\n\treturn " + constructor + ";\n};"); | ||
| return callback(null, "module.exports = function(options) {\n\treturn " + constructor + ";\n};"); | ||
| } else { | ||
| return callback(null, null); | ||
| } | ||
|
|
||
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.
This definitely has to be changed, but I don't know how to do this right.
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.
@sokra Do you know how this can be fixed? I'd really like to get this PR production ready and merged, but I'm lost.
It functions, it just uses two hacks that I think should be fixed before merging.
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.
loaderUtils.interpolateName(this, name, options)