diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index 08389ea..c6d0f6e 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/eslint.config.js b/eslint.config.js index 694b6c9..5428fba 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,3 +1,3 @@ -module.exports = require('eslint-config-kagura').nonzzz({ ts: true, jsx: true }, { +module.exports = require('eslint-config-kagura').nonzzz({ ts: true, jsx: true, unusedImports: false }, { ignores: ['packages/**/output.js'] }) diff --git a/examples/vite-vue-demo/package.json b/examples/vite-vue-demo/package.json index 10a86a2..2d19fcf 100644 --- a/examples/vite-vue-demo/package.json +++ b/examples/vite-vue-demo/package.json @@ -7,6 +7,7 @@ "preview": "vite preview" }, "devDependencies": { + "@stylex-extend/vite-plugin": "workspace:*", "@stylex-extend/vue": "workspace:*", "@vitejs/plugin-vue": "^5.0.4", "@vitejs/plugin-vue-jsx": "^3.1.0", diff --git a/examples/vite-vue-demo/src/colors.stylex.ts b/examples/vite-vue-demo/src/colors.stylex.ts new file mode 100644 index 0000000..1ee7639 --- /dev/null +++ b/examples/vite-vue-demo/src/colors.stylex.ts @@ -0,0 +1,5 @@ +import { defineVars } from '@stylexjs/stylex' + +export const colors = defineVars({ + purple: 'purple' +}) diff --git a/examples/vite-vue-demo/src/main.tsx b/examples/vite-vue-demo/src/main.tsx index c4a1047..795e2ad 100644 --- a/examples/vite-vue-demo/src/main.tsx +++ b/examples/vite-vue-demo/src/main.tsx @@ -1,9 +1,11 @@ import { createApp, defineComponent } from 'vue' import { keyframes } from '@stylexjs/stylex' +import { injectGlobalStyle } from '@stylex-extend/core' +import { colors } from './colors.stylex' const pulse = keyframes({ '0%': { transform: 'scale(1)' }, - '50%': { transform: 'scale(1.1)' }, + '50%': { transform: 'scale(0.5)' }, '100%': { transform: 'scale(1)' } }) @@ -13,13 +15,21 @@ const A = defineComponent(() => { const color = 'purple' +injectGlobalStyle({ + p: { + color: colors.purple + } +}) + const App = defineComponent({ + setup() { return () => ( <>
456
+

text

t.isImportDeclaration(p.node)) ctx.setupOptions(pluginOptions, identifiers, anchor === -1 ? 0 : anchor) scanImportStmt(body, ctx) - path.traverse({ - CallExpression(path) { - const CSS = transformInjectGlobalStyle(path, ctx) - Reflect.set(state.file.metadata, 'globalStyle', CSS) - } - }) + if (ctx.options.enableInjectGlobalStyle) { + path.traverse({ + CallExpression(path) { + const CSS = transformInjectGlobalStyle(path, ctx) + Reflect.set(state.file.metadata, 'globalStyle', CSS) + } + }) + } }, exit(path) { const body = path.get('body') @@ -93,3 +95,5 @@ export type StylexExtendTransformObject = { } export default declare as unknown as StylexExtendTransformObject + +export type { StylexExtendBabelPluginOptions } diff --git a/packages/babel-plugin/src/interface.ts b/packages/babel-plugin/src/interface.ts index 68938d7..28a9dd5 100644 --- a/packages/babel-plugin/src/interface.ts +++ b/packages/babel-plugin/src/interface.ts @@ -22,7 +22,7 @@ export interface ModuleResolutionExperimentalCrossFileParsing { export type ModuleResolution = ModuleResolutionCommonJS | MOduleResolutionHaste | ModuleResolutionExperimentalCrossFileParsing export interface StylexExtendBabelPluginOptions { - stylex: boolean | StylexBindingMeta + stylex?: boolean | StylexBindingMeta enableInjectGlobalStyle?: boolean /** * @default 'x' diff --git a/packages/babel-plugin/src/state-context.ts b/packages/babel-plugin/src/state-context.ts index 33fa8a4..c89e5af 100644 --- a/packages/babel-plugin/src/state-context.ts +++ b/packages/babel-plugin/src/state-context.ts @@ -1,4 +1,5 @@ import path from 'path' +import { createRequire } from 'module' import { types } from '@babel/core' import type { StylexBindingMeta, StylexExtendBabelPluginOptions } from './interface' @@ -6,6 +7,8 @@ export type InternalPluginOptions = Required +const _require = createRequire(__filename) + interface FileNamesForHashing { fileName: string exportName: string @@ -28,11 +31,11 @@ function filePathResolver(relativePath: string, sourceFilePath: string) { const importPathStr = relativePath + ext if (importPathStr.startsWith('.')) { try { - return require.resolve(importPathStr, { + return _require.resolve(importPathStr, { paths: [path.dirname(sourceFilePath)] }) } catch { - return null + } } } diff --git a/packages/babel-plugin/src/visitor/import-stmt.ts b/packages/babel-plugin/src/visitor/import-stmt.ts index c5187a7..29fad09 100644 --- a/packages/babel-plugin/src/visitor/import-stmt.ts +++ b/packages/babel-plugin/src/visitor/import-stmt.ts @@ -12,7 +12,7 @@ function isTopLevelCalled(p: NodePath) { } export function scanImportStmt(stmts: NodePath[], ctx: Context) { - const matchers = matchesFileSuffix(ctx.options.unstable_moduleResolution.themeFileExtension!) + const matchers = matchesFileSuffix(ctx.options.unstable_moduleResolution.themeFileExtension ?? '.stylex') for (const stmt of stmts) { if (!stmt.isImportDeclaration()) continue if (stmt.node.source.value === STYLEX_EXTEND || matchers(stmt.node.source.value)) { @@ -20,6 +20,7 @@ export function scanImportStmt(stmts: NodePath[], ctx: Context) if (stmt.node.source.value === STYLEX_EXTEND) { ctx.addImports(specs.map((s) => s.local.name)) } else { + if (!ctx.options.enableInjectGlobalStyle) continue const [filePath, fileName] = ctx.importPathResolver(stmt.node.source.value) const codeContent = fs.readFileSync(filePath, 'utf-8') const ast = parseSync(codeContent, { babelrc: true }) diff --git a/yarn.lock b/yarn.lock index dd002b7..6c7bd19 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1085,6 +1085,15 @@ __metadata: languageName: unknown linkType: soft +"@stylex-extend/vite-plugin@workspace:*, @stylex-extend/vite-plugin@workspace:internal/vite-plugin": + version: 0.0.0-use.local + resolution: "@stylex-extend/vite-plugin@workspace:internal/vite-plugin" + dependencies: + "@babel/core": "npm:^7.23.9" + "@types/babel__core": "npm:^7.20.5" + languageName: unknown + linkType: soft + "@stylex-extend/vue@workspace:*, @stylex-extend/vue@workspace:packages/vue": version: 0.0.0-use.local resolution: "@stylex-extend/vue@workspace:packages/vue" @@ -5118,6 +5127,7 @@ __metadata: version: 0.0.0-use.local resolution: "vite-vue-demo@workspace:examples/vite-vue-demo" dependencies: + "@stylex-extend/vite-plugin": "workspace:*" "@stylex-extend/vue": "workspace:*" "@vitejs/plugin-vue": "npm:^5.0.4" "@vitejs/plugin-vue-jsx": "npm:^3.1.0"