Skip to content

Commit 44c6d01

Browse files
authored
fix(css): remove alias exclude logic from rebaseUrl (#20100)
1 parent be78e35 commit 44c6d01

File tree

1 file changed

+16
-70
lines changed
  • packages/vite/src/node/plugins

1 file changed

+16
-70
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import type * as PostCSS from 'postcss'
2121
import type Sass from 'sass'
2222
import type Stylus from 'stylus'
2323
import type Less from 'less'
24-
import type { Alias } from 'dep-types/alias'
2524
import type { LightningCSSOptions } from 'types/internal/lightningcssOptions'
2625
import type { TransformOptions } from 'esbuild'
2726
import { formatMessages, transform } from 'esbuild'
@@ -1277,7 +1276,6 @@ async function compileCSSPreprocessors(
12771276
)
12781277
const opts = {
12791278
...((preprocessorOptions && preprocessorOptions[lang]) || {}),
1280-
alias: config.resolve.alias,
12811279
// important: set this for relative import resolving
12821280
filename: cleanUrl(id),
12831281
enableSourcemap: devSourcemap ?? false,
@@ -2281,7 +2279,6 @@ export type StylusPreprocessorOptions = {
22812279
type StylePreprocessorInternalOptions = {
22822280
maxWorkers?: number | true
22832281
filename: string
2284-
alias: Alias[]
22852282
enableSourcemap: boolean
22862283
}
22872284

@@ -2395,7 +2392,6 @@ function cleanScssBugUrl(url: string) {
23952392
const makeModernScssWorker = (
23962393
environment: PartialEnvironment,
23972394
resolvers: CSSAtImportResolvers,
2398-
alias: Alias[],
23992395
maxWorkers: number | undefined,
24002396
) => {
24012397
const internalCanonicalize = async (
@@ -2422,7 +2418,6 @@ const makeModernScssWorker = (
24222418
environment,
24232419
file,
24242420
rootFile,
2425-
alias,
24262421
resolvers.sass,
24272422
skipRebaseUrls,
24282423
)
@@ -2536,7 +2531,6 @@ const makeModernScssWorker = (
25362531
const makeModernCompilerScssWorker = (
25372532
environment: PartialEnvironment,
25382533
resolvers: CSSAtImportResolvers,
2539-
alias: Alias[],
25402534
_maxWorkers: number | undefined,
25412535
) => {
25422536
let compilerPromise: Promise<Sass.AsyncCompiler> | undefined
@@ -2596,7 +2590,6 @@ const makeModernCompilerScssWorker = (
25962590
environment,
25972591
fileURLToPath(canonicalUrl),
25982592
options.filename,
2599-
alias,
26002593
resolvers.sass,
26012594
skipRebaseUrls,
26022595
)
@@ -2639,44 +2632,25 @@ type ScssWorkerResult = {
26392632
const scssProcessor = (
26402633
maxWorkers: number | undefined,
26412634
): StylePreprocessor<SassStylePreprocessorInternalOptions> => {
2642-
const workerMap = new Map<
2643-
unknown,
2644-
ReturnType<
2645-
typeof makeModernScssWorker | typeof makeModernCompilerScssWorker
2646-
>
2647-
>()
2635+
let worker:
2636+
| ReturnType<
2637+
typeof makeModernScssWorker | typeof makeModernCompilerScssWorker
2638+
>
2639+
| undefined
26482640

26492641
return {
26502642
close() {
2651-
for (const worker of workerMap.values()) {
2652-
worker.stop()
2653-
}
2643+
worker?.stop()
26542644
},
26552645
async process(environment, source, root, options, resolvers) {
26562646
const sassPackage = loadSassPackage(root)
26572647
const api =
26582648
options.api ??
26592649
(sassPackage.name === 'sass-embedded' ? 'modern-compiler' : 'modern')
2660-
2661-
if (!workerMap.has(options.alias)) {
2662-
workerMap.set(
2663-
options.alias,
2664-
api === 'modern-compiler'
2665-
? makeModernCompilerScssWorker(
2666-
environment,
2667-
resolvers,
2668-
options.alias,
2669-
maxWorkers,
2670-
)
2671-
: makeModernScssWorker(
2672-
environment,
2673-
resolvers,
2674-
options.alias,
2675-
maxWorkers,
2676-
),
2677-
)
2678-
}
2679-
const worker = workerMap.get(options.alias)!
2650+
worker ??=
2651+
api === 'modern-compiler'
2652+
? makeModernCompilerScssWorker(environment, resolvers, maxWorkers)
2653+
: makeModernScssWorker(environment, resolvers, maxWorkers)
26802654

26812655
const { content: data, map: additionalMap } = await getSource(
26822656
source,
@@ -2743,7 +2717,6 @@ async function rebaseUrls(
27432717
environment: PartialEnvironment,
27442718
file: string,
27452719
rootFile: string,
2746-
alias: Alias[],
27472720
resolver: ResolveIdFn,
27482721
ignoreUrl?: (unquotedUrl: string, rawUrl: string) => boolean,
27492722
): Promise<{ file: string; contents?: string }> {
@@ -2771,16 +2744,6 @@ async function rebaseUrls(
27712744
const rebaseFn = async (unquotedUrl: string, rawUrl: string) => {
27722745
if (ignoreUrl?.(unquotedUrl, rawUrl)) return false
27732746
if (unquotedUrl[0] === '/') return unquotedUrl
2774-
// match alias, no need to rewrite
2775-
for (const { find } of alias) {
2776-
const matches =
2777-
typeof find === 'string'
2778-
? unquotedUrl.startsWith(find)
2779-
: find.test(unquotedUrl)
2780-
if (matches) {
2781-
return unquotedUrl
2782-
}
2783-
}
27842747
const absolute =
27852748
(await resolver(environment, unquotedUrl, file)) ||
27862749
path.resolve(fileDir, unquotedUrl)
@@ -2812,7 +2775,6 @@ async function rebaseUrls(
28122775
const makeLessWorker = (
28132776
environment: PartialEnvironment,
28142777
resolvers: CSSAtImportResolvers,
2815-
alias: Alias[],
28162778
maxWorkers: number | undefined,
28172779
) => {
28182780
const skipRebaseUrls = (unquotedUrl: string, _rawUrl: string) => {
@@ -2845,7 +2807,6 @@ const makeLessWorker = (
28452807
environment,
28462808
resolved,
28472809
rootFile,
2848-
alias,
28492810
resolvers.less,
28502811
skipRebaseUrls,
28512812
)
@@ -2960,24 +2921,15 @@ const makeLessWorker = (
29602921
const lessProcessor = (
29612922
maxWorkers: number | undefined,
29622923
): StylePreprocessor<LessStylePreprocessorInternalOptions> => {
2963-
const workerMap = new Map<unknown, ReturnType<typeof makeLessWorker>>()
2924+
let worker: ReturnType<typeof makeLessWorker> | undefined
29642925

29652926
return {
29662927
close() {
2967-
for (const worker of workerMap.values()) {
2968-
worker.stop()
2969-
}
2928+
worker?.stop()
29702929
},
29712930
async process(environment, source, root, options, resolvers) {
29722931
const lessPath = loadPreprocessorPath(PreprocessLang.less, root)
2973-
2974-
if (!workerMap.has(options.alias)) {
2975-
workerMap.set(
2976-
options.alias,
2977-
makeLessWorker(environment, resolvers, options.alias, maxWorkers),
2978-
)
2979-
}
2980-
const worker = workerMap.get(options.alias)!
2932+
worker ??= makeLessWorker(environment, resolvers, maxWorkers)
29812933

29822934
const { content, map: additionalMap } = await getSource(
29832935
source,
@@ -3089,21 +3041,15 @@ const makeStylWorker = (maxWorkers: number | undefined) => {
30893041
const stylProcessor = (
30903042
maxWorkers: number | undefined,
30913043
): StylePreprocessor<StylusStylePreprocessorInternalOptions> => {
3092-
const workerMap = new Map<unknown, ReturnType<typeof makeStylWorker>>()
3044+
let worker: ReturnType<typeof makeStylWorker> | undefined
30933045

30943046
return {
30953047
close() {
3096-
for (const worker of workerMap.values()) {
3097-
worker.stop()
3098-
}
3048+
worker?.stop()
30993049
},
31003050
async process(_environment, source, root, options, _resolvers) {
31013051
const stylusPath = loadPreprocessorPath(PreprocessLang.stylus, root)
3102-
3103-
if (!workerMap.has(options.alias)) {
3104-
workerMap.set(options.alias, makeStylWorker(maxWorkers))
3105-
}
3106-
const worker = workerMap.get(options.alias)!
3052+
worker ??= makeStylWorker(maxWorkers)
31073053

31083054
// Get source with preprocessor options.additionalData. Make sure a new line separator
31093055
// is added to avoid any render error, as added stylus content may not have semi-colon separators

0 commit comments

Comments
 (0)