From c8210d60eb65b30bf0e7589ad5ae2ba8c468e690 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Thu, 23 Jul 2020 13:55:45 -0700 Subject: [PATCH] depcache implementation (#2134) --- src/common.js | 8 +++++++- .../{browser-scripts.js => import-maps.js} | 14 ++++++++++++-- src/s.js | 4 ++-- src/system-core.js | 2 +- src/system.js | 4 ++-- test/browser/core.js | 7 +++++++ test/fixtures/browser/depcache.js | 9 +++++++++ test/fixtures/browser/depcache1.js | 9 +++++++++ test/fixtures/browser/depcache10.js | 7 +++++++ test/fixtures/browser/depcache2.js | 9 +++++++++ test/fixtures/browser/depcache3.js | 9 +++++++++ test/fixtures/browser/depcache4.js | 9 +++++++++ test/fixtures/browser/depcache5.js | 9 +++++++++ test/fixtures/browser/depcache6.js | 9 +++++++++ test/fixtures/browser/depcache7.js | 9 +++++++++ test/fixtures/browser/depcache8.js | 9 +++++++++ test/fixtures/browser/depcache9.js | 9 +++++++++ test/fixtures/browser/importmap.json | 12 ++++++++++++ 18 files changed, 140 insertions(+), 8 deletions(-) rename src/features/{browser-scripts.js => import-maps.js} (82%) create mode 100644 test/fixtures/browser/depcache.js create mode 100644 test/fixtures/browser/depcache1.js create mode 100644 test/fixtures/browser/depcache10.js create mode 100644 test/fixtures/browser/depcache2.js create mode 100644 test/fixtures/browser/depcache3.js create mode 100644 test/fixtures/browser/depcache4.js create mode 100644 test/fixtures/browser/depcache5.js create mode 100644 test/fixtures/browser/depcache6.js create mode 100644 test/fixtures/browser/depcache7.js create mode 100644 test/fixtures/browser/depcache8.js create mode 100644 test/fixtures/browser/depcache9.js diff --git a/src/common.js b/src/common.js index 48a4b26e9..0226e4c89 100644 --- a/src/common.js +++ b/src/common.js @@ -152,7 +152,7 @@ function resolveAndComposePackages (packages, outPackages, baseUrl, parentMap, p } export function resolveAndComposeImportMap (json, baseUrl, parentMap) { - var outMap = { imports: objectAssign({}, parentMap.imports), scopes: objectAssign({}, parentMap.scopes) }; + var outMap = { imports: objectAssign({}, parentMap.imports), scopes: objectAssign({}, parentMap.scopes), depcache: objectAssign({}, parentMap.depcache) }; if (json.imports) resolveAndComposePackages(json.imports, outMap.imports, baseUrl, parentMap, null); @@ -163,6 +163,12 @@ export function resolveAndComposeImportMap (json, baseUrl, parentMap) { resolveAndComposePackages(json.scopes[s], outMap.scopes[resolvedScope] || (outMap.scopes[resolvedScope] = {}), baseUrl, parentMap, resolvedScope); } + if (json.depcache) + for (var d in json.depcache) { + var resolvedDepcache = resolveUrl(d, baseUrl); + outMap.depcache[resolvedDepcache] = json.depcache[d]; + } + return outMap; } diff --git a/src/features/browser-scripts.js b/src/features/import-maps.js similarity index 82% rename from src/features/browser-scripts.js rename to src/features/import-maps.js index ba9b702bb..74c272d77 100644 --- a/src/features/browser-scripts.js +++ b/src/features/import-maps.js @@ -2,10 +2,10 @@ * SystemJS browser attachments for script and import map processing */ import { baseUrl, resolveAndComposeImportMap, hasDocument, resolveUrl, IMPORT_MAP } from '../common.js'; -import { systemJSPrototype } from '../system-core.js'; +import { systemJSPrototype, getOrCreateLoad } from '../system-core.js'; import { errMsg } from '../err-msg.js'; -var importMapPromise = Promise.resolve({ imports: {}, scopes: {} }); +var importMapPromise = Promise.resolve({ imports: {}, scopes: {}, depcache: {} }); // Scripts are processed immediately, on the first System.import, and on DOMReady. // Import map scripts are processed only once (by being marked) and in order for each phase. @@ -26,6 +26,16 @@ if (hasDocument) { window.addEventListener('DOMContentLoaded', processScripts); } +const systemInstantiate = systemJSPrototype.instantiate; +systemJSPrototype.instantiate = function (url, firstParentUrl) { + var preloads = this[IMPORT_MAP].depcache[url]; + if (preloads) { + for (var i = 0; i < preloads.length; i++) + getOrCreateLoad(this, this.resolve(preloads[i], url), url); + } + return systemInstantiate.call(this, url, firstParentUrl); +}; + function processScripts () { [].forEach.call(document.querySelectorAll('script'), function (script) { if (script.sp) // sp marker = systemjs processed diff --git a/src/s.js b/src/s.js index f589c885a..e9f48c868 100644 --- a/src/s.js +++ b/src/s.js @@ -1,5 +1,5 @@ -import './features/resolve.js'; -import './features/browser-scripts.js'; import './features/script-load.js'; +import './features/resolve.js'; +import './features/import-maps.js'; import './features/auto-import.js'; import './features/worker-load.js'; diff --git a/src/system-core.js b/src/system-core.js index a3bc34d58..12dcb85ba 100644 --- a/src/system-core.js +++ b/src/system-core.js @@ -72,7 +72,7 @@ systemJSPrototype.getRegister = function () { return _lastRegister; }; -function getOrCreateLoad (loader, id, firstParentUrl) { +export function getOrCreateLoad (loader, id, firstParentUrl) { var load = loader[REGISTRY][id]; if (load) return load; diff --git a/src/system.js b/src/system.js index 57e37e7c4..fe79441c8 100644 --- a/src/system.js +++ b/src/system.js @@ -1,6 +1,6 @@ -import './features/resolve.js'; -import './features/browser-scripts.js'; import './features/script-load.js'; +import './features/resolve.js'; +import './features/import-maps.js'; import './features/auto-import.js'; import './features/worker-load.js'; import './extras/global.js'; diff --git a/test/browser/core.js b/test/browser/core.js index bbcc5e795..fab78763e 100644 --- a/test/browser/core.js +++ b/test/browser/core.js @@ -226,6 +226,13 @@ suite('SystemJS Standard Tests', function() { }); }); + test('should support depcache', function () { + return System.import('/test/fixtures/browser/depcache.js').then(function (m) { + assert.ok(m); + assert.equal(m.default, '10th module'); + }); + }); + test('should not get confused by filenames in url hash when resolving module type', function () { return System.import('fixturesbase/css-modules/hash.css?foo=bar.html').then(function (m) { assert.ok(m); diff --git a/test/fixtures/browser/depcache.js b/test/fixtures/browser/depcache.js new file mode 100644 index 000000000..cb34e26f2 --- /dev/null +++ b/test/fixtures/browser/depcache.js @@ -0,0 +1,9 @@ +System.register(['./depcache1.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache1.js b/test/fixtures/browser/depcache1.js new file mode 100644 index 000000000..0f604c2aa --- /dev/null +++ b/test/fixtures/browser/depcache1.js @@ -0,0 +1,9 @@ +System.register(['./depcache2.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache10.js b/test/fixtures/browser/depcache10.js new file mode 100644 index 000000000..5c547a371 --- /dev/null +++ b/test/fixtures/browser/depcache10.js @@ -0,0 +1,7 @@ +System.register([], function(_export) { + return { + execute: function () { + _export('default', '10th module'); + } + }; +}); diff --git a/test/fixtures/browser/depcache2.js b/test/fixtures/browser/depcache2.js new file mode 100644 index 000000000..4ef9e3e42 --- /dev/null +++ b/test/fixtures/browser/depcache2.js @@ -0,0 +1,9 @@ +System.register(['./depcache3.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache3.js b/test/fixtures/browser/depcache3.js new file mode 100644 index 000000000..2d77e0648 --- /dev/null +++ b/test/fixtures/browser/depcache3.js @@ -0,0 +1,9 @@ +System.register(['./depcache4.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache4.js b/test/fixtures/browser/depcache4.js new file mode 100644 index 000000000..6fb84552d --- /dev/null +++ b/test/fixtures/browser/depcache4.js @@ -0,0 +1,9 @@ +System.register(['./depcache5.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache5.js b/test/fixtures/browser/depcache5.js new file mode 100644 index 000000000..139d0e756 --- /dev/null +++ b/test/fixtures/browser/depcache5.js @@ -0,0 +1,9 @@ +System.register(['./depcache6.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache6.js b/test/fixtures/browser/depcache6.js new file mode 100644 index 000000000..d7e9adb39 --- /dev/null +++ b/test/fixtures/browser/depcache6.js @@ -0,0 +1,9 @@ +System.register(['./depcache7.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache7.js b/test/fixtures/browser/depcache7.js new file mode 100644 index 000000000..7057aceb0 --- /dev/null +++ b/test/fixtures/browser/depcache7.js @@ -0,0 +1,9 @@ +System.register(['./depcache8.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache8.js b/test/fixtures/browser/depcache8.js new file mode 100644 index 000000000..3681f3e6d --- /dev/null +++ b/test/fixtures/browser/depcache8.js @@ -0,0 +1,9 @@ +System.register(['./depcache9.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/depcache9.js b/test/fixtures/browser/depcache9.js new file mode 100644 index 000000000..522a1f8f5 --- /dev/null +++ b/test/fixtures/browser/depcache9.js @@ -0,0 +1,9 @@ +System.register(['./depcache10.js'], function(_export) { + var dep; + return { + setters: [function (_dep) { dep = _dep.default }], + execute: function () { + _export('default', dep); + } + }; +}); diff --git a/test/fixtures/browser/importmap.json b/test/fixtures/browser/importmap.json index 420a9e32c..63441c287 100644 --- a/test/fixtures/browser/importmap.json +++ b/test/fixtures/browser/importmap.json @@ -19,5 +19,17 @@ "wasm/": { "example": "./wasm/example.js" } + }, + "depcache": { + "depcache.js": ["./depcache1.js"], + "depcache1.js": ["./depcache2.js"], + "depcache2.js": ["./depcache3.js"], + "depcache3.js": ["./depcache4.js"], + "depcache4.js": ["./depcache5.js"], + "depcache5.js": ["./depcache6.js"], + "depcache6.js": ["./depcache7.js"], + "depcache7.js": ["./depcache8.js"], + "depcache8.js": ["./depcache9.js"], + "depcache9.js": ["./depcache10.js"] } } \ No newline at end of file