From 2ff913ed1d9323dd5fe74eda697418f70273be0a Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Thu, 26 Sep 2024 12:04:08 +0900 Subject: [PATCH] wip: use builtin css handling --- packages/vite/src/node/build.ts | 14 +++++++++++++- packages/vite/src/node/optimizer/index.ts | 10 ---------- packages/vite/src/node/plugins/css.ts | 3 +-- .../vite/src/node/plugins/importAnalysisBuild.ts | 3 ++- packages/vite/src/node/plugins/worker.ts | 5 ----- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index f6a133bed15d3c..86505e24b20f47 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -641,7 +641,19 @@ async function buildEnvironment( // TODO: remove this and enable rolldown's CSS support later moduleTypes: { ...options.rollupOptions.moduleTypes, - '.css': 'js', + // https://github.com/rolldown/rolldown/blob/4020de442a8ab0f7973794ead3b8aa04e316d558/crates/rolldown/src/module_loader/module_task.rs#L120 + // @ts-expect-error css + '.sass': 'css', + // @ts-expect-error css + '.scss': 'css', + // @ts-expect-error css + '.sss': 'css', + // @ts-expect-error css + '.styl': 'css', + // @ts-expect-error css + '.stylus': 'css', + // @ts-expect-error css + '.less': 'css', }, } diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 5bc40f2d705498..51f9fba708225b 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -799,11 +799,6 @@ async function prepareRolldownOptimizerRun( extensions: ['.js', '.css'], conditionNames: ['browser'], }, - // TODO: remove this and enable rolldown's CSS support later - moduleTypes: { - '.css': 'js', - ...rollupOptions.moduleTypes, - }, }) if (canceled) { await bundle.close() @@ -1083,11 +1078,6 @@ export async function extractExportsData( ...remainingRollupOptions, plugins, input: [filePath], - // TODO: remove this and enable rolldown's CSS support later - moduleTypes: { - '.css': 'js', - ...remainingRollupOptions.moduleTypes, - }, }) const result = await build.generate({ ...rollupOptions.output, diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 57e94207e441ef..f0fb0798f93396 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -622,8 +622,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 = css } return { diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 7a475e5f7af59d..633052cbe5df14 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -232,7 +232,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): [Plugin] { } }, - async transform(source, importer) { + async transform(source, importer, opts) { + if (opts?.moduleType === 'css') return if (isInNodeModules(importer) && !dynamicImportPrefixRE.test(source)) { return } diff --git a/packages/vite/src/node/plugins/worker.ts b/packages/vite/src/node/plugins/worker.ts index 7c72d72b3c3f9e..822b4fdf4c5f25 100644 --- a/packages/vite/src/node/plugins/worker.ts +++ b/packages/vite/src/node/plugins/worker.ts @@ -85,11 +85,6 @@ async function bundleWorkerEntry( onwarn(warning, warn) { onRollupWarning(warning, warn, workerEnvironment) }, - // TODO: remove this and enable rolldown's CSS support later - moduleTypes: { - '.css': 'js', - ...rollupOptions.moduleTypes, - }, // preserveEntrySignatures: false, }) let chunk: OutputChunk