Skip to content

Commit

Permalink
fix(dynamic-import-vars): Give default promise rejection timing parit…
Browse files Browse the repository at this point in the history
…y with cases (#825)

* Allow the default clause to be disabled; When enabled, give timing parity with cases

* Revert defaultClause option

* Avoid arrow functions
  • Loading branch information
bearfriend authored Mar 27, 2021
1 parent e85d786 commit c328e2c
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 89 deletions.
6 changes: 5 additions & 1 deletion packages/dynamic-import-vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ function __variableDynamicImportRuntime__(path) {
case './locales/nl-NL.js':
return import('./locales/nl-NL.js');
default:
throw new Error('Unknown variable dynamic import: ' + path);
return new Promise(function(resolve, reject) {
queueMicrotask(
reject.bind(null, new Error("Unknown variable dynamic import: " + path))
);
});
}
}

Expand Down
11 changes: 7 additions & 4 deletions packages/dynamic-import-vars/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) {
// will turn these into chunks automatically
ms.prepend(
`function __variableDynamicImportRuntime${dynamicImportIndex}__(path) {
switch (path) {
${paths.map((p) => ` case '${p}': return import('${p}');`).join('\n ')}
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));
}
switch (path) {
${paths.map((p) => ` case '${p}': return import('${p}');`).join('\n')}
${` default: return new Promise(function(resolve, reject) {
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(
reject.bind(null, new Error("Unknown variable dynamic import: " + path))
);
})\n`} }
}\n\n`
);
// call the runtime function instead of doing a dynamic import, the import specifier will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,123 @@

The actual snapshot is saved in `rollup-plugin-dynamic-import-vars.test.js.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## can exclude files
## single dir

> Snapshot 1
`function importModule(name) {␊
return import(`./module-dir-a/${name}.js`);␊
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`./module-dir-a/${name}.js`);␊
}␊
␊
export { importModule };␊
`

## complex concatenation
## multiple dirs

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-35191ee0.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function importModule(dirName, name) {␊
return __variableDynamicImportRuntime0__(`${`./${`module-dir-${dirName}`}` + '/'}${name}.js`);␊
function importModule(dir, name) {␊
return __variableDynamicImportRuntime0__(`./${dir}/${name}.js`);␊
}␊
␊
export { importModule };␊
`

## doesn't change imports that should not be changed
## upwards dir path

> Snapshot 1
`console.log('a-1');␊
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case '../module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case '../module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function importModuleA() {␊
return import('./module-a-2-28fb3b62.js');␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`../module-dir-a/${name}.js`);␊
}␊
␊
function importModuleB() {␊
return import('./' + 'module-dir-a' + '/' + 'module-a-2' + '.js');␊
export { importModule };␊
`

## complex concatenation

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function importModule(dirName, name) {␊
return __variableDynamicImportRuntime0__(`${`./${`module-dir-${dirName}`}` + '/'}${name}.js`);␊
}␊
␊
export { importModuleA, importModuleB };␊
export { importModule };␊
`

## multiple dirs
## own directory

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-39977978.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
switch (path) {␊
case './root-module-a.js': return import('./root-module-a-53575bcc.js');␊
case './root-module-b.js': return import('./root-module-b-55333443.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function importModule(dir, name) {␊
return __variableDynamicImportRuntime0__(`./${dir}/${name}.js`);␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`./root-module-${name}.js`);␊
}␊
␊
export { importModule };␊
Expand All @@ -80,29 +129,41 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
`function __variableDynamicImportRuntime2__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-39977978.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
case './module-dir-b/module-b-1.js': return import('./module-b-1-a616b1e9.js');␊
case './module-dir-b/module-b-2.js': return import('./module-b-2-219276fa.js');␊
case './sub-dir/fixture-upwards-path.js': return import('./fixture-upwards-path-35191ee0.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function __variableDynamicImportRuntime1__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return new Promise(function(resolve, reject) {␊
(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊
reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊
);␊
})␊
}␊
}␊
␊
Expand All @@ -123,58 +184,29 @@ Generated by [AVA](https://ava.li).
export { Foo, importModuleFromDir };␊
`

## own directory
## doesn't change imports that should not be changed

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './root-module-a.js': return import('./root-module-a-53575bcc.js');␊
case './root-module-b.js': return import('./root-module-b-55333443.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
}␊
}␊
`console.log('a-1');␊
␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`./root-module-${name}.js`);␊
function importModuleA() {␊
return import('./module-a-2-28fb3b62.js');␊
}␊
␊
export { importModule };␊
`

## single dir

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case './module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case './module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
}␊
}␊
␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`./module-dir-a/${name}.js`);␊
function importModuleB() {␊
return import('./' + 'module-dir-a' + '/' + 'module-a-2' + '.js');␊
}␊
␊
export { importModule };␊
export { importModuleA, importModuleB };␊
`

## upwards dir path
## can exclude files

> Snapshot 1
`function __variableDynamicImportRuntime0__(path) {␊
switch (path) {␊
case '../module-dir-a/module-a-1.js': return import('./module-a-1-a1a9dfd0.js');␊
case '../module-dir-a/module-a-2.js': return import('./module-a-2-28fb3b62.js');␊
default: return Promise.reject(new Error("Unknown variable dynamic import: " + path));␊
}␊
}␊
␊
function importModule(name) {␊
return __variableDynamicImportRuntime0__(`../module-dir-a/${name}.js`);␊
`function importModule(name) {␊
return import(`./module-dir-a/${name}.js`);␊
}␊
␊
export { importModule };␊
Expand Down
Binary file not shown.

0 comments on commit c328e2c

Please sign in to comment.