Skip to content

Commit

Permalink
feat: use filter for plugins (#49) (#50) (#51) (#52) (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: IWANABETHATGUY <iwanabethatguy@qq.com>
  • Loading branch information
2 people authored and underfin committed Oct 11, 2024
1 parent c010293 commit 2ab8e25
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 deletions.
57 changes: 36 additions & 21 deletions packages/vite/src/node/plugins/assetImportMetaUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'node:path'
import MagicString from 'magic-string'
import { stripLiteral } from 'strip-literal'
import { parseAst } from 'rollup/parseAst'
import type { Plugin } from '../plugin'
import type { RolldownPlugin } from 'rolldown'
import type { ResolvedConfig } from '../config'
import {
injectQuery,
Expand Down Expand Up @@ -76,31 +76,46 @@ export function assetImportMetaUrlPlugin(

if (!s) s = new MagicString(code)

// potential dynamic template string
if (rawUrl[0] === '`' && rawUrl.includes('${')) {
const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl)
const hasQueryDelimiter = queryDelimiterIndex !== -1
const pureUrl = hasQueryDelimiter
? rawUrl.slice(0, queryDelimiterIndex) + '`'
: rawUrl
const queryString = hasQueryDelimiter
? rawUrl.slice(queryDelimiterIndex, -1)
: ''
const ast = parseAst(pureUrl)
const templateLiteral = (ast as any).body[0].expression
if (templateLiteral.expressions.length) {
const pattern = buildGlobPattern(templateLiteral)
if (pattern.startsWith('**')) {
// don't transform for patterns like this
// because users won't intend to do that in most cases
// potential dynamic template string
if (rawUrl[0] === '`' && rawUrl.includes('${')) {
const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl)
const hasQueryDelimiter = queryDelimiterIndex !== -1
const pureUrl = hasQueryDelimiter
? rawUrl.slice(0, queryDelimiterIndex) + '`'
: rawUrl
const queryString = hasQueryDelimiter
? rawUrl.slice(queryDelimiterIndex, -1)
: ''
const ast = parseAst(pureUrl)
const templateLiteral = (ast as any).body[0].expression
if (templateLiteral.expressions.length) {
const pattern = buildGlobPattern(templateLiteral)
if (pattern.startsWith('**')) {
// don't transform for patterns like this
// because users won't intend to do that in most cases
continue
}

const globOptions = {
eager: true,
import: 'default',
// A hack to allow 'as' & 'query' exist at the same time
query: injectQuery(queryString, 'url'),
}
s.update(
startIndex,
endIndex,
`new URL((import.meta.glob(${JSON.stringify(
pattern,
)}, ${JSON.stringify(
globOptions,
)}))[${pureUrl}], import.meta.url)`,
)
continue
}
}

const url = rawUrl.slice(1, -1)
if (isDataUrl(url)) {
continue
}
let file: string | undefined
if (url[0] === '.') {
file = slash(path.resolve(path.dirname(id), url))
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,7 @@ export function cssPostPlugin(config: ResolvedConfig): RolldownPlugin {
code = `export default ${JSON.stringify(content)}`
} else {
// empty module when it's not a CSS module nor `?inline`
// NOTE: add `export {}` otherwise rolldown treats the module as CJS (https://github.com/rolldown/rolldown/issues/2394)
code = 'export {}'
code = ''
}

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): RolldownPlugin {
}
}

processedHtml(this).set(id, s.toString())
processedHtml.set(id, s.toString())

// inject module preload polyfill only when configured and needed
const { modulePreload } = this.environment.config.build
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import MagicString from 'magic-string'
import type { OutputChunk } from 'rolldown'
import type { OutputChunk, RolldownPlugin } from 'rolldown'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import { ENV_ENTRY, ENV_PUBLIC_PATH } from '../constants'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/workerImportMetaUrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path'
import MagicString from 'magic-string'
import type { RollupError } from 'rolldown'
import type { RolldownPlugin, RollupError } from 'rolldown'
import { stripLiteral } from 'strip-literal'
import type { ResolvedConfig } from '../config'
import { evalValue, injectQuery, transformStableResult } from '../utils'
Expand Down

0 comments on commit 2ab8e25

Please sign in to comment.