Skip to content

Commit 0a33562

Browse files
committed
feat(plugin/alias): add acornOptions for #59
1 parent a45237c commit 0a33562

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

examples/alias/vite.config.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ export default defineConfig({
1212
'electron',
1313
],
1414
plugins: [
15-
alias([{
16-
find: '@common',
17-
replacement: path.join(__dirname, 'common'),
18-
}]),
15+
alias(
16+
[
17+
{ find: '@common', replacement: path.join(__dirname, 'common') },
18+
],
19+
{
20+
acornOptions: { ecmaVersion: 2022 },
21+
},
22+
),
1923
],
2024
})],
2125
})

src-plugin/alias.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ export interface AliasNode {
1818
}
1919
}
2020

21-
export function alias(options: {
22-
find: string | RegExp
23-
replacement: string
24-
}[]): Plugin {
21+
export function alias(
22+
aliases: {
23+
find: string | RegExp
24+
replacement: string
25+
}[],
26+
options?: {
27+
acornOptions?: Partial<import('acorn').Options>
28+
},
29+
): Plugin {
2530
let acorn: typeof import('acorn')
2631
return {
2732
name: 'plugin-alias',
@@ -34,7 +39,13 @@ export function alias(options: {
3439
throw new Error('[plugin/alias] dependency "acorn". Did you install it?')
3540
}
3641

37-
const ast = acorn.parse(code, { ecmaVersion: 2020 }) // acorn7.x Last supported 2020
42+
const ast = acorn.parse(
43+
code,
44+
Object.assign({
45+
// acorn7.x Last supported 2020
46+
ecmaVersion: 2020
47+
}, options?.acornOptions),
48+
)
3849
const ms = new MagicString(code)
3950
const nodes: AliasNode[] = []
4051

@@ -82,7 +93,7 @@ export function alias(options: {
8293
} = node
8394

8495
const source = importee.raw.slice(1, -1)
85-
const option = options.find(opt => opt.find instanceof RegExp
96+
const option = aliases.find(opt => opt.find instanceof RegExp
8697
? opt.find.test(source)
8798
: source.startsWith(opt.find + '/'))
8899
if (!option) continue

0 commit comments

Comments
 (0)