Skip to content

Commit d9ceac2

Browse files
fix: dynamic imports from arrow func expressions. (#31)
1 parent cd28a7e commit d9ceac2

File tree

6 files changed

+157
-63
lines changed

6 files changed

+157
-63
lines changed

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knighted/specifier",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Node.js tool for updating your ES module and CommonJS specifiers.",
55
"type": "module",
66
"main": "dist",
@@ -54,9 +54,9 @@
5454
"url": "https://github.com/knightedcodemonkey/specifier/issues"
5555
},
5656
"dependencies": {
57-
"@knighted/walk": "^1.0.0-rc.0",
57+
"@knighted/walk": "^1.0.0-rc.1",
5858
"magic-string": "^0.30.17",
59-
"oxc-parser": "^0.62.0"
59+
"oxc-parser": "^0.63.0"
6060
},
6161
"devDependencies": {
6262
"@eslint/js": "^9.3.0",

src/format.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,22 @@ const format = async (src: string, ast: ParseResult, cb: Callback) => {
148148
}
149149
}
150150

151+
if (node.type === 'ArrowFunctionExpression') {
152+
const { body } = node
153+
154+
if (body.type === 'ImportExpression') {
155+
formatExpression(body)
156+
}
157+
158+
if (
159+
body.type === 'CallExpression' &&
160+
body.callee.type === 'Identifier' &&
161+
body.callee.name === 'require'
162+
) {
163+
formatExpression(body)
164+
}
165+
}
166+
151167
if (node.type === 'TSImportType') {
152168
const { argument } = node
153169

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
type RouteMap = Record<string, object>
2+
3+
export const routes: RouteMap = {
4+
all: {
5+
paths: ['*'],
6+
components: {
7+
interface: () =>
8+
import(
9+
/* webpackChunkName: "user" */
10+
'./user.js'
11+
),
12+
},
13+
},
14+
};
15+
16+
function foo () {
17+
import('./code.js')
18+
}
19+
20+
const bar = () => {
21+
import('./code.js')
22+
}
23+
24+
(() => import('./code.js'))()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const routes = {
2+
all: {
3+
paths: ['*'],
4+
components: {
5+
interface: () =>
6+
require(
7+
'./user.js'
8+
),
9+
},
10+
},
11+
};
12+
13+
function foo () {
14+
require('./code.js')
15+
}
16+
17+
const bar = () => {
18+
require('./code.js')
19+
}
20+
21+
(() => require('./code.js'))()

test/update.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ describe('update', () => {
7474
assert.ok(update.indexOf(tl) > -1)
7575
})
7676

77+
it('updates complex import expressions', async () => {
78+
const update = await specifier.update(
79+
join(fixtures, 'complexImportExpression.ts'),
80+
spec => {
81+
if (spec.value === './user.js') {
82+
return './other-user.js'
83+
}
84+
85+
if (spec.value === './code.js') {
86+
return './other-code.js'
87+
}
88+
},
89+
)
90+
91+
assert.ok(update.indexOf('./other-user.js') > -1)
92+
assert.equal([...update.matchAll(/other-code\.js/g)].length, 3)
93+
})
94+
7795
it('works with typescript', async () => {
7896
let update = await specifier.update(join(fixtures, 'types.d.ts'), spec => {
7997
if (spec.value === './user.js') {
@@ -116,6 +134,21 @@ describe('update', () => {
116134
assert.ok(update.indexOf('require("./esm.mjs")') > -1)
117135
})
118136

137+
it('updates complex require expressions', async () => {
138+
const update = await specifier.update(join(fixtures, 'complexRequire.js'), spec => {
139+
if (spec.value === './user.js') {
140+
return './other-user.js'
141+
}
142+
143+
if (spec.value === './code.js') {
144+
return './other-code.js'
145+
}
146+
})
147+
148+
assert.ok(update.indexOf('./other-user.js') > -1)
149+
assert.equal([...update.matchAll(/other-code\.js/g)].length, 3)
150+
})
151+
119152
it('updates `resolve` from different module types', async () => {
120153
const update = await specifier.update(join(fixtures, 'modules.js'), ({ value }) => {
121154
if (value === './require/file.js') {

0 commit comments

Comments
 (0)