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,102 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
import { optimizeDeps } from '.'
|
|
3
|
-
import { ViteDevServer } from '..'
|
|
4
|
-
import { resolveSSRExternal } from '../ssr/ssrExternal'
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The amount to wait for requests to register newly found dependencies before triggering
|
|
8
|
-
* a re-bundle + page reload
|
|
9
|
-
*/
|
|
10
|
-
const debounceMs = 100
|
|
11
|
-
|
|
12
|
-
export function createMissingImporterRegisterFn(
|
|
13
|
-
server: ViteDevServer
|
|
14
|
-
): (id: string, resolved: string, ssr?: boolean) => void {
|
|
15
|
-
const { logger } = server.config
|
|
16
|
-
let knownOptimized = server._optimizeDepsMetadata!.optimized
|
|
17
|
-
let currentMissing: Record<string, string> = {}
|
|
18
|
-
let handle: NodeJS.Timeout
|
|
19
|
-
|
|
20
|
-
let pendingResolve: (() => void) | null = null
|
|
21
|
-
|
|
22
|
-
async function rerun(ssr: boolean | undefined) {
|
|
23
|
-
const newDeps = currentMissing
|
|
24
|
-
currentMissing = {}
|
|
25
|
-
|
|
26
|
-
logger.info(
|
|
27
|
-
chalk.yellow(
|
|
28
|
-
`new dependencies found: ${Object.keys(newDeps).join(
|
|
29
|
-
', '
|
|
30
|
-
)}, updating...`
|
|
31
|
-
),
|
|
32
|
-
{
|
|
33
|
-
timestamp: true
|
|
34
|
-
}
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
for (const id in knownOptimized) {
|
|
38
|
-
newDeps[id] = knownOptimized[id].src
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
// Nullify previous metadata so that the resolver won't
|
|
43
|
-
// resolve to optimized files during the optimizer re-run
|
|
44
|
-
server._isRunningOptimizer = true
|
|
45
|
-
server._optimizeDepsMetadata = null
|
|
46
|
-
|
|
47
|
-
const newData = (server._optimizeDepsMetadata = await optimizeDeps(
|
|
48
|
-
server.config,
|
|
49
|
-
true,
|
|
50
|
-
false,
|
|
51
|
-
newDeps,
|
|
52
|
-
ssr
|
|
53
|
-
))
|
|
54
|
-
knownOptimized = newData!.optimized
|
|
55
|
-
|
|
56
|
-
// update ssr externals
|
|
57
|
-
server._ssrExternals = resolveSSRExternal(
|
|
58
|
-
server.config,
|
|
59
|
-
Object.keys(knownOptimized)
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
logger.info(
|
|
63
|
-
chalk.greenBright(`✨ dependencies updated, reloading page...`),
|
|
64
|
-
{ timestamp: true }
|
|
65
|
-
)
|
|
66
|
-
} catch (e) {
|
|
67
|
-
logger.error(
|
|
68
|
-
chalk.red(`error while updating dependencies:\n${e.stack}`),
|
|
69
|
-
{ timestamp: true, error: e }
|
|
70
|
-
)
|
|
71
|
-
} finally {
|
|
72
|
-
server._isRunningOptimizer = false
|
|
73
|
-
pendingResolve && pendingResolve()
|
|
74
|
-
server._pendingReload = pendingResolve = null
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Cached transform results have stale imports (resolved to
|
|
78
|
-
// old locations) so they need to be invalidated before the page is
|
|
79
|
-
// reloaded.
|
|
80
|
-
server.moduleGraph.invalidateAll()
|
|
81
|
-
|
|
82
|
-
server.ws.send({
|
|
83
|
-
type: 'full-reload',
|
|
84
|
-
path: '*'
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return function registerMissingImport(
|
|
89
|
-
id: string,
|
|
90
|
-
resolved: string,
|
|
91
|
-
ssr?: boolean
|
|
92
|
-
) {
|
|
93
|
-
if (!knownOptimized[id]) {
|
|
94
|
-
currentMissing[id] = resolved
|
|
95
|
-
if (handle) clearTimeout(handle)
|
|
96
|
-
handle = setTimeout(() => rerun(ssr), debounceMs)
|
|
97
|
-
server._pendingReload = new Promise((r) => {
|
|
98
|
-
pendingResolve = r
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
@@ -1,457 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import glob from 'fast-glob'
|
|
4
|
-
import { ResolvedConfig } from '..'
|
|
5
|
-
import { Loader, Plugin, build, transform } from 'esbuild'
|
|
6
|
-
import {
|
|
7
|
-
KNOWN_ASSET_TYPES,
|
|
8
|
-
JS_TYPES_RE,
|
|
9
|
-
SPECIAL_QUERY_RE,
|
|
10
|
-
OPTIMIZABLE_ENTRY_RE
|
|
11
|
-
} from '../constants'
|
|
12
|
-
import {
|
|
13
|
-
createDebugger,
|
|
14
|
-
normalizePath,
|
|
15
|
-
isObject,
|
|
16
|
-
cleanUrl,
|
|
17
|
-
externalRE,
|
|
18
|
-
dataUrlRE,
|
|
19
|
-
multilineCommentsRE,
|
|
20
|
-
singlelineCommentsRE
|
|
21
|
-
} from '../utils'
|
|
22
|
-
import {
|
|
23
|
-
createPluginContainer,
|
|
24
|
-
PluginContainer
|
|
25
|
-
} from '../server/pluginContainer'
|
|
26
|
-
import { init, parse } from 'es-module-lexer'
|
|
27
|
-
import MagicString from 'magic-string'
|
|
28
|
-
import { transformImportGlob } from '../importGlob'
|
|
29
|
-
|
|
30
|
-
const debug = createDebugger('vite:deps')
|
|
31
|
-
|
|
32
|
-
const htmlTypesRE = /\.(html|vue|svelte)$/
|
|
33
|
-
|
|
34
|
-
// A simple regex to detect import sources. This is only used on
|
|
35
|
-
// <script lang="ts"> blocks in vue (setup only) or svelte files, since
|
|
36
|
-
// seemingly unused imports are dropped by esbuild when transpiling TS which
|
|
37
|
-
// prevents it from crawling further.
|
|
38
|
-
// We can't use es-module-lexer because it can't handle TS, and don't want to
|
|
39
|
-
// use Acorn because it's slow. Luckily this doesn't have to be bullet proof
|
|
40
|
-
// since even missed imports can be caught at runtime, and false positives will
|
|
41
|
-
// simply be ignored.
|
|
42
|
-
export const importsRE =
|
|
43
|
-
/(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from\s*)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm
|
|
44
|
-
|
|
45
|
-
export async function scanImports(config: ResolvedConfig): Promise<{
|
|
46
|
-
deps: Record<string, string>
|
|
47
|
-
missing: Record<string, string>
|
|
48
|
-
}> {
|
|
49
|
-
const s = Date.now()
|
|
50
|
-
|
|
51
|
-
let entries: string[] = []
|
|
52
|
-
|
|
53
|
-
const explicitEntryPatterns = config.optimizeDeps?.entries
|
|
54
|
-
const buildInput = config.build.rollupOptions?.input
|
|
55
|
-
|
|
56
|
-
if (explicitEntryPatterns) {
|
|
57
|
-
entries = await globEntries(explicitEntryPatterns, config)
|
|
58
|
-
} else if (buildInput) {
|
|
59
|
-
const resolvePath = (p: string) => path.resolve(config.root, p)
|
|
60
|
-
if (typeof buildInput === 'string') {
|
|
61
|
-
entries = [resolvePath(buildInput)]
|
|
62
|
-
} else if (Array.isArray(buildInput)) {
|
|
63
|
-
entries = buildInput.map(resolvePath)
|
|
64
|
-
} else if (isObject(buildInput)) {
|
|
65
|
-
entries = Object.values(buildInput).map(resolvePath)
|
|
66
|
-
} else {
|
|
67
|
-
throw new Error('invalid rollupOptions.input value.')
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
entries = await globEntries('**/*.html', config)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// Non-supported entry file types and virtual files should not be scanned for
|
|
74
|
-
// dependencies.
|
|
75
|
-
entries = entries.filter(
|
|
76
|
-
(entry) =>
|
|
77
|
-
(JS_TYPES_RE.test(entry) || htmlTypesRE.test(entry)) &&
|
|
78
|
-
fs.existsSync(entry)
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
if (!entries.length) {
|
|
82
|
-
config.logger.warn(
|
|
83
|
-
'Could not determine entry point from rollupOptions or html files. Skipping dependency pre-bundling.'
|
|
84
|
-
)
|
|
85
|
-
return { deps: {}, missing: {} }
|
|
86
|
-
} else {
|
|
87
|
-
debug(`Crawling dependencies using entries:\n ${entries.join('\n ')}`)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const deps: Record<string, string> = {}
|
|
91
|
-
const missing: Record<string, string> = {}
|
|
92
|
-
const container = await createPluginContainer(config)
|
|
93
|
-
const plugin = esbuildScanPlugin(config, container, deps, missing, entries)
|
|
94
|
-
|
|
95
|
-
const { plugins = [], ...esbuildOptions } =
|
|
96
|
-
config.optimizeDeps?.esbuildOptions ?? {}
|
|
97
|
-
|
|
98
|
-
await Promise.all(
|
|
99
|
-
entries.map((entry) =>
|
|
100
|
-
build({
|
|
101
|
-
absWorkingDir: process.cwd(),
|
|
102
|
-
write: false,
|
|
103
|
-
entryPoints: [entry],
|
|
104
|
-
bundle: true,
|
|
105
|
-
format: 'esm',
|
|
106
|
-
logLevel: 'error',
|
|
107
|
-
plugins: [...plugins, plugin],
|
|
108
|
-
...esbuildOptions
|
|
109
|
-
})
|
|
110
|
-
)
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
debug(`Scan completed in ${Date.now() - s}ms:`, deps)
|
|
114
|
-
|
|
115
|
-
return {
|
|
116
|
-
deps,
|
|
117
|
-
missing
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function globEntries(pattern: string | string[], config: ResolvedConfig) {
|
|
122
|
-
return glob(pattern, {
|
|
123
|
-
cwd: config.root,
|
|
124
|
-
ignore: [
|
|
125
|
-
'**/node_modules/**',
|
|
126
|
-
`**/${config.build.outDir}/**`,
|
|
127
|
-
`**/__tests__/**`
|
|
128
|
-
],
|
|
129
|
-
absolute: true
|
|
130
|
-
})
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const scriptModuleRE =
|
|
134
|
-
/(<script\b[^>]*type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gims
|
|
135
|
-
export const scriptRE = /(<script\b(?:\s[^>]*>|>))(.*?)<\/script>/gims
|
|
136
|
-
export const commentRE = /<!--(.|[\r\n])*?-->/
|
|
137
|
-
const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
|
|
138
|
-
const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
|
|
139
|
-
const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/im
|
|
140
|
-
|
|
141
|
-
function esbuildScanPlugin(
|
|
142
|
-
config: ResolvedConfig,
|
|
143
|
-
container: PluginContainer,
|
|
144
|
-
depImports: Record<string, string>,
|
|
145
|
-
missing: Record<string, string>,
|
|
146
|
-
entries: string[]
|
|
147
|
-
): Plugin {
|
|
148
|
-
const seen = new Map<string, string | undefined>()
|
|
149
|
-
|
|
150
|
-
const resolve = async (id: string, importer?: string) => {
|
|
151
|
-
const key = id + (importer && path.dirname(importer))
|
|
152
|
-
if (seen.has(key)) {
|
|
153
|
-
return seen.get(key)
|
|
154
|
-
}
|
|
155
|
-
const resolved = await container.resolveId(
|
|
156
|
-
id,
|
|
157
|
-
importer && normalizePath(importer)
|
|
158
|
-
)
|
|
159
|
-
const res = resolved?.id
|
|
160
|
-
seen.set(key, res)
|
|
161
|
-
return res
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const include = config.optimizeDeps?.include
|
|
165
|
-
const exclude = [
|
|
166
|
-
...(config.optimizeDeps?.exclude || []),
|
|
167
|
-
'@vite/client',
|
|
168
|
-
'@vite/env'
|
|
169
|
-
]
|
|
170
|
-
|
|
171
|
-
const externalUnlessEntry = ({ path }: { path: string }) => ({
|
|
172
|
-
path,
|
|
173
|
-
external: !entries.includes(path)
|
|
174
|
-
})
|
|
175
|
-
|
|
176
|
-
return {
|
|
177
|
-
name: 'vite:dep-scan',
|
|
178
|
-
setup(build) {
|
|
179
|
-
// external urls
|
|
180
|
-
build.onResolve({ filter: externalRE }, ({ path }) => ({
|
|
181
|
-
path,
|
|
182
|
-
external: true
|
|
183
|
-
}))
|
|
184
|
-
|
|
185
|
-
// data urls
|
|
186
|
-
build.onResolve({ filter: dataUrlRE }, ({ path }) => ({
|
|
187
|
-
path,
|
|
188
|
-
external: true
|
|
189
|
-
}))
|
|
190
|
-
|
|
191
|
-
// html types: extract script contents -----------------------------------
|
|
192
|
-
build.onResolve({ filter: htmlTypesRE }, async ({ path, importer }) => {
|
|
193
|
-
return {
|
|
194
|
-
path: await resolve(path, importer),
|
|
195
|
-
namespace: 'html'
|
|
196
|
-
}
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
// extract scripts inside HTML-like files and treat it as a js module
|
|
200
|
-
build.onLoad(
|
|
201
|
-
{ filter: htmlTypesRE, namespace: 'html' },
|
|
202
|
-
async ({ path }) => {
|
|
203
|
-
let raw = fs.readFileSync(path, 'utf-8')
|
|
204
|
-
// Avoid matching the content of the comment
|
|
205
|
-
raw = raw.replace(commentRE, '<!---->')
|
|
206
|
-
const isHtml = path.endsWith('.html')
|
|
207
|
-
const regex = isHtml ? scriptModuleRE : scriptRE
|
|
208
|
-
regex.lastIndex = 0
|
|
209
|
-
let js = ''
|
|
210
|
-
let loader: Loader = 'js'
|
|
211
|
-
let match: RegExpExecArray | null
|
|
212
|
-
while ((match = regex.exec(raw))) {
|
|
213
|
-
const [, openTag, content] = match
|
|
214
|
-
const srcMatch = openTag.match(srcRE)
|
|
215
|
-
const typeMatch = openTag.match(typeRE)
|
|
216
|
-
const langMatch = openTag.match(langRE)
|
|
217
|
-
const type =
|
|
218
|
-
typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3])
|
|
219
|
-
const lang =
|
|
220
|
-
langMatch && (langMatch[1] || langMatch[2] || langMatch[3])
|
|
221
|
-
// skip type="application/ld+json" and other non-JS types
|
|
222
|
-
if (
|
|
223
|
-
type &&
|
|
224
|
-
!(
|
|
225
|
-
type.includes('javascript') ||
|
|
226
|
-
type.includes('ecmascript') ||
|
|
227
|
-
type === 'module'
|
|
228
|
-
)
|
|
229
|
-
) {
|
|
230
|
-
continue
|
|
231
|
-
}
|
|
232
|
-
if (lang === 'ts' || lang === 'tsx' || lang === 'jsx') {
|
|
233
|
-
loader = lang
|
|
234
|
-
}
|
|
235
|
-
if (srcMatch) {
|
|
236
|
-
const src = srcMatch[1] || srcMatch[2] || srcMatch[3]
|
|
237
|
-
js += `import ${JSON.stringify(src)}\n`
|
|
238
|
-
} else if (content.trim()) {
|
|
239
|
-
js += content + '\n'
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
// empty singleline & multiline comments to avoid matching comments
|
|
243
|
-
const code = js
|
|
244
|
-
.replace(multilineCommentsRE, '/* */')
|
|
245
|
-
.replace(singlelineCommentsRE, '')
|
|
246
|
-
|
|
247
|
-
if (
|
|
248
|
-
loader.startsWith('ts') &&
|
|
249
|
-
(path.endsWith('.svelte') ||
|
|
250
|
-
(path.endsWith('.vue') && /<script\s+setup/.test(raw)))
|
|
251
|
-
) {
|
|
252
|
-
// when using TS + (Vue + <script setup>) or Svelte, imports may seem
|
|
253
|
-
// unused to esbuild and dropped in the build output, which prevents
|
|
254
|
-
// esbuild from crawling further.
|
|
255
|
-
// the solution is to add `import 'x'` for every source to force
|
|
256
|
-
// esbuild to keep crawling due to potential side effects.
|
|
257
|
-
let m
|
|
258
|
-
while ((m = importsRE.exec(code)) != null) {
|
|
259
|
-
// This is necessary to avoid infinite loops with zero-width matches
|
|
260
|
-
if (m.index === importsRE.lastIndex) {
|
|
261
|
-
importsRE.lastIndex++
|
|
262
|
-
}
|
|
263
|
-
js += `\nimport ${m[1]}`
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
if (!code.includes(`export default`)) {
|
|
268
|
-
js += `\nexport default {}`
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (code.includes('import.meta.glob')) {
|
|
272
|
-
return {
|
|
273
|
-
// transformGlob already transforms to js
|
|
274
|
-
loader: 'js',
|
|
275
|
-
contents: await transformGlob(js, path, config.root, loader)
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
return {
|
|
280
|
-
loader,
|
|
281
|
-
contents: js
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
// bare imports: record and externalize ----------------------------------
|
|
287
|
-
build.onResolve(
|
|
288
|
-
{
|
|
289
|
-
// avoid matching windows volume
|
|
290
|
-
filter: /^[\w@][^:]/
|
|
291
|
-
},
|
|
292
|
-
async ({ path: id, importer }) => {
|
|
293
|
-
if (exclude?.some((e) => e === id || id.startsWith(e + '/'))) {
|
|
294
|
-
return externalUnlessEntry({ path: id })
|
|
295
|
-
}
|
|
296
|
-
if (depImports[id]) {
|
|
297
|
-
return externalUnlessEntry({ path: id })
|
|
298
|
-
}
|
|
299
|
-
const resolved = await resolve(id, importer)
|
|
300
|
-
if (resolved) {
|
|
301
|
-
if (shouldExternalizeDep(resolved, id)) {
|
|
302
|
-
return externalUnlessEntry({ path: id })
|
|
303
|
-
}
|
|
304
|
-
if (resolved.includes('node_modules') || include?.includes(id)) {
|
|
305
|
-
// dependency or forced included, externalize and stop crawling
|
|
306
|
-
if (OPTIMIZABLE_ENTRY_RE.test(resolved)) {
|
|
307
|
-
depImports[id] = resolved
|
|
308
|
-
}
|
|
309
|
-
return externalUnlessEntry({ path: id })
|
|
310
|
-
} else {
|
|
311
|
-
const namespace = htmlTypesRE.test(resolved) ? 'html' : undefined
|
|
312
|
-
// linked package, keep crawling
|
|
313
|
-
return {
|
|
314
|
-
path: path.resolve(resolved),
|
|
315
|
-
namespace
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
missing[id] = normalizePath(importer)
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
)
|
|
323
|
-
|
|
324
|
-
// Externalized file types -----------------------------------------------
|
|
325
|
-
// these are done on raw ids using esbuild's native regex filter so it
|
|
326
|
-
// should be faster than doing it in the catch-all via js
|
|
327
|
-
// they are done after the bare import resolve because a package name
|
|
328
|
-
// may end with these extensions
|
|
329
|
-
|
|
330
|
-
// css & json
|
|
331
|
-
build.onResolve(
|
|
332
|
-
{
|
|
333
|
-
filter: /\.(css|less|sass|scss|styl|stylus|pcss|postcss|json)$/
|
|
334
|
-
},
|
|
335
|
-
externalUnlessEntry
|
|
336
|
-
)
|
|
337
|
-
|
|
338
|
-
// known asset types
|
|
339
|
-
build.onResolve(
|
|
340
|
-
{
|
|
341
|
-
filter: new RegExp(`\\.(${KNOWN_ASSET_TYPES.join('|')})$`)
|
|
342
|
-
},
|
|
343
|
-
externalUnlessEntry
|
|
344
|
-
)
|
|
345
|
-
|
|
346
|
-
// known vite query types: ?worker, ?raw
|
|
347
|
-
build.onResolve({ filter: SPECIAL_QUERY_RE }, ({ path }) => ({
|
|
348
|
-
path,
|
|
349
|
-
external: true
|
|
350
|
-
}))
|
|
351
|
-
|
|
352
|
-
// catch all -------------------------------------------------------------
|
|
353
|
-
|
|
354
|
-
build.onResolve(
|
|
355
|
-
{
|
|
356
|
-
filter: /.*/
|
|
357
|
-
},
|
|
358
|
-
async ({ path: id, importer }) => {
|
|
359
|
-
// use vite resolver to support urls and omitted extensions
|
|
360
|
-
const resolved = await resolve(id, importer)
|
|
361
|
-
if (resolved) {
|
|
362
|
-
if (shouldExternalizeDep(resolved, id)) {
|
|
363
|
-
return externalUnlessEntry({ path: id })
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
const namespace = htmlTypesRE.test(resolved) ? 'html' : undefined
|
|
367
|
-
|
|
368
|
-
return {
|
|
369
|
-
path: path.resolve(cleanUrl(resolved)),
|
|
370
|
-
namespace
|
|
371
|
-
}
|
|
372
|
-
} else {
|
|
373
|
-
// resolve failed... probably unsupported type
|
|
374
|
-
return externalUnlessEntry({ path: id })
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
)
|
|
378
|
-
|
|
379
|
-
// for jsx/tsx, we need to access the content and check for
|
|
380
|
-
// presence of import.meta.glob, since it results in import relationships
|
|
381
|
-
// but isn't crawled by esbuild.
|
|
382
|
-
build.onLoad({ filter: JS_TYPES_RE }, ({ path: id }) => {
|
|
383
|
-
let ext = path.extname(id).slice(1)
|
|
384
|
-
if (ext === 'mjs') ext = 'js'
|
|
385
|
-
|
|
386
|
-
let contents = fs.readFileSync(id, 'utf-8')
|
|
387
|
-
if (ext.endsWith('x') && config.esbuild && config.esbuild.jsxInject) {
|
|
388
|
-
contents = config.esbuild.jsxInject + `\n` + contents
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
if (contents.includes('import.meta.glob')) {
|
|
392
|
-
return transformGlob(contents, id, config.root, ext as Loader).then(
|
|
393
|
-
(contents) => ({
|
|
394
|
-
loader: ext as Loader,
|
|
395
|
-
contents
|
|
396
|
-
})
|
|
397
|
-
)
|
|
398
|
-
}
|
|
399
|
-
return {
|
|
400
|
-
loader: ext as Loader,
|
|
401
|
-
contents
|
|
402
|
-
}
|
|
403
|
-
})
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
async function transformGlob(
|
|
409
|
-
source: string,
|
|
410
|
-
importer: string,
|
|
411
|
-
root: string,
|
|
412
|
-
loader: Loader
|
|
413
|
-
) {
|
|
414
|
-
// transform the content first since es-module-lexer can't handle non-js
|
|
415
|
-
if (loader !== 'js') {
|
|
416
|
-
source = (await transform(source, { loader })).code
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
await init
|
|
420
|
-
const imports = parse(source)[0]
|
|
421
|
-
const s = new MagicString(source)
|
|
422
|
-
for (let index = 0; index < imports.length; index++) {
|
|
423
|
-
const { s: start, e: end, ss: expStart } = imports[index]
|
|
424
|
-
const url = source.slice(start, end)
|
|
425
|
-
if (url !== 'import.meta') continue
|
|
426
|
-
if (source.slice(end, end + 5) !== '.glob') continue
|
|
427
|
-
const { importsString, exp, endIndex } = await transformImportGlob(
|
|
428
|
-
source,
|
|
429
|
-
start,
|
|
430
|
-
normalizePath(importer),
|
|
431
|
-
index,
|
|
432
|
-
root
|
|
433
|
-
)
|
|
434
|
-
s.prepend(importsString)
|
|
435
|
-
s.overwrite(expStart, endIndex, exp)
|
|
436
|
-
}
|
|
437
|
-
return s.toString()
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export function shouldExternalizeDep(
|
|
441
|
-
resolvedId: string,
|
|
442
|
-
rawId: string
|
|
443
|
-
): boolean {
|
|
444
|
-
// not a valid file path
|
|
445
|
-
if (!path.isAbsolute(resolvedId)) {
|
|
446
|
-
return true
|
|
447
|
-
}
|
|
448
|
-
// virtual id
|
|
449
|
-
if (resolvedId === rawId || resolvedId.includes('\0')) {
|
|
450
|
-
return true
|
|
451
|
-
}
|
|
452
|
-
// resolved is not a scannable type
|
|
453
|
-
if (!JS_TYPES_RE.test(resolvedId) && !htmlTypesRE.test(resolvedId)) {
|
|
454
|
-
return true
|
|
455
|
-
}
|
|
456
|
-
return false
|
|
457
|
-
}
|
package/src/node/plugin.ts
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { UserConfig } from './config'
|
|
2
|
-
import {
|
|
3
|
-
CustomPluginOptions,
|
|
4
|
-
LoadResult,
|
|
5
|
-
Plugin as RollupPlugin,
|
|
6
|
-
PluginContext,
|
|
7
|
-
ResolveIdResult,
|
|
8
|
-
TransformPluginContext,
|
|
9
|
-
TransformResult
|
|
10
|
-
} from 'rollup'
|
|
11
|
-
import { ServerHook } from './server'
|
|
12
|
-
import { IndexHtmlTransform } from './plugins/html'
|
|
13
|
-
import { ModuleNode } from './server/moduleGraph'
|
|
14
|
-
import { ConfigEnv, ResolvedConfig } from './'
|
|
15
|
-
import { HmrContext } from './server/hmr'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Vite plugins extends the Rollup plugin interface with a few extra
|
|
19
|
-
* vite-specific options. A valid vite plugin is also a valid Rollup plugin.
|
|
20
|
-
* On the contrary, a Rollup plugin may or may NOT be a valid vite universal
|
|
21
|
-
* plugin, since some Rollup features do not make sense in an unbundled
|
|
22
|
-
* dev server context. That said, as long as a rollup plugin doesn't have strong
|
|
23
|
-
* coupling between its bundle phase and output phase hooks then it should
|
|
24
|
-
* just work (that means, most of them).
|
|
25
|
-
*
|
|
26
|
-
* By default, the plugins are run during both serve and build. When a plugin
|
|
27
|
-
* is applied during serve, it will only run **non output plugin hooks** (see
|
|
28
|
-
* rollup type definition of {@link rollup#PluginHooks}). You can think of the
|
|
29
|
-
* dev server as only running `const bundle = rollup.rollup()` but never calling
|
|
30
|
-
* `bundle.generate()`.
|
|
31
|
-
*
|
|
32
|
-
* A plugin that expects to have different behavior depending on serve/build can
|
|
33
|
-
* export a factory function that receives the command being run via options.
|
|
34
|
-
*
|
|
35
|
-
* If a plugin should be applied only for server or build, a function format
|
|
36
|
-
* config file can be used to conditional determine the plugins to use.
|
|
37
|
-
*/
|
|
38
|
-
export interface Plugin extends RollupPlugin {
|
|
39
|
-
/**
|
|
40
|
-
* Enforce plugin invocation tier similar to webpack loaders.
|
|
41
|
-
*
|
|
42
|
-
* Plugin invocation order:
|
|
43
|
-
* - alias resolution
|
|
44
|
-
* - `enforce: 'pre'` plugins
|
|
45
|
-
* - vite core plugins
|
|
46
|
-
* - normal plugins
|
|
47
|
-
* - vite build plugins
|
|
48
|
-
* - `enforce: 'post'` plugins
|
|
49
|
-
* - vite build post plugins
|
|
50
|
-
*/
|
|
51
|
-
enforce?: 'pre' | 'post'
|
|
52
|
-
/**
|
|
53
|
-
* Apply the plugin only for serve or build, or on certain conditions.
|
|
54
|
-
*/
|
|
55
|
-
apply?: 'serve' | 'build' | ((config: UserConfig, env: ConfigEnv) => boolean)
|
|
56
|
-
/**
|
|
57
|
-
* Modify vite config before it's resolved. The hook can either mutate the
|
|
58
|
-
* passed-in config directly, or return a partial config object that will be
|
|
59
|
-
* deeply merged into existing config.
|
|
60
|
-
*
|
|
61
|
-
* Note: User plugins are resolved before running this hook so injecting other
|
|
62
|
-
* plugins inside the `config` hook will have no effect.
|
|
63
|
-
*/
|
|
64
|
-
config?: (
|
|
65
|
-
config: UserConfig,
|
|
66
|
-
env: ConfigEnv
|
|
67
|
-
) => UserConfig | null | void | Promise<UserConfig | null | void>
|
|
68
|
-
/**
|
|
69
|
-
* Use this hook to read and store the final resolved vite config.
|
|
70
|
-
*/
|
|
71
|
-
configResolved?: (config: ResolvedConfig) => void | Promise<void>
|
|
72
|
-
/**
|
|
73
|
-
* Configure the vite server. The hook receives the {@link ViteDevServer}
|
|
74
|
-
* instance. This can also be used to store a reference to the server
|
|
75
|
-
* for use in other hooks.
|
|
76
|
-
*
|
|
77
|
-
* The hooks will be called before internal middlewares are applied. A hook
|
|
78
|
-
* can return a post hook that will be called after internal middlewares
|
|
79
|
-
* are applied. Hook can be async functions and will be called in series.
|
|
80
|
-
*/
|
|
81
|
-
configureServer?: ServerHook
|
|
82
|
-
/**
|
|
83
|
-
* Transform index.html.
|
|
84
|
-
* The hook receives the following arguments:
|
|
85
|
-
*
|
|
86
|
-
* - html: string
|
|
87
|
-
* - ctx?: vite.ServerContext (only present during serve)
|
|
88
|
-
* - bundle?: rollup.OutputBundle (only present during build)
|
|
89
|
-
*
|
|
90
|
-
* It can either return a transformed string, or a list of html tag
|
|
91
|
-
* descriptors that will be injected into the <head> or <body>.
|
|
92
|
-
*
|
|
93
|
-
* By default the transform is applied **after** vite's internal html
|
|
94
|
-
* transform. If you need to apply the transform before vite, use an object:
|
|
95
|
-
* `{ enforce: 'pre', transform: hook }`
|
|
96
|
-
*/
|
|
97
|
-
transformIndexHtml?: IndexHtmlTransform
|
|
98
|
-
/**
|
|
99
|
-
* Perform custom handling of HMR updates.
|
|
100
|
-
* The handler receives a context containing changed filename, timestamp, a
|
|
101
|
-
* list of modules affected by the file change, and the dev server instance.
|
|
102
|
-
*
|
|
103
|
-
* - The hook can return a filtered list of modules to narrow down the update.
|
|
104
|
-
* e.g. for a Vue SFC, we can narrow down the part to update by comparing
|
|
105
|
-
* the descriptors.
|
|
106
|
-
*
|
|
107
|
-
* - The hook can also return an empty array and then perform custom updates
|
|
108
|
-
* by sending a custom hmr payload via server.ws.send().
|
|
109
|
-
*
|
|
110
|
-
* - If the hook doesn't return a value, the hmr update will be performed as
|
|
111
|
-
* normal.
|
|
112
|
-
*/
|
|
113
|
-
handleHotUpdate?(
|
|
114
|
-
ctx: HmrContext
|
|
115
|
-
): Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* extend hooks with ssr flag
|
|
119
|
-
*/
|
|
120
|
-
resolveId?(
|
|
121
|
-
this: PluginContext,
|
|
122
|
-
source: string,
|
|
123
|
-
importer: string | undefined,
|
|
124
|
-
options: { custom?: CustomPluginOptions },
|
|
125
|
-
ssr?: boolean
|
|
126
|
-
): Promise<ResolveIdResult> | ResolveIdResult
|
|
127
|
-
load?(
|
|
128
|
-
this: PluginContext,
|
|
129
|
-
id: string,
|
|
130
|
-
ssr?: boolean
|
|
131
|
-
): Promise<LoadResult> | LoadResult
|
|
132
|
-
transform?(
|
|
133
|
-
this: TransformPluginContext,
|
|
134
|
-
code: string,
|
|
135
|
-
id: string,
|
|
136
|
-
ssr?: boolean
|
|
137
|
-
): Promise<TransformResult> | TransformResult
|
|
138
|
-
}
|