Skip to content

Commit

Permalink
Emit inline comments inside dynamic import (#2797)
Browse files Browse the repository at this point in the history
* Emit inline comments inside dynamic import

fixes #2778

* space after import still generates valid JS

* Using findFirstOccurrenceOutsideComment()
  • Loading branch information
manucorporat authored and lukastaegert committed Apr 10, 2019
1 parent fadf7bc commit 6709c17
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
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*/);

0 comments on commit 6709c17

Please sign in to comment.