Skip to content

Commit

Permalink
feat: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 committed Jan 2, 2024
1 parent 6046d9e commit b13689e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 12 deletions.
2 changes: 1 addition & 1 deletion examples/vite/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import stylexVitePlugin from 'unplugin-stylex/vite'

export default defineConfig({
plugins: [
stylexVitePlugin({ dev: true, stylex: { filename: 'stylexxx.css' } }),
stylexVitePlugin({ dev: true, stylex: { filename: 'stylex-test.css' } }),
],
})
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>
const options = getOptions(rawOptions)
const filter = createFilter(options.include, options.exclude)
const stylexRules = {}
let viteBuildConfig = null

return {
name: PLUGIN_NAME,
Expand Down Expand Up @@ -44,10 +45,30 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>

buildEnd() {
const rules = Object.values(stylexRules).flat()
if (rules.length > 0) {
if (rules.length === 0) return

const collectedCSS = (stylexBabelPlugin as any).processStylexRules(rules, options.stylex.useCSSLayers)
const fileName = options.stylex.filename

this.emitFile({
fileName,
source: collectedCSS,
type: 'asset',
})
},
vite: {
config(config) {
viteBuildConfig = config.build;
},
buildEnd() {
const rules = Object.values(stylexRules).flat()
if (!viteBuildConfig || rules.length === 0) return

const collectedCSS = (stylexBabelPlugin as any).processStylexRules(rules, options.stylex.useCSSLayers)
const fileName = `${viteBuildConfig.build?.assetsDir ?? 'assets'}/${options.stylex.filename}`

this.emitFile({
fileName: options.stylex.filename,
fileName,
source: collectedCSS,
type: 'asset',
})
Expand Down
3 changes: 0 additions & 3 deletions test/esbuild.test.ts

This file was deleted.

Empty file added test/fixtures/js/app.js
Empty file.
Empty file added test/fixtures/js/button.js
Empty file.
Empty file added test/fixtures/js/common.js
Empty file.
Empty file added test/fixtures/ts/app.ts
Empty file.
Empty file added test/fixtures/ts/button.ts
Empty file.
Empty file added test/fixtures/ts/common.ts
Empty file.
3 changes: 0 additions & 3 deletions test/rspack.test.ts

This file was deleted.

38 changes: 38 additions & 0 deletions test/transformer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest'
import { transformer } from '../src/core/transformer'

[ 'javascript', 'typescript' ].forEach((name) => {
describe(name, () => {
it('basic', async () => {
const context = {
id: '',
source: '',
options: {
dev: true,
stylex: {},
},
}

const { code } = await transformer(context)
expect(code).toMatchSnapshot()
})

it('useCSSLayer', async () => {
const stylexOptions = {
useCSSLayer: true,
}

const context = {
id: '',
source: '',
options: {
dev: true,
stylex: stylexOptions,
},
}

const { code } = await transformer(context)
expect(code).toMatchSnapshot()
})
})
})
3 changes: 0 additions & 3 deletions test/vite.test.ts

This file was deleted.

0 comments on commit b13689e

Please sign in to comment.