Skip to content

Commit

Permalink
import.meta.resolve implementation (#2230)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Aug 10, 2020
1 parent 5547865 commit a0dd823
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/system-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ systemJSPrototype.import = function (id, parentUrl) {

// Hookable createContext function -> allowing eg custom import meta
systemJSPrototype.createContext = function (parentId) {
var loader = this;
return {
url: parentId
url: parentId,
resolve: function (id, parentUrl) {
return Promise.resolve(loader.resolve(id, parentUrl || parentId));
}
};
};

Expand Down
47 changes: 47 additions & 0 deletions test/browser/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,51 @@ suite('SystemJS Standard Tests', function() {
});
});

test('import.meta.resolve package maps', function () {
return System.import('fixtures/resolve.js').then(function (m) {
return m.resolve('a')
})
.then(function (resolved) {
assert.equal(resolved, rootURL + 'b');
});
});

test('import.meta.resolve package maps paths', function () {
return System.import('fixtures/resolve.js').then(function (m) {
return m.resolve('a/')
})
.then(function (resolved) {
assert.equal(resolved, baseURL + 'fixtures/browser/a/');
});
});

test('Contextual package maps', function () {
return System.import('fixtures/scope-test/index.js')
.then(function (m) {
assert.equal(m.mapdep, 'mapdep');
});
});

test('import.meta.resolve contextual package maps', function () {
return System.import('fixtures/resolve.js').then(function (m) {
return m.resolve('maptest', baseURL + 'fixtures/browser/scope-test/index.js')
})
.then(function (resolved) {
assert.equal(resolved, baseURL + 'fixtures/browser/contextual-map-dep.js');
});
});

test('import.meta.resolve contextual package maps fail', function () {
return System.import('fixtures/resolve.js').then(function (m) {
return m.resolve('maptest')
})
.then(function (resolved) {
assert.ok(false);
}, function (error) {
assert.equal(error.message.indexOf('Unable to resolve'), 0);
});
});

test('Loading named System.register fails', function () {
return System.import('fixtures/named-register.js')
.then(function () {
Expand Down Expand Up @@ -219,6 +257,15 @@ suite('SystemJS Standard Tests', function() {
assert.equal(System.get(resolved).auto, 'import');
});

test('import.meta.resolve', function () {
return System.import('fixtures/resolve.js').then(function (m) {
return m.resolve('./test.js')
})
.then(function (resolved) {
assert.equal(resolved, baseURL + 'fixtures/browser/test.js');
});
});

test('non-enumerable __esModule property export (issue 2090)', function () {
return System.import('fixtures/__esModule.js').then(function (m) {
// Even though __esModule is not enumerable on the exported object, it should be preserved on the systemjs namespace
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/browser/resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
System.register([], function (_export, _context) {
return {
setters: [],
execute: function () {
_export({
url: _context.meta.url,
resolve: _context.meta.resolve
});
}
};
});

0 comments on commit a0dd823

Please sign in to comment.