From 1b9dd7b12a7733cccb5620350943e4e7a7152e01 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 5f103e3e032dbf..73ed6ca2ba9375 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -644,7 +644,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 ebf08ec440ffe4..467321b297004d 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -794,11 +794,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() @@ -1078,11 +1073,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 9e15f188a511c8..12b061b4096cf3 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -611,8 +611,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 9892f7fc78e400..9b6d3ba1da5655 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -235,7 +235,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