Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: "fix(css): spread lightningcss options (#14024)" #14209

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vite/api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"mainEntryPointFilePath": "./temp/node/index.d.ts",

"bundledPackages": ["lightningcss"],

"dtsRollup": {
"enabled": true,
"untrimmedFilePath": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"build-types": "run-s build-types-temp build-types-pre-patch build-types-roll build-types-post-patch build-types-check",
"build-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
"build-types-roll": "api-extractor run && rimraf temp",
"build-types-roll": "tsx scripts/api-extractor.ts run && rimraf temp",
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
"build-types-check": "tsx scripts/checkBuiltTypes.ts && tsc --project tsconfig.check.json",
"typecheck": "tsc --noEmit",
Expand Down
25 changes: 25 additions & 0 deletions packages/vite/scripts/api-extractor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Extractor, ExtractorConfig } from '@microsoft/api-extractor'

const result = Extractor.invoke(
ExtractorConfig.loadFileAndPrepare('./api-extractor.json'),
{
messageCallback: (message) => {
const ignore = () => {
// @ts-expect-error TS requires to use the const enum, which is not available as the named export in tsx
message.logLevel = 'none'
}
if (message.sourceFilePath?.includes('lightningcss')) {
ignore()
}
if (message.messageId === 'ae-forgotten-export') {
if (message.sourceFilePath?.endsWith('/src/types/lightningcss.d.ts')) {
// We only expose LightningCSS types via prefixed types to avoid
// having confusing name like "Targets" in Vite types
ignore()
}
}
},
},
)

if (!result.succeeded) process.exit(1)
4 changes: 1 addition & 3 deletions packages/vite/scripts/checkBuiltTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ const distDir = resolve(dir, '../dist')
const pkgJson = JSON.parse(
readFileSync(resolve(dir, '../package.json'), 'utf-8'),
)
const deps = new Set(
Object.keys(Object.assign(pkgJson.dependencies, pkgJson.peerDependencies)),
)
const deps = new Set(Object.keys(pkgJson.dependencies))

type SpecifierError = {
loc: SourceLocation | null | undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export type {
export type {
CSSOptions,
CSSModulesOptions,
LightningCSSOptions,
PreprocessCSSResult,
ResolvedCSSOptions,
} from './plugins/css'
Expand Down Expand Up @@ -143,3 +142,4 @@ export type { Terser } from 'dep-types/terser'
export type { RollupCommonJSOptions } from 'dep-types/commonjs'
export type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
export type { Matcher, AnymatchPattern, AnymatchFn } from 'dep-types/anymatch'
export type { LightningCSSOptions } from 'dep-types/lightningcss'
14 changes: 4 additions & 10 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import type Sass from 'sass'
import type Stylus from 'stylus'
import type Less from 'less'
import type { Alias } from 'dep-types/alias'
import type { LightningCSSOptions } from 'dep-types/lightningcss'
import type { TransformOptions } from 'esbuild'
import { formatMessages, transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import type { BundleAsyncOptions, CustomAtRules } from 'lightningcss'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { ModuleNode } from '../server/moduleGraph'
import type { ResolveFn, ViteDevServer } from '../'
Expand Down Expand Up @@ -139,12 +139,6 @@ export type ResolvedCSSOptions = Omit<CSSOptions, 'lightningcss'> & {
}
}

// remove options set by Vite
export type LightningCSSOptions = Omit<
BundleAsyncOptions<CustomAtRules>,
'filename' | 'resolver' | 'minify' | 'sourceMap' | 'analyzeDependencies'
>

export function resolveCSSOptions(
options: CSSOptions | undefined,
): ResolvedCSSOptions | undefined {
Expand Down Expand Up @@ -2185,15 +2179,13 @@ async function compileLightningCSS(
? (await importLightningCSS()).transformStyleAttribute({
filename,
code: Buffer.from(src),
minify: config.isProduction && !!config.build.cssMinify,
targets: config.css?.lightningcss?.targets,
minify: config.isProduction && !!config.build.cssMinify,
analyzeDependencies: true,
visitor: config.css?.lightningcss?.visitor,
})
: await (
await importLightningCSS()
).bundleAsync({
...config.css?.lightningcss,
filename,
resolver: {
read(filePath) {
Expand Down Expand Up @@ -2224,12 +2216,14 @@ async function compileLightningCSS(
return id
},
},
targets: config.css?.lightningcss?.targets,
minify: config.isProduction && !!config.build.cssMinify,
sourceMap: config.css?.devSourcemap,
analyzeDependencies: true,
cssModules: cssModuleRE.test(id)
? config.css?.lightningcss?.cssModules ?? true
: undefined,
drafts: config.css?.lightningcss?.drafts,
})

let css = res.code.toString()
Expand Down
23 changes: 23 additions & 0 deletions packages/vite/src/types/lightningcss.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {
CSSModulesConfig,
Drafts,
Features,
NonStandard,
PseudoClasses,
Targets,
} from 'lightningcss'

/**
* Options are spread, so you can also use options that are not typed here like
* visitor (not exposed because it would impact too much the bundle size)
*/
export type LightningCSSOptions = {
targets?: Targets
include?: Features
exclude?: Features
drafts?: Drafts
nonStandard?: NonStandard
pseudoClasses?: PseudoClasses
unusedSymbols?: string[]
cssModules?: CSSModulesConfig
}
Loading