vite 2.6.0-beta.0 → 2.6.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of vite might be problematic. Click here for more details.

Files changed (110) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/LICENSE.md +161 -3011
  3. package/bin/vite.js +2 -1
  4. package/dist/node/chunks/{dep-91aa74e8.js → dep-1e0a75a8.js} +58 -32
  5. package/dist/node/chunks/dep-1e0a75a8.js.map +1 -0
  6. package/dist/node/chunks/{dep-e36486f6.js → dep-ac1b4bf9.js} +1 -1
  7. package/dist/node/chunks/dep-ac1b4bf9.js.map +1 -0
  8. package/dist/node/chunks/{dep-c7e510f9.js → dep-ba6b30a0.js} +27538 -4953
  9. package/dist/node/chunks/dep-ba6b30a0.js.map +1 -0
  10. package/dist/node/chunks/{dep-11213a75.js → dep-c4cf6e92.js} +19 -6
  11. package/dist/node/chunks/dep-c4cf6e92.js.map +1 -0
  12. package/dist/node/chunks/{dep-eb6ef720.js → dep-d574094c.js} +18 -5
  13. package/dist/node/chunks/dep-d574094c.js.map +1 -0
  14. package/dist/node/chunks/{dep-0d2f9464.js → dep-e39b05d6.js} +18 -5
  15. package/dist/node/chunks/dep-e39b05d6.js.map +1 -0
  16. package/dist/node/cli.js +60 -23992
  17. package/dist/node/cli.js.map +1 -1
  18. package/dist/node/index.d.ts +26 -8
  19. package/dist/node/index.js +18 -15
  20. package/dist/node/index.js.map +1 -1
  21. package/dist/node/terser.js +102 -55
  22. package/package.json +28 -13
  23. package/types/package.json +3 -0
  24. package/api-extractor.json +0 -54
  25. package/dist/node/chunks/dep-0d2f9464.js.map +0 -1
  26. package/dist/node/chunks/dep-11213a75.js.map +0 -1
  27. package/dist/node/chunks/dep-91aa74e8.js.map +0 -1
  28. package/dist/node/chunks/dep-c7e510f9.js.map +0 -1
  29. package/dist/node/chunks/dep-e36486f6.js.map +0 -1
  30. package/dist/node/chunks/dep-eb6ef720.js.map +0 -1
  31. package/dist/node/terser.js.map +0 -1
  32. package/rollup.config.js +0 -389
  33. package/scripts/patchTypes.js +0 -70
  34. package/src/node/__tests__/asset.spec.ts +0 -156
  35. package/src/node/__tests__/build.spec.ts +0 -67
  36. package/src/node/__tests__/config.spec.ts +0 -166
  37. package/src/node/__tests__/packages/name/package.json +0 -3
  38. package/src/node/__tests__/packages/noname/package.json +0 -1
  39. package/src/node/__tests__/plugins/css.spec.ts +0 -116
  40. package/src/node/__tests__/scan.spec.ts +0 -118
  41. package/src/node/__tests__/utils.spec.ts +0 -40
  42. package/src/node/build.ts +0 -783
  43. package/src/node/cli.ts +0 -239
  44. package/src/node/config.ts +0 -1033
  45. package/src/node/constants.ts +0 -87
  46. package/src/node/importGlob.ts +0 -173
  47. package/src/node/index.ts +0 -88
  48. package/src/node/logger.ts +0 -167
  49. package/src/node/optimizer/esbuildDepPlugin.ts +0 -216
  50. package/src/node/optimizer/index.ts +0 -410
  51. package/src/node/optimizer/registerMissing.ts +0 -102
  52. package/src/node/optimizer/scan.ts +0 -457
  53. package/src/node/plugin.ts +0 -138
  54. package/src/node/plugins/asset.ts +0 -365
  55. package/src/node/plugins/assetImportMetaUrl.ts +0 -99
  56. package/src/node/plugins/clientInjections.ts +0 -72
  57. package/src/node/plugins/css.ts +0 -1279
  58. package/src/node/plugins/dataUri.ts +0 -64
  59. package/src/node/plugins/define.ts +0 -107
  60. package/src/node/plugins/esbuild.ts +0 -280
  61. package/src/node/plugins/html.ts +0 -673
  62. package/src/node/plugins/importAnalysis.ts +0 -614
  63. package/src/node/plugins/importAnalysisBuild.ts +0 -334
  64. package/src/node/plugins/index.ts +0 -69
  65. package/src/node/plugins/json.ts +0 -75
  66. package/src/node/plugins/loadFallback.ts +0 -19
  67. package/src/node/plugins/manifest.ts +0 -123
  68. package/src/node/plugins/modulePreloadPolyfill.ts +0 -100
  69. package/src/node/plugins/preAlias.ts +0 -22
  70. package/src/node/plugins/reporter.ts +0 -244
  71. package/src/node/plugins/resolve.ts +0 -925
  72. package/src/node/plugins/terser.ts +0 -40
  73. package/src/node/plugins/wasm.ts +0 -72
  74. package/src/node/plugins/worker.ts +0 -117
  75. package/src/node/preview.ts +0 -82
  76. package/src/node/server/__tests__/fixtures/none/nested/package.json +0 -3
  77. package/src/node/server/__tests__/fixtures/pnpm/nested/package.json +0 -3
  78. package/src/node/server/__tests__/fixtures/pnpm/package.json +0 -3
  79. package/src/node/server/__tests__/fixtures/pnpm/pnpm-workspace.yaml +0 -0
  80. package/src/node/server/__tests__/fixtures/yarn/nested/package.json +0 -3
  81. package/src/node/server/__tests__/fixtures/yarn/package.json +0 -6
  82. package/src/node/server/__tests__/search-root.spec.ts +0 -31
  83. package/src/node/server/hmr.ts +0 -489
  84. package/src/node/server/http.ts +0 -198
  85. package/src/node/server/index.ts +0 -705
  86. package/src/node/server/middlewares/base.ts +0 -52
  87. package/src/node/server/middlewares/error.ts +0 -98
  88. package/src/node/server/middlewares/indexHtml.ts +0 -170
  89. package/src/node/server/middlewares/proxy.ts +0 -124
  90. package/src/node/server/middlewares/spaFallback.ts +0 -32
  91. package/src/node/server/middlewares/static.ts +0 -153
  92. package/src/node/server/middlewares/time.ts +0 -18
  93. package/src/node/server/middlewares/transform.ts +0 -196
  94. package/src/node/server/moduleGraph.ts +0 -200
  95. package/src/node/server/openBrowser.ts +0 -101
  96. package/src/node/server/pluginContainer.ts +0 -546
  97. package/src/node/server/searchRoot.ts +0 -70
  98. package/src/node/server/send.ts +0 -54
  99. package/src/node/server/sourcemap.ts +0 -54
  100. package/src/node/server/transformRequest.ts +0 -168
  101. package/src/node/server/ws.ts +0 -131
  102. package/src/node/ssr/__tests__/ssrTransform.spec.ts +0 -309
  103. package/src/node/ssr/ssrExternal.ts +0 -161
  104. package/src/node/ssr/ssrManifestPlugin.ts +0 -53
  105. package/src/node/ssr/ssrModuleLoader.ts +0 -214
  106. package/src/node/ssr/ssrStacktrace.ts +0 -75
  107. package/src/node/ssr/ssrTransform.ts +0 -452
  108. package/src/node/tsconfig.json +0 -14
  109. package/src/node/utils.ts +0 -565
  110. package/tsconfig.base.json +0 -11
@@ -1,54 +0,0 @@
1
- import path from 'path'
2
- import { promises as fs } from 'fs'
3
- import { Logger } from '../logger'
4
- import { createDebugger } from '../utils'
5
-
6
- const isDebug = !!process.env.DEBUG
7
- const debug = createDebugger('vite:sourcemap', {
8
- onlyWhenFocused: true
9
- })
10
-
11
- interface SourceMapLike {
12
- sources: string[]
13
- sourcesContent?: (string | null)[]
14
- sourceRoot?: string
15
- }
16
-
17
- export async function injectSourcesContent(
18
- map: SourceMapLike,
19
- file: string,
20
- logger: Logger
21
- ): Promise<void> {
22
- let sourceRoot: string | undefined
23
- try {
24
- // The source root is undefined for virtual modules and permission errors.
25
- sourceRoot = await fs.realpath(
26
- path.resolve(path.dirname(file), map.sourceRoot || '')
27
- )
28
- } catch {}
29
-
30
- const missingSources: string[] = []
31
- map.sourcesContent = await Promise.all(
32
- map.sources.map((sourcePath) => {
33
- if (sourcePath) {
34
- sourcePath = decodeURI(sourcePath)
35
- if (sourceRoot) {
36
- sourcePath = path.resolve(sourceRoot, sourcePath)
37
- }
38
- return fs.readFile(sourcePath, 'utf-8').catch(() => {
39
- missingSources.push(sourcePath)
40
- return null
41
- })
42
- }
43
- return null
44
- })
45
- )
46
-
47
- // Use this command…
48
- // DEBUG="vite:sourcemap" vite build
49
- // …to log the missing sources.
50
- if (missingSources.length) {
51
- logger.warnOnce(`Sourcemap for "${file}" points to missing source files`)
52
- isDebug && debug(`Missing sources:\n ` + missingSources.join(`\n `))
53
- }
54
- }
@@ -1,168 +0,0 @@
1
- import { promises as fs } from 'fs'
2
- import path from 'path'
3
- import getEtag from 'etag'
4
- import * as convertSourceMap from 'convert-source-map'
5
- import { SourceDescription, SourceMap } from 'rollup'
6
- import { ViteDevServer } from '..'
7
- import chalk from 'chalk'
8
- import {
9
- createDebugger,
10
- cleanUrl,
11
- prettifyUrl,
12
- removeTimestampQuery,
13
- timeFrom,
14
- ensureWatchedFile,
15
- isObject
16
- } from '../utils'
17
- import { checkPublicFile } from '../plugins/asset'
18
- import { ssrTransform } from '../ssr/ssrTransform'
19
- import { injectSourcesContent } from './sourcemap'
20
- import { isFileServingAllowed } from './middlewares/static'
21
-
22
- const debugLoad = createDebugger('vite:load')
23
- const debugTransform = createDebugger('vite:transform')
24
- const debugCache = createDebugger('vite:cache')
25
- const isDebug = !!process.env.DEBUG
26
-
27
- export interface TransformResult {
28
- code: string
29
- map: SourceMap | null
30
- etag?: string
31
- deps?: string[]
32
- dynamicDeps?: string[]
33
- }
34
-
35
- export interface TransformOptions {
36
- ssr?: boolean
37
- html?: boolean
38
- }
39
-
40
- export async function transformRequest(
41
- url: string,
42
- server: ViteDevServer,
43
- options: TransformOptions = {}
44
- ): Promise<TransformResult | null> {
45
- const { config, pluginContainer, moduleGraph, watcher } = server
46
-
47
- url = removeTimestampQuery(url)
48
- const { root, logger } = config
49
- const prettyUrl = isDebug ? prettifyUrl(url, root) : ''
50
- const ssr = !!options.ssr
51
-
52
- // check if we have a fresh cache
53
- const module = await moduleGraph.getModuleByUrl(url)
54
- const cached =
55
- module && (ssr ? module.ssrTransformResult : module.transformResult)
56
- if (cached) {
57
- isDebug && debugCache(`[memory] ${prettyUrl}`)
58
- return cached
59
- }
60
-
61
- // resolve
62
- const id = (await pluginContainer.resolveId(url))?.id || url
63
- const file = cleanUrl(id)
64
-
65
- let code: string | null = null
66
- let map: SourceDescription['map'] = null
67
-
68
- // load
69
- const loadStart = isDebug ? Date.now() : 0
70
- const loadResult = await pluginContainer.load(id, ssr)
71
- if (loadResult == null) {
72
- // if this is an html request and there is no load result, skip ahead to
73
- // SPA fallback.
74
- if (options.html && !id.endsWith('.html')) {
75
- return null
76
- }
77
- // try fallback loading it from fs as string
78
- // if the file is a binary, there should be a plugin that already loaded it
79
- // as string
80
- // only try the fallback if access is allowed, skip for out of root url
81
- // like /service-worker.js or /api/users
82
- if (options.ssr || isFileServingAllowed(file, server)) {
83
- try {
84
- code = await fs.readFile(file, 'utf-8')
85
- isDebug && debugLoad(`${timeFrom(loadStart)} [fs] ${prettyUrl}`)
86
- } catch (e) {
87
- if (e.code !== 'ENOENT') {
88
- throw e
89
- }
90
- }
91
- }
92
- if (code) {
93
- try {
94
- map = (
95
- convertSourceMap.fromSource(code) ||
96
- convertSourceMap.fromMapFileSource(code, path.dirname(file))
97
- )?.toObject()
98
- } catch (e) {
99
- logger.warn(`Failed to load source map for ${url}.`, {
100
- timestamp: true
101
- })
102
- }
103
- }
104
- } else {
105
- isDebug && debugLoad(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`)
106
- if (isObject(loadResult)) {
107
- code = loadResult.code
108
- map = loadResult.map
109
- } else {
110
- code = loadResult
111
- }
112
- }
113
- if (code == null) {
114
- if (checkPublicFile(url, config)) {
115
- throw new Error(
116
- `Failed to load url ${url} (resolved id: ${id}). ` +
117
- `This file is in /public and will be copied as-is during build without ` +
118
- `going through the plugin transforms, and therefore should not be ` +
119
- `imported from source code. It can only be referenced via HTML tags.`
120
- )
121
- } else {
122
- return null
123
- }
124
- }
125
-
126
- // ensure module in graph after successful load
127
- const mod = await moduleGraph.ensureEntryFromUrl(url)
128
- ensureWatchedFile(watcher, mod.file, root)
129
-
130
- // transform
131
- const transformStart = isDebug ? Date.now() : 0
132
- const transformResult = await pluginContainer.transform(code, id, map, ssr)
133
- if (
134
- transformResult == null ||
135
- (isObject(transformResult) && transformResult.code == null)
136
- ) {
137
- // no transform applied, keep code as-is
138
- isDebug &&
139
- debugTransform(
140
- timeFrom(transformStart) + chalk.dim(` [skipped] ${prettyUrl}`)
141
- )
142
- } else {
143
- isDebug && debugTransform(`${timeFrom(transformStart)} ${prettyUrl}`)
144
- code = transformResult.code!
145
- map = transformResult.map
146
- }
147
-
148
- if (map && mod.file) {
149
- map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
150
- if (map.mappings && !map.sourcesContent) {
151
- await injectSourcesContent(map, mod.file, logger)
152
- }
153
- }
154
-
155
- if (ssr) {
156
- return (mod.ssrTransformResult = await ssrTransform(
157
- code,
158
- map as SourceMap,
159
- url
160
- ))
161
- } else {
162
- return (mod.transformResult = {
163
- code,
164
- map,
165
- etag: getEtag(code, { weak: true })
166
- } as TransformResult)
167
- }
168
- }
@@ -1,131 +0,0 @@
1
- import chalk from 'chalk'
2
- import { Server, STATUS_CODES } from 'http'
3
- import {
4
- createServer as createHttpsServer,
5
- ServerOptions as HttpsServerOptions
6
- } from 'https'
7
- import WebSocket from 'ws'
8
- import { ErrorPayload, HMRPayload } from 'types/hmrPayload'
9
- import { ResolvedConfig } from '..'
10
- import { isObject } from '../utils'
11
- import { Socket } from 'net'
12
- export const HMR_HEADER = 'vite-hmr'
13
-
14
- export interface WebSocketServer {
15
- send(payload: HMRPayload): void
16
- close(): Promise<void>
17
- }
18
-
19
- export function createWebSocketServer(
20
- server: Server | null,
21
- config: ResolvedConfig,
22
- httpsOptions?: HttpsServerOptions
23
- ): WebSocketServer {
24
- let wss: WebSocket.Server
25
- let httpsServer: Server | undefined = undefined
26
-
27
- const hmr = isObject(config.server.hmr) && config.server.hmr
28
- const wsServer = (hmr && hmr.server) || server
29
-
30
- if (wsServer) {
31
- wss = new WebSocket.Server({ noServer: true })
32
- wsServer.on('upgrade', (req, socket, head) => {
33
- if (req.headers['sec-websocket-protocol'] === HMR_HEADER) {
34
- wss.handleUpgrade(req, socket as Socket, head, (ws) => {
35
- wss.emit('connection', ws, req)
36
- })
37
- }
38
- })
39
- } else {
40
- const websocketServerOptions: WebSocket.ServerOptions = {}
41
- const port = (hmr && hmr.port) || 24678
42
- if (httpsOptions) {
43
- // if we're serving the middlewares over https, the ws library doesn't support automatically creating an https server, so we need to do it ourselves
44
- // create an inline https server and mount the websocket server to it
45
- httpsServer = createHttpsServer(httpsOptions, (req, res) => {
46
- const statusCode = 426
47
- const body = STATUS_CODES[statusCode]
48
- if (!body)
49
- throw new Error(
50
- `No body text found for the ${statusCode} status code`
51
- )
52
-
53
- res.writeHead(statusCode, {
54
- 'Content-Length': body.length,
55
- 'Content-Type': 'text/plain'
56
- })
57
- res.end(body)
58
- })
59
-
60
- httpsServer.listen(port)
61
- websocketServerOptions.server = httpsServer
62
- } else {
63
- // we don't need to serve over https, just let ws handle its own server
64
- websocketServerOptions.port = port
65
- }
66
-
67
- // vite dev server in middleware mode
68
- wss = new WebSocket.Server(websocketServerOptions)
69
- }
70
-
71
- wss.on('connection', (socket) => {
72
- socket.send(JSON.stringify({ type: 'connected' }))
73
- if (bufferedError) {
74
- socket.send(JSON.stringify(bufferedError))
75
- bufferedError = null
76
- }
77
- })
78
-
79
- wss.on('error', (e: Error & { code: string }) => {
80
- if (e.code !== 'EADDRINUSE') {
81
- config.logger.error(
82
- chalk.red(`WebSocket server error:\n${e.stack || e.message}`),
83
- { error: e }
84
- )
85
- }
86
- })
87
-
88
- // On page reloads, if a file fails to compile and returns 500, the server
89
- // sends the error payload before the client connection is established.
90
- // If we have no open clients, buffer the error and send it to the next
91
- // connected client.
92
- let bufferedError: ErrorPayload | null = null
93
-
94
- return {
95
- send(payload: HMRPayload) {
96
- if (payload.type === 'error' && !wss.clients.size) {
97
- bufferedError = payload
98
- return
99
- }
100
-
101
- const stringified = JSON.stringify(payload)
102
- wss.clients.forEach((client) => {
103
- if (client.readyState === WebSocket.OPEN) {
104
- client.send(stringified)
105
- }
106
- })
107
- },
108
-
109
- close() {
110
- return new Promise((resolve, reject) => {
111
- wss.close((err) => {
112
- if (err) {
113
- reject(err)
114
- } else {
115
- if (httpsServer) {
116
- httpsServer.close((err) => {
117
- if (err) {
118
- reject(err)
119
- } else {
120
- resolve()
121
- }
122
- })
123
- } else {
124
- resolve()
125
- }
126
- }
127
- })
128
- })
129
- }
130
- }
131
- }
@@ -1,309 +0,0 @@
1
- import { traverseHtml } from '../../plugins/html'
2
- import { ssrTransform } from '../ssrTransform'
3
-
4
- test('default import', async () => {
5
- expect(
6
- (
7
- await ssrTransform(
8
- `import foo from 'vue';console.log(foo.bar)`,
9
- null,
10
- null
11
- )
12
- ).code
13
- ).toMatchInlineSnapshot(`
14
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
15
- console.log(__vite_ssr_import_0__.default.bar)"
16
- `)
17
- })
18
-
19
- test('named import', async () => {
20
- expect(
21
- (
22
- await ssrTransform(
23
- `import { ref } from 'vue';function foo() { return ref(0) }`,
24
- null,
25
- null
26
- )
27
- ).code
28
- ).toMatchInlineSnapshot(`
29
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
30
- function foo() { return __vite_ssr_import_0__.ref(0) }"
31
- `)
32
- })
33
-
34
- test('namespace import', async () => {
35
- expect(
36
- (
37
- await ssrTransform(
38
- `import * as vue from 'vue';function foo() { return vue.ref(0) }`,
39
- null,
40
- null
41
- )
42
- ).code
43
- ).toMatchInlineSnapshot(`
44
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
45
- function foo() { return __vite_ssr_import_0__.ref(0) }"
46
- `)
47
- })
48
-
49
- test('export function declaration', async () => {
50
- expect((await ssrTransform(`export function foo() {}`, null, null)).code)
51
- .toMatchInlineSnapshot(`
52
- "function foo() {}
53
- Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
54
- `)
55
- })
56
-
57
- test('export class declaration', async () => {
58
- expect((await ssrTransform(`export class foo {}`, null, null)).code)
59
- .toMatchInlineSnapshot(`
60
- "class foo {}
61
- Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return foo }});"
62
- `)
63
- })
64
-
65
- test('export var declaration', async () => {
66
- expect((await ssrTransform(`export const a = 1, b = 2`, null, null)).code)
67
- .toMatchInlineSnapshot(`
68
- "const a = 1, b = 2
69
- Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
70
- Object.defineProperty(__vite_ssr_exports__, \\"b\\", { enumerable: true, configurable: true, get(){ return b }});"
71
- `)
72
- })
73
-
74
- test('export named', async () => {
75
- expect(
76
- (await ssrTransform(`const a = 1, b = 2; export { a, b as c }`, null, null))
77
- .code
78
- ).toMatchInlineSnapshot(`
79
- "const a = 1, b = 2;
80
- Object.defineProperty(__vite_ssr_exports__, \\"a\\", { enumerable: true, configurable: true, get(){ return a }});
81
- Object.defineProperty(__vite_ssr_exports__, \\"c\\", { enumerable: true, configurable: true, get(){ return b }});"
82
- `)
83
- })
84
-
85
- test('export named from', async () => {
86
- expect(
87
- (await ssrTransform(`export { ref, computed as c } from 'vue'`, null, null))
88
- .code
89
- ).toMatchInlineSnapshot(`
90
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
91
-
92
- Object.defineProperty(__vite_ssr_exports__, \\"ref\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.ref }});
93
- Object.defineProperty(__vite_ssr_exports__, \\"c\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.computed }});"
94
- `)
95
- })
96
-
97
- test('named exports of imported binding', async () => {
98
- expect(
99
- (
100
- await ssrTransform(
101
- `import {createApp} from 'vue';export {createApp}`,
102
- null,
103
- null
104
- )
105
- ).code
106
- ).toMatchInlineSnapshot(`
107
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
108
-
109
- Object.defineProperty(__vite_ssr_exports__, \\"createApp\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__.createApp }});"
110
- `)
111
- })
112
-
113
- test('export * from', async () => {
114
- expect(
115
- (
116
- await ssrTransform(
117
- `export * from 'vue'\n` + `export * from 'react'`,
118
- null,
119
- null
120
- )
121
- ).code
122
- ).toMatchInlineSnapshot(`
123
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
124
- __vite_ssr_exportAll__(__vite_ssr_import_0__);
125
- const __vite_ssr_import_1__ = await __vite_ssr_import__(\\"react\\");
126
- __vite_ssr_exportAll__(__vite_ssr_import_1__);"
127
- `)
128
- })
129
-
130
- test('export * as from', async () => {
131
- expect((await ssrTransform(`export * as foo from 'vue'`, null, null)).code)
132
- .toMatchInlineSnapshot(`
133
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
134
-
135
- Object.defineProperty(__vite_ssr_exports__, \\"foo\\", { enumerable: true, configurable: true, get(){ return __vite_ssr_import_0__ }});"
136
- `)
137
- })
138
-
139
- test('export default', async () => {
140
- expect(
141
- (await ssrTransform(`export default {}`, null, null)).code
142
- ).toMatchInlineSnapshot(`"__vite_ssr_exports__.default = {}"`)
143
- })
144
-
145
- test('import.meta', async () => {
146
- expect(
147
- (await ssrTransform(`console.log(import.meta.url)`, null, null)).code
148
- ).toMatchInlineSnapshot(`"console.log(__vite_ssr_import_meta__.url)"`)
149
- })
150
-
151
- test('dynamic import', async () => {
152
- const result = await ssrTransform(
153
- `export const i = () => import('./foo')`,
154
- null,
155
- null
156
- )
157
- expect(result.code).toMatchInlineSnapshot(`
158
- "const i = () => __vite_ssr_dynamic_import__('./foo')
159
- Object.defineProperty(__vite_ssr_exports__, \\"i\\", { enumerable: true, configurable: true, get(){ return i }});"
160
- `)
161
- expect(result.deps).toEqual([])
162
- expect(result.dynamicDeps).toEqual(['./foo'])
163
- })
164
-
165
- test('do not rewrite method definition', async () => {
166
- const result = await ssrTransform(
167
- `import { fn } from 'vue';class A { fn() { fn() } }`,
168
- null,
169
- null
170
- )
171
- expect(result.code).toMatchInlineSnapshot(`
172
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
173
- class A { fn() { __vite_ssr_import_0__.fn() } }"
174
- `)
175
- expect(result.deps).toEqual(['vue'])
176
- })
177
-
178
- test('do not rewrite catch clause', async () => {
179
- const result = await ssrTransform(
180
- `import {error} from './dependency';try {} catch(error) {}`,
181
- null,
182
- null
183
- )
184
- expect(result.code).toMatchInlineSnapshot(`
185
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
186
- try {} catch(error) {}"
187
- `)
188
- expect(result.deps).toEqual(['./dependency'])
189
- })
190
-
191
- // #2221
192
- test('should declare variable for imported super class', async () => {
193
- expect(
194
- (
195
- await ssrTransform(
196
- `import { Foo } from './dependency';` + `class A extends Foo {}`,
197
- null,
198
- null
199
- )
200
- ).code
201
- ).toMatchInlineSnapshot(`
202
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
203
- const Foo = __vite_ssr_import_0__.Foo;
204
- class A extends Foo {}"
205
- `)
206
-
207
- // exported classes: should prepend the declaration at root level, before the
208
- // first class that uses the binding
209
- expect(
210
- (
211
- await ssrTransform(
212
- `import { Foo } from './dependency';` +
213
- `export default class A extends Foo {}\n` +
214
- `export class B extends Foo {}`,
215
- null,
216
- null
217
- )
218
- ).code
219
- ).toMatchInlineSnapshot(`
220
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"./dependency\\");
221
- const Foo = __vite_ssr_import_0__.Foo;
222
- class A extends Foo {}
223
- class B extends Foo {}
224
- Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });
225
- Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});"
226
- `)
227
- })
228
-
229
- // #4049
230
- test('should handle default export variants', async () => {
231
- // default anonymous functions
232
- expect(
233
- (await ssrTransform(`export default function() {}\n`, null, null)).code
234
- ).toMatchInlineSnapshot(`
235
- "__vite_ssr_exports__.default = function() {}
236
- "
237
- `)
238
- // default anonymous class
239
- expect((await ssrTransform(`export default class {}\n`, null, null)).code)
240
- .toMatchInlineSnapshot(`
241
- "__vite_ssr_exports__.default = class {}
242
- "
243
- `)
244
- // default named functions
245
- expect(
246
- (
247
- await ssrTransform(
248
- `export default function foo() {}\n` +
249
- `foo.prototype = Object.prototype;`,
250
- null,
251
- null
252
- )
253
- ).code
254
- ).toMatchInlineSnapshot(`
255
- "function foo() {}
256
- foo.prototype = Object.prototype;
257
- Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: foo });"
258
- `)
259
- // default named classes
260
- expect(
261
- (
262
- await ssrTransform(
263
- `export default class A {}\n` + `export class B extends A {}`,
264
- null,
265
- null
266
- )
267
- ).code
268
- ).toMatchInlineSnapshot(`
269
- "class A {}
270
- class B extends A {}
271
- Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });
272
- Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});"
273
- `)
274
- })
275
-
276
- test('sourcemap source', async () => {
277
- expect(
278
- (await ssrTransform(`export const a = 1`, null, 'input.js')).map.sources
279
- ).toStrictEqual(['input.js'])
280
- })
281
-
282
- test('overwrite bindings', async () => {
283
- expect(
284
- (
285
- await ssrTransform(
286
- `import { inject } from 'vue';` +
287
- `const a = { inject }\n` +
288
- `const b = { test: inject }\n` +
289
- `function c() { const { test: inject } = { test: true }; console.log(inject) }\n` +
290
- `const d = inject \n` +
291
- `function f() { console.log(inject) }\n` +
292
- `function e() { const { inject } = { inject: true } }\n` +
293
- `function g() { const f = () => { const inject = true }; console.log(inject) }\n`,
294
- null,
295
- null
296
- )
297
- ).code
298
- ).toMatchInlineSnapshot(`
299
- "const __vite_ssr_import_0__ = await __vite_ssr_import__(\\"vue\\");
300
- const a = { inject: __vite_ssr_import_0__.inject }
301
- const b = { test: __vite_ssr_import_0__.inject }
302
- function c() { const { test: inject } = { test: true }; console.log(inject) }
303
- const d = __vite_ssr_import_0__.inject
304
- function f() { console.log(__vite_ssr_import_0__.inject) }
305
- function e() { const { inject } = { inject: true } }
306
- function g() { const f = () => { const inject = true }; console.log(__vite_ssr_import_0__.inject) }
307
- "
308
- `)
309
- })