Skip to content

Commit 28bbf63

Browse files
authored
CJS: Fix shouldFetch false (#14)
1 parent 26b1f08 commit 28bbf63

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@
2525
"rollup": "^3.28.1",
2626
"rollup-plugin-esbuild": "^5.0.0",
2727
"serve": "^14.2.1"
28+
},
29+
"workspaces": {
30+
"packages": [
31+
"packages/*"
32+
]
2833
}
2934
}

packages/systemjs-cjs-extra/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# SystemJS CJS Extra
22

3-
This is mostly useful if attempting to migrate from SystemJS 0.2x.x to 6.x.x and you do not have control over the built modules that are imported in a project at runtime by SystemJS.
3+
This package adds the ability to load CJS code in the browser via SystemJS.
4+
5+
This is mostly useful if you want to migrate from SystemJS 0.2x.x to 6.x.x and you do not have control over the built modules that are imported in a project at runtime by SystemJS.
46

57
## Installation
68

packages/systemjs-cjs-extra/src/index.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ systemJSPrototype.shouldFetch = function (url) {
2323
systemJSPrototype.instantiate = function (url, parentUrl, meta) {
2424
const isJavascriptFile = JAVASCRIPT_REGEX.test(url);
2525

26+
if (!this.shouldFetch(url)) {
27+
return originalInstantiate.call(this, url, parentUrl, meta);
28+
}
29+
2630
if (isJavascriptFile) {
2731
return systemJSPrototype
2832
.fetch(url, {
@@ -39,8 +43,8 @@ systemJSPrototype.instantiate = function (url, parentUrl, meta) {
3943
res.statusText +
4044
", loading " +
4145
url +
42-
(parentUrl ? " from " + parentUrl : "")
43-
)
46+
(parentUrl ? " from " + parentUrl : ""),
47+
),
4448
);
4549
}
4650
const contentType = res.headers.get("content-type");
@@ -53,8 +57,8 @@ systemJSPrototype.instantiate = function (url, parentUrl, meta) {
5357
contentType +
5458
'", loading ' +
5559
url +
56-
(parentUrl ? " from " + parentUrl : "")
57-
)
60+
(parentUrl ? " from " + parentUrl : ""),
61+
),
5862
);
5963
}
6064

@@ -67,7 +71,7 @@ systemJSPrototype.instantiate = function (url, parentUrl, meta) {
6771

6872
// Import all dependencies into the SystemJS registry
6973
const depsPromises = dependencies.map((dep) =>
70-
global.System.import(dep, parentUrl ?? url)
74+
global.System.import(dep, parentUrl ?? url),
7175
);
7276

7377
// Make sure all deps are loaded before attempting to exec the module
@@ -99,18 +103,15 @@ systemJSPrototype.instantiate = function (url, parentUrl, meta) {
99103

100104
// Create a System.register format and pass the dependencies
101105
// and module exports.
102-
systemJSPrototype.register(
103-
dependencies,
104-
function (_export, _context) {
105-
return {
106-
setters: [],
107-
execute: function () {
108-
_export(module.exports);
109-
_export("default", module.exports);
110-
},
111-
};
112-
}
113-
);
106+
systemJSPrototype.register(dependencies, function (_export) {
107+
return {
108+
setters: [],
109+
execute: function () {
110+
_export(module.exports);
111+
_export("default", module.exports);
112+
},
113+
};
114+
});
114115

115116
// Return the new module
116117
return systemJSPrototype.getRegister(url);

0 commit comments

Comments
 (0)