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

fix: support jsr #22

Merged
merged 1 commit into from
Mar 6, 2024
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
4 changes: 3 additions & 1 deletion src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createEsbuildPlugin } from 'unplugin'
import type { EsbuildPlugin } from 'unplugin'
import { unpluginFactory } from './index'
import type { UnpluginStylexOptions } from './types'

export default createEsbuildPlugin(unpluginFactory)
export default createEsbuildPlugin(unpluginFactory) as (options?: UnpluginStylexOptions) => EsbuildPlugin
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as path from 'node:path'

import { createUnplugin } from 'unplugin'
import type { UnpluginFactory } from 'unplugin'
import type { UnpluginFactory, UnpluginInstance } from 'unplugin'
import type { BuildOptions } from 'vite'

import { buildStylexRules } from './core/build'
import { PLUGIN_NAME } from './core/constants'
Expand All @@ -12,7 +13,7 @@ import type { UnpluginStylexOptions } from './types'
export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined> = (rawOptions = {}) => {
const options = getOptions(rawOptions)
const stylexRules = {}
let viteConfig = null
let viteConfig: { build: BuildOptions | undefined; base: string | undefined; } | null = null

return {
name: PLUGIN_NAME,
Expand Down Expand Up @@ -74,7 +75,7 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>
},

buildEnd() {
const fileName = `${viteConfig.build?.assetsDir ?? 'assets'}/${options.stylex.filename}`
const fileName = `${viteConfig!.build?.assetsDir ?? 'assets'}/${options.stylex.filename}`
const collectedCSS = buildStylexRules(stylexRules, options.stylex.useCSSLayers)

if (!collectedCSS) return
Expand All @@ -87,15 +88,15 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>
},

transformIndexHtml(html, ctx) {
const fileName = `${viteConfig.build?.assetsDir ?? 'assets'}/${options.stylex.filename}`
const fileName = `${viteConfig!.build?.assetsDir ?? 'assets'}/${options.stylex.filename}`
const css = ctx.bundle?.[fileName]

if (!css) {
return html
}

const publicPath = path.posix.join(
viteConfig.base ?? '/',
viteConfig!.base ?? '/',
fileName.replace(/\\/g, '/')
)

Expand All @@ -114,7 +115,7 @@ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>
}
}

export const unplugin = createUnplugin(unpluginFactory)
export const unplugin: UnpluginInstance<UnpluginStylexOptions | undefined, boolean> = createUnplugin(unpluginFactory)

export * from './types'

Expand Down
3 changes: 2 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isDevelopment } from './core/constants'
import type { UnpluginStylexOptions } from './types'

export default (options?) => {
export default (options?: UnpluginStylexOptions) => {
if (isDevelopment || options?.dev) {
throw new Error('If you want to use this plugin through rollup, please use "@stylexjs/rollup-plugin" instead')
}
Expand Down
4 changes: 3 additions & 1 deletion src/rspack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createRspackPlugin } from 'unplugin'
import type { RspackPluginInstance } from 'unplugin'
import { unpluginFactory } from './index'
import type { UnpluginStylexOptions } from './types'

export default createRspackPlugin(unpluginFactory)
export default createRspackPlugin(unpluginFactory) as (options?: UnpluginStylexOptions) => RspackPluginInstance
4 changes: 3 additions & 1 deletion src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createVitePlugin } from 'unplugin'
import type { VitePlugin } from 'unplugin'
import { unpluginFactory } from './index'
import type { UnpluginStylexOptions } from './types'

export default createVitePlugin(unpluginFactory)
export default createVitePlugin(unpluginFactory) as (option?: UnpluginStylexOptions) => VitePlugin
3 changes: 2 additions & 1 deletion src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isDevelopment } from './core/constants'
import type { UnpluginStylexOptions } from './types'

export default (options?) => {
export default (options?: UnpluginStylexOptions) => {
if (isDevelopment || options?.dev) {
throw new Error('If you want to use this plugin through webpack, please use "@stylexjs/webpack-plugin" instead')
}
Expand Down
Loading