@stylexjs/rollup-plugin 0.1.0-beta.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.
@@ -0,0 +1,12 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "ie": 11
8
+ }
9
+ }
10
+ ]
11
+ ]
12
+ }
@@ -0,0 +1,33 @@
1
+ // index.js
2
+
3
+ 'use strict';
4
+
5
+ import stylex from 'stylex';
6
+ import otherStyles from './otherStyles';
7
+ import npmStyles from './npmStyles';
8
+
9
+ const fadeAnimation = stylex.keyframes({
10
+ '0%': {
11
+ opacity: 0.25,
12
+ },
13
+ '100%': {
14
+ opacity: 1,
15
+ },
16
+ });
17
+
18
+ const styles = stylex.create({
19
+ foo: {
20
+ animationName: fadeAnimation,
21
+ display: 'flex',
22
+ marginStart: 10,
23
+ marginBlockStart: 99,
24
+ height: 500,
25
+ ':hover': {
26
+ background: 'red',
27
+ },
28
+ },
29
+ });
30
+
31
+ export default function App() {
32
+ return stylex(otherStyles.bar, styles.foo, npmStyles.baz);
33
+ }
@@ -0,0 +1,15 @@
1
+ // npmStyles.js
2
+
3
+ 'use strict';
4
+
5
+ import stylex from 'stylex';
6
+
7
+ const styles = stylex.create({
8
+ baz: {
9
+ display: 'inline',
10
+ height: 500,
11
+ width: '50%',
12
+ },
13
+ });
14
+
15
+ export default styles;
@@ -0,0 +1,14 @@
1
+ // otherStyles.js
2
+
3
+ 'use strict';
4
+
5
+ import stylex from 'stylex';
6
+
7
+ const styles = stylex.create({
8
+ bar: {
9
+ display: 'block',
10
+ width: '100%',
11
+ },
12
+ });
13
+
14
+ export default styles;
@@ -0,0 +1,177 @@
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
+ 'use strict';
9
+
10
+ const path = require('path');
11
+ const rollup = require('rollup');
12
+ const { babel } = require('@rollup/plugin-babel');
13
+ const commonjs = require('@rollup/plugin-commonjs');
14
+ const { nodeResolve } = require('@rollup/plugin-node-resolve');
15
+ const stylexPlugin = require('../src/index');
16
+
17
+ describe('rollup-plugin-stylex', () => {
18
+ async function runStylex(options) {
19
+ // Configure a rollup bundle
20
+ const bundle = await rollup.rollup({
21
+ // Remove stylex runtime from bundle
22
+ external: ['stylex'],
23
+ input: path.resolve(__dirname, '__fixtures__/index.js'),
24
+ plugins: [
25
+ nodeResolve(),
26
+ commonjs(),
27
+ babel({
28
+ babelHelpers: 'bundled',
29
+ configFile: path.resolve(__dirname, '__fixtures__/.babelrc.json'),
30
+ exclude: [/npmStyles\.js/],
31
+ }),
32
+ stylexPlugin(options),
33
+ ],
34
+ });
35
+
36
+ // Generate output specific code in-memory
37
+ // You can call this function multiple times on the same bundle object
38
+ const { output } = await bundle.generate({
39
+ file: path.resolve(__dirname, '/__builds__/bundle.js'),
40
+ });
41
+
42
+ let css, js;
43
+
44
+ for (const chunkOrAsset of output) {
45
+ if (chunkOrAsset.fileName === 'stylex.css') {
46
+ css = chunkOrAsset.source;
47
+ } else if (chunkOrAsset.fileName === 'bundle.js') {
48
+ js = chunkOrAsset.code;
49
+ }
50
+ }
51
+
52
+ return { css, js };
53
+ }
54
+
55
+ it('extracts CSS and removes stylex.inject calls', async () => {
56
+ const { css, js } = await runStylex({ fileName: 'stylex.css' });
57
+
58
+ expect(css).toMatchInlineSnapshot(`
59
+ "@keyframes x11gtny7-B{0%{opacity:.25;}100%{opacity:1;}}
60
+ .x188knyk{margin-block-start:99px}
61
+ .x1c4r43l{display:flex}
62
+ .x1je5kxa{height:500px}
63
+ html:not([dir='rtl']) .x1n8p6zw{margin-left:10px}
64
+ html[dir='rtl'] .x1n8p6zw{margin-right:10px}
65
+ .x1nrqb13{animation-name:x11gtny7-B}
66
+ .x1u78jha{width:50%}
67
+ .x1wdx05y{display:inline}
68
+ .x6mlivy{width:100%}
69
+ .xntgbld{display:block}
70
+ .x1kflwvg:hover{background:red}"
71
+ `);
72
+
73
+ expect(js).toMatchInlineSnapshot(`
74
+ "import stylex from 'stylex';
75
+
76
+ // otherStyles.js
77
+ var styles$2 = {
78
+ bar: {
79
+ display: "xntgbld",
80
+ width: "x6mlivy"
81
+ }
82
+ };
83
+
84
+ // npmStyles.js
85
+ const styles$1 = {
86
+ baz: {
87
+ display: "x1wdx05y",
88
+ height: "x1je5kxa",
89
+ width: "x1u78jha"
90
+ }
91
+ };
92
+
93
+ // index.js
94
+ var styles = {
95
+ foo: {
96
+ animationName: "x1nrqb13",
97
+ display: "x1c4r43l",
98
+ marginStart: "x1n8p6zw",
99
+ marginBlockStart: "x188knyk",
100
+ height: "x1je5kxa",
101
+ ":hover_background": "x1kflwvg"
102
+ }
103
+ };
104
+ function App() {
105
+ return stylex(styles$2.bar, styles.foo, styles$1.baz);
106
+ }
107
+
108
+ export { App as default };
109
+ "
110
+ `);
111
+ });
112
+
113
+ describe('when in dev mode', () => {
114
+ it('preserves stylex.inject calls and does not extract CSS', async () => {
115
+ const { css, js } = await runStylex({
116
+ dev: true,
117
+ fileName: 'stylex.css',
118
+ });
119
+
120
+ expect(css).toBeUndefined();
121
+
122
+ expect(js).toMatchInlineSnapshot(`
123
+ "import stylex from 'stylex';
124
+
125
+ // otherStyles.js
126
+ stylex.inject(".xntgbld{display:block}", 1);
127
+ stylex.inject(".x6mlivy{width:100%}", 1);
128
+ var styles$2 = {
129
+ bar: {
130
+ "otherStyles__styles.bar": "otherStyles__styles.bar",
131
+ display: "xntgbld",
132
+ width: "x6mlivy"
133
+ }
134
+ };
135
+
136
+ // npmStyles.js
137
+ stylex.inject(".x1wdx05y{display:inline}", 1);
138
+ stylex.inject(".x1je5kxa{height:500px}", 1);
139
+ stylex.inject(".x1u78jha{width:50%}", 1);
140
+ const styles$1 = {
141
+ baz: {
142
+ "npmStyles__styles.baz": "npmStyles__styles.baz",
143
+ display: "x1wdx05y",
144
+ height: "x1je5kxa",
145
+ width: "x1u78jha"
146
+ }
147
+ };
148
+
149
+ // index.js
150
+ stylex.inject("@keyframes x11gtny7-B{0%{opacity:.25;}100%{opacity:1;}}", 1);
151
+ stylex.inject(".x1nrqb13{animation-name:x11gtny7-B}", 1);
152
+ stylex.inject(".x1c4r43l{display:flex}", 1);
153
+ stylex.inject(".x1n8p6zw{margin-left:10px}", 1, ".x1n8p6zw{margin-right:10px}");
154
+ stylex.inject(".x188knyk{margin-block-start:99px}", 1);
155
+ stylex.inject(".x1je5kxa{height:500px}", 1);
156
+ stylex.inject(".x1kflwvg:hover{background:red}", 8);
157
+ var styles = {
158
+ foo: {
159
+ "index__styles.foo": "index__styles.foo",
160
+ animationName: "x1nrqb13",
161
+ display: "x1c4r43l",
162
+ marginStart: "x1n8p6zw",
163
+ marginBlockStart: "x188knyk",
164
+ height: "x1je5kxa",
165
+ ":hover_background": "x1kflwvg"
166
+ }
167
+ };
168
+ function App() {
169
+ return stylex(styles$2.bar, styles.foo, styles$1.baz);
170
+ }
171
+
172
+ export { App as default };
173
+ "
174
+ `);
175
+ });
176
+ });
177
+ });
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@stylexjs/rollup-plugin",
3
+ "version": "0.1.0-beta.1",
4
+ "description": "Rollup plugin for stylex",
5
+ "main": "src/index.js",
6
+ "repository": "https://www.github.com/facebookexternal/stylex",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "test": "jest"
10
+ },
11
+ "jest": {
12
+ "testPathIgnorePatterns": [
13
+ "/node_modules/",
14
+ "__builds__",
15
+ "/__fixtures__/"
16
+ ],
17
+ "testEnvironment": "node"
18
+ },
19
+ "dependencies": {
20
+ "@babel/core": "^7.16.0",
21
+ "@stylexjs/babel-plugin": "0.1.0"
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.1.0"
30
+ }
31
+ }
package/src/index.js ADDED
@@ -0,0 +1,51 @@
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
+ const babel = require('@babel/core');
9
+ const stylexBabelPlugin = require('@stylexjs/babel-plugin');
10
+
11
+ const IS_DEV_ENV =
12
+ process.env.NODE_ENV === 'development' ||
13
+ process.env.BABEL_ENV === 'development';
14
+
15
+ module.exports = function stylexPlugin({
16
+ dev = IS_DEV_ENV,
17
+ fileName = 'stylex.css',
18
+ } = {}) {
19
+ let stylexRules = [];
20
+
21
+ return {
22
+ name: 'rollup-plugin-stylex',
23
+ buildStart() {
24
+ stylexRules = [];
25
+ },
26
+ generateBundle() {
27
+ if (stylexRules.length > 0) {
28
+ const collectedCSS = stylexBabelPlugin.processStylexRules(stylexRules);
29
+
30
+ this.emitFile({
31
+ fileName,
32
+ source: collectedCSS,
33
+ type: 'asset',
34
+ });
35
+ }
36
+ },
37
+ async transform(inputCode, id) {
38
+ const { code, map, metadata } = await babel.transformAsync(inputCode, {
39
+ babelrc: false,
40
+ filename: id,
41
+ plugins: [[stylexBabelPlugin, { dev, stylexSheetName: fileName }]],
42
+ });
43
+
44
+ if (!dev && metadata.stylex != null && metadata.stylex.length > 0) {
45
+ stylexRules = stylexRules.concat(metadata.stylex);
46
+ }
47
+
48
+ return { code, map };
49
+ },
50
+ };
51
+ };