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.
- package/CHANGELOG.md +66 -0
- package/LICENSE.md +161 -3011
- package/bin/vite.js +2 -1
- package/dist/node/chunks/{dep-91aa74e8.js → dep-1e0a75a8.js} +58 -32
- package/dist/node/chunks/dep-1e0a75a8.js.map +1 -0
- package/dist/node/chunks/{dep-e36486f6.js → dep-ac1b4bf9.js} +1 -1
- package/dist/node/chunks/dep-ac1b4bf9.js.map +1 -0
- package/dist/node/chunks/{dep-c7e510f9.js → dep-ba6b30a0.js} +27538 -4953
- package/dist/node/chunks/dep-ba6b30a0.js.map +1 -0
- package/dist/node/chunks/{dep-11213a75.js → dep-c4cf6e92.js} +19 -6
- package/dist/node/chunks/dep-c4cf6e92.js.map +1 -0
- package/dist/node/chunks/{dep-eb6ef720.js → dep-d574094c.js} +18 -5
- package/dist/node/chunks/dep-d574094c.js.map +1 -0
- package/dist/node/chunks/{dep-0d2f9464.js → dep-e39b05d6.js} +18 -5
- package/dist/node/chunks/dep-e39b05d6.js.map +1 -0
- package/dist/node/cli.js +60 -23992
- package/dist/node/cli.js.map +1 -1
- package/dist/node/index.d.ts +26 -8
- package/dist/node/index.js +18 -15
- package/dist/node/index.js.map +1 -1
- package/dist/node/terser.js +102 -55
- package/package.json +28 -13
- package/types/package.json +3 -0
- package/api-extractor.json +0 -54
- package/dist/node/chunks/dep-0d2f9464.js.map +0 -1
- package/dist/node/chunks/dep-11213a75.js.map +0 -1
- package/dist/node/chunks/dep-91aa74e8.js.map +0 -1
- package/dist/node/chunks/dep-c7e510f9.js.map +0 -1
- package/dist/node/chunks/dep-e36486f6.js.map +0 -1
- package/dist/node/chunks/dep-eb6ef720.js.map +0 -1
- package/dist/node/terser.js.map +0 -1
- package/rollup.config.js +0 -389
- package/scripts/patchTypes.js +0 -70
- package/src/node/__tests__/asset.spec.ts +0 -156
- package/src/node/__tests__/build.spec.ts +0 -67
- package/src/node/__tests__/config.spec.ts +0 -166
- package/src/node/__tests__/packages/name/package.json +0 -3
- package/src/node/__tests__/packages/noname/package.json +0 -1
- package/src/node/__tests__/plugins/css.spec.ts +0 -116
- package/src/node/__tests__/scan.spec.ts +0 -118
- package/src/node/__tests__/utils.spec.ts +0 -40
- package/src/node/build.ts +0 -783
- package/src/node/cli.ts +0 -239
- package/src/node/config.ts +0 -1033
- package/src/node/constants.ts +0 -87
- package/src/node/importGlob.ts +0 -173
- package/src/node/index.ts +0 -88
- package/src/node/logger.ts +0 -167
- package/src/node/optimizer/esbuildDepPlugin.ts +0 -216
- package/src/node/optimizer/index.ts +0 -410
- package/src/node/optimizer/registerMissing.ts +0 -102
- package/src/node/optimizer/scan.ts +0 -457
- package/src/node/plugin.ts +0 -138
- package/src/node/plugins/asset.ts +0 -365
- package/src/node/plugins/assetImportMetaUrl.ts +0 -99
- package/src/node/plugins/clientInjections.ts +0 -72
- package/src/node/plugins/css.ts +0 -1279
- package/src/node/plugins/dataUri.ts +0 -64
- package/src/node/plugins/define.ts +0 -107
- package/src/node/plugins/esbuild.ts +0 -280
- package/src/node/plugins/html.ts +0 -673
- package/src/node/plugins/importAnalysis.ts +0 -614
- package/src/node/plugins/importAnalysisBuild.ts +0 -334
- package/src/node/plugins/index.ts +0 -69
- package/src/node/plugins/json.ts +0 -75
- package/src/node/plugins/loadFallback.ts +0 -19
- package/src/node/plugins/manifest.ts +0 -123
- package/src/node/plugins/modulePreloadPolyfill.ts +0 -100
- package/src/node/plugins/preAlias.ts +0 -22
- package/src/node/plugins/reporter.ts +0 -244
- package/src/node/plugins/resolve.ts +0 -925
- package/src/node/plugins/terser.ts +0 -40
- package/src/node/plugins/wasm.ts +0 -72
- package/src/node/plugins/worker.ts +0 -117
- package/src/node/preview.ts +0 -82
- package/src/node/server/__tests__/fixtures/none/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/package.json +0 -3
- package/src/node/server/__tests__/fixtures/pnpm/pnpm-workspace.yaml +0 -0
- package/src/node/server/__tests__/fixtures/yarn/nested/package.json +0 -3
- package/src/node/server/__tests__/fixtures/yarn/package.json +0 -6
- package/src/node/server/__tests__/search-root.spec.ts +0 -31
- package/src/node/server/hmr.ts +0 -489
- package/src/node/server/http.ts +0 -198
- package/src/node/server/index.ts +0 -705
- package/src/node/server/middlewares/base.ts +0 -52
- package/src/node/server/middlewares/error.ts +0 -98
- package/src/node/server/middlewares/indexHtml.ts +0 -170
- package/src/node/server/middlewares/proxy.ts +0 -124
- package/src/node/server/middlewares/spaFallback.ts +0 -32
- package/src/node/server/middlewares/static.ts +0 -153
- package/src/node/server/middlewares/time.ts +0 -18
- package/src/node/server/middlewares/transform.ts +0 -196
- package/src/node/server/moduleGraph.ts +0 -200
- package/src/node/server/openBrowser.ts +0 -101
- package/src/node/server/pluginContainer.ts +0 -546
- package/src/node/server/searchRoot.ts +0 -70
- package/src/node/server/send.ts +0 -54
- package/src/node/server/sourcemap.ts +0 -54
- package/src/node/server/transformRequest.ts +0 -168
- package/src/node/server/ws.ts +0 -131
- package/src/node/ssr/__tests__/ssrTransform.spec.ts +0 -309
- package/src/node/ssr/ssrExternal.ts +0 -161
- package/src/node/ssr/ssrManifestPlugin.ts +0 -53
- package/src/node/ssr/ssrModuleLoader.ts +0 -214
- package/src/node/ssr/ssrStacktrace.ts +0 -75
- package/src/node/ssr/ssrTransform.ts +0 -452
- package/src/node/tsconfig.json +0 -14
- package/src/node/utils.ts +0 -565
- package/tsconfig.base.json +0 -11
|
@@ -1,614 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import { Plugin } from '../plugin'
|
|
4
|
-
import { ResolvedConfig } from '../config'
|
|
5
|
-
import chalk from 'chalk'
|
|
6
|
-
import MagicString from 'magic-string'
|
|
7
|
-
import { init, parse as parseImports, ImportSpecifier } from 'es-module-lexer'
|
|
8
|
-
import { isCSSRequest, isDirectCSSRequest } from './css'
|
|
9
|
-
import {
|
|
10
|
-
isBuiltin,
|
|
11
|
-
cleanUrl,
|
|
12
|
-
createDebugger,
|
|
13
|
-
generateCodeFrame,
|
|
14
|
-
injectQuery,
|
|
15
|
-
isDataUrl,
|
|
16
|
-
isExternalUrl,
|
|
17
|
-
isJSRequest,
|
|
18
|
-
prettifyUrl,
|
|
19
|
-
timeFrom,
|
|
20
|
-
normalizePath
|
|
21
|
-
} from '../utils'
|
|
22
|
-
import {
|
|
23
|
-
debugHmr,
|
|
24
|
-
handlePrunedModules,
|
|
25
|
-
lexAcceptedHmrDeps
|
|
26
|
-
} from '../server/hmr'
|
|
27
|
-
import {
|
|
28
|
-
FS_PREFIX,
|
|
29
|
-
CLIENT_DIR,
|
|
30
|
-
CLIENT_PUBLIC_PATH,
|
|
31
|
-
DEP_VERSION_RE,
|
|
32
|
-
VALID_ID_PREFIX,
|
|
33
|
-
NULL_BYTE_PLACEHOLDER
|
|
34
|
-
} from '../constants'
|
|
35
|
-
import { ViteDevServer } from '..'
|
|
36
|
-
import { checkPublicFile } from './asset'
|
|
37
|
-
import { parse as parseJS } from 'acorn'
|
|
38
|
-
import type { Node } from 'estree'
|
|
39
|
-
import { transformImportGlob } from '../importGlob'
|
|
40
|
-
import { makeLegalIdentifier } from '@rollup/pluginutils'
|
|
41
|
-
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'
|
|
42
|
-
|
|
43
|
-
const isDebug = !!process.env.DEBUG
|
|
44
|
-
const debugRewrite = createDebugger('vite:rewrite')
|
|
45
|
-
|
|
46
|
-
const clientDir = normalizePath(CLIENT_DIR)
|
|
47
|
-
|
|
48
|
-
const skipRE = /\.(map|json)$/
|
|
49
|
-
const canSkip = (id: string) => skipRE.test(id) || isDirectCSSRequest(id)
|
|
50
|
-
|
|
51
|
-
function isExplicitImportRequired(url: string) {
|
|
52
|
-
return !isJSRequest(cleanUrl(url)) && !isCSSRequest(url)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function markExplicitImport(url: string) {
|
|
56
|
-
if (isExplicitImportRequired(url)) {
|
|
57
|
-
return injectQuery(url, 'import')
|
|
58
|
-
}
|
|
59
|
-
return url
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Server-only plugin that lexes, resolves, rewrites and analyzes url imports.
|
|
64
|
-
*
|
|
65
|
-
* - Imports are resolved to ensure they exist on disk
|
|
66
|
-
*
|
|
67
|
-
* - Lexes HMR accept calls and updates import relationships in the module graph
|
|
68
|
-
*
|
|
69
|
-
* - Bare module imports are resolved (by @rollup-plugin/node-resolve) to
|
|
70
|
-
* absolute file paths, e.g.
|
|
71
|
-
*
|
|
72
|
-
* ```js
|
|
73
|
-
* import 'foo'
|
|
74
|
-
* ```
|
|
75
|
-
* is rewritten to
|
|
76
|
-
* ```js
|
|
77
|
-
* import '/@fs//project/node_modules/foo/dist/foo.js'
|
|
78
|
-
* ```
|
|
79
|
-
*
|
|
80
|
-
* - CSS imports are appended with `.js` since both the js module and the actual
|
|
81
|
-
* css (referenced via <link>) may go through the transform pipeline:
|
|
82
|
-
*
|
|
83
|
-
* ```js
|
|
84
|
-
* import './style.css'
|
|
85
|
-
* ```
|
|
86
|
-
* is rewritten to
|
|
87
|
-
* ```js
|
|
88
|
-
* import './style.css.js'
|
|
89
|
-
* ```
|
|
90
|
-
*/
|
|
91
|
-
export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
|
|
92
|
-
const { root, base } = config
|
|
93
|
-
const clientPublicPath = path.posix.join(base, CLIENT_PUBLIC_PATH)
|
|
94
|
-
|
|
95
|
-
let server: ViteDevServer
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
name: 'vite:import-analysis',
|
|
99
|
-
|
|
100
|
-
configureServer(_server) {
|
|
101
|
-
server = _server
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
async transform(source, importer, ssr) {
|
|
105
|
-
const prettyImporter = prettifyUrl(importer, root)
|
|
106
|
-
|
|
107
|
-
if (canSkip(importer)) {
|
|
108
|
-
isDebug && debugRewrite(chalk.dim(`[skipped] ${prettyImporter}`))
|
|
109
|
-
return null
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const rewriteStart = Date.now()
|
|
113
|
-
await init
|
|
114
|
-
let imports: readonly ImportSpecifier[] = []
|
|
115
|
-
// strip UTF-8 BOM
|
|
116
|
-
if (source.charCodeAt(0) === 0xfeff) {
|
|
117
|
-
source = source.slice(1)
|
|
118
|
-
}
|
|
119
|
-
try {
|
|
120
|
-
imports = parseImports(source)[0]
|
|
121
|
-
} catch (e: any) {
|
|
122
|
-
const isVue = importer.endsWith('.vue')
|
|
123
|
-
const maybeJSX = !isVue && isJSRequest(importer)
|
|
124
|
-
|
|
125
|
-
const msg = isVue
|
|
126
|
-
? `Install @vitejs/plugin-vue to handle .vue files.`
|
|
127
|
-
: maybeJSX
|
|
128
|
-
? `If you are using JSX, make sure to name the file with the .jsx or .tsx extension.`
|
|
129
|
-
: `You may need to install appropriate plugins to handle the ${path.extname(
|
|
130
|
-
importer
|
|
131
|
-
)} file format.`
|
|
132
|
-
|
|
133
|
-
this.error(
|
|
134
|
-
`Failed to parse source for import analysis because the content ` +
|
|
135
|
-
`contains invalid JS syntax. ` +
|
|
136
|
-
msg,
|
|
137
|
-
e.idx
|
|
138
|
-
)
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
if (!imports.length) {
|
|
142
|
-
isDebug &&
|
|
143
|
-
debugRewrite(
|
|
144
|
-
`${timeFrom(rewriteStart)} ${chalk.dim(
|
|
145
|
-
`[no imports] ${prettyImporter}`
|
|
146
|
-
)}`
|
|
147
|
-
)
|
|
148
|
-
return source
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
let hasHMR = false
|
|
152
|
-
let isSelfAccepting = false
|
|
153
|
-
let hasEnv = false
|
|
154
|
-
let needQueryInjectHelper = false
|
|
155
|
-
let s: MagicString | undefined
|
|
156
|
-
const str = () => s || (s = new MagicString(source))
|
|
157
|
-
// vite-only server context
|
|
158
|
-
const { moduleGraph } = server
|
|
159
|
-
// since we are already in the transform phase of the importer, it must
|
|
160
|
-
// have been loaded so its entry is guaranteed in the module graph.
|
|
161
|
-
const importerModule = moduleGraph.getModuleById(importer)!
|
|
162
|
-
const importedUrls = new Set<string>()
|
|
163
|
-
const acceptedUrls = new Set<{
|
|
164
|
-
url: string
|
|
165
|
-
start: number
|
|
166
|
-
end: number
|
|
167
|
-
}>()
|
|
168
|
-
const toAbsoluteUrl = (url: string) =>
|
|
169
|
-
path.posix.resolve(path.posix.dirname(importerModule.url), url)
|
|
170
|
-
|
|
171
|
-
const normalizeUrl = async (
|
|
172
|
-
url: string,
|
|
173
|
-
pos: number
|
|
174
|
-
): Promise<[string, string]> => {
|
|
175
|
-
if (base !== '/' && url.startsWith(base)) {
|
|
176
|
-
url = url.replace(base, '/')
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
const resolved = await this.resolve(url, importer)
|
|
180
|
-
|
|
181
|
-
if (!resolved) {
|
|
182
|
-
this.error(
|
|
183
|
-
`Failed to resolve import "${url}" from "${path.relative(
|
|
184
|
-
process.cwd(),
|
|
185
|
-
importer
|
|
186
|
-
)}". Does the file exist?`,
|
|
187
|
-
pos
|
|
188
|
-
)
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const isRelative = url.startsWith('.')
|
|
192
|
-
const isSelfImport = !isRelative && cleanUrl(url) === cleanUrl(importer)
|
|
193
|
-
|
|
194
|
-
// normalize all imports into resolved URLs
|
|
195
|
-
// e.g. `import 'foo'` -> `import '/@fs/.../node_modules/foo/index.js`
|
|
196
|
-
if (resolved.id.startsWith(root + '/')) {
|
|
197
|
-
// in root: infer short absolute path from root
|
|
198
|
-
url = resolved.id.slice(root.length)
|
|
199
|
-
} else if (fs.existsSync(cleanUrl(resolved.id))) {
|
|
200
|
-
// exists but out of root: rewrite to absolute /@fs/ paths
|
|
201
|
-
url = path.posix.join(FS_PREFIX + resolved.id)
|
|
202
|
-
} else {
|
|
203
|
-
url = resolved.id
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (isExternalUrl(url)) {
|
|
207
|
-
return [url, url]
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// if the resolved id is not a valid browser import specifier,
|
|
211
|
-
// prefix it to make it valid. We will strip this before feeding it
|
|
212
|
-
// back into the transform pipeline
|
|
213
|
-
if (!url.startsWith('.') && !url.startsWith('/')) {
|
|
214
|
-
url =
|
|
215
|
-
VALID_ID_PREFIX + resolved.id.replace('\0', NULL_BYTE_PLACEHOLDER)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// make the URL browser-valid if not SSR
|
|
219
|
-
if (!ssr) {
|
|
220
|
-
// mark non-js/css imports with `?import`
|
|
221
|
-
url = markExplicitImport(url)
|
|
222
|
-
|
|
223
|
-
// for relative js/css imports, or self-module virtual imports
|
|
224
|
-
// (e.g. vue blocks), inherit importer's version query
|
|
225
|
-
// do not do this for unknown type imports, otherwise the appended
|
|
226
|
-
// query can break 3rd party plugin's extension checks.
|
|
227
|
-
if ((isRelative || isSelfImport) && !/[\?&]import=?\b/.test(url)) {
|
|
228
|
-
const versionMatch = importer.match(DEP_VERSION_RE)
|
|
229
|
-
if (versionMatch) {
|
|
230
|
-
url = injectQuery(url, versionMatch[1])
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// check if the dep has been hmr updated. If yes, we need to attach
|
|
235
|
-
// its last updated timestamp to force the browser to fetch the most
|
|
236
|
-
// up-to-date version of this module.
|
|
237
|
-
try {
|
|
238
|
-
const depModule = await moduleGraph.ensureEntryFromUrl(url)
|
|
239
|
-
if (depModule.lastHMRTimestamp > 0) {
|
|
240
|
-
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)
|
|
241
|
-
}
|
|
242
|
-
} catch (e: any) {
|
|
243
|
-
// it's possible that the dep fails to resolve (non-existent import)
|
|
244
|
-
// attach location to the missing import
|
|
245
|
-
e.pos = pos
|
|
246
|
-
throw e
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// prepend base (dev base is guaranteed to have ending slash)
|
|
250
|
-
url = base + url.replace(/^\//, '')
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
return [url, resolved.id]
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
for (let index = 0; index < imports.length; index++) {
|
|
257
|
-
const {
|
|
258
|
-
s: start,
|
|
259
|
-
e: end,
|
|
260
|
-
ss: expStart,
|
|
261
|
-
se: expEnd,
|
|
262
|
-
d: dynamicIndex,
|
|
263
|
-
// #2083 User may use escape path,
|
|
264
|
-
// so use imports[index].n to get the unescaped string
|
|
265
|
-
// @ts-ignore
|
|
266
|
-
n: specifier
|
|
267
|
-
} = imports[index]
|
|
268
|
-
|
|
269
|
-
const rawUrl = source.slice(start, end)
|
|
270
|
-
|
|
271
|
-
// check import.meta usage
|
|
272
|
-
if (rawUrl === 'import.meta') {
|
|
273
|
-
const prop = source.slice(end, end + 4)
|
|
274
|
-
if (prop === '.hot') {
|
|
275
|
-
hasHMR = true
|
|
276
|
-
if (source.slice(end + 4, end + 11) === '.accept') {
|
|
277
|
-
// further analyze accepted modules
|
|
278
|
-
if (
|
|
279
|
-
lexAcceptedHmrDeps(
|
|
280
|
-
source,
|
|
281
|
-
source.indexOf('(', end + 11) + 1,
|
|
282
|
-
acceptedUrls
|
|
283
|
-
)
|
|
284
|
-
) {
|
|
285
|
-
isSelfAccepting = true
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
} else if (prop === '.env') {
|
|
289
|
-
hasEnv = true
|
|
290
|
-
} else if (prop === '.glo' && source[end + 4] === 'b') {
|
|
291
|
-
// transform import.meta.glob()
|
|
292
|
-
// e.g. `import.meta.glob('glob:./dir/*.js')`
|
|
293
|
-
const { imports, importsString, exp, endIndex, base, pattern } =
|
|
294
|
-
await transformImportGlob(
|
|
295
|
-
source,
|
|
296
|
-
start,
|
|
297
|
-
importer,
|
|
298
|
-
index,
|
|
299
|
-
root,
|
|
300
|
-
normalizeUrl
|
|
301
|
-
)
|
|
302
|
-
str().prepend(importsString)
|
|
303
|
-
str().overwrite(expStart, endIndex, exp)
|
|
304
|
-
imports.forEach((url) => importedUrls.add(url.replace(base, '/')))
|
|
305
|
-
if (!(importerModule.file! in server._globImporters)) {
|
|
306
|
-
server._globImporters[importerModule.file!] = {
|
|
307
|
-
module: importerModule,
|
|
308
|
-
importGlobs: []
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
server._globImporters[importerModule.file!].importGlobs.push({
|
|
312
|
-
base,
|
|
313
|
-
pattern
|
|
314
|
-
})
|
|
315
|
-
}
|
|
316
|
-
continue
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
const isDynamicImport = dynamicIndex >= 0
|
|
320
|
-
|
|
321
|
-
// static import or valid string in dynamic import
|
|
322
|
-
// If resolvable, let's resolve it
|
|
323
|
-
if (specifier) {
|
|
324
|
-
// skip external / data uri
|
|
325
|
-
if (isExternalUrl(specifier) || isDataUrl(specifier)) {
|
|
326
|
-
continue
|
|
327
|
-
}
|
|
328
|
-
// skip ssr external
|
|
329
|
-
if (ssr) {
|
|
330
|
-
if (
|
|
331
|
-
server._ssrExternals &&
|
|
332
|
-
shouldExternalizeForSSR(specifier, server._ssrExternals)
|
|
333
|
-
) {
|
|
334
|
-
continue
|
|
335
|
-
}
|
|
336
|
-
if (isBuiltin(specifier)) {
|
|
337
|
-
continue
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
// skip client
|
|
341
|
-
if (specifier === clientPublicPath) {
|
|
342
|
-
continue
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// warn imports to non-asset /public files
|
|
346
|
-
if (
|
|
347
|
-
specifier.startsWith('/') &&
|
|
348
|
-
!config.assetsInclude(cleanUrl(specifier)) &&
|
|
349
|
-
!specifier.endsWith('.json') &&
|
|
350
|
-
checkPublicFile(specifier, config)
|
|
351
|
-
) {
|
|
352
|
-
throw new Error(
|
|
353
|
-
`Cannot import non-asset file ${specifier} which is inside /public.` +
|
|
354
|
-
`JS/CSS files inside /public are copied as-is on build and ` +
|
|
355
|
-
`can only be referenced via <script src> or <link href> in html.`
|
|
356
|
-
)
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// normalize
|
|
360
|
-
const [normalizedUrl, resolvedId] = await normalizeUrl(
|
|
361
|
-
specifier,
|
|
362
|
-
start
|
|
363
|
-
)
|
|
364
|
-
let url = normalizedUrl
|
|
365
|
-
|
|
366
|
-
// record as safe modules
|
|
367
|
-
server?.moduleGraph.safeModulesPath.add(
|
|
368
|
-
cleanUrl(url).slice(4 /* '/@fs'.length */)
|
|
369
|
-
)
|
|
370
|
-
|
|
371
|
-
// rewrite
|
|
372
|
-
if (url !== specifier) {
|
|
373
|
-
// for optimized cjs deps, support named imports by rewriting named
|
|
374
|
-
// imports to const assignments.
|
|
375
|
-
if (resolvedId.endsWith(`&es-interop`)) {
|
|
376
|
-
url = url.slice(0, -11)
|
|
377
|
-
if (isDynamicImport) {
|
|
378
|
-
// rewrite `import('package')` to expose the default directly
|
|
379
|
-
str().overwrite(
|
|
380
|
-
dynamicIndex,
|
|
381
|
-
end + 1,
|
|
382
|
-
`import('${url}').then(m => m.default && m.default.__esModule ? m.default : ({ ...m.default, default: m.default }))`
|
|
383
|
-
)
|
|
384
|
-
} else {
|
|
385
|
-
const exp = source.slice(expStart, expEnd)
|
|
386
|
-
const rewritten = transformCjsImport(exp, url, rawUrl, index)
|
|
387
|
-
if (rewritten) {
|
|
388
|
-
str().overwrite(expStart, expEnd, rewritten)
|
|
389
|
-
} else {
|
|
390
|
-
// #1439 export * from '...'
|
|
391
|
-
str().overwrite(start, end, url)
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
} else {
|
|
395
|
-
str().overwrite(start, end, isDynamicImport ? `'${url}'` : url)
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// record for HMR import chain analysis
|
|
400
|
-
// make sure to normalize away base
|
|
401
|
-
importedUrls.add(url.replace(base, '/'))
|
|
402
|
-
} else if (!importer.startsWith(clientDir) && !ssr) {
|
|
403
|
-
// check @vite-ignore which suppresses dynamic import warning
|
|
404
|
-
const hasViteIgnore = /\/\*\s*@vite-ignore\s*\*\//.test(rawUrl)
|
|
405
|
-
|
|
406
|
-
const url = rawUrl
|
|
407
|
-
.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, '')
|
|
408
|
-
.trim()
|
|
409
|
-
if (!hasViteIgnore && !isSupportedDynamicImport(url)) {
|
|
410
|
-
this.warn(
|
|
411
|
-
`\n` +
|
|
412
|
-
chalk.cyan(importerModule.file) +
|
|
413
|
-
`\n` +
|
|
414
|
-
generateCodeFrame(source, start) +
|
|
415
|
-
`\nThe above dynamic import cannot be analyzed by vite.\n` +
|
|
416
|
-
`See ${chalk.blue(
|
|
417
|
-
`https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`
|
|
418
|
-
)} ` +
|
|
419
|
-
`for supported dynamic import formats. ` +
|
|
420
|
-
`If this is intended to be left as-is, you can use the ` +
|
|
421
|
-
`/* @vite-ignore */ comment inside the import() call to suppress this warning.\n`
|
|
422
|
-
)
|
|
423
|
-
}
|
|
424
|
-
if (
|
|
425
|
-
!/^('.*'|".*"|`.*`)$/.test(url) ||
|
|
426
|
-
isExplicitImportRequired(url.slice(1, -1))
|
|
427
|
-
) {
|
|
428
|
-
needQueryInjectHelper = true
|
|
429
|
-
str().overwrite(start, end, `__vite__injectQuery(${url}, 'import')`)
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
if (hasEnv) {
|
|
435
|
-
// inject import.meta.env
|
|
436
|
-
let env = `import.meta.env = ${JSON.stringify({
|
|
437
|
-
...config.env,
|
|
438
|
-
SSR: !!ssr
|
|
439
|
-
})};`
|
|
440
|
-
// account for user env defines
|
|
441
|
-
for (const key in config.define) {
|
|
442
|
-
if (key.startsWith(`import.meta.env.`)) {
|
|
443
|
-
const val = config.define[key]
|
|
444
|
-
env += `${key} = ${
|
|
445
|
-
typeof val === 'string' ? val : JSON.stringify(val)
|
|
446
|
-
};`
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
str().prepend(env)
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
if (hasHMR && !ssr) {
|
|
453
|
-
debugHmr(
|
|
454
|
-
`${
|
|
455
|
-
isSelfAccepting
|
|
456
|
-
? `[self-accepts]`
|
|
457
|
-
: acceptedUrls.size
|
|
458
|
-
? `[accepts-deps]`
|
|
459
|
-
: `[detected api usage]`
|
|
460
|
-
} ${prettyImporter}`
|
|
461
|
-
)
|
|
462
|
-
// inject hot context
|
|
463
|
-
str().prepend(
|
|
464
|
-
`import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";` +
|
|
465
|
-
`import.meta.hot = __vite__createHotContext(${JSON.stringify(
|
|
466
|
-
importerModule.url
|
|
467
|
-
)});`
|
|
468
|
-
)
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
if (needQueryInjectHelper) {
|
|
472
|
-
str().prepend(
|
|
473
|
-
`import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`
|
|
474
|
-
)
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// normalize and rewrite accepted urls
|
|
478
|
-
const normalizedAcceptedUrls = new Set<string>()
|
|
479
|
-
for (const { url, start, end } of acceptedUrls) {
|
|
480
|
-
const [normalized] = await moduleGraph.resolveUrl(
|
|
481
|
-
toAbsoluteUrl(markExplicitImport(url))
|
|
482
|
-
)
|
|
483
|
-
normalizedAcceptedUrls.add(normalized)
|
|
484
|
-
str().overwrite(start, end, JSON.stringify(normalized))
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// update the module graph for HMR analysis.
|
|
488
|
-
// node CSS imports does its own graph update in the css plugin so we
|
|
489
|
-
// only handle js graph updates here.
|
|
490
|
-
if (!isCSSRequest(importer)) {
|
|
491
|
-
// attached by pluginContainer.addWatchFile
|
|
492
|
-
const pluginImports = (this as any)._addedImports as
|
|
493
|
-
| Set<string>
|
|
494
|
-
| undefined
|
|
495
|
-
if (pluginImports) {
|
|
496
|
-
;(
|
|
497
|
-
await Promise.all(
|
|
498
|
-
[...pluginImports].map((id) => normalizeUrl(id, 0))
|
|
499
|
-
)
|
|
500
|
-
).forEach(([url]) => importedUrls.add(url))
|
|
501
|
-
}
|
|
502
|
-
const prunedImports = await moduleGraph.updateModuleInfo(
|
|
503
|
-
importerModule,
|
|
504
|
-
importedUrls,
|
|
505
|
-
normalizedAcceptedUrls,
|
|
506
|
-
isSelfAccepting
|
|
507
|
-
)
|
|
508
|
-
if (hasHMR && prunedImports) {
|
|
509
|
-
handlePrunedModules(prunedImports, server)
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
if (s) {
|
|
514
|
-
return s.toString()
|
|
515
|
-
} else {
|
|
516
|
-
return source
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
/**
|
|
523
|
-
* https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations
|
|
524
|
-
* This is probably less accurate but is much cheaper than a full AST parse.
|
|
525
|
-
*/
|
|
526
|
-
function isSupportedDynamicImport(url: string) {
|
|
527
|
-
url = url.trim().slice(1, -1)
|
|
528
|
-
// must be relative
|
|
529
|
-
if (!url.startsWith('./') && !url.startsWith('../')) {
|
|
530
|
-
return false
|
|
531
|
-
}
|
|
532
|
-
// must have extension
|
|
533
|
-
if (!path.extname(url)) {
|
|
534
|
-
return false
|
|
535
|
-
}
|
|
536
|
-
// must be more specific if importing from same dir
|
|
537
|
-
if (url.startsWith('./${') && url.indexOf('/') === url.lastIndexOf('/')) {
|
|
538
|
-
return false
|
|
539
|
-
}
|
|
540
|
-
return true
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
type ImportNameSpecifier = { importedName: string; localName: string }
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* Detect import statements to a known optimized CJS dependency and provide
|
|
547
|
-
* ES named imports interop. We do this by rewriting named imports to a variable
|
|
548
|
-
* assignment to the corresponding property on the `module.exports` of the cjs
|
|
549
|
-
* module. Note this doesn't support dynamic re-assignments from within the cjs
|
|
550
|
-
* module.
|
|
551
|
-
*
|
|
552
|
-
* Note that es-module-lexer treats `export * from '...'` as an import as well,
|
|
553
|
-
* so, we may encounter ExportAllDeclaration here, in which case `undefined`
|
|
554
|
-
* will be returned.
|
|
555
|
-
*
|
|
556
|
-
* Credits \@csr632 via #837
|
|
557
|
-
*/
|
|
558
|
-
function transformCjsImport(
|
|
559
|
-
importExp: string,
|
|
560
|
-
url: string,
|
|
561
|
-
rawUrl: string,
|
|
562
|
-
importIndex: number
|
|
563
|
-
): string | undefined {
|
|
564
|
-
const node = (
|
|
565
|
-
parseJS(importExp, {
|
|
566
|
-
ecmaVersion: 2020,
|
|
567
|
-
sourceType: 'module'
|
|
568
|
-
}) as any
|
|
569
|
-
).body[0] as Node
|
|
570
|
-
|
|
571
|
-
if (node.type === 'ImportDeclaration') {
|
|
572
|
-
if (!node.specifiers.length) {
|
|
573
|
-
return `import "${url}"`
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
const importNames: ImportNameSpecifier[] = []
|
|
577
|
-
for (const spec of node.specifiers) {
|
|
578
|
-
if (
|
|
579
|
-
spec.type === 'ImportSpecifier' &&
|
|
580
|
-
spec.imported.type === 'Identifier'
|
|
581
|
-
) {
|
|
582
|
-
const importedName = spec.imported.name
|
|
583
|
-
const localName = spec.local.name
|
|
584
|
-
importNames.push({ importedName, localName })
|
|
585
|
-
} else if (spec.type === 'ImportDefaultSpecifier') {
|
|
586
|
-
importNames.push({
|
|
587
|
-
importedName: 'default',
|
|
588
|
-
localName: spec.local.name
|
|
589
|
-
})
|
|
590
|
-
} else if (spec.type === 'ImportNamespaceSpecifier') {
|
|
591
|
-
importNames.push({ importedName: '*', localName: spec.local.name })
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
// If there is multiple import for same id in one file,
|
|
596
|
-
// importIndex will prevent the cjsModuleName to be duplicate
|
|
597
|
-
const cjsModuleName = makeLegalIdentifier(
|
|
598
|
-
`__vite__cjsImport${importIndex}_${rawUrl}`
|
|
599
|
-
)
|
|
600
|
-
const lines: string[] = [`import ${cjsModuleName} from "${url}"`]
|
|
601
|
-
importNames.forEach(({ importedName, localName }) => {
|
|
602
|
-
if (importedName === '*') {
|
|
603
|
-
lines.push(`const ${localName} = ${cjsModuleName}`)
|
|
604
|
-
} else if (importedName === 'default') {
|
|
605
|
-
lines.push(
|
|
606
|
-
`const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`
|
|
607
|
-
)
|
|
608
|
-
} else {
|
|
609
|
-
lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"]`)
|
|
610
|
-
}
|
|
611
|
-
})
|
|
612
|
-
return lines.join('; ')
|
|
613
|
-
}
|
|
614
|
-
}
|