Skip to content

Commit a4e3a15

Browse files
authored
Merge pull request #4 from morganney/develop
fix: ignore imports inside strings better
2 parents 53f58a9 + e4870d0 commit a4e3a15

File tree

6 files changed

+44
-8
lines changed

6 files changed

+44
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ If you must have a multiline comment then this style is ok (only one `import()`
3434
*/
3535
```
3636

37-
This module uses a [RegExp](https://github.com/morganney/magic-comments-loader/blob/master/src/loader.js#L8) not a parser. If you would like to add better support for ignoring `import()` behind multiline comments please open a pull request. See some more [examples on regexr](https://regexr.com/65fg0).
37+
This module uses a RegExp not a parser. If you would like to add better support for ignoring `import()` behind multiline comments please open a pull request. See some more [examples on regexr](https://regexr.com/65fg0).
3838

3939
### Configuration
4040

__tests__/util.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,33 @@
1-
import { pathIsMatch, getOverrideConfig } from '../src/util.js'
1+
import {
2+
pathIsMatch,
3+
getOverrideConfig,
4+
dynamicImportsWithoutComments
5+
} from '../src/util.js'
6+
7+
describe('dynamicImportsWithoutComments', () => {
8+
it('is a regex to match dyanmic imports', () => {
9+
expect(
10+
`const str = 'import("some/path")'`.replace(dynamicImportsWithoutComments, 'test')
11+
).toEqual(`const str = 'import("some/path")'`)
12+
expect('import("some/path")'.replace(dynamicImportsWithoutComments, 'test')).toEqual(
13+
'test'
14+
)
15+
expect(
16+
`import(
17+
"some/path"
18+
)`.replace(dynamicImportsWithoutComments, 'test')
19+
).toEqual('test')
20+
expect(
21+
`import(/* with comments */ "some/path")`.replace(
22+
dynamicImportsWithoutComments,
23+
'test'
24+
)
25+
).toEqual(`import(/* with comments */ "some/path")`)
26+
expect(
27+
`console.log('import("some/path")')`.replace(dynamicImportsWithoutComments, 'test')
28+
).toEqual(`console.log('import("some/path")')`)
29+
})
30+
})
231

332
describe('pathIsMatch', () => {
433
it('compares a filepath to glob patterns', () => {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "magic-comments-loader",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"description": "Add webpack magic comments to your dynamic imports during build time",
55
"main": "dist",
66
"type": "module",

src/loader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { validate } from 'schema-utils'
33

44
import { schema } from './schema.js'
55
import { getCommenter } from './comment.js'
6+
import { dynamicImportsWithoutComments } from './util.js'
67

7-
const dynamicImportsWithoutComments =
8-
/(?<![\w.]|#!|\*[\s\w]*?|\/\/\s*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?![\s]*?\*\/)/g
98
const loader = function (source, map, meta) {
109
const options = getOptions(this)
1110
const optionKeys = Object.keys(options)

src/util.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,13 @@ const getOverrideConfig = (overrides, filepath, config) => {
5656
return config
5757
}
5858
const importPrefix = /^(?:(\.{1,2}\/)+)|^\/|^.+:\/\/\/?[.-\w]+\//
59+
const dynamicImportsWithoutComments =
60+
/(?<![\w.]|#!|\*[\s\w]*?|\/\/\s*|['"`][^)$]*)import\s*\((?!\s*\/\*)(?<path>\s*?['"`][^)]+['"`]\s*)\)(?!\s*?\*\/)/g
5961

60-
export { getOverrideConfig, getOverrideSchema, pathIsMatch, importPrefix }
62+
export {
63+
getOverrideConfig,
64+
getOverrideSchema,
65+
pathIsMatch,
66+
importPrefix,
67+
dynamicImportsWithoutComments
68+
}

0 commit comments

Comments
 (0)