Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit inline comments inside dynamic import #2797

Merged
merged 3 commits into from Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/ast/nodes/Import.ts
@@ -1,5 +1,5 @@
import MagicString from 'magic-string';
import { RenderOptions } from '../../utils/renderHelpers';
import { findFirstOccurrenceOutsideComment, RenderOptions } from '../../utils/renderHelpers';
import CallExpression from './CallExpression';
import * as NodeType from './NodeType';
import { NodeBase } from './shared/Node';
Expand Down Expand Up @@ -81,11 +81,13 @@ export default class Import extends NodeBase {
if (importMechanism) {
const leftMechanism =
(this.resolutionInterop && importMechanism.interopLeft) || importMechanism.left;
code.overwrite(this.parent.start, this.parent.arguments[0].start, leftMechanism);
const leftMechanismEnd =
findFirstOccurrenceOutsideComment(code.original, '(', this.parent.callee.end) + 1;
code.overwrite(this.parent.start, leftMechanismEnd, leftMechanism);

const rightMechanism =
(this.resolutionInterop && importMechanism.interopRight) || importMechanism.right;
code.overwrite(this.parent.arguments[0].end, this.parent.end, rightMechanism);
code.overwrite(this.parent.end - 1, this.parent.end, rightMechanism);
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/chunking-form/samples/dynamic-import-comments/_config.js
@@ -0,0 +1,15 @@
module.exports = {
description: 'should not remove inline comments inside dynamic import',
options: {
input: 'main.js',
onwarn() {},
plugins: {
resolveDynamicImport() {
return false;
}
},
output: {
dynamicImportFunction: 'foobar'
}
}
};
@@ -0,0 +1,7 @@
define(['require'], function (require) { 'use strict';

new Promise(function (resolve, reject) { require([
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/], resolve, reject) });

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

Promise.resolve(require(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/));
@@ -0,0 +1,3 @@
foobar(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

module.import(
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);

}
};
});
3 changes: 3 additions & 0 deletions test/chunking-form/samples/dynamic-import-comments/main.js
@@ -0,0 +1,3 @@
import /* () should not break */ (
/* webpackChunkName: "chunk-name" */
'./foo.js'/*suffix*/);