Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit c5bdd07

Browse files
committed
support actual modules
1 parent ad02e00 commit c5bdd07

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

core/common.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function createSymbol (name) {
1616
return hasSymbol ? Symbol() : '@@' + name;
1717
}
1818

19+
export var toStringTag = hasSymbol && Symbol.toStringTag;
20+
1921
export function pathToFileUrl (filePath) {
2022
return 'file://' + (isWindows ? '/' : '') + (isWindows ? filePath.replace(/\\/g, '/') : filePath);
2123
}

core/loader-polyfill.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addToError, createSymbol } from './common.js';
1+
import { addToError, createSymbol, toStringTag } from './common.js';
22

33
export { Loader, ModuleNamespace, REGISTRY }
44

@@ -49,7 +49,7 @@ function Loader () {
4949
Loader.prototype.constructor = Loader;
5050

5151
function ensureInstantiated (module) {
52-
if (!(module instanceof ModuleNamespace))
52+
if (!(module instanceof ModuleNamespace || module[toStringTag] === 'module'))
5353
throw new TypeError('Module instantiation did not return a valid namespace object.');
5454
return module;
5555
}
@@ -170,7 +170,7 @@ Registry.prototype.get = function (key) {
170170
};
171171
// 4.4.7
172172
Registry.prototype.set = function (key, namespace) {
173-
if (!(namespace instanceof ModuleNamespace))
173+
if (!(namespace instanceof ModuleNamespace || namespace[toStringTag] === 'module'))
174174
throw new Error('Registry must be set with an instance of Module Namespace');
175175
this[REGISTRY][key] = namespace;
176176
return this;
@@ -227,8 +227,8 @@ function ModuleNamespace (baseObject/*, evaluate*/) {
227227
// 8.4.2
228228
ModuleNamespace.prototype = Object.create(null);
229229

230-
if (typeof Symbol !== 'undefined' && Symbol.toStringTag)
231-
Object.defineProperty(ModuleNamespace.prototype, Symbol.toStringTag, {
230+
if (toStringTag)
231+
Object.defineProperty(ModuleNamespace.prototype, toStringTag, {
232232
value: 'Module'
233233
});
234234

core/register-loader.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Loader, ModuleNamespace, REGISTRY } from './loader-polyfill.js';
22
import { resolveIfNotPlain } from './resolve.js';
3-
import { addToError, global, createSymbol, baseURI } from './common.js';
3+
import { addToError, global, createSymbol, baseURI, toStringTag } from './common.js';
44

55
export default RegisterLoader;
66

@@ -204,7 +204,7 @@ function instantiate (loader, load, link, registry, state) {
204204
.then(function (instantiation) {
205205
// direct module return from instantiate -> we're done
206206
if (instantiation !== undefined) {
207-
if (!(instantiation instanceof ModuleNamespace))
207+
if (!(instantiation instanceof ModuleNamespace || instantiation[toStringTag] === 'module'))
208208
throw new TypeError('Instantiate did not return a valid Module object.');
209209

210210
delete state.records[load.key];
@@ -379,7 +379,7 @@ function instantiateDeps (loader, load, link, registry, state) {
379379
if (setter) {
380380
var instantiation = dependencyInstantiations[i];
381381

382-
if (instantiation instanceof ModuleNamespace) {
382+
if (instantiation instanceof ModuleNamespace || instantiation[toStringTag] === 'module') {
383383
setter(instantiation);
384384
}
385385
else {
@@ -428,7 +428,7 @@ function deepInstantiateDeps (loader, load, link, registry, state) {
428428
var depPromises = [];
429429
for (let i = 0; i < link.dependencies.length; i++) {
430430
var depLoad = link.dependencyInstantiations[i];
431-
if (!(depLoad instanceof ModuleNamespace))
431+
if (!(depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module'))
432432
depPromises.push(addDeps(depLoad, depLoad.linkRecord));
433433
}
434434
return Promise.all(depPromises);
@@ -524,7 +524,7 @@ function makeDynamicRequire (loader, key, dependencies, dependencyInstantiations
524524
var depLoad = dependencyInstantiations[i];
525525
var module;
526526

527-
if (depLoad instanceof ModuleNamespace)
527+
if (depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module')
528528
module = depLoad;
529529
else
530530
module = ensureEvaluate(loader, depLoad, depLoad.linkRecord, registry, state, seen);
@@ -550,7 +550,7 @@ function doEvaluate (loader, load, link, registry, state, seen) {
550550
for (var i = 0; i < link.dependencies.length; i++) {
551551
depLoad = link.dependencyInstantiations[i];
552552

553-
if (depLoad instanceof ModuleNamespace)
553+
if (depLoad instanceof ModuleNamespace || depLoad[toStringTag] === 'module')
554554
continue;
555555

556556
// custom Module returned from instantiate

0 commit comments

Comments
 (0)