@stylexswc/postcss-plugin 0.6.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 +21 -0
- package/README.md +139 -0
- package/dist/builder.d.ts +20 -0
- package/dist/builder.d.ts.map +1 -0
- package/dist/builder.js +150 -0
- package/dist/bundler.d.ts +9 -0
- package/dist/bundler.d.ts.map +1 -0
- package/dist/bundler.js +57 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -0
- package/dist/types.d.ts +24 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/package.json +53 -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,139 @@
|
|
|
1
|
+
# PostCSS plugin with NAPI-RS StyleX compiler integration
|
|
2
|
+
|
|
3
|
+
`PostCSS plugin` 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/postcss-plugin
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
Modify `postcss.config.js`. For example:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
module.exports = {
|
|
21
|
+
plugins: {
|
|
22
|
+
'@stylexjs/postcss-plugin': {
|
|
23
|
+
include: ['src/**/*.{js,jsx,ts,tsx}'],
|
|
24
|
+
},
|
|
25
|
+
autoprefixer: {},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Use on of the plugins to process JS/TS files with StyleX code. For example:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
/// next.config.js
|
|
34
|
+
const path = require('path');
|
|
35
|
+
const stylexPlugin = require('@stylexswc/nextjs-plugin');
|
|
36
|
+
const rootDir = __dirname;
|
|
37
|
+
|
|
38
|
+
module.exports = stylexPlugin({
|
|
39
|
+
// Add any StyleX options here
|
|
40
|
+
rsOptions: {
|
|
41
|
+
aliases: {
|
|
42
|
+
'@/*': [path.join(rootDir, '*')],
|
|
43
|
+
},
|
|
44
|
+
unstable_moduleResolution: {
|
|
45
|
+
type: 'commonJS',
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
// It's important to prevent creating a new CSS file with StyleX classes twice
|
|
49
|
+
extractCSS: false,
|
|
50
|
+
})({
|
|
51
|
+
transpilePackages: ['@stylexjs/open-props'],
|
|
52
|
+
// Optionally, add any other Next.js config below
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
> [!WARNING]
|
|
57
|
+
> Each plugin of `@stylexswc` namespace accepts an `extractCSS` option to control CSS
|
|
58
|
+
> extraction. When using the `postcss` plugin, this option should be set to
|
|
59
|
+
> `false` to avoid double generation of CSS files with StyleX styles.
|
|
60
|
+
|
|
61
|
+
> [!NOTE]
|
|
62
|
+
> This approach requires transpiling JS/TS files with StyleX code twice:
|
|
63
|
+
> first the source code and then using the PostCSS plugin. To avoid this
|
|
64
|
+
> behavior when using `NextJS`, use the regular `@stylexswc/nextjs-plugin`
|
|
65
|
+
> passing the `transformCss` parameter to transform the generated CSS if it's
|
|
66
|
+
> possible, for example:
|
|
67
|
+
>
|
|
68
|
+
> ```js
|
|
69
|
+
> /// next.config.js
|
|
70
|
+
>
|
|
71
|
+
> //...other code
|
|
72
|
+
> transformCss: async css => {
|
|
73
|
+
> const postcss = require('postcss');
|
|
74
|
+
> const result = await postcss([require('autoprefixer')]).process(css);
|
|
75
|
+
> return result.css;
|
|
76
|
+
> },
|
|
77
|
+
> //...other code
|
|
78
|
+
> ```
|
|
79
|
+
|
|
80
|
+
Add the following CSS file to your project:
|
|
81
|
+
|
|
82
|
+
```css
|
|
83
|
+
/*[fileName].css*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* The @stylex directive is used by the @stylexjs/postcss-plugin.
|
|
87
|
+
* It is automatically replaced with generated CSS during builds.
|
|
88
|
+
*/
|
|
89
|
+
@stylex;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
And import it in your JS/TS files:
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
import '[fileName].css';
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Plugin Options
|
|
99
|
+
|
|
100
|
+
The plugin accepts the following configuration options:
|
|
101
|
+
|
|
102
|
+
### `rsOptions`
|
|
103
|
+
|
|
104
|
+
- Type: `StyleXOptions`
|
|
105
|
+
- Optional
|
|
106
|
+
- Default: `{}`
|
|
107
|
+
- Description: StyleX compiler options passed to the StyleX compiler
|
|
108
|
+
|
|
109
|
+
### `useCSSLayers`
|
|
110
|
+
|
|
111
|
+
- Type: `boolean`
|
|
112
|
+
- Optional
|
|
113
|
+
- Default: `false`
|
|
114
|
+
- Description: Whether to use CSS layers for better style isolation
|
|
115
|
+
|
|
116
|
+
### `exclude`
|
|
117
|
+
|
|
118
|
+
- Type: `string[]`
|
|
119
|
+
- Optional
|
|
120
|
+
- Description: Array of glob patterns to exclude from processing
|
|
121
|
+
|
|
122
|
+
### `include`
|
|
123
|
+
|
|
124
|
+
- Type: `string[]`
|
|
125
|
+
- Optional
|
|
126
|
+
- Description: Array of glob patterns to include for processing
|
|
127
|
+
|
|
128
|
+
### `cwd`
|
|
129
|
+
|
|
130
|
+
- Type: `string`
|
|
131
|
+
- Optional
|
|
132
|
+
- Default: `process.cwd()`
|
|
133
|
+
- Description: Current working directory for resolving files
|
|
134
|
+
|
|
135
|
+
### `isDev`
|
|
136
|
+
|
|
137
|
+
- Type: `boolean`
|
|
138
|
+
- Optional
|
|
139
|
+
- Description: Whether the plugin is running in development mode
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import postcss, { AtRule } from 'postcss';
|
|
2
|
+
import type { StyleXPluginOption, TransformOptions } from './types';
|
|
3
|
+
declare function createBuilder(): {
|
|
4
|
+
findStyleXAtRule: (root: postcss.Root) => AtRule | null;
|
|
5
|
+
configure: (options: StyleXPluginOption) => void;
|
|
6
|
+
build: ({ shouldSkipTransformError }: TransformOptions) => string;
|
|
7
|
+
getDependencies: () => ({
|
|
8
|
+
type: string;
|
|
9
|
+
dir: string;
|
|
10
|
+
glob: string;
|
|
11
|
+
file?: undefined;
|
|
12
|
+
} | {
|
|
13
|
+
type: string;
|
|
14
|
+
file: string;
|
|
15
|
+
dir?: undefined;
|
|
16
|
+
glob?: undefined;
|
|
17
|
+
})[];
|
|
18
|
+
};
|
|
19
|
+
export default createBuilder;
|
|
20
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../src/builder.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,EAAE,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAS1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAkDpE,iBAAS,aAAa;6BAsBY,OAAO,CAAC,IAAI,KAAG,MAAM,GAAG,IAAI;yBAdhC,kBAAkB;0CA0CD,gBAAgB;;;;;;;;;;;;EAsE9D;AAED,eAAe,aAAa,CAAC"}
|
package/dist/builder.js
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
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 node_path_1 = __importDefault(require("node:path"));
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const fast_glob_1 = require("fast-glob");
|
|
10
|
+
const is_glob_1 = __importDefault(require("is-glob"));
|
|
11
|
+
const glob_parent_1 = __importDefault(require("glob-parent"));
|
|
12
|
+
const bundler_1 = __importDefault(require("./bundler"));
|
|
13
|
+
// Parses a glob pattern and extracts its base directory and pattern.
|
|
14
|
+
// Returns an object with `base` and `glob` properties.
|
|
15
|
+
function parseGlob(pattern) {
|
|
16
|
+
// License: MIT
|
|
17
|
+
// Based on:
|
|
18
|
+
// https://github.com/chakra-ui/panda/blob/6ab003795c0b076efe6879a2e6a2a548cb96580e/packages/node/src/parse-glob.ts
|
|
19
|
+
let glob = pattern;
|
|
20
|
+
const base = (0, glob_parent_1.default)(pattern);
|
|
21
|
+
if (base !== '.') {
|
|
22
|
+
glob = pattern.substring(base.length);
|
|
23
|
+
if (glob.charAt(0) === '/') {
|
|
24
|
+
glob = glob.substring(1);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (glob.substring(0, 2) === './') {
|
|
28
|
+
glob = glob.substring(2);
|
|
29
|
+
}
|
|
30
|
+
if (glob.charAt(0) === '/') {
|
|
31
|
+
glob = glob.substring(1);
|
|
32
|
+
}
|
|
33
|
+
return { base, glob };
|
|
34
|
+
}
|
|
35
|
+
// Parses a file path or glob pattern into a PostCSS dependency message.
|
|
36
|
+
function parseDependency(fileOrGlob) {
|
|
37
|
+
// License: MIT
|
|
38
|
+
// Based on:
|
|
39
|
+
// https://github.com/chakra-ui/panda/blob/6ab003795c0b076efe6879a2e6a2a548cb96580e/packages/node/src/parse-dependency.ts
|
|
40
|
+
if (fileOrGlob.startsWith('!')) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
let message = null;
|
|
44
|
+
if ((0, is_glob_1.default)(fileOrGlob)) {
|
|
45
|
+
const { base, glob } = parseGlob(fileOrGlob);
|
|
46
|
+
message = { type: 'dir-dependency', dir: (0, path_1.normalize)((0, path_1.resolve)(base)), glob };
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
message = { type: 'dependency', file: (0, path_1.normalize)((0, path_1.resolve)(fileOrGlob)) };
|
|
50
|
+
}
|
|
51
|
+
return message;
|
|
52
|
+
}
|
|
53
|
+
// Creates a builder for transforming files and bundling StyleX CSS.
|
|
54
|
+
function createBuilder() {
|
|
55
|
+
let config = null;
|
|
56
|
+
const bundler = (0, bundler_1.default)();
|
|
57
|
+
const fileModifiedMap = new Map();
|
|
58
|
+
// Configures the builder with the provided options.
|
|
59
|
+
function configure(options) {
|
|
60
|
+
config = options;
|
|
61
|
+
}
|
|
62
|
+
/// Retrieves the current configuration.
|
|
63
|
+
function getConfig() {
|
|
64
|
+
if (config == null) {
|
|
65
|
+
throw new Error('Builder not configured');
|
|
66
|
+
}
|
|
67
|
+
return config;
|
|
68
|
+
}
|
|
69
|
+
// Finds the `@stylex;` at-rule in the provided PostCSS root.
|
|
70
|
+
function findStyleXAtRule(root) {
|
|
71
|
+
let styleXAtRule = null;
|
|
72
|
+
root.walkAtRules(atRule => {
|
|
73
|
+
if (atRule.name === 'stylex' && !atRule.params) {
|
|
74
|
+
styleXAtRule = atRule;
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return styleXAtRule;
|
|
78
|
+
}
|
|
79
|
+
// Retrieves all files that match the include and exclude patterns.
|
|
80
|
+
function getFiles() {
|
|
81
|
+
const { cwd, include, exclude } = getConfig();
|
|
82
|
+
let files = (0, fast_glob_1.globSync)(include || [], {
|
|
83
|
+
onlyFiles: true,
|
|
84
|
+
ignore: exclude,
|
|
85
|
+
cwd,
|
|
86
|
+
});
|
|
87
|
+
// Normalize file paths
|
|
88
|
+
files = files.map(file => (file.includes(cwd || '/') ? file : node_path_1.default.join(cwd || '/', file)));
|
|
89
|
+
return files;
|
|
90
|
+
}
|
|
91
|
+
// Transforms the included files, bundles the CSS, and returns the result.
|
|
92
|
+
function build({ shouldSkipTransformError }) {
|
|
93
|
+
const { cwd, rsOptions, useCSSLayers, isDev } = getConfig();
|
|
94
|
+
const files = getFiles();
|
|
95
|
+
const filesToTransform = [];
|
|
96
|
+
// Remove deleted files since the last build
|
|
97
|
+
for (const file of fileModifiedMap.keys()) {
|
|
98
|
+
if (!files.includes(file)) {
|
|
99
|
+
fileModifiedMap.delete(file);
|
|
100
|
+
bundler.remove(file);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
for (const file of files) {
|
|
104
|
+
const filePath = node_path_1.default.resolve(cwd || '/', file);
|
|
105
|
+
const mtimeMs = node_fs_1.default.existsSync(filePath) ? node_fs_1.default.statSync(filePath).mtimeMs : -Infinity;
|
|
106
|
+
// Skip files that have not been modified since the last build
|
|
107
|
+
// On first run, all files will be transformed
|
|
108
|
+
const shouldSkip = fileModifiedMap.has(file) && mtimeMs === fileModifiedMap.get(file);
|
|
109
|
+
if (shouldSkip) {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
fileModifiedMap.set(file, mtimeMs);
|
|
113
|
+
filesToTransform.push(file);
|
|
114
|
+
}
|
|
115
|
+
filesToTransform.forEach(file => {
|
|
116
|
+
const filePath = node_path_1.default.resolve(cwd || '/', file);
|
|
117
|
+
const contents = node_fs_1.default.readFileSync(filePath, 'utf-8');
|
|
118
|
+
if (!bundler.shouldTransform(contents)) {
|
|
119
|
+
console.log('contents: ', contents);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const transformedResult = bundler.transform(file, contents, rsOptions || {}, {
|
|
123
|
+
isDev,
|
|
124
|
+
shouldSkipTransformError,
|
|
125
|
+
});
|
|
126
|
+
return transformedResult;
|
|
127
|
+
});
|
|
128
|
+
const css = bundler.bundle({ useCSSLayers });
|
|
129
|
+
return css;
|
|
130
|
+
}
|
|
131
|
+
// Retrieves the dependencies that PostCSS should watch.
|
|
132
|
+
function getDependencies() {
|
|
133
|
+
const { include } = getConfig();
|
|
134
|
+
const dependencies = [];
|
|
135
|
+
for (const fileOrGlob of include || []) {
|
|
136
|
+
const dependency = parseDependency(fileOrGlob);
|
|
137
|
+
if (dependency != null) {
|
|
138
|
+
dependencies.push(dependency);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return dependencies;
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
findStyleXAtRule,
|
|
145
|
+
configure,
|
|
146
|
+
build,
|
|
147
|
+
getDependencies,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
exports.default = createBuilder;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { StyleXOptions } from '@stylexswc/rs-compiler';
|
|
2
|
+
import type { TransformOptions, StyleXPluginOption } from './types';
|
|
3
|
+
export default function createBundler(): {
|
|
4
|
+
shouldTransform: (sourceCode: string) => boolean;
|
|
5
|
+
transform: (id: string, sourceCode: string, rsConfig: StyleXOptions, options: TransformOptions) => import("@stylexswc/rs-compiler").StyleXTransformResult;
|
|
6
|
+
remove: (id: string) => void;
|
|
7
|
+
bundle: ({ useCSSLayers }: Pick<StyleXPluginOption, "useCSSLayers">) => string;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=bundler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bundler.d.ts","sourceRoot":"","sources":["../src/bundler.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAgC,MAAM,wBAAwB,CAAC;AAErF,OAAO,KAAK,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAGpE,MAAM,CAAC,OAAO,UAAU,aAAa;kCAIE,MAAM;oBAMrC,MAAM,cACE,MAAM,YACR,aAAa,WACd,gBAAgB;iBAmCP,MAAM;+BAKQ,IAAI,CAAC,kBAAkB,EAAE,cAAc,CAAC;EAc3E"}
|
package/dist/bundler.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
exports.default = createBundler;
|
|
7
|
+
const babel_plugin_1 = __importDefault(require("@stylexjs/babel-plugin"));
|
|
8
|
+
const rs_compiler_1 = require("@stylexswc/rs-compiler");
|
|
9
|
+
// Creates a stateful bundler for processing StyleX rules using Babel.
|
|
10
|
+
function createBundler() {
|
|
11
|
+
const styleXRulesMap = new Map();
|
|
12
|
+
// Determines if the source code should be transformed based on the presence of StyleX imports.
|
|
13
|
+
function shouldTransform(sourceCode) {
|
|
14
|
+
return sourceCode.includes('stylex');
|
|
15
|
+
}
|
|
16
|
+
// Transforms the source code using Babel, extracting StyleX rules and storing them.
|
|
17
|
+
function transform(id, sourceCode, rsConfig, options) {
|
|
18
|
+
const { shouldSkipTransformError } = options;
|
|
19
|
+
let transformReult = {
|
|
20
|
+
code: sourceCode,
|
|
21
|
+
map: undefined,
|
|
22
|
+
metadata: { stylex: [] },
|
|
23
|
+
};
|
|
24
|
+
try {
|
|
25
|
+
transformReult = (0, rs_compiler_1.transform)(id, sourceCode, rsConfig);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (shouldSkipTransformError) {
|
|
29
|
+
console.warn(`[@stylexswc/postcss-plugin] Failed to transform "${id}": ${error.message}`);
|
|
30
|
+
return transformReult;
|
|
31
|
+
}
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
const { code, map, metadata } = transformReult;
|
|
35
|
+
const stylex = metadata.stylex;
|
|
36
|
+
if (stylex != null && stylex.length > 0) {
|
|
37
|
+
styleXRulesMap.set(id, stylex);
|
|
38
|
+
}
|
|
39
|
+
return { code, map, metadata };
|
|
40
|
+
}
|
|
41
|
+
// Removes the stored StyleX rules for the specified file.
|
|
42
|
+
function remove(id) {
|
|
43
|
+
styleXRulesMap.delete(id);
|
|
44
|
+
}
|
|
45
|
+
// Bundles all collected StyleX rules into a single CSS string.
|
|
46
|
+
function bundle({ useCSSLayers }) {
|
|
47
|
+
const rules = Array.from(styleXRulesMap.values()).flat();
|
|
48
|
+
const css = babel_plugin_1.default.processStylexRules(rules, !!useCSSLayers);
|
|
49
|
+
return css;
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
shouldTransform,
|
|
53
|
+
transform,
|
|
54
|
+
remove,
|
|
55
|
+
bundle,
|
|
56
|
+
};
|
|
57
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import postcss from 'postcss';
|
|
2
|
+
import type { StyleXPluginOption } from './types';
|
|
3
|
+
declare const plugin: {
|
|
4
|
+
({ cwd, rsOptions, include, exclude, useCSSLayers, }: StyleXPluginOption): {
|
|
5
|
+
postcssPlugin: string;
|
|
6
|
+
plugins: ((root: postcss.Root, result: postcss.Result) => void)[];
|
|
7
|
+
};
|
|
8
|
+
postcss: boolean;
|
|
9
|
+
};
|
|
10
|
+
export default plugin;
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAQlD,QAAA,MAAM,MAAM;0DAQT,kBAAkB;;yBAiBC,OAAO,CAAC,IAAI,UAAU,OAAO,CAAC,MAAM;;;CAsDzD,CAAC;AAKF,eAAe,MAAM,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
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 postcss_1 = __importDefault(require("postcss"));
|
|
7
|
+
const builder_1 = __importDefault(require("./builder"));
|
|
8
|
+
const PLUGIN_NAME = '@stylexswc/postcss-plugin';
|
|
9
|
+
const builder = (0, builder_1.default)();
|
|
10
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
11
|
+
const plugin = ({ cwd = process.cwd(),
|
|
12
|
+
// By default reuses the Babel configuration from the project root.
|
|
13
|
+
// Use `babelrc: false` to disable this behavior.
|
|
14
|
+
rsOptions = {}, include, exclude, useCSSLayers = false, }) => {
|
|
15
|
+
exclude = [
|
|
16
|
+
// Exclude type declaration files by default because it never contains any CSS rules.
|
|
17
|
+
'**/*.d.ts',
|
|
18
|
+
'**/*.flow',
|
|
19
|
+
...(exclude ?? []),
|
|
20
|
+
];
|
|
21
|
+
// Whether to skip the error when transforming StyleX rules.
|
|
22
|
+
// Useful in watch mode where Fast Refresh can recover from errors.
|
|
23
|
+
// Initial transform will still throw errors in watch mode to surface issues early.
|
|
24
|
+
let shouldSkipTransformError = false;
|
|
25
|
+
return {
|
|
26
|
+
postcssPlugin: PLUGIN_NAME,
|
|
27
|
+
plugins: [
|
|
28
|
+
// Processes the PostCSS root node to find and transform StyleX at-rules.
|
|
29
|
+
function (root, result) {
|
|
30
|
+
const fileName = result.opts.from;
|
|
31
|
+
// Configure the builder with the provided options
|
|
32
|
+
builder.configure({
|
|
33
|
+
include,
|
|
34
|
+
exclude,
|
|
35
|
+
cwd,
|
|
36
|
+
rsOptions,
|
|
37
|
+
useCSSLayers,
|
|
38
|
+
isDev,
|
|
39
|
+
});
|
|
40
|
+
// Find the "@stylex" at-rule
|
|
41
|
+
const styleXAtRule = builder.findStyleXAtRule(root);
|
|
42
|
+
if (styleXAtRule == null) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
// Get dependencies to be watched for changes
|
|
46
|
+
const dependencies = builder.getDependencies();
|
|
47
|
+
// Add each dependency to the PostCSS result messages.
|
|
48
|
+
// This watches the entire "./src" directory for "./src/**/*.{ts,tsx}"
|
|
49
|
+
// to handle new files and deletions reliably in watch mode.
|
|
50
|
+
for (const dependency of dependencies) {
|
|
51
|
+
result.messages.push({
|
|
52
|
+
plugin: PLUGIN_NAME,
|
|
53
|
+
parent: fileName,
|
|
54
|
+
...dependency,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Build and parse the CSS from collected StyleX rules
|
|
58
|
+
const css = builder.build({
|
|
59
|
+
shouldSkipTransformError,
|
|
60
|
+
});
|
|
61
|
+
const parsed = postcss_1.default.parse(css, {
|
|
62
|
+
from: fileName,
|
|
63
|
+
});
|
|
64
|
+
// Replace the "@stylex" rule with the generated CSS
|
|
65
|
+
styleXAtRule.replaceWith(parsed);
|
|
66
|
+
result.root = root;
|
|
67
|
+
if (!shouldSkipTransformError) {
|
|
68
|
+
// Build was successful, subsequent builds are for watch mode
|
|
69
|
+
shouldSkipTransformError = true;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
plugin.postcss = true;
|
|
76
|
+
module.exports = plugin;
|
|
77
|
+
exports.default = plugin;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { StyleXOptions } from '@stylexswc/rs-compiler';
|
|
2
|
+
export interface StyleXPluginOption {
|
|
3
|
+
/**
|
|
4
|
+
* stylex options passed to stylex babel plugin
|
|
5
|
+
*
|
|
6
|
+
* @see https://stylexjs.com/docs/api/configuration/babel-plugin/
|
|
7
|
+
*/
|
|
8
|
+
rsOptions?: Partial<StyleXOptions>;
|
|
9
|
+
/**
|
|
10
|
+
* Whether to use CSS layers
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
useCSSLayers?: boolean;
|
|
15
|
+
exclude?: string[];
|
|
16
|
+
include?: string[];
|
|
17
|
+
cwd?: string;
|
|
18
|
+
isDev?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface TransformOptions {
|
|
21
|
+
shouldSkipTransformError?: boolean;
|
|
22
|
+
isDev?: boolean;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAE5D,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACnC;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IAEvB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;CAEjB;AAED,MAAM,WAAW,gBAAgB;IAC/B,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stylexswc/postcss-plugin",
|
|
3
|
+
"description": "StyleX PostCSS plugin with NAPI-RS compiler",
|
|
4
|
+
"version": "0.6.0-rc.1",
|
|
5
|
+
"config": {
|
|
6
|
+
"scripty": {
|
|
7
|
+
"path": "../../scripts/packages"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@stylexjs/babel-plugin": "^0.9.3",
|
|
12
|
+
"fast-glob": "^3.3.2",
|
|
13
|
+
"glob-parent": "^6.0.2",
|
|
14
|
+
"is-glob": "^4.0.3",
|
|
15
|
+
"postcss": "^8.4.49"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@babel/types": "^7.23.9",
|
|
19
|
+
"@stylexswc/eslint-config": "0.6.0-rc.1",
|
|
20
|
+
"@stylexswc/rs-compiler": "0.6.0-rc.1",
|
|
21
|
+
"@stylexswc/typescript-config": "0.6.0-rc.1",
|
|
22
|
+
"@types/babel__core": "^7.20.5",
|
|
23
|
+
"@types/glob-parent": "^5.1.3",
|
|
24
|
+
"@types/is-glob": "^4.0.4",
|
|
25
|
+
"@types/loader-utils": "^2.0.6",
|
|
26
|
+
"@types/node": "^22.5.1"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"keywords": [
|
|
32
|
+
"postcss",
|
|
33
|
+
"postcss-plugin",
|
|
34
|
+
"stylex",
|
|
35
|
+
"swc"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"main": "dist/index.js",
|
|
39
|
+
"private": false,
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"registry": "https://registry.npmjs.org/",
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"repository": "https://github.com/Dwlad90/stylex-swc-plugin",
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "scripty --ts",
|
|
48
|
+
"clean": "del-cli dist",
|
|
49
|
+
"precommit": "lint-staged",
|
|
50
|
+
"prepush": "lint-prepush",
|
|
51
|
+
"test": "echo \"Error: no test specified\" && exit 0"
|
|
52
|
+
}
|
|
53
|
+
}
|