unplugin-stylex 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,131 +0,0 @@
1
- import {
2
- PLUGIN_NAME,
3
- isDevelopment
4
- } from "./chunk-36ARBXVP.js";
5
-
6
- // src/index.ts
7
- import { createFilter } from "@rollup/pluginutils";
8
- import stylexBabelPlugin2 from "@stylexjs/babel-plugin";
9
- import { createUnplugin } from "unplugin";
10
-
11
- // src/core/utils.ts
12
- function getOptions(options = {}) {
13
- const stylex = options.stylex || {};
14
- return {
15
- ...options,
16
- dev: options.dev || isDevelopment,
17
- include: options.include || [/\.[jt]sx?$/],
18
- exclude: options.exclude || [/node_modules/, /\.git/],
19
- enforce: options.enforce || "pre",
20
- stylex: {
21
- ...stylex,
22
- filename: stylex.filename || "stylex.css",
23
- stylexImports: stylex.stylexImports || ["stylex", "@stylexjs/stylex"],
24
- useCSSLayers: stylex.useCSSLayers || false,
25
- unstable_moduleResolution: stylex.unstable_moduleResolution || { type: "commonJS", rootDir: process.cwd() },
26
- babelConfig: {
27
- babelrc: (stylex.babelConfig || {}).babelrc || false,
28
- plugins: (stylex.babelConfig || {}).plugins || [],
29
- presets: (stylex.babelConfig || {}).presets || []
30
- }
31
- }
32
- };
33
- }
34
-
35
- // src/core/transformer.ts
36
- import * as fs from "fs/promises";
37
- import * as path from "path";
38
- import { transformAsync } from "@babel/core";
39
- import flowSyntaxPlugin from "@babel/plugin-syntax-flow";
40
- import jsxSyntaxPlugin from "@babel/plugin-syntax-jsx";
41
- import typescriptSyntaxPlugin from "@babel/plugin-syntax-typescript";
42
- import stylexBabelPlugin from "@stylexjs/babel-plugin";
43
- async function transformer(context) {
44
- const { id, source, options } = context;
45
- const stylex = options.stylex;
46
- const extname2 = path.extname(id);
47
- const stylexRules = {};
48
- if (stylex.stylexImports.some((importName) => source.includes(importName))) {
49
- const originSource = stylex.babelConfig.babelrc ? await fs.readFile(id, "utf-8") : source;
50
- const { code, map, metadata } = await transformAsync(
51
- originSource,
52
- {
53
- babelrc: stylex.babelConfig.babelrc,
54
- filename: id,
55
- plugins: [
56
- ...stylex.babelConfig.plugins || [],
57
- extname2 === ".ts" ? typescriptSyntaxPlugin : extname2 === ".tsx" ? [typescriptSyntaxPlugin, { isTSX: true }] : flowSyntaxPlugin,
58
- jsxSyntaxPlugin,
59
- [
60
- stylexBabelPlugin,
61
- {
62
- dev: options.dev,
63
- unstable_moduleResolution: stylex.unstable_moduleResolution,
64
- importSources: stylex.stylexImports,
65
- ...stylex
66
- }
67
- ]
68
- ],
69
- presets: stylex.babelConfig.presets
70
- }
71
- );
72
- if (metadata.stylex && metadata.stylex.length > 0) {
73
- stylexRules[id] = metadata.stylex;
74
- }
75
- if (!stylex.babelConfig.babelrc) {
76
- return { code, map, stylexRules };
77
- }
78
- return { code, stylexRules };
79
- }
80
- return { code: source };
81
- }
82
-
83
- // src/index.ts
84
- var unpluginFactory = (rawOptions = {}, meta) => {
85
- const options = getOptions(rawOptions);
86
- const filter = createFilter(options.include, options.exclude);
87
- const stylexRules = {};
88
- return {
89
- name: PLUGIN_NAME,
90
- enforce: options.enforce,
91
- transformInclude(id) {
92
- return filter(id);
93
- },
94
- async transform(source, id) {
95
- const context = {
96
- pluginContext: this,
97
- options,
98
- source,
99
- id
100
- };
101
- try {
102
- const result = await transformer(context);
103
- if (result.stylexRules && result.stylexRules[id]) {
104
- stylexRules[id] = result.stylexRules[id];
105
- }
106
- return result;
107
- } catch (error) {
108
- this.error(error);
109
- }
110
- },
111
- buildEnd() {
112
- const rules = Object.values(stylexRules).flat();
113
- if (rules.length > 0) {
114
- const collectedCSS = stylexBabelPlugin2.processStylexRules(rules, options.stylex.useCSSLayers);
115
- this.emitFile({
116
- fileName: options.stylex.filename,
117
- source: collectedCSS,
118
- type: "asset"
119
- });
120
- }
121
- }
122
- };
123
- };
124
- var unplugin = createUnplugin(unpluginFactory);
125
- var src_default = unplugin;
126
-
127
- export {
128
- unpluginFactory,
129
- unplugin,
130
- src_default
131
- };