@stylexswc/rollup-plugin 0.4.0-rc.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.
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,55 @@
1
+ # Webpack plugin with NAPI-RS StyleX compiler integration
2
+
3
+ `Webpack plugin` for an unofficial
4
+ [`napi-rs`](https://github.com/dwlad90/stylex-swc-plugin/tree/master/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/webpack-plugin
13
+ ```
14
+
15
+ Please install `@stylexswc/rs-compiler` if you haven't done so already:
16
+
17
+ ```bash
18
+ npm install --save-dev @stylexswc/rs-compiler
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Modify Webpack config. For example:
24
+
25
+ ```js
26
+ const StylexPlugin = require('@stylexswc/webpack-plugin');
27
+ const path = require('path');
28
+
29
+ const config = (env, argv) => ({
30
+ entry: {
31
+ main: './js/index.js',
32
+ },
33
+ output: {
34
+ path: path.resolve(__dirname, 'dist'),
35
+ filename: '[name].js',
36
+ },
37
+ plugins: [
38
+ new StylexPlugin({
39
+ filename: 'styles.[contenthash].css',
40
+ dev: argv.mode === 'development',
41
+ }),
42
+ ],
43
+ cache: true,
44
+ });
45
+
46
+ module.exports = config;
47
+ ```
48
+
49
+ ## Plugin Options
50
+ The options are similar like `@stylexjs/babel-plugin` and can be found [here](https://stylexjs.com/docs/api/configuration/babel-plugin/)
51
+
52
+ ## Documentation
53
+
54
+ * [StyleX Documentation](https://stylexjs.com)
55
+ * [NAPI-RS compiler for StyleX](https://github.com/Dwlad90/stylex-swc-plugin/tree/master/crates/stylex-rs-compiler)
@@ -0,0 +1,11 @@
1
+ import type { TransformOptions } from 'lightningcss';
2
+ import type { Plugin } from 'rollup';
3
+ import type { StyleXOptions } from '@stylexswc/rs-compiler';
4
+ export type PluginOptions = {
5
+ rsOptions?: StyleXOptions;
6
+ fileName?: string;
7
+ useCSSLayers?: boolean;
8
+ lightningcssOptions?: Omit<TransformOptions<{}>, 'code' | 'filename' | 'visitor'>;
9
+ };
10
+ export default function stylexPlugin({ rsOptions, fileName, useCSSLayers, lightningcssOptions, }?: PluginOptions): Plugin;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,KAAK,EAAE,MAAM,EAA0D,MAAM,QAAQ,CAAC;AAK7F,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAI5D,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,mBAAmB,CAAC,EAAE,IAAI,CAAC,gBAAgB,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,CAAC,CAAC;CACnF,CAAC;AAEF,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,EACnC,SAAc,EACd,QAAuB,EACvB,YAAoB,EACpB,mBAAmB,GACpB,GAAE,aAAkB,GAAG,MAAM,CAiG7B"}
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const rs_compiler_1 = __importDefault(require("@stylexswc/rs-compiler"));
7
+ const lightningcss_1 = require("lightningcss");
8
+ const browserslist_1 = __importDefault(require("browserslist"));
9
+ const lightningcss_2 = require("lightningcss");
10
+ const babel_plugin_1 = __importDefault(require("@stylexjs/babel-plugin"));
11
+ const IS_DEV_ENV = process.env.NODE_ENV === 'development';
12
+ function stylexPlugin({ rsOptions = {}, fileName = 'stylex.css', useCSSLayers = false, lightningcssOptions, } = {}) {
13
+ const { dev = IS_DEV_ENV, unstable_moduleResolution = { type: 'commonJS', rootDir: process.cwd() }, importSources = ['stylex', '@stylexjs/stylex'], ...options } = rsOptions;
14
+ let stylexRules = {};
15
+ return {
16
+ name: 'rollup-plugin-stylex',
17
+ buildStart() {
18
+ stylexRules = {};
19
+ },
20
+ generateBundle() {
21
+ const rules = Object.values(stylexRules).flat();
22
+ if (rules.length > 0) {
23
+ const collectedCSS = babel_plugin_1.default.processStylexRules(rules, useCSSLayers);
24
+ // Process the CSS using lightningcss
25
+ const { code } = (0, lightningcss_1.transform)({
26
+ targets: (0, lightningcss_2.browserslistToTargets)((0, browserslist_1.default)('>= 1%')),
27
+ ...lightningcssOptions,
28
+ filename: fileName,
29
+ code: Buffer.from(collectedCSS),
30
+ });
31
+ // Convert the Buffer back to a string
32
+ const processedCSS = code.toString();
33
+ this.emitFile({
34
+ fileName,
35
+ source: processedCSS,
36
+ type: 'asset',
37
+ });
38
+ }
39
+ },
40
+ shouldTransformCachedModule({ code: _code, id, meta }) {
41
+ stylexRules[id] = meta.stylex;
42
+ return false;
43
+ },
44
+ async transform(inputCode, id) {
45
+ if (!importSources.some(importName => typeof importName === 'string'
46
+ ? inputCode.includes(importName)
47
+ : inputCode.includes(importName.from))) {
48
+ // In rollup, returning null from any plugin phase means
49
+ // "no changes made".
50
+ return null;
51
+ }
52
+ let result = rs_compiler_1.default.transform(id, inputCode, {
53
+ ...options,
54
+ dev,
55
+ unstable_moduleResolution,
56
+ importSources,
57
+ });
58
+ if (result == null) {
59
+ console.warn('stylex: transformAsync returned null');
60
+ return { code: inputCode };
61
+ }
62
+ const { code, map, metadata } = result;
63
+ if (code == null) {
64
+ console.warn('stylex: transformAsync returned null code');
65
+ return { code: inputCode };
66
+ }
67
+ if (this.meta.watchMode) {
68
+ const ast = this.parse(code);
69
+ for (const stmt of ast.body) {
70
+ if (stmt.type === 'ImportDeclaration') {
71
+ const resolved = await this.resolve(stmt.source.value?.toString() || '', id);
72
+ if (resolved && !resolved.external) {
73
+ const result = await this.load(resolved);
74
+ if (result && result.meta && 'stylex' in result.meta) {
75
+ stylexRules[resolved.id] = result.meta.stylex;
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ if (!dev && metadata.stylex != null && metadata.stylex.length > 0) {
82
+ stylexRules[id] = metadata.stylex;
83
+ }
84
+ return { code, map: map, meta: metadata };
85
+ },
86
+ };
87
+ }
88
+ exports.default = stylexPlugin;
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@stylexswc/rollup-plugin",
3
+ "description": "Stylex rollup plugin with NAPI-RS compiler",
4
+ "version": "0.4.0-rc.1",
5
+ "config": {
6
+ "scripty": {
7
+ "path": "../../scripts/packages"
8
+ }
9
+ },
10
+ "dependencies": {
11
+ "@stylexjs/babel-plugin": "0.8.0",
12
+ "@stylexswc/rs-compiler": "0.4.0-rc.1",
13
+ "lightningcss": "^1.27.0"
14
+ },
15
+ "devDependencies": {
16
+ "@types/node": "22.8.5",
17
+ "browserslist": "^4.24.2",
18
+ "rollup": "^4.24.3"
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "jest": {
24
+ "testPathIgnorePatterns": [
25
+ "/node_modules/",
26
+ "__builds__",
27
+ "/__fixtures__/"
28
+ ],
29
+ "testEnvironment": "node"
30
+ },
31
+ "keywords": [
32
+ "rollup-plugin",
33
+ "rolup",
34
+ "stylex"
35
+ ],
36
+ "license": "MIT",
37
+ "main": "dist/index.js",
38
+ "private": false,
39
+ "publishConfig": {
40
+ "registry": "https://registry.npmjs.org/",
41
+ "access": "public"
42
+ },
43
+ "repository": "https://github.com/Dwlad90/stylex-swc-plugin",
44
+ "sideEffects": false,
45
+ "scripts": {
46
+ "build": "scripty --ts",
47
+ "clean": "del-cli dist",
48
+ "precommit": "lint-staged",
49
+ "prepush": "lint-prepush",
50
+ "test": "echo \"Error: no test specified\" && exit 0"
51
+ }
52
+ }