Skip to content

Commit

Permalink
feat: ssrTransform support import assertion by default (#14202)
Browse files Browse the repository at this point in the history
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
  • Loading branch information
sun0day and bluwy authored Sep 15, 2023
1 parent e9b1e85 commit 70a379f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ Repository: https://github.com/acornjs/acorn.git
---------------------------------------

## acorn-import-assertions
License: MIT
By: Sven Sauleau
Repository: https://github.com/xtuc/acorn-import-assertions

---------------------------------------

## acorn-walk
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Expand Down
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"@types/escape-html": "^1.0.2",
"@types/pnpapi": "^0.0.2",
"acorn": "^8.10.0",
"acorn-import-assertions": "^1.9.0",
"acorn-walk": "^8.2.0",
"cac": "^6.7.14",
"chokidar": "^3.5.3",
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import type {
import * as acorn from 'acorn'
import type { RawSourceMap } from '@ampproject/remapping'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
// @ts-expect-error untyped
import { importAssertions } from 'acorn-import-assertions'
import MagicString from 'magic-string'
import type { FSWatcher } from 'chokidar'
import colors from 'picocolors'
Expand Down Expand Up @@ -150,7 +152,7 @@ type PluginContext = Omit<
| 'moduleIds'
>

export let parser = acorn.Parser
export let parser = acorn.Parser.extend(importAssertions)

export async function createPluginContainer(
config: ResolvedConfig,
Expand Down Expand Up @@ -628,6 +630,7 @@ export async function createPluginContainer(
}
if (options.acornInjectPlugins) {
parser = acorn.Parser.extend(
importAssertions,
...(arraify(options.acornInjectPlugins) as any),
)
}
Expand Down
14 changes: 14 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,3 +932,17 @@ const Baz = class extends Foo {}
"
`)
})

test('import assertion attribute', async () => {
expect(
await ssrTransformSimpleCode(`
import * as foo from './foo.json' assert { type: 'json' };
import('./bar.json', { assert: { type: 'json' } });
`),
).toMatchInlineSnapshot(`
"const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./foo.json\\");
__vite_ssr_dynamic_import__('./bar.json', { assert: { type: 'json' } });
"`)
})
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 70a379f

Please sign in to comment.