Skip to content

Commit

Permalink
Keep assertions on fully dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 3, 2022
1 parent e1786e5 commit c9dc363
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/Chunk.ts
Expand Up @@ -857,12 +857,12 @@ export default class Chunk {
private getDynamicImportStringAndAssertions(
resolution: ExternalModule | string | null,
fileName: string
): [importPath: string, assertions: string | null] {
): [importPath: string, assertions: string | null | true] {
if (resolution instanceof ExternalModule) {
const chunk = this.externalChunkByModule.get(resolution)!;
return [`'${chunk.getImportPath(fileName)}'`, chunk.getImportAssertions(this.snippets)];
}
return [resolution || '', null];
return [resolution || '', this.outputOptions.format === 'es' || null];
}

private getFallbackChunkName(): string {
Expand Down
37 changes: 18 additions & 19 deletions src/ast/nodes/ImportExpression.ts
Expand Up @@ -29,7 +29,7 @@ export default class ImportExpression extends NodeBase {
declare source: ExpressionNode;
declare type: NodeType.tImportExpression;

private assertions: string | null = null;
private assertions: string | null | true = null;
private mechanism: DynamicImportMechanism | null = null;
private namespaceExportName: string | false | undefined = undefined;
private resolution: Module | ExternalModule | string | null = null;
Expand Down Expand Up @@ -70,34 +70,20 @@ export default class ImportExpression extends NodeBase {
code.overwrite(
this.start,
this.end,
`Promise.resolve().then(${left}${this.inlineNamespace.getName(getPropertyAccess)}${right})`,
{ contentOnly: true }
`Promise.resolve().then(${left}${this.inlineNamespace.getName(getPropertyAccess)}${right})`
);
return;
}

if (this.arguments) {
code.remove(this.source.end, this.end - 1);
}
if (this.mechanism) {
code.overwrite(
this.start,
findFirstOccurrenceOutsideComment(code.original, '(', this.start + 6) + 1,
this.mechanism.left,
{ contentOnly: true }
this.mechanism.left
);
code.overwrite(this.end - 1, this.end, this.mechanism.right, { contentOnly: true });
code.overwrite(this.end - 1, this.end, this.mechanism.right);
}
if (this.resolutionString) {
code.overwrite(this.source.start, this.source.end, this.resolutionString);
if (this.assertions) {
code.appendLeft(
this.end - 1,
`,${_}${getObject([['assert', this.assertions]], {
lineBreakIndent: null
})}`
);
}
if (this.namespaceExportName) {
const [left, right] = getDirectReturnFunction(['n'], {
functionReturn: true,
Expand All @@ -109,6 +95,19 @@ export default class ImportExpression extends NodeBase {
} else {
this.source.render(code, options);
}
if (this.assertions !== true) {
if (this.arguments) {
code.overwrite(this.source.end, this.end - 1, '', { contentOnly: true });
}
if (this.assertions) {
code.appendLeft(
this.end - 1,
`,${_}${getObject([['assert', this.assertions]], {
lineBreakIndent: null
})}`
);
}
}
}

setExternalResolution(
Expand All @@ -120,7 +119,7 @@ export default class ImportExpression extends NodeBase {
accessedGlobalsByScope: Map<ChildScope, Set<string>>,
resolutionString: string,
namespaceExportName: string | false | undefined,
assertions: string | null
assertions: string | null | true
): void {
const { format } = options;
this.inlineNamespace = null;
Expand Down
@@ -0,0 +1,6 @@
module.exports = {
description: 'keep import assertions for non-resolvable dynamic imports',
options: {
external: true
}
};
@@ -0,0 +1,22 @@
define(['require'], (function (require) { 'use strict';

function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}

(function (t) { return new Promise(function (resolve, reject) { require([t], function (m) { resolve(/*#__PURE__*/_interopNamespaceDefault(m)); }, reject); }); })(globalThis.unknown);

}));
@@ -0,0 +1,20 @@
'use strict';

function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}

(function (t) { return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefault(require(t)); }); })(globalThis.unknown);
@@ -0,0 +1 @@
import(globalThis.unknown, { assert: { type: 'special' } });
@@ -0,0 +1,6 @@
(function () {
'use strict';

import(globalThis.unknown);

})();
@@ -0,0 +1,10 @@
System.register([], (function (exports, module) {
'use strict';
return {
execute: (function () {

module.import(globalThis.unknown);

})
};
}));
@@ -0,0 +1,8 @@
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';

import(globalThis.unknown);

}));
@@ -0,0 +1 @@
import(globalThis.unknown, { assert: { type: 'special' } });

0 comments on commit c9dc363

Please sign in to comment.