@stylexjs/rollup-plugin 0.2.0-beta.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import { type PluginItem } from '@babel/core';
11
+ import type { Options } from '@stylexjs/babel-plugin';
12
+ import type { Plugin } from 'rollup';
13
+ export type PluginOptions = Readonly<
14
+ Omit<
15
+ Partial<Options>,
16
+ keyof ({
17
+ fileName?: string;
18
+ babelConfig?: Readonly<{
19
+ plugins?: ReadonlyArray<PluginItem>;
20
+ presets?: ReadonlyArray<PluginItem>;
21
+ }>;
22
+ useCSSLayers?: boolean;
23
+ })
24
+ > & {
25
+ fileName?: string;
26
+ babelConfig?: Readonly<{
27
+ plugins?: ReadonlyArray<PluginItem>;
28
+ presets?: ReadonlyArray<PluginItem>;
29
+ }>;
30
+ useCSSLayers?: boolean;
31
+ }
32
+ >;
33
+ declare function stylexPlugin($$PARAM_0$$: PluginOptions): Plugin;
34
+ export default stylexPlugin;
package/lib/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { transformAsync } from '@babel/core';
2
+ import stylexBabelPlugin from '@stylexjs/babel-plugin';
3
+ import flowSyntaxPlugin from '@babel/plugin-syntax-flow';
4
+ import jsxSyntaxPlugin from '@babel/plugin-syntax-jsx';
5
+ import typescriptSyntaxPlugin from '@babel/plugin-syntax-typescript';
6
+ import path from 'path';
7
+ const IS_DEV_ENV = process.env.NODE_ENV === 'development' || process.env.BABEL_ENV === 'development';
8
+ export default function stylexPlugin() {
9
+ let {
10
+ dev = IS_DEV_ENV,
11
+ unstable_moduleResolution = {
12
+ type: 'commonJS',
13
+ rootDir: process.cwd()
14
+ },
15
+ fileName = 'stylex.css',
16
+ babelConfig: {
17
+ plugins = [],
18
+ presets = []
19
+ } = {},
20
+ importSources = ['stylex', '@stylexjs/stylex'],
21
+ useCSSLayers = false,
22
+ ...options
23
+ } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
24
+ let stylexRules = {};
25
+ return {
26
+ name: 'rollup-plugin-stylex',
27
+ buildStart() {
28
+ stylexRules = {};
29
+ },
30
+ generateBundle() {
31
+ const rules = Object.values(stylexRules).flat();
32
+ if (rules.length > 0) {
33
+ const collectedCSS = stylexBabelPlugin.processStylexRules(rules, useCSSLayers);
34
+ this.emitFile({
35
+ fileName,
36
+ source: collectedCSS,
37
+ type: 'asset'
38
+ });
39
+ }
40
+ },
41
+ shouldTransformCachedModule(_ref) {
42
+ let {
43
+ code: _code,
44
+ id,
45
+ meta
46
+ } = _ref;
47
+ stylexRules[id] = meta.stylex;
48
+ return false;
49
+ },
50
+ async transform(inputCode, id) {
51
+ if (!importSources.some(importName => typeof importName === 'string' ? inputCode.includes(importName) : inputCode.includes(importName.from))) {
52
+ return null;
53
+ }
54
+ const result = await transformAsync(inputCode, {
55
+ babelrc: false,
56
+ filename: id,
57
+ presets,
58
+ plugins: [...plugins, /\.jsx?/.test(path.extname(id)) ? flowSyntaxPlugin : typescriptSyntaxPlugin, jsxSyntaxPlugin, stylexBabelPlugin.withOptions({
59
+ ...options,
60
+ dev,
61
+ unstable_moduleResolution
62
+ })]
63
+ });
64
+ if (result == null) {
65
+ console.warn('stylex: transformAsync returned null');
66
+ return {
67
+ code: inputCode
68
+ };
69
+ }
70
+ const {
71
+ code,
72
+ map,
73
+ metadata
74
+ } = result;
75
+ if (code == null) {
76
+ console.warn('stylex: transformAsync returned null code');
77
+ return {
78
+ code: inputCode
79
+ };
80
+ }
81
+ if (!dev && metadata.stylex != null && metadata.stylex.length > 0) {
82
+ stylexRules[id] = metadata.stylex;
83
+ }
84
+ return {
85
+ code,
86
+ map: map,
87
+ meta: metadata
88
+ };
89
+ }
90
+ };
91
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import { type PluginItem } from '../flow_modules/@babel/core';
11
+ import type { Options } from '@stylexjs/babel-plugin';
12
+ import type { Plugin } from '../flow_modules/rollup';
13
+
14
+ export type PluginOptions = $ReadOnly<{
15
+ ...Partial<Options>,
16
+ fileName?: string,
17
+ babelConfig?: $ReadOnly<{
18
+ plugins?: $ReadOnlyArray<PluginItem>,
19
+ presets?: $ReadOnlyArray<PluginItem>,
20
+ }>,
21
+ useCSSLayers?: boolean,
22
+ ...
23
+ }>;
24
+
25
+ declare export default function stylexPlugin(PluginOptions): Plugin<>;
package/package.json CHANGED
@@ -1,11 +1,14 @@
1
1
  {
2
2
  "name": "@stylexjs/rollup-plugin",
3
- "version": "0.2.0-beta.9",
4
- "description": "Rollup plugin for stylex",
5
- "main": "src/index.js",
6
- "repository": "https://www.github.com/facebookexternal/stylex",
3
+ "version": "0.4.0",
4
+ "description": "Rollup plugin for StyleX",
5
+ "main": "lib/index.js",
6
+ "type": "module",
7
+ "repository": "https://www.github.com/facebook/stylex",
7
8
  "license": "MIT",
8
9
  "scripts": {
10
+ "prebuild": "gen-types -i src/ -o lib/",
11
+ "build": "babel src/ --out-dir lib/ --copy-files",
9
12
  "test": "jest"
10
13
  },
11
14
  "jest": {
@@ -17,15 +20,10 @@
17
20
  "testEnvironment": "node"
18
21
  },
19
22
  "dependencies": {
20
- "@babel/core": "^7.16.0",
21
- "@stylexjs/babel-plugin": "0.2.0-beta.9"
22
- },
23
- "devDependencies": {
24
- "@babel/preset-env": "^7.16.8",
25
- "@rollup/plugin-babel": "^6.0.0",
26
- "@rollup/plugin-commonjs": "^23.0.1",
27
- "@rollup/plugin-node-resolve": "^15.0.0",
28
- "rollup": "^3.2.3",
29
- "@stylexjs/stylex": "0.2.0-beta.9"
23
+ "@babel/core": "^7.23.6",
24
+ "@stylexjs/babel-plugin": "0.4.0",
25
+ "@babel/plugin-syntax-flow": "^7.23.3",
26
+ "@babel/plugin-syntax-jsx": "^7.23.3",
27
+ "@babel/plugin-syntax-typescript": "^7.23.3"
30
28
  }
31
29
  }
package/src/index.js CHANGED
@@ -3,36 +3,59 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
6
8
  */
7
9
 
8
- const babel = require('@babel/core');
9
- const stylexBabelPlugin = require('@stylexjs/babel-plugin');
10
- const flowSyntaxPlugin = require('@babel/plugin-syntax-flow');
11
- const jsxSyntaxPlugin = require('@babel/plugin-syntax-jsx');
12
- const typescriptSyntaxPlugin = require('@babel/plugin-syntax-typescript');
13
- const path = require('path');
10
+ import { transformAsync, type PluginItem } from '@babel/core';
11
+ import stylexBabelPlugin from '@stylexjs/babel-plugin';
12
+ import flowSyntaxPlugin from '@babel/plugin-syntax-flow';
13
+ import jsxSyntaxPlugin from '@babel/plugin-syntax-jsx';
14
+ import typescriptSyntaxPlugin from '@babel/plugin-syntax-typescript';
15
+ import path from 'path';
16
+ import type { Options, Rule } from '@stylexjs/babel-plugin';
17
+ import type { Plugin, PluginContext, TransformResult } from 'rollup';
14
18
 
15
19
  const IS_DEV_ENV =
16
20
  process.env.NODE_ENV === 'development' ||
17
21
  process.env.BABEL_ENV === 'development';
18
22
 
19
- module.exports = function stylexPlugin({
23
+ export type PluginOptions = $ReadOnly<{
24
+ ...Partial<Options>,
25
+ fileName?: string,
26
+ babelConfig?: $ReadOnly<{
27
+ plugins?: $ReadOnlyArray<PluginItem>,
28
+ presets?: $ReadOnlyArray<PluginItem>,
29
+ }>,
30
+ useCSSLayers?: boolean,
31
+ ...
32
+ }>;
33
+
34
+ export default function stylexPlugin({
20
35
  dev = IS_DEV_ENV,
36
+ unstable_moduleResolution = { type: 'commonJS', rootDir: process.cwd() },
21
37
  fileName = 'stylex.css',
22
- babelConig: { plugins = [], presets = [] } = {},
23
- } = {}) {
24
- let stylexRules = {};
25
-
38
+ babelConfig: { plugins = [], presets = [] } = {},
39
+ importSources = ['stylex', '@stylexjs/stylex'],
40
+ useCSSLayers = false,
41
+ ...options
42
+ }: PluginOptions = {}): Plugin<> {
43
+ let stylexRules: { [string]: $ReadOnlyArray<Rule> } = {};
26
44
  return {
27
45
  name: 'rollup-plugin-stylex',
28
46
  buildStart() {
29
47
  stylexRules = {};
30
48
  },
31
- generateBundle() {
32
- const rules = Object.values(stylexRules).flat();
49
+ generateBundle(this: PluginContext) {
50
+ const rules: Array<Rule> = Object.values(stylexRules).flat();
33
51
  if (rules.length > 0) {
34
- const collectedCSS = stylexBabelPlugin.processStylexRules(rules);
52
+ const collectedCSS = stylexBabelPlugin.processStylexRules(
53
+ rules,
54
+ useCSSLayers,
55
+ );
35
56
 
57
+ // This is the intended API, but Flow doesn't support this pattern.
58
+ // $FlowExpectedError[object-this-reference]
36
59
  this.emitFile({
37
60
  fileName,
38
61
  source: collectedCSS,
@@ -40,12 +63,24 @@ module.exports = function stylexPlugin({
40
63
  });
41
64
  }
42
65
  },
43
- shouldTransformCachedModule({ code, id, cache, meta }) {
66
+ shouldTransformCachedModule({ code: _code, id, meta }) {
44
67
  stylexRules[id] = meta.stylex;
45
68
  return false;
46
69
  },
47
- async transform(inputCode, id) {
48
- const { code, map, metadata } = await babel.transformAsync(inputCode, {
70
+ async transform(inputCode, id): Promise<null | TransformResult> {
71
+ if (
72
+ !importSources.some((importName) =>
73
+ typeof importName === 'string'
74
+ ? inputCode.includes(importName)
75
+ : inputCode.includes(importName.from),
76
+ )
77
+ ) {
78
+ // In rollup, returning null from any plugin phase means
79
+ // "no changes made".
80
+ return null;
81
+ }
82
+
83
+ const result = await transformAsync(inputCode, {
49
84
  babelrc: false,
50
85
  filename: id,
51
86
  presets,
@@ -55,15 +90,32 @@ module.exports = function stylexPlugin({
55
90
  ? flowSyntaxPlugin
56
91
  : typescriptSyntaxPlugin,
57
92
  jsxSyntaxPlugin,
58
- [stylexBabelPlugin, { dev, stylexSheetName: fileName }],
93
+ stylexBabelPlugin.withOptions({
94
+ ...options,
95
+ dev,
96
+ unstable_moduleResolution,
97
+ }),
59
98
  ],
60
99
  });
100
+ if (result == null) {
101
+ console.warn('stylex: transformAsync returned null');
102
+ return { code: inputCode };
103
+ }
104
+ const { code, map, metadata } = result;
105
+ if (code == null) {
106
+ console.warn('stylex: transformAsync returned null code');
107
+ return { code: inputCode };
108
+ }
61
109
 
62
- if (!dev && metadata.stylex != null && metadata.stylex.length > 0) {
63
- stylexRules[id] = metadata.stylex;
110
+ if (
111
+ !dev &&
112
+ (metadata: $FlowFixMe).stylex != null &&
113
+ (metadata: $FlowFixMe).stylex.length > 0
114
+ ) {
115
+ stylexRules[id] = (metadata: $FlowFixMe).stylex;
64
116
  }
65
117
 
66
- return { code, map, meta: metadata };
118
+ return { code, map: (map: $FlowFixMe), meta: (metadata: $FlowFixMe) };
67
119
  },
68
120
  };
69
- };
121
+ }