@stylexswc/unplugin 0.4.1-rc.2
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.
- package/LICENSE +21 -0
- package/README.md +131 -0
- package/dist/astro.cjs +21 -0
- package/dist/astro.d.cts +6 -0
- package/dist/astro.d.ts +6 -0
- package/dist/astro.js +19 -0
- package/dist/chunk-35OMBCRC.js +11 -0
- package/dist/chunk-6F4PWJZI.js +0 -0
- package/dist/chunk-6JD3XP4C.js +221 -0
- package/dist/chunk-CH55POH7.cjs +11 -0
- package/dist/chunk-EHGZXCQ7.js +11 -0
- package/dist/chunk-HSQ4VL6M.cjs +221 -0
- package/dist/chunk-Q6JHATKS.cjs +11 -0
- package/dist/chunk-ZBPRDZS4.cjs +1 -0
- package/dist/esbuild.cjs +13 -0
- package/dist/esbuild.d.cts +7 -0
- package/dist/esbuild.d.ts +7 -0
- package/dist/esbuild.js +11 -0
- package/dist/farm.cjs +13 -0
- package/dist/farm.d.cts +7 -0
- package/dist/farm.d.ts +7 -0
- package/dist/farm.js +11 -0
- package/dist/index.cjs +11 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +11 -0
- package/dist/nuxt.cjs +30 -0
- package/dist/nuxt.d.cts +9 -0
- package/dist/nuxt.d.ts +9 -0
- package/dist/nuxt.js +28 -0
- package/dist/rollup.cjs +13 -0
- package/dist/rollup.d.cts +7 -0
- package/dist/rollup.d.ts +7 -0
- package/dist/rollup.js +11 -0
- package/dist/rspack.cjs +13 -0
- package/dist/rspack.d.cts +6 -0
- package/dist/rspack.d.ts +6 -0
- package/dist/rspack.js +11 -0
- package/dist/types.cjs +1 -0
- package/dist/types.d.cts +10 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.js +1 -0
- package/dist/vite.cjs +10 -0
- package/dist/vite.d.cts +7 -0
- package/dist/vite.d.ts +7 -0
- package/dist/vite.js +8 -0
- package/dist/webpack.cjs +10 -0
- package/dist/webpack.d.cts +7 -0
- package/dist/webpack.d.ts +7 -0
- package/dist/webpack.js +8 -0
- package/package.json +163 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Vladislav Buinovski
|
|
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,131 @@
|
|
|
1
|
+
# Unplugin with NAPI-RS StyleX compiler integration
|
|
2
|
+
|
|
3
|
+
`Uplugin` for an unofficial
|
|
4
|
+
[`napi-rs`](https://github.com/dwlad90/stylex-swc-plugin/tree/develop/crates/stylex-rs-compiler)
|
|
5
|
+
compiler that includes the StyleX SWC code transformation under the hood.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
To install the package, run the following command:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install --save-dev @stylexswc/unplugin
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
To use the plugin, you need to add it to your build tool configuration.
|
|
18
|
+
|
|
19
|
+
For every plugin have an example of how to use it in
|
|
20
|
+
[`apps/{pluginName}-unplugin-example`](https://github.com/Dwlad90/stylex-swc-plugin/tree/develop/apps)
|
|
21
|
+
folder.
|
|
22
|
+
|
|
23
|
+
## Plugins
|
|
24
|
+
|
|
25
|
+
<details>
|
|
26
|
+
<summary>Vite</summary><br>
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
// vite.config.ts
|
|
30
|
+
import StylexRsPlugin from '@stylexswc/unplugin/vite';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [
|
|
34
|
+
StylexRsPlugin({
|
|
35
|
+
/* options */
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
<br></details>
|
|
42
|
+
|
|
43
|
+
<details>
|
|
44
|
+
<summary>Rollup</summary><br>
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
// rollup.config.js
|
|
48
|
+
import StylexRsPlugin from '@stylexswc/unplugin/rollup';
|
|
49
|
+
|
|
50
|
+
export default {
|
|
51
|
+
plugins: [
|
|
52
|
+
StylexRsPlugin({
|
|
53
|
+
/* options */
|
|
54
|
+
}),
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
<br></details>
|
|
60
|
+
|
|
61
|
+
<details>
|
|
62
|
+
<summary>Webpack</summary><br>
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
// webpack.config.js
|
|
66
|
+
module.exports = {
|
|
67
|
+
/* ... */
|
|
68
|
+
plugins: [
|
|
69
|
+
require('@stylexswc/unplugin/webpack')({
|
|
70
|
+
/* options */
|
|
71
|
+
}),
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
<br></details>
|
|
77
|
+
|
|
78
|
+
<details>
|
|
79
|
+
<summary>Nuxt</summary><br>
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
// nuxt.config.js
|
|
83
|
+
export default defineNuxtConfig({
|
|
84
|
+
modules: [
|
|
85
|
+
[
|
|
86
|
+
'@stylexswc/unplugin/nuxt',
|
|
87
|
+
{
|
|
88
|
+
/* options */
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
> This module works for both Nuxt 2 and
|
|
96
|
+
> [Nuxt Vite](https://github.com/nuxt/vite)
|
|
97
|
+
|
|
98
|
+
<br></details>
|
|
99
|
+
|
|
100
|
+
<details>
|
|
101
|
+
<summary>Vue CLI</summary><br>
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// vue.config.js
|
|
105
|
+
module.exports = {
|
|
106
|
+
configureWebpack: {
|
|
107
|
+
plugins: [
|
|
108
|
+
require('@stylexswc/unplugin/webpack')({
|
|
109
|
+
/* options */
|
|
110
|
+
}),
|
|
111
|
+
],
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
<br></details>
|
|
117
|
+
|
|
118
|
+
<details>
|
|
119
|
+
<summary>esbuild</summary><br>
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
// esbuild.config.js
|
|
123
|
+
import { build } from 'esbuild';
|
|
124
|
+
import StylexRsPlugin from '@stylexswc/unplugin/esbuild';
|
|
125
|
+
|
|
126
|
+
build({
|
|
127
|
+
plugins: [StylexRsPlugin()],
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
<br></details>
|
package/dist/astro.cjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkHSQ4VL6Mcjs = require('./chunk-HSQ4VL6M.cjs');
|
|
4
|
+
require('./chunk-ZBPRDZS4.cjs');
|
|
5
|
+
|
|
6
|
+
// src/astro.ts
|
|
7
|
+
var astro_default = (options) => ({
|
|
8
|
+
name: "unplugin-starter",
|
|
9
|
+
hooks: {
|
|
10
|
+
"astro:config:setup": async (astro) => {
|
|
11
|
+
var _a;
|
|
12
|
+
(_a = astro.config.vite).plugins || (_a.plugins = []);
|
|
13
|
+
astro.config.vite.plugins.push(_chunkHSQ4VL6Mcjs.src_default.vite(options));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
exports.default = astro_default;
|
|
20
|
+
|
|
21
|
+
module.exports = exports.default;
|
package/dist/astro.d.cts
ADDED
package/dist/astro.d.ts
ADDED
package/dist/astro.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
src_default
|
|
3
|
+
} from "./chunk-6JD3XP4C.js";
|
|
4
|
+
import "./chunk-6F4PWJZI.js";
|
|
5
|
+
|
|
6
|
+
// src/astro.ts
|
|
7
|
+
var astro_default = (options) => ({
|
|
8
|
+
name: "unplugin-starter",
|
|
9
|
+
hooks: {
|
|
10
|
+
"astro:config:setup": async (astro) => {
|
|
11
|
+
var _a;
|
|
12
|
+
(_a = astro.config.vite).plugins || (_a.plugins = []);
|
|
13
|
+
astro.config.vite.plugins.push(src_default.vite(options));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
export {
|
|
18
|
+
astro_default as default
|
|
19
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
import * as path from "node:path";
|
|
23
|
+
import { promises } from "node:fs";
|
|
24
|
+
import { createUnplugin } from "unplugin";
|
|
25
|
+
|
|
26
|
+
// src/utils/getStyleXRules.ts
|
|
27
|
+
import stylexBabelPlugin from "@stylexjs/babel-plugin";
|
|
28
|
+
function getStyleXRules(stylexRules, useCSSLayers) {
|
|
29
|
+
const rules = Object.values(stylexRules).flat();
|
|
30
|
+
if (!rules.length) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const allRules = rules.filter((rule) => !!rule);
|
|
34
|
+
return stylexBabelPlugin.processStylexRules(allRules, useCSSLayers);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/utils/normalizeOptions.ts
|
|
38
|
+
var IS_DEV_ENV = process.env.NODE_ENV === "development";
|
|
39
|
+
function normalizeOptions(options) {
|
|
40
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
41
|
+
return __spreadProps(__spreadValues({}, options), {
|
|
42
|
+
fileName: (_a = options.fileName) != null ? _a : "stylex.css",
|
|
43
|
+
useCSSLayers: (_b = options.useCSSLayers) != null ? _b : false,
|
|
44
|
+
pageExtensions: (_c = options.pageExtensions) != null ? _c : ["tsx", "jsx", "js", "ts"],
|
|
45
|
+
rsOptions: __spreadProps(__spreadValues({}, options.rsOptions), {
|
|
46
|
+
dev: (_e = (_d = options.rsOptions) == null ? void 0 : _d.dev) != null ? _e : IS_DEV_ENV,
|
|
47
|
+
unstable_moduleResolution: (_g = (_f = options.rsOptions) == null ? void 0 : _f.unstable_moduleResolution) != null ? _g : {
|
|
48
|
+
type: "commonJS",
|
|
49
|
+
rootDir: process.cwd()
|
|
50
|
+
},
|
|
51
|
+
importSources: (_i = (_h = options.rsOptions) == null ? void 0 : _h.importSources) != null ? _i : ["stylex", "@stylexjs/stylex"]
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/index.ts
|
|
57
|
+
import stylexRsCompiler from "@stylexswc/rs-compiler";
|
|
58
|
+
|
|
59
|
+
// src/utils/generateHash.ts
|
|
60
|
+
import crypto from "crypto";
|
|
61
|
+
function generateHash(content) {
|
|
62
|
+
return crypto.createHash("sha256").update(content).digest("hex");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/index.ts
|
|
66
|
+
var { writeFile, mkdir } = promises;
|
|
67
|
+
var unpluginFactory = (options = {}) => {
|
|
68
|
+
const normalizedOptions = normalizeOptions(options);
|
|
69
|
+
const stylexRules = {};
|
|
70
|
+
let viteConfig = null;
|
|
71
|
+
let hasCssToExtract = false;
|
|
72
|
+
return {
|
|
73
|
+
name: "unplugin-stylex-rs",
|
|
74
|
+
transformInclude(id) {
|
|
75
|
+
const pageExtensions = normalizedOptions.pageExtensions;
|
|
76
|
+
const extensionName = path.extname(id);
|
|
77
|
+
const questionSignIndex = extensionName.indexOf("?");
|
|
78
|
+
let cleanedExtensionName = questionSignIndex > -1 ? extensionName.slice(0, questionSignIndex) : extensionName;
|
|
79
|
+
if (cleanedExtensionName.startsWith(".")) {
|
|
80
|
+
cleanedExtensionName = cleanedExtensionName.slice(1);
|
|
81
|
+
}
|
|
82
|
+
return pageExtensions.includes(cleanedExtensionName);
|
|
83
|
+
},
|
|
84
|
+
async transform(inputCode, id) {
|
|
85
|
+
var _a;
|
|
86
|
+
const dir = path.dirname(id);
|
|
87
|
+
const basename2 = path.basename(id);
|
|
88
|
+
const file = path.join(dir, basename2.includes("?") ? basename2.split("?")[0] : basename2);
|
|
89
|
+
if (!((_a = normalizedOptions.rsOptions.importSources) == null ? void 0 : _a.some(
|
|
90
|
+
(importName) => typeof importName === "string" ? inputCode.includes(importName) : inputCode.includes(importName.from)
|
|
91
|
+
))) {
|
|
92
|
+
return {
|
|
93
|
+
code: inputCode
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const { code, metadata, map } = stylexRsCompiler.transform(
|
|
98
|
+
file,
|
|
99
|
+
inputCode,
|
|
100
|
+
normalizedOptions.rsOptions
|
|
101
|
+
);
|
|
102
|
+
if (metadata.stylex && metadata.stylex.length > 0) {
|
|
103
|
+
stylexRules[id] = metadata.stylex;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
code,
|
|
107
|
+
map,
|
|
108
|
+
stylexRules
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error("Tansformation error:", error);
|
|
112
|
+
this.error(error);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
buildEnd() {
|
|
116
|
+
var _a;
|
|
117
|
+
const framework = (_a = this.getNativeBuildContext) == null ? void 0 : _a.call(this).framework;
|
|
118
|
+
if (framework === "esbuild") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
122
|
+
if (!collectedCSS) return;
|
|
123
|
+
hasCssToExtract = true;
|
|
124
|
+
this.emitFile({
|
|
125
|
+
fileName: normalizedOptions.fileName,
|
|
126
|
+
source: collectedCSS,
|
|
127
|
+
type: "asset"
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
vite: {
|
|
131
|
+
config(config) {
|
|
132
|
+
viteConfig = {
|
|
133
|
+
build: config.build,
|
|
134
|
+
base: config.base
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
configResolved(config) {
|
|
138
|
+
config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
|
|
139
|
+
config.optimizeDeps.exclude.push("@stylexjs/open-props");
|
|
140
|
+
},
|
|
141
|
+
buildEnd() {
|
|
142
|
+
var _a, _b;
|
|
143
|
+
const fileName = `${(_b = (_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir) != null ? _b : "assets"}/${normalizedOptions.fileName}`;
|
|
144
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
145
|
+
if (!collectedCSS) return;
|
|
146
|
+
this.emitFile({
|
|
147
|
+
fileName,
|
|
148
|
+
source: collectedCSS,
|
|
149
|
+
type: "asset"
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
transformIndexHtml(html, ctx) {
|
|
153
|
+
var _a, _b, _c, _d;
|
|
154
|
+
const fileName = `${(_b = (_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir) != null ? _b : "assets"}/${normalizedOptions.fileName}`;
|
|
155
|
+
const css = (_c = ctx.bundle) == null ? void 0 : _c[fileName];
|
|
156
|
+
if (!css) {
|
|
157
|
+
return html;
|
|
158
|
+
}
|
|
159
|
+
const publicPath = path.posix.join((_d = viteConfig == null ? void 0 : viteConfig.base) != null ? _d : "/", fileName.replace(/\\/g, "/"));
|
|
160
|
+
return [
|
|
161
|
+
{
|
|
162
|
+
tag: "link",
|
|
163
|
+
attrs: {
|
|
164
|
+
rel: "stylesheet",
|
|
165
|
+
href: publicPath
|
|
166
|
+
},
|
|
167
|
+
injectTo: "head"
|
|
168
|
+
}
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
esbuild: {
|
|
173
|
+
setup(build) {
|
|
174
|
+
build.onEnd(async ({ outputFiles }) => {
|
|
175
|
+
const fileName = normalizedOptions.fileName;
|
|
176
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
177
|
+
if (!collectedCSS) return;
|
|
178
|
+
const shouldWriteToDisk = build.initialOptions.write === void 0 || build.initialOptions.write;
|
|
179
|
+
if (shouldWriteToDisk) {
|
|
180
|
+
const generatedCSSFileName = path.join(process.cwd(), fileName);
|
|
181
|
+
await mkdir(path.dirname(generatedCSSFileName), {
|
|
182
|
+
recursive: true
|
|
183
|
+
});
|
|
184
|
+
await writeFile(generatedCSSFileName, collectedCSS, "utf8");
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (outputFiles !== void 0) {
|
|
188
|
+
outputFiles.push({
|
|
189
|
+
path: "<stdout>",
|
|
190
|
+
contents: new TextEncoder().encode(collectedCSS),
|
|
191
|
+
hash: generateHash(collectedCSS),
|
|
192
|
+
get text() {
|
|
193
|
+
return collectedCSS;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
farm: {
|
|
201
|
+
transformHtml: {
|
|
202
|
+
executor(resource) {
|
|
203
|
+
if (!hasCssToExtract) return Promise.resolve(resource.htmlResource);
|
|
204
|
+
const htmlResource = resource.htmlResource;
|
|
205
|
+
let htmlContent = Buffer.from(htmlResource.bytes).toString("utf-8");
|
|
206
|
+
htmlContent = `${htmlContent}<link rel="stylesheet" href="/${normalizedOptions.fileName}" />`;
|
|
207
|
+
htmlResource.bytes = [...Buffer.from(htmlContent, "utf-8")];
|
|
208
|
+
return Promise.resolve(resource.htmlResource);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
var unplugin = createUnplugin(unpluginFactory);
|
|
215
|
+
var src_default = unplugin;
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
unpluginFactory,
|
|
219
|
+
unplugin,
|
|
220
|
+
src_default
|
|
221
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
var _chunkHSQ4VL6Mcjs = require('./chunk-HSQ4VL6M.cjs');
|
|
4
|
+
|
|
5
|
+
// src/webpack.ts
|
|
6
|
+
var _unplugin = require('unplugin');
|
|
7
|
+
var webpack_default = _unplugin.createWebpackPlugin.call(void 0, _chunkHSQ4VL6Mcjs.unpluginFactory);
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
exports.webpack_default = webpack_default;
|
|
@@ -0,0 +1,221 @@
|
|
|
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 }; }var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
23
|
+
var _fs = require('fs');
|
|
24
|
+
var _unplugin = require('unplugin');
|
|
25
|
+
|
|
26
|
+
// src/utils/getStyleXRules.ts
|
|
27
|
+
var _babelplugin = require('@stylexjs/babel-plugin'); var _babelplugin2 = _interopRequireDefault(_babelplugin);
|
|
28
|
+
function getStyleXRules(stylexRules, useCSSLayers) {
|
|
29
|
+
const rules = Object.values(stylexRules).flat();
|
|
30
|
+
if (!rules.length) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const allRules = rules.filter((rule) => !!rule);
|
|
34
|
+
return _babelplugin2.default.processStylexRules(allRules, useCSSLayers);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/utils/normalizeOptions.ts
|
|
38
|
+
var IS_DEV_ENV = process.env.NODE_ENV === "development";
|
|
39
|
+
function normalizeOptions(options) {
|
|
40
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
41
|
+
return __spreadProps(__spreadValues({}, options), {
|
|
42
|
+
fileName: (_a = options.fileName) != null ? _a : "stylex.css",
|
|
43
|
+
useCSSLayers: (_b = options.useCSSLayers) != null ? _b : false,
|
|
44
|
+
pageExtensions: (_c = options.pageExtensions) != null ? _c : ["tsx", "jsx", "js", "ts"],
|
|
45
|
+
rsOptions: __spreadProps(__spreadValues({}, options.rsOptions), {
|
|
46
|
+
dev: (_e = (_d = options.rsOptions) == null ? void 0 : _d.dev) != null ? _e : IS_DEV_ENV,
|
|
47
|
+
unstable_moduleResolution: (_g = (_f = options.rsOptions) == null ? void 0 : _f.unstable_moduleResolution) != null ? _g : {
|
|
48
|
+
type: "commonJS",
|
|
49
|
+
rootDir: process.cwd()
|
|
50
|
+
},
|
|
51
|
+
importSources: (_i = (_h = options.rsOptions) == null ? void 0 : _h.importSources) != null ? _i : ["stylex", "@stylexjs/stylex"]
|
|
52
|
+
})
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/index.ts
|
|
57
|
+
var _rscompiler = require('@stylexswc/rs-compiler'); var _rscompiler2 = _interopRequireDefault(_rscompiler);
|
|
58
|
+
|
|
59
|
+
// src/utils/generateHash.ts
|
|
60
|
+
var _crypto = require('crypto'); var _crypto2 = _interopRequireDefault(_crypto);
|
|
61
|
+
function generateHash(content) {
|
|
62
|
+
return _crypto2.default.createHash("sha256").update(content).digest("hex");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/index.ts
|
|
66
|
+
var { writeFile, mkdir } = _fs.promises;
|
|
67
|
+
var unpluginFactory = (options = {}) => {
|
|
68
|
+
const normalizedOptions = normalizeOptions(options);
|
|
69
|
+
const stylexRules = {};
|
|
70
|
+
let viteConfig = null;
|
|
71
|
+
let hasCssToExtract = false;
|
|
72
|
+
return {
|
|
73
|
+
name: "unplugin-stylex-rs",
|
|
74
|
+
transformInclude(id) {
|
|
75
|
+
const pageExtensions = normalizedOptions.pageExtensions;
|
|
76
|
+
const extensionName = path.extname(id);
|
|
77
|
+
const questionSignIndex = extensionName.indexOf("?");
|
|
78
|
+
let cleanedExtensionName = questionSignIndex > -1 ? extensionName.slice(0, questionSignIndex) : extensionName;
|
|
79
|
+
if (cleanedExtensionName.startsWith(".")) {
|
|
80
|
+
cleanedExtensionName = cleanedExtensionName.slice(1);
|
|
81
|
+
}
|
|
82
|
+
return pageExtensions.includes(cleanedExtensionName);
|
|
83
|
+
},
|
|
84
|
+
async transform(inputCode, id) {
|
|
85
|
+
var _a;
|
|
86
|
+
const dir = path.dirname(id);
|
|
87
|
+
const basename2 = path.basename(id);
|
|
88
|
+
const file = path.join(dir, basename2.includes("?") ? basename2.split("?")[0] : basename2);
|
|
89
|
+
if (!((_a = normalizedOptions.rsOptions.importSources) == null ? void 0 : _a.some(
|
|
90
|
+
(importName) => typeof importName === "string" ? inputCode.includes(importName) : inputCode.includes(importName.from)
|
|
91
|
+
))) {
|
|
92
|
+
return {
|
|
93
|
+
code: inputCode
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const { code, metadata, map } = _rscompiler2.default.transform(
|
|
98
|
+
file,
|
|
99
|
+
inputCode,
|
|
100
|
+
normalizedOptions.rsOptions
|
|
101
|
+
);
|
|
102
|
+
if (metadata.stylex && metadata.stylex.length > 0) {
|
|
103
|
+
stylexRules[id] = metadata.stylex;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
code,
|
|
107
|
+
map,
|
|
108
|
+
stylexRules
|
|
109
|
+
};
|
|
110
|
+
} catch (error) {
|
|
111
|
+
console.error("Tansformation error:", error);
|
|
112
|
+
this.error(error);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
buildEnd() {
|
|
116
|
+
var _a;
|
|
117
|
+
const framework = (_a = this.getNativeBuildContext) == null ? void 0 : _a.call(this).framework;
|
|
118
|
+
if (framework === "esbuild") {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
122
|
+
if (!collectedCSS) return;
|
|
123
|
+
hasCssToExtract = true;
|
|
124
|
+
this.emitFile({
|
|
125
|
+
fileName: normalizedOptions.fileName,
|
|
126
|
+
source: collectedCSS,
|
|
127
|
+
type: "asset"
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
vite: {
|
|
131
|
+
config(config) {
|
|
132
|
+
viteConfig = {
|
|
133
|
+
build: config.build,
|
|
134
|
+
base: config.base
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
configResolved(config) {
|
|
138
|
+
config.optimizeDeps.exclude = config.optimizeDeps.exclude || [];
|
|
139
|
+
config.optimizeDeps.exclude.push("@stylexjs/open-props");
|
|
140
|
+
},
|
|
141
|
+
buildEnd() {
|
|
142
|
+
var _a, _b;
|
|
143
|
+
const fileName = `${(_b = (_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir) != null ? _b : "assets"}/${normalizedOptions.fileName}`;
|
|
144
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
145
|
+
if (!collectedCSS) return;
|
|
146
|
+
this.emitFile({
|
|
147
|
+
fileName,
|
|
148
|
+
source: collectedCSS,
|
|
149
|
+
type: "asset"
|
|
150
|
+
});
|
|
151
|
+
},
|
|
152
|
+
transformIndexHtml(html, ctx) {
|
|
153
|
+
var _a, _b, _c, _d;
|
|
154
|
+
const fileName = `${(_b = (_a = viteConfig == null ? void 0 : viteConfig.build) == null ? void 0 : _a.assetsDir) != null ? _b : "assets"}/${normalizedOptions.fileName}`;
|
|
155
|
+
const css = (_c = ctx.bundle) == null ? void 0 : _c[fileName];
|
|
156
|
+
if (!css) {
|
|
157
|
+
return html;
|
|
158
|
+
}
|
|
159
|
+
const publicPath = path.posix.join((_d = viteConfig == null ? void 0 : viteConfig.base) != null ? _d : "/", fileName.replace(/\\/g, "/"));
|
|
160
|
+
return [
|
|
161
|
+
{
|
|
162
|
+
tag: "link",
|
|
163
|
+
attrs: {
|
|
164
|
+
rel: "stylesheet",
|
|
165
|
+
href: publicPath
|
|
166
|
+
},
|
|
167
|
+
injectTo: "head"
|
|
168
|
+
}
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
esbuild: {
|
|
173
|
+
setup(build) {
|
|
174
|
+
build.onEnd(async ({ outputFiles }) => {
|
|
175
|
+
const fileName = normalizedOptions.fileName;
|
|
176
|
+
const collectedCSS = getStyleXRules(stylexRules, normalizedOptions.useCSSLayers);
|
|
177
|
+
if (!collectedCSS) return;
|
|
178
|
+
const shouldWriteToDisk = build.initialOptions.write === void 0 || build.initialOptions.write;
|
|
179
|
+
if (shouldWriteToDisk) {
|
|
180
|
+
const generatedCSSFileName = path.join(process.cwd(), fileName);
|
|
181
|
+
await mkdir(path.dirname(generatedCSSFileName), {
|
|
182
|
+
recursive: true
|
|
183
|
+
});
|
|
184
|
+
await writeFile(generatedCSSFileName, collectedCSS, "utf8");
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (outputFiles !== void 0) {
|
|
188
|
+
outputFiles.push({
|
|
189
|
+
path: "<stdout>",
|
|
190
|
+
contents: new TextEncoder().encode(collectedCSS),
|
|
191
|
+
hash: generateHash(collectedCSS),
|
|
192
|
+
get text() {
|
|
193
|
+
return collectedCSS;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
farm: {
|
|
201
|
+
transformHtml: {
|
|
202
|
+
executor(resource) {
|
|
203
|
+
if (!hasCssToExtract) return Promise.resolve(resource.htmlResource);
|
|
204
|
+
const htmlResource = resource.htmlResource;
|
|
205
|
+
let htmlContent = Buffer.from(htmlResource.bytes).toString("utf-8");
|
|
206
|
+
htmlContent = `${htmlContent}<link rel="stylesheet" href="/${normalizedOptions.fileName}" />`;
|
|
207
|
+
htmlResource.bytes = [...Buffer.from(htmlContent, "utf-8")];
|
|
208
|
+
return Promise.resolve(resource.htmlResource);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
var unplugin = _unplugin.createUnplugin.call(void 0, unpluginFactory);
|
|
215
|
+
var src_default = unplugin;
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
exports.unpluginFactory = unpluginFactory; exports.unplugin = unplugin; exports.src_default = src_default;
|