Skip to content

Commit

Permalink
Don't use build config in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Jun 15, 2023
1 parent 67c821d commit b27fd4f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ export async function resolveConfig(
mainConfig: null,
isProduction,
plugins: userPlugins,
css: resolveCSSOptions(config.css, resolvedBuildOptions),
css: resolveCSSOptions(config.css),
esbuild:
config.esbuild === false
? false
Expand Down
16 changes: 12 additions & 4 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ import { formatMessages, transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { ModuleNode } from '../server/moduleGraph'
import type { ResolveFn, ResolvedBuildOptions, ViteDevServer } from '../'
import type { ResolveFn, ViteDevServer } from '../'
import { resolveUserExternal, toOutputFilePathInCss } from '../build'
import {
CLIENT_PUBLIC_PATH,
CSS_LANGS_RE,
ESBUILD_MODULES_TARGET,
SPECIAL_QUERY_RE,
} from '../constants'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -140,7 +141,6 @@ export type ResolvedCSSOptions = Omit<CSSOptions, 'lightningcss'> & {

export function resolveCSSOptions(
options: CSSOptions | undefined,
resolvedBuildOptions: ResolvedBuildOptions,
): ResolvedCSSOptions | undefined {
if (options?.lightningcss) {
return {
Expand All @@ -149,7 +149,7 @@ export function resolveCSSOptions(
...options.lightningcss,
targets:
options.lightningcss.targets ??
convertTargets(resolvedBuildOptions.cssTarget),
convertTargets(ESBUILD_MODULES_TARGET),
},
}
}
Expand Down Expand Up @@ -1472,6 +1472,7 @@ async function minifyCSS(css: string, config: ResolvedConfig) {
if (config.build.cssMinify === 'lightningcss') {
const { code, warnings } = (await importLightningCSS()).transform({
...config.css?.lightningcss,
targets: convertTargets(config.build.cssTarget),
cssModules: undefined,
filename: cssBundleName,
code: Buffer.from(css),
Expand Down Expand Up @@ -2257,11 +2258,17 @@ const esMap: Record<number, string[]> = {
const esRE = /es(\d{4})/
const versionRE = /\d/

const convertTargetsCache = new Map<
string | string[],
LightningCSSOptions['targets']
>()
export const convertTargets = (
esbuildTarget: string | string[] | false,
): LightningCSSOptions['targets'] => {
if (!esbuildTarget) return {}
const cached = convertTargetsCache.get(esbuildTarget)
if (cached) return cached
const targets: LightningCSSOptions['targets'] = {}
if (!esbuildTarget) return targets

const entriesWithoutES = arraify(esbuildTarget).flatMap((e) => {
const match = e.match(esRE)
Expand Down Expand Up @@ -2294,5 +2301,6 @@ export const convertTargets = (
throw new Error(`Unsupported target "${entry}"`)
}

convertTargetsCache.set(esbuildTarget, targets)
return targets
}
2 changes: 1 addition & 1 deletion packages/vite/src/types/lightningcss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {

/**
* Options are spread, so you can also use options that are not typed here like
* visitor that will impact to much the bundle size.
* visitor (not exposed because it would impact too much the bundle size)
*/
export type LightningCSSOptions = {
targets?: Targets
Expand Down

0 comments on commit b27fd4f

Please sign in to comment.