Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/APIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ const REPLACEMENTS = {
type: "string",
assign: true
},
__webpack_cache_storage__: {
expr: RuntimeGlobals.cacheStorage,
req: [RuntimeGlobals.cacheStorage],
type: "string",
assign: true
},
__webpack_hash__: {
expr: `${RuntimeGlobals.getFullHash}()`,
req: [RuntimeGlobals.getFullHash],
Expand Down
26 changes: 19 additions & 7 deletions lib/ExternalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,25 @@ const getSourceForScriptExternal = (urlAndGlobal, runtimeTemplate) => {
url
)}, ${runtimeTemplate.basicFunction("event", [
`if(typeof ${globalName} !== "undefined") return resolve();`,
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"__webpack_error__.message = 'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';",
"__webpack_error__.name = 'ScriptExternalLoadError';",
"__webpack_error__.type = errorType;",
"__webpack_error__.request = realSrc;",
"reject(__webpack_error__);"
`mfCache.match('${url}')
.then((response) => {
var reader = response.body.getReader();

reader.read().then((data) => {
new Function(new TextDecoder().decode(data.value).replace('var remote', 'window.remote'))();
resolve();
});
})
.catch(() => {
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"__webpack_error__.message = 'Loading script failed.\\n(' + errorType + ': ' + realSrc + ')';",
"__webpack_error__.name = 'ScriptExternalLoadError';",
"__webpack_error__.type = errorType;",
"__webpack_error__.request = realSrc;",
"reject(__webpack_error__);"
});
`
])}, ${JSON.stringify(globalName)});`
]
)}).then(${runtimeTemplate.returningFunction(
Expand Down
5 changes: 5 additions & 0 deletions lib/RuntimeGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ exports.uncaughtErrorHandler = "__webpack_require__.oe";
*/
exports.scriptNonce = "__webpack_require__.nc";

/**
* property to store cache storage link
*/
exports.cacheStorage = "__webpack_require__.cs";

/**
* function to load a script tag.
* Arguments: (url: string, done: (event) => void), key?: string | number, chunkId?: string | number) => void
Expand Down
8 changes: 8 additions & 0 deletions lib/RuntimePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const JavascriptModulesPlugin = require("./javascript/JavascriptModulesPlugin");
const AsyncModuleRuntimeModule = require("./runtime/AsyncModuleRuntimeModule");
const AutoPublicPathRuntimeModule = require("./runtime/AutoPublicPathRuntimeModule");
const BaseUriRuntimeModule = require("./runtime/BaseUriRuntimeModule");
const CacheStorageRuntimeModule = require("./runtime/CacheStorageRuntimeModule");
const CompatGetDefaultExportRuntimeModule = require("./runtime/CompatGetDefaultExportRuntimeModule");
const CompatRuntimeModule = require("./runtime/CompatRuntimeModule");
const CreateFakeNamespaceObjectRuntimeModule = require("./runtime/CreateFakeNamespaceObjectRuntimeModule");
Expand Down Expand Up @@ -42,6 +43,7 @@ const StringXor = require("./util/StringXor");
const GLOBALS_ON_REQUIRE = [
RuntimeGlobals.chunkName,
RuntimeGlobals.runtimeId,
RuntimeGlobals.cacheStorage,
RuntimeGlobals.compatGetDefaultExport,
RuntimeGlobals.createFakeNamespaceObject,
RuntimeGlobals.createScript,
Expand Down Expand Up @@ -438,6 +440,12 @@ class RuntimePlugin {
compilation.addRuntimeModule(chunk, new NonceRuntimeModule());
return true;
});
compilation.hooks.runtimeRequirementInTree
.for(RuntimeGlobals.cacheStorage)
.tap("RuntimePlugin", chunk => {
compilation.addRuntimeModule(chunk, new CacheStorageRuntimeModule());
return true;
});
// TODO webpack 6: remove CompatRuntimeModule
compilation.hooks.additionalTreeRuntimeRequirements.tap(
"RuntimePlugin",
Expand Down
37 changes: 37 additions & 0 deletions lib/runtime/CacheStorageRuntimeModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/

"use strict";

const RuntimeModule = require("../RuntimeModule");

class CacheStorageRuntimeModule extends RuntimeModule {
constructor() {
super("cache storage", RuntimeModule.STAGE_ATTACH);
}

/**
* @returns {string} runtime code
*/
generate() {
return `
if ('caches' in window) {
caches.open('mf-cache').then((cache) => {

// TODO: get values from module arguments or global variables
cache.addAll([
'http://localhost:5002/remoteEntry.js',
'http://localhost:5002/vendors-node_modules_react_index_js.js',
'http://localhost:5002/vendors-node_modules_react-dom_index_js.js',
'http://localhost:5002/src_Button_tsx.js'
]);

// TODO: add mfCache to RuntimeGlobals.cacheStorage (__webpack_require__.cs)
mfCache = cache;
`;
}
}

module.exports = CacheStorageRuntimeModule;
29 changes: 22 additions & 7 deletions lib/web/JsonpChunkLoadingRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,28 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
"if(installedChunkData !== 0) installedChunks[chunkId] = undefined;",
"if(installedChunkData) {",
Template.indent([
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"error.message = 'Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';",
"error.name = 'ChunkLoadError';",
"error.type = errorType;",
"error.request = realSrc;",
"installedChunkData[1](error);"
`
mfCache.match(url)
.then((response) => {
var reader = response.body.getReader();

reader.read().then((data) => {
new Function(new TextDecoder().decode(data.value))();

installedChunkData[0]();
installedChunkData = 0;
});
})
.catch(() => {
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
"var realSrc = event && event.target && event.target.src;",
"error.message = 'Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')';",
"error.name = 'ChunkLoadError';",
"error.type = errorType;",
"error.request = realSrc;",
"installedChunkData[1](error);"
});
`
]),
"}"
]),
Expand Down