Skip to content

Commit

Permalink
chore: build
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Jun 24, 2024
1 parent 572983f commit 020c921
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 29 deletions.
6 changes: 2 additions & 4 deletions __tests__/__snapshots__/plugin.spec.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`.xju2f9n{color:blue}␊
.x1e2nbdu{color:red}␊
`
.x1e2nbdu{color:red}`

> Snapshot 2
Expand Down Expand Up @@ -64,8 +63,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1
`:root{--x1rp20gp:#000;}␊
.x97kcnl:not(#\\#){color:var(--x1rp20gp)}␊
`
.x97kcnl:not(#\\#){color:var(--x1rp20gp)}`

## empty

Expand Down
Binary file modified __tests__/__snapshots__/plugin.spec.ts.snap
Binary file not shown.
1 change: 1 addition & 0 deletions __tests__/fixtures/empty/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { create, props } from '@stylexjs/stylex'
import 'virtual:stylex.css'

const styles = create({
link: {
Expand Down
2 changes: 2 additions & 0 deletions __tests__/fixtures/normal/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as stylex from '@stylexjs/stylex'
import * as blue from './blue'
import * as red from './red'

import 'virtual:stylex.css'

const { className } = stylex.props(blue.styles.blue, red.styles.red)

export { className }
1 change: 1 addition & 0 deletions __tests__/fixtures/path-alias/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { create, props } from '@stylexjs/stylex'
import { colors } from '@/tokens/color.stylex'
import 'virtual:stylex.css'

const styles = create({
black: {
Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures/pxtorem/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as stylex from '@stylexjs/stylex'
import 'virtual:stylex.css'

const styles = stylex.create({
foo: {
Expand Down
49 changes: 25 additions & 24 deletions src/plugins/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,33 @@ export function stylexBuild(plugin: Plugin, ctx: PluginContext, cssPlugins: Plug
return CONSTANTS.STYLEX_BUNDLE_MARK
}
}
},
renderChunk: {
async handler(_, chunk) {
if (!cssHooks.size) {
cssPlugins.forEach((p) => {
cssHooks.set(p.name, hijackHook(p, 'transform', (fn, c, args) => fn.apply(c, args), true))
})
}
if (!chunk.moduleIds.some(s => entries.has(s))) {
return null
}
for (const entry of [...entries]) {
let css = ctx.produceCSS()
if (!CONSTANTS.RESOLVED_ID_REG.test(entry) && ctx.isManuallyControlCSS) {
const { original } = parseRequest(entry)
css = ctx.isManuallyControlCSS ? fs.readFileSync(original, 'utf8').replace(ctx.controlCSSByManually.symbol!, css) : css
}
const res = await cssHooks.get('vite:css')?.apply({} as any, [css, entry])
// @ts-expect-error
await cssHooks.get('vite:css-post')?.apply({} as any, [res?.code || '', entry])
chunk.modules[entry] = { code: null, originalLength: 0, removedExports: [], renderedExports: [], renderedLength: 0 }
}
return null
},
order: 'pre'
}
}

Object.assign(plugin, self)

hijackHook(plugin, 'transform', async (fn, c, args) => {
if (!cssHooks.size) {
cssPlugins.forEach((p) => {
cssHooks.set(p.name, hijackHook(p, 'transform', (fn, c, args) => fn.apply(c, args), true))
})
}
const id = args[1]
const { original } = parseRequest(id)
const result = await fn.apply(c, args)
if (result && ctx.styleRules.has(original)) {
for (const entry of [...entries]) {
let css = ctx.produceCSS()
if (!CONSTANTS.RESOLVED_ID_REG.test(entry) && ctx.isManuallyControlCSS) {
const { original } = parseRequest(entry)
css = ctx.isManuallyControlCSS ? fs.readFileSync(original, 'utf8').replace(ctx.controlCSSByManually.symbol!, css) : css
}
const res = await cssHooks.get('vite:css')?.apply(c, [css, entry, args[2]])
// @ts-expect-error
await cssHooks.get('vite:css-post')?.apply(c, [res?.code || '', entry, args[2]])
}
}
return result
})
}
2 changes: 1 addition & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function createForViteServer(ctx: PluginContext) {
}
cssPlugins.push(...conf.plugins.filter(p => CONSTANTS.CSS_PLUGINS.includes(p.name)))
cssPlugins.sort((a, b) => a.name.length < b.name.length ? -1 : 1)
ctx.env === 'build' ? stylexBuild(plugin, ctx, cssPlugins) : stylexServer(plugin, ctx)
}
ctx.env === 'build' ? stylexBuild(plugin, ctx, cssPlugins) : stylexServer(plugin, ctx)
}
}

0 comments on commit 020c921

Please sign in to comment.