unplugin-stylex 0.0.1

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.
Files changed (48) hide show
  1. package/.eslintrc.json +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +98 -0
  4. package/dist/chunk-36ARBXVP.js +8 -0
  5. package/dist/chunk-5FLQNHMU.cjs +131 -0
  6. package/dist/chunk-N4Z3Z2PU.cjs +8 -0
  7. package/dist/chunk-R3ITJTXO.js +131 -0
  8. package/dist/esbuild.cjs +11 -0
  9. package/dist/esbuild.d.cts +7 -0
  10. package/dist/esbuild.d.ts +7 -0
  11. package/dist/esbuild.js +11 -0
  12. package/dist/index.cjs +11 -0
  13. package/dist/index.d.cts +9 -0
  14. package/dist/index.d.ts +9 -0
  15. package/dist/index.js +11 -0
  16. package/dist/rollup.cjs +13 -0
  17. package/dist/rollup.d.cts +3 -0
  18. package/dist/rollup.d.ts +3 -0
  19. package/dist/rollup.js +13 -0
  20. package/dist/rspack.cjs +11 -0
  21. package/dist/rspack.d.cts +3 -0
  22. package/dist/rspack.d.ts +3 -0
  23. package/dist/rspack.js +11 -0
  24. package/dist/types.cjs +1 -0
  25. package/dist/types.d.cts +27 -0
  26. package/dist/types.d.ts +27 -0
  27. package/dist/types.js +0 -0
  28. package/dist/vite.cjs +11 -0
  29. package/dist/vite.d.cts +3 -0
  30. package/dist/vite.d.ts +3 -0
  31. package/dist/vite.js +11 -0
  32. package/dist/webpack.cjs +13 -0
  33. package/dist/webpack.d.cts +3 -0
  34. package/dist/webpack.d.ts +3 -0
  35. package/dist/webpack.js +13 -0
  36. package/package.json +75 -0
  37. package/pnpm-workspace.yaml +2 -0
  38. package/src/core/constants.ts +7 -0
  39. package/src/core/transformer.ts +61 -0
  40. package/src/core/utils.ts +26 -0
  41. package/src/esbuild.ts +4 -0
  42. package/src/index.ts +61 -0
  43. package/src/rollup.ts +7 -0
  44. package/src/rspack.ts +4 -0
  45. package/src/types.ts +27 -0
  46. package/src/vite.ts +4 -0
  47. package/src/webpack.ts +7 -0
  48. package/tsconfig.json +18 -0
package/.eslintrc.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "rules": {
3
+ "semi": ["error", "always"]
4
+ }
5
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 cin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # unplugin-stylex
2
+
3
+ ## Installation
4
+
5
+ Install the package from the following command
6
+
7
+ ```shell
8
+ npm install unplugin-stylex --save-dev
9
+ ```
10
+
11
+ or with yarn:
12
+
13
+ ```shell
14
+ yarn add unplugin-stylex --save-dev
15
+ ```
16
+
17
+ or with pnpm:
18
+
19
+ ```shell
20
+ pnpm i unplugin-stylex --save-dev
21
+ ```
22
+
23
+ ## Configuration
24
+
25
+ <details>
26
+ <summary>Vite</summary><br>
27
+
28
+ ```ts
29
+ // vite.config.js
30
+ import { defineConfig } from 'vite'
31
+ import stylexPlugin from 'unplugin-stylex/vite'
32
+
33
+ export default defineConfig({
34
+ plugins: [
35
+ stylexPlugin({ /* options */}),
36
+ ],
37
+ })
38
+ ```
39
+
40
+ </br></details>
41
+
42
+ <details>
43
+ <summary>esbuild</summary><br>
44
+
45
+ ```ts
46
+ // esbuild.config.js
47
+ import { build } from 'esbuild'
48
+ import stylexPlugin from 'unplugin-stylex/esbuild'
49
+
50
+ export default {
51
+ plugins: [
52
+ stylexPlugin({ /* options */ }),
53
+ ],
54
+ }
55
+ ```
56
+
57
+ </br></details>
58
+
59
+ <details>
60
+ <summary>rspack</summary><br>
61
+
62
+ ```ts
63
+ // rspack.config.js
64
+ import stylexPlugin from 'unplugin-stylex/rspack'
65
+
66
+ module.exports = {
67
+ // other rspack config
68
+ plugins: [
69
+ stylexPlugin({ /* options */}),
70
+ ],
71
+ }
72
+ ```
73
+
74
+ </br></details>
75
+
76
+ ## Options
77
+
78
+ ### options.dev
79
+
80
+ ### options.enforce
81
+
82
+ ### options.stylex
83
+
84
+ #### options.stylex.runtimeInjection
85
+
86
+ #### options.stylex.classNamePrefix
87
+
88
+ #### options.stylex.useCSSLayers
89
+
90
+ #### options.stylex.babelConfig
91
+
92
+ #### options.stylex.stylexImports
93
+
94
+ #### options.stylex.unstable_moduleResolution
95
+
96
+ ## License
97
+
98
+ StyleX is [MIT licensed](./LICENSE)
@@ -0,0 +1,8 @@
1
+ // src/core/constants.ts
2
+ var PLUGIN_NAME = "unplugin-stylex";
3
+ var isDevelopment = process.env.NODE_ENV !== "production" || process.env.BABEL_ENV !== "production";
4
+
5
+ export {
6
+ PLUGIN_NAME,
7
+ isDevelopment
8
+ };
@@ -0,0 +1,131 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2
+
3
+
4
+ var _chunkN4Z3Z2PUcjs = require('./chunk-N4Z3Z2PU.cjs');
5
+
6
+ // src/index.ts
7
+ var _pluginutils = require('@rollup/pluginutils');
8
+ var _babelplugin = require('@stylexjs/babel-plugin'); var _babelplugin2 = _interopRequireDefault(_babelplugin);
9
+ var _unplugin = require('unplugin');
10
+
11
+ // src/core/utils.ts
12
+ function getOptions(options = {}) {
13
+ const stylex = options.stylex || {};
14
+ return {
15
+ ...options,
16
+ dev: options.dev || _chunkN4Z3Z2PUcjs.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
+ var _promises = require('fs/promises'); var fs = _interopRequireWildcard(_promises);
37
+ var _path = require('path'); var path = _interopRequireWildcard(_path);
38
+ var _core = require('@babel/core');
39
+ var _pluginsyntaxflow = require('@babel/plugin-syntax-flow'); var _pluginsyntaxflow2 = _interopRequireDefault(_pluginsyntaxflow);
40
+ var _pluginsyntaxjsx = require('@babel/plugin-syntax-jsx'); var _pluginsyntaxjsx2 = _interopRequireDefault(_pluginsyntaxjsx);
41
+ var _pluginsyntaxtypescript = require('@babel/plugin-syntax-typescript'); var _pluginsyntaxtypescript2 = _interopRequireDefault(_pluginsyntaxtypescript);
42
+
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 _core.transformAsync.call(void 0,
51
+ originSource,
52
+ {
53
+ babelrc: stylex.babelConfig.babelrc,
54
+ filename: id,
55
+ plugins: [
56
+ ...stylex.babelConfig.plugins || [],
57
+ extname2 === ".ts" ? _pluginsyntaxtypescript2.default : extname2 === ".tsx" ? [_pluginsyntaxtypescript2.default, { isTSX: true }] : _pluginsyntaxflow2.default,
58
+ _pluginsyntaxjsx2.default,
59
+ [
60
+ _babelplugin2.default,
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 = _pluginutils.createFilter.call(void 0, options.include, options.exclude);
87
+ const stylexRules = {};
88
+ return {
89
+ name: _chunkN4Z3Z2PUcjs.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 = _babelplugin2.default.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 = _unplugin.createUnplugin.call(void 0, unpluginFactory);
125
+ var src_default = unplugin;
126
+
127
+
128
+
129
+
130
+
131
+ exports.unpluginFactory = unpluginFactory; exports.unplugin = unplugin; exports.src_default = src_default;
@@ -0,0 +1,8 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/constants.ts
2
+ var PLUGIN_NAME = "unplugin-stylex";
3
+ var isDevelopment = process.env.NODE_ENV !== "production" || process.env.BABEL_ENV !== "production";
4
+
5
+
6
+
7
+
8
+ exports.PLUGIN_NAME = PLUGIN_NAME; exports.isDevelopment = isDevelopment;
@@ -0,0 +1,131 @@
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
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk5FLQNHMUcjs = require('./chunk-5FLQNHMU.cjs');
4
+ require('./chunk-N4Z3Z2PU.cjs');
5
+
6
+ // src/esbuild.ts
7
+ var _unplugin = require('unplugin');
8
+ var esbuild_default = _unplugin.createEsbuildPlugin.call(void 0, _chunk5FLQNHMUcjs.unpluginFactory);
9
+
10
+
11
+ exports.default = esbuild_default;
@@ -0,0 +1,7 @@
1
+ import * as unplugin from 'unplugin';
2
+ import { UnpluginStylexOptions } from './types.cjs';
3
+ import '@rollup/pluginutils';
4
+
5
+ declare const _default: (options?: UnpluginStylexOptions) => unplugin.EsbuildPlugin;
6
+
7
+ export { _default as default };
@@ -0,0 +1,7 @@
1
+ import * as unplugin from 'unplugin';
2
+ import { UnpluginStylexOptions } from './types.js';
3
+ import '@rollup/pluginutils';
4
+
5
+ declare const _default: (options?: UnpluginStylexOptions) => unplugin.EsbuildPlugin;
6
+
7
+ export { _default as default };
@@ -0,0 +1,11 @@
1
+ import {
2
+ unpluginFactory
3
+ } from "./chunk-R3ITJTXO.js";
4
+ import "./chunk-36ARBXVP.js";
5
+
6
+ // src/esbuild.ts
7
+ import { createEsbuildPlugin } from "unplugin";
8
+ var esbuild_default = createEsbuildPlugin(unpluginFactory);
9
+ export {
10
+ esbuild_default as default
11
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+ var _chunk5FLQNHMUcjs = require('./chunk-5FLQNHMU.cjs');
6
+ require('./chunk-N4Z3Z2PU.cjs');
7
+
8
+
9
+
10
+
11
+ exports.default = _chunk5FLQNHMUcjs.src_default; exports.unplugin = _chunk5FLQNHMUcjs.unplugin; exports.unpluginFactory = _chunk5FLQNHMUcjs.unpluginFactory;
@@ -0,0 +1,9 @@
1
+ import * as unplugin$1 from 'unplugin';
2
+ import { UnpluginFactory } from 'unplugin';
3
+ import { UnpluginStylexOptions } from './types.cjs';
4
+ import '@rollup/pluginutils';
5
+
6
+ declare const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>;
7
+ declare const unplugin: unplugin$1.UnpluginInstance<UnpluginStylexOptions, boolean>;
8
+
9
+ export { unplugin as default, unplugin, unpluginFactory };
@@ -0,0 +1,9 @@
1
+ import * as unplugin$1 from 'unplugin';
2
+ import { UnpluginFactory } from 'unplugin';
3
+ import { UnpluginStylexOptions } from './types.js';
4
+ import '@rollup/pluginutils';
5
+
6
+ declare const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined>;
7
+ declare const unplugin: unplugin$1.UnpluginInstance<UnpluginStylexOptions, boolean>;
8
+
9
+ export { unplugin as default, unplugin, unpluginFactory };
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ import {
2
+ src_default,
3
+ unplugin,
4
+ unpluginFactory
5
+ } from "./chunk-R3ITJTXO.js";
6
+ import "./chunk-36ARBXVP.js";
7
+ export {
8
+ src_default as default,
9
+ unplugin,
10
+ unpluginFactory
11
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkN4Z3Z2PUcjs = require('./chunk-N4Z3Z2PU.cjs');
4
+
5
+ // src/rollup.ts
6
+ var rollup_default = (options) => {
7
+ if (_chunkN4Z3Z2PUcjs.isDevelopment || (options == null ? void 0 : options.dev)) {
8
+ throw new Error(`If you want to use this plugin through rollup, please use "@stylexjs/rollup-plugin" instead`);
9
+ }
10
+ };
11
+
12
+
13
+ exports.default = rollup_default;
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: any) => void;
2
+
3
+ export { _default as default };
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: any) => void;
2
+
3
+ export { _default as default };
package/dist/rollup.js ADDED
@@ -0,0 +1,13 @@
1
+ import {
2
+ isDevelopment
3
+ } from "./chunk-36ARBXVP.js";
4
+
5
+ // src/rollup.ts
6
+ var rollup_default = (options) => {
7
+ if (isDevelopment || (options == null ? void 0 : options.dev)) {
8
+ throw new Error(`If you want to use this plugin through rollup, please use "@stylexjs/rollup-plugin" instead`);
9
+ }
10
+ };
11
+ export {
12
+ rollup_default as default
13
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk5FLQNHMUcjs = require('./chunk-5FLQNHMU.cjs');
4
+ require('./chunk-N4Z3Z2PU.cjs');
5
+
6
+ // src/rspack.ts
7
+ var _unplugin = require('unplugin');
8
+ var rspack_default = _unplugin.createRspackPlugin.call(void 0, _chunk5FLQNHMUcjs.unpluginFactory);
9
+
10
+
11
+ exports.default = rspack_default;
@@ -0,0 +1,3 @@
1
+ declare const _default: any;
2
+
3
+ export { _default as default };
@@ -0,0 +1,3 @@
1
+ declare const _default: any;
2
+
3
+ export { _default as default };
package/dist/rspack.js ADDED
@@ -0,0 +1,11 @@
1
+ import {
2
+ unpluginFactory
3
+ } from "./chunk-R3ITJTXO.js";
4
+ import "./chunk-36ARBXVP.js";
5
+
6
+ // src/rspack.ts
7
+ import { createRspackPlugin } from "unplugin";
8
+ var rspack_default = createRspackPlugin(unpluginFactory);
9
+ export {
10
+ rspack_default as default
11
+ };
package/dist/types.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,27 @@
1
+ import { FilterPattern } from '@rollup/pluginutils';
2
+
3
+ type BabelConfig = {
4
+ plugins: any[];
5
+ presets: any[];
6
+ babelrc: boolean;
7
+ };
8
+ type StylexOptions = {
9
+ filename?: string;
10
+ stylexImports?: string[];
11
+ classNamePrefix?: string;
12
+ useCSSLayers?: boolean;
13
+ unstable_moduleResolution?: {
14
+ type: "commonjs" | "haste";
15
+ rootDir: string;
16
+ };
17
+ babelConfig?: BabelConfig;
18
+ };
19
+ type UnpluginStylexOptions = {
20
+ dev?: boolean;
21
+ include?: FilterPattern;
22
+ exclude?: FilterPattern;
23
+ enforce?: "post" | "pre";
24
+ stylex?: StylexOptions;
25
+ };
26
+
27
+ export type { BabelConfig, UnpluginStylexOptions };
@@ -0,0 +1,27 @@
1
+ import { FilterPattern } from '@rollup/pluginutils';
2
+
3
+ type BabelConfig = {
4
+ plugins: any[];
5
+ presets: any[];
6
+ babelrc: boolean;
7
+ };
8
+ type StylexOptions = {
9
+ filename?: string;
10
+ stylexImports?: string[];
11
+ classNamePrefix?: string;
12
+ useCSSLayers?: boolean;
13
+ unstable_moduleResolution?: {
14
+ type: "commonjs" | "haste";
15
+ rootDir: string;
16
+ };
17
+ babelConfig?: BabelConfig;
18
+ };
19
+ type UnpluginStylexOptions = {
20
+ dev?: boolean;
21
+ include?: FilterPattern;
22
+ exclude?: FilterPattern;
23
+ enforce?: "post" | "pre";
24
+ stylex?: StylexOptions;
25
+ };
26
+
27
+ export type { BabelConfig, UnpluginStylexOptions };
package/dist/types.js ADDED
File without changes
package/dist/vite.cjs ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk5FLQNHMUcjs = require('./chunk-5FLQNHMU.cjs');
4
+ require('./chunk-N4Z3Z2PU.cjs');
5
+
6
+ // src/vite.ts
7
+ var _unplugin = require('unplugin');
8
+ var vite_default = _unplugin.createVitePlugin.call(void 0, _chunk5FLQNHMUcjs.unpluginFactory);
9
+
10
+
11
+ exports.default = vite_default;
@@ -0,0 +1,3 @@
1
+ declare const _default: any;
2
+
3
+ export { _default as default };
package/dist/vite.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare const _default: any;
2
+
3
+ export { _default as default };
package/dist/vite.js ADDED
@@ -0,0 +1,11 @@
1
+ import {
2
+ unpluginFactory
3
+ } from "./chunk-R3ITJTXO.js";
4
+ import "./chunk-36ARBXVP.js";
5
+
6
+ // src/vite.ts
7
+ import { createVitePlugin } from "unplugin";
8
+ var vite_default = createVitePlugin(unpluginFactory);
9
+ export {
10
+ vite_default as default
11
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkN4Z3Z2PUcjs = require('./chunk-N4Z3Z2PU.cjs');
4
+
5
+ // src/webpack.ts
6
+ var webpack_default = (options) => {
7
+ if (_chunkN4Z3Z2PUcjs.isDevelopment || (options == null ? void 0 : options.dev)) {
8
+ throw new Error(`If you want to use this plugin through webpack, please use "@stylexjs/webpack-plugin" instead`);
9
+ }
10
+ };
11
+
12
+
13
+ exports.default = webpack_default;
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: any) => void;
2
+
3
+ export { _default as default };
@@ -0,0 +1,3 @@
1
+ declare const _default: (options?: any) => void;
2
+
3
+ export { _default as default };
@@ -0,0 +1,13 @@
1
+ import {
2
+ isDevelopment
3
+ } from "./chunk-36ARBXVP.js";
4
+
5
+ // src/webpack.ts
6
+ var webpack_default = (options) => {
7
+ if (isDevelopment || (options == null ? void 0 : options.dev)) {
8
+ throw new Error(`If you want to use this plugin through webpack, please use "@stylexjs/webpack-plugin" instead`);
9
+ }
10
+ };
11
+ export {
12
+ webpack_default as default
13
+ };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "unplugin-stylex",
3
+ "version": "0.0.1",
4
+ "packageManager": "pnpm@8.11.0",
5
+ "description": "Unplugin for stylex",
6
+ "repository": "https://github.com/eryue0220/unplugin-stylex",
7
+ "keywords": [
8
+ "unplugin",
9
+ "stylex",
10
+ "stylex-plugin",
11
+ "esbuild",
12
+ "rspack",
13
+ "vite"
14
+ ],
15
+ "author": "eryue0220<eryue0220@gmail.com>",
16
+ "license": "MIT",
17
+ "type": "module",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "require": "./dist/index.cjs",
25
+ "import": "./dist/index.js"
26
+ },
27
+ "./esbuild": {
28
+ "types": "./dist/esbuild.d.ts",
29
+ "require": "./dist/esbuild.cjs",
30
+ "import": "./dist/esbuild.js"
31
+ },
32
+ "./vite": {
33
+ "types": "./dist/vite.d.ts",
34
+ "require": "./dist/vite.cjs",
35
+ "import": "./dist/vite.js"
36
+ },
37
+ "./rspack": {
38
+ "types": "./dist/rspack.d.ts",
39
+ "require": "./dist/rspack.cjs",
40
+ "import": "./dist/rspack.js"
41
+ },
42
+ "./types": {
43
+ "types": "./dist/types.d.ts",
44
+ "import": "./dist/types.js",
45
+ "require": "./dist/types.cjs"
46
+ },
47
+ "./*": "./*"
48
+ },
49
+ "scripts": {
50
+ "dev": "tsup src/*ts --watch src",
51
+ "build": "tsup src/*.ts --format cjs,esm --dts --splitting --clean",
52
+ "test": "vitest",
53
+ "test:update": "vitest -u",
54
+ "test:coverage": "vitest --coverage"
55
+ },
56
+ "engines": {
57
+ "node": ">=16.14.0"
58
+ },
59
+ "dependencies": {
60
+ "@babel/core": "^7.23.6",
61
+ "@babel/plugin-syntax-flow": "^7.23.3",
62
+ "@babel/plugin-syntax-jsx": "^7.23.3",
63
+ "@babel/plugin-syntax-typescript": "^7.23.3",
64
+ "@rollup/pluginutils": "^5.1.0",
65
+ "@stylexjs/babel-plugin": "^0.3.0",
66
+ "unplugin": "^1.5.1"
67
+ },
68
+ "devDependencies": {
69
+ "@types/node": "^20.10.5",
70
+ "tsup": "^8.0.1",
71
+ "typescript": "^5.3.3",
72
+ "vite": "^5.0.10",
73
+ "vitest": "^1.1.0"
74
+ }
75
+ }
@@ -0,0 +1,2 @@
1
+ packages:
2
+ - examples/*
@@ -0,0 +1,7 @@
1
+ export const PLUGIN_NAME = 'unplugin-stylex'
2
+
3
+ export const isDevelopment = (
4
+ process.env.NODE_ENV !== 'production' ||
5
+ process.env.BABEL_ENV !== 'production'
6
+ )
7
+
@@ -0,0 +1,61 @@
1
+ import * as fs from 'fs/promises'
2
+ import * as path from 'path'
3
+
4
+ import { transformAsync } from '@babel/core'
5
+ import flowSyntaxPlugin from '@babel/plugin-syntax-flow'
6
+ import jsxSyntaxPlugin from '@babel/plugin-syntax-jsx'
7
+ import typescriptSyntaxPlugin from '@babel/plugin-syntax-typescript'
8
+ import stylexBabelPlugin from '@stylexjs/babel-plugin'
9
+
10
+ export async function transformer(context) {
11
+ const { id, source, options } = context
12
+ const stylex = options.stylex
13
+ const extname = path.extname(id)
14
+ const stylexRules = {}
15
+
16
+ if (stylex.stylexImports.some((importName) => source.includes(importName))) {
17
+ const originSource = stylex.babelConfig.babelrc
18
+ ? await fs.readFile(id, 'utf-8')
19
+ : source
20
+
21
+ const { code, map, metadata } = await transformAsync(
22
+ originSource,
23
+ {
24
+ babelrc: stylex.babelConfig.babelrc,
25
+ filename: id,
26
+ plugins: [
27
+ ...(stylex.babelConfig.plugins || []),
28
+ extname === '.ts'
29
+ ? typescriptSyntaxPlugin
30
+ : extname === '.tsx'
31
+ ? [typescriptSyntaxPlugin, { isTSX: true }]
32
+ : flowSyntaxPlugin,
33
+
34
+ jsxSyntaxPlugin,
35
+ [
36
+ stylexBabelPlugin,
37
+ {
38
+ dev: options.dev,
39
+ unstable_moduleResolution: stylex.unstable_moduleResolution,
40
+ importSources: stylex.stylexImports,
41
+ ...stylex,
42
+ }
43
+ ],
44
+ ],
45
+ presets: stylex.babelConfig.presets,
46
+ }
47
+ )
48
+
49
+ if (metadata.stylex && metadata.stylex.length > 0) {
50
+ stylexRules[id] = metadata.stylex
51
+ }
52
+
53
+ if (!stylex.babelConfig.babelrc) {
54
+ return { code, map, stylexRules }
55
+ }
56
+
57
+ return { code, stylexRules }
58
+ }
59
+
60
+ return { code: source }
61
+ }
@@ -0,0 +1,26 @@
1
+ import type { UnpluginStylexOptions, BabelConfig } from '@/types'
2
+ import { isDevelopment } from './constants'
3
+
4
+ export function getOptions(options: UnpluginStylexOptions = ({} as UnpluginStylexOptions)) {
5
+ const stylex = options.stylex || {}
6
+
7
+ return {
8
+ ...options,
9
+ dev: options.dev || isDevelopment,
10
+ include: options.include || [/\.[jt]sx?$/],
11
+ exclude: options.exclude || [/node_modules/, /\.git/],
12
+ enforce: options.enforce || 'pre',
13
+ stylex: {
14
+ ...stylex,
15
+ filename: stylex.filename || 'stylex.css',
16
+ stylexImports: stylex.stylexImports || ['stylex', '@stylexjs/stylex'],
17
+ useCSSLayers: stylex.useCSSLayers || false,
18
+ unstable_moduleResolution: stylex.unstable_moduleResolution || { type: 'commonJS', rootDir: process.cwd() },
19
+ babelConfig: {
20
+ babelrc: (stylex.babelConfig || {}).babelrc || false,
21
+ plugins: (stylex.babelConfig || {}).plugins || [],
22
+ presets: (stylex.babelConfig || {}).presets || [],
23
+ },
24
+ },
25
+ }
26
+ }
package/src/esbuild.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { createEsbuildPlugin } from 'unplugin'
2
+ import { unpluginFactory } from '.'
3
+
4
+ export default createEsbuildPlugin(unpluginFactory)
package/src/index.ts ADDED
@@ -0,0 +1,61 @@
1
+ import { createFilter } from '@rollup/pluginutils'
2
+ import stylexBabelPlugin from '@stylexjs/babel-plugin'
3
+ import { createUnplugin } from 'unplugin'
4
+ import type { UnpluginFactory } from 'unplugin'
5
+
6
+ import { PLUGIN_NAME } from './core/constants'
7
+ import { getOptions } from './core/utils'
8
+ import { transformer } from './core/transformer'
9
+ import type { UnpluginStylexOptions } from './types'
10
+
11
+ export const unpluginFactory: UnpluginFactory<UnpluginStylexOptions | undefined> = (rawOptions = {}, meta) => {
12
+ const options = getOptions(rawOptions)
13
+ const filter = createFilter(options.include, options.exclude)
14
+ const stylexRules = {}
15
+
16
+ return {
17
+ name: PLUGIN_NAME,
18
+ enforce: options.enforce,
19
+
20
+ transformInclude(id) {
21
+ return filter(id)
22
+ },
23
+
24
+ async transform(source, id) {
25
+ const context = {
26
+ pluginContext: this,
27
+ options,
28
+ source,
29
+ id,
30
+ }
31
+
32
+ try {
33
+ const result = await transformer(context)
34
+
35
+ if (result.stylexRules && result.stylexRules[id]) {
36
+ stylexRules[id] = result.stylexRules[id]
37
+ }
38
+
39
+ return result
40
+ } catch (error) {
41
+ this.error(error)
42
+ }
43
+ },
44
+
45
+ buildEnd() {
46
+ const rules = Object.values(stylexRules).flat()
47
+ if (rules.length > 0) {
48
+ const collectedCSS = (stylexBabelPlugin as any).processStylexRules(rules, options.stylex.useCSSLayers)
49
+ this.emitFile({
50
+ fileName: options.stylex.filename,
51
+ source: collectedCSS,
52
+ type: 'asset',
53
+ })
54
+ }
55
+ },
56
+ }
57
+ }
58
+
59
+ export const unplugin = createUnplugin(unpluginFactory)
60
+
61
+ export default unplugin
package/src/rollup.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { isDevelopment } from './core/constants'
2
+
3
+ export default (options?) => {
4
+ if (isDevelopment || options?.dev) {
5
+ throw new Error(`If you want to use this plugin through rollup, please use "@stylexjs/rollup-plugin" instead`)
6
+ }
7
+ }
package/src/rspack.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { createRspackPlugin } from 'unplugin'
2
+ import { unpluginFactory } from '.'
3
+
4
+ export default createRspackPlugin(unpluginFactory) as any
package/src/types.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type { FilterPattern } from '@rollup/pluginutils'
2
+
3
+ export type BabelConfig = {
4
+ plugins: any[]
5
+ presets: any[]
6
+ babelrc: boolean
7
+ }
8
+
9
+ type StylexOptions = {
10
+ filename?: string
11
+ stylexImports?: string[]
12
+ classNamePrefix?: string
13
+ useCSSLayers?: boolean
14
+ unstable_moduleResolution?: {
15
+ type: "commonjs" | "haste"
16
+ rootDir: string
17
+ }
18
+ babelConfig?: BabelConfig
19
+ }
20
+
21
+ export type UnpluginStylexOptions = {
22
+ dev?: boolean
23
+ include?: FilterPattern
24
+ exclude?: FilterPattern
25
+ enforce?: "post" | "pre"
26
+ stylex?: StylexOptions
27
+ }
package/src/vite.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { createVitePlugin } from 'unplugin'
2
+ import { unpluginFactory } from '.'
3
+
4
+ export default createVitePlugin(unpluginFactory) as any
package/src/webpack.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { isDevelopment } from './core/constants'
2
+
3
+ export default (options?) => {
4
+ if (isDevelopment || options?.dev) {
5
+ throw new Error(`If you want to use this plugin through webpack, please use "@stylexjs/webpack-plugin" instead`)
6
+ }
7
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".",
4
+ "declaration": true,
5
+ "declarationMap": true,
6
+ "declarationDir": "dist",
7
+ "outDir": "./dist",
8
+ "lib": ["ESNext"],
9
+ "moduleResolution": "Node",
10
+ "preserveSymlinks": true,
11
+ "paths": {
12
+ "@/*": ["./src/*"]
13
+ }
14
+ },
15
+ "include": [
16
+ "src"
17
+ ]
18
+ }