-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
685e77f
commit 42308a1
Showing
1 changed file
with
37 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,70 @@ | ||
const externalModule = 'fixtures/esm.js'; | ||
const IMPORT_MAP_TYPE = 'systemjs-importmap'; | ||
|
||
const appendImportMap = ({ moduleId, src }) => { | ||
const appendImportMap = function (moduleId, src) { | ||
const script = document.createElement('script'); | ||
script.type = IMPORT_MAP_TYPE; | ||
script.type = 'systemjs-importmap'; | ||
|
||
if (src) { | ||
script.async = true; // avoid theoretical timing issues | ||
script.src = src; | ||
} | ||
|
||
if (moduleId) { | ||
script.textContent = JSON.stringify({ imports: { [moduleId]: externalModule } }); | ||
var map = { imports: {} }; | ||
map.imports[moduleId] = 'fixtures/esm.js'; | ||
script.textContent = JSON.stringify(map); | ||
} | ||
|
||
container.append(script); | ||
container.appendChild(script); | ||
return script; | ||
}; | ||
|
||
const editImportMap = ({ needleModuleId, needleSrc, newModuleId, newSrc }) => { | ||
const importMapType = `script[type="${IMPORT_MAP_TYPE}"]`; | ||
if (needleModuleId && newModuleId) { | ||
[...document.querySelectorAll(importMapType)] | ||
.filter(element => element.textContent.includes(needleModuleId)) | ||
.forEach(element => element.textContent = element.textContent.replace(needleModuleId, newModuleId)); | ||
} else if (needleSrc && newSrc) { | ||
document.querySelector(`${importMapType}[src="${needleSrc}"]`).src = newSrc; | ||
} else { | ||
throw new Error('Incorrect configuration'); | ||
} | ||
const importAfterImportMap = function (id) { | ||
return new Promise(function (resolve) { setTimeout(resolve, 10); }).then(function () { | ||
return System.import(id); | ||
}); | ||
}; | ||
|
||
const importAfterImportMap = id => new Promise(resolve => setTimeout(resolve, 10)).then(() => { | ||
return System.import(id); | ||
}); | ||
|
||
const importNonExistent = id => System.import(id) | ||
.catch(error => error) | ||
.then(result => { | ||
const importNonExistent = function (id) { | ||
return System.import(id) | ||
.catch(function (err) { return err }) | ||
.then(function (result) { | ||
if (!(result instanceof Error)) { | ||
throw new Error(`Unexpected mapping found for module "${id}"`); | ||
throw new Error('Unexpected mapping found for module "' + id + '"'); | ||
} | ||
}); | ||
} | ||
|
||
let container; | ||
|
||
suite('Dynamic import maps', () => { | ||
suiteSetup(() => System.import('../../dist/extras/dynamic-import-maps.js')); | ||
suite('Dynamic import maps', function () { | ||
suiteSetup(function () { | ||
return System.import('../../dist/extras/dynamic-import-maps.js') | ||
}); | ||
|
||
setup(() => { | ||
setup(function () { | ||
container = document.createElement('div'); | ||
container.id = 'importmap-container'; | ||
document.body.append(container); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
teardown(() => container.remove()); | ||
teardown(function () { | ||
container.parentNode.removeChild(container); | ||
}); | ||
|
||
test('Loading newly added elements (inline)', async () => { | ||
test('Loading newly added elements (inline)', function () { | ||
const moduleId = 'dynamic-inline-map-1'; | ||
await importNonExistent(moduleId); | ||
appendImportMap({ moduleId }); | ||
return importAfterImportMap(moduleId); | ||
return importNonExistent(moduleId) | ||
.then(function () { | ||
appendImportMap(moduleId); | ||
return importAfterImportMap(moduleId); | ||
}); | ||
}); | ||
|
||
test('Loading newly added elements (external)', async () => { | ||
test('Loading newly added elements (external)', function () { | ||
const moduleId = 'dynamic-external-map-1'; | ||
await importNonExistent(moduleId); | ||
appendImportMap({ src: 'fixtures/browser/dynamic-importmap1.json' }); | ||
return importAfterImportMap(moduleId); | ||
return importNonExistent(moduleId) | ||
.then(function () { | ||
appendImportMap(null, 'fixtures/browser/dynamic-importmap1.json'); | ||
return importAfterImportMap(moduleId); | ||
}); | ||
}); | ||
}); |