|
1 |
| -import { parseAstAsync } from 'vite' |
| 1 | +import { createServer, createServerModuleRunner, parseAstAsync } from 'vite' |
2 | 2 | import { describe, expect, it } from 'vitest'
|
3 | 3 | import { debugSourceMap } from './test-utils'
|
4 | 4 | import { transformCjsToEsm } from './cjs'
|
| 5 | +import path from 'node:path' |
5 | 6 |
|
6 | 7 | describe(transformCjsToEsm, () => {
|
7 | 8 | async function testTransform(input: string) {
|
@@ -83,4 +84,51 @@ if (true) {
|
83 | 84 | "
|
84 | 85 | `)
|
85 | 86 | })
|
| 87 | + |
| 88 | + it('e2e', async () => { |
| 89 | + const server = await createServer({ |
| 90 | + configFile: false, |
| 91 | + logLevel: 'error', |
| 92 | + root: path.join(import.meta.dirname, 'fixtures/cjs'), |
| 93 | + plugins: [ |
| 94 | + { |
| 95 | + name: 'cjs-module-runner-transform', |
| 96 | + async transform(code, id) { |
| 97 | + if (id.endsWith('.cjs')) { |
| 98 | + const ast = await parseAstAsync(code) |
| 99 | + const { output } = transformCjsToEsm(code, ast) |
| 100 | + output.append(` |
| 101 | +;__vite_ssr_exportAll__(module.exports); |
| 102 | +export default module.exports; |
| 103 | +`) |
| 104 | + return { |
| 105 | + code: output.toString(), |
| 106 | + map: output.generateMap({ hires: 'boundary' }), |
| 107 | + } |
| 108 | + } |
| 109 | + }, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }) |
| 113 | + const runner = createServerModuleRunner(server.environments.ssr, { |
| 114 | + hmr: false, |
| 115 | + }) |
| 116 | + const mod = await runner.import('/entry.mjs') |
| 117 | + expect(mod).toMatchInlineSnapshot(` |
| 118 | + { |
| 119 | + "depDefault": { |
| 120 | + "a": "a", |
| 121 | + "b": "b", |
| 122 | + }, |
| 123 | + "depNamespace": { |
| 124 | + "a": "a", |
| 125 | + "b": "b", |
| 126 | + "default": { |
| 127 | + "a": "a", |
| 128 | + "b": "b", |
| 129 | + }, |
| 130 | + }, |
| 131 | + } |
| 132 | + `) |
| 133 | + }) |
86 | 134 | })
|
0 commit comments