0.5.1 • Published 7 months ago

@stylexswc/swc-plugin v0.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

SWC plugin for StyleX (**unofficial)

!CAUTION DEPRECATED: This package is deprecated as of version 0.5.0. Please migrate to @stylexswc/rs-compiler which is the official replacement.

Overview

StyleX is a JavaScript library developed by Meta for defining styles optimized for user interfaces. You can find the official repository here.

This is an unofficial plugin for StyleX. It uses SWC instead of Babel for build step, which allows us to completely ditch Babel and make StyleX faster.

This plugin successfully passes almost all tests from the official StyleX library and is designed to be a drop-in replacement for the official StyleX Babel plugin.

The usage of StyleX does not change, all changes are internal.

This plugin is particularly beneficial for Next.js projets as it allows the use of the SWC Next.js Compiler.

Installation

To install the package, run the following command:

npm install --save-dev @stylexswc/swc-plugin

Usage

Modify your bundler configuration to use the StyleX SWC plugin.

!NOTE All awailable options the same as in the official StyleX Babel plugin and can be found on the StyleX babel plugin documentation page.

For example:

  • Register SWC plugin in Next.js config:
module.exports = {
  experimental: {
    swcPlugins: [[
      "@stylexswc/swc-plugin",
      {
        dev: process.env.NODE_ENV === 'development',
        genConditionalClasses: true,
        treeshakeCompensation: true,
        aliases: {
          '@/*': [path.join(rootDir, '*')],
        },
        unstable_moduleResolution: {
          type: 'commonJS',
          rootDir: rootDir,
        },
      },
    ]],
  },
};
  • Register SWC plugin in Webpack config:
module.exports = {
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'swc-loader',
            options: {
              jsc: {
                experimental: {
                  plugins: [
                    [
                      '@stylexswc/swc-plugin',
                      {
                        dev: process.env.NODE_ENV === 'development',
                        genConditionalClasses: true,
                        treeshakeCompensation: true,
                        aliases: {
                          '@/*': [path.join(rootDir, '*')],
                        },
                        unstable_moduleResolution: {
                          type: 'commonJS',
                          rootDir: rootDir,
                        },
                        // ... other options
                      },
                    ],
                  ],
                },
              },
            },
          },
        ],
      },
    ],
  },
};
  • Regiter SWC plugin in Rsbuild config:
export default {
  tools: {
    swc: {
      jsc: {
        experimental: {
          plugins: [
            [
              '@swc/plugin-styled-components',
              {
                dev: process.env.NODE_ENV === 'development',
                genConditionalClasses: true,
                treeshakeCompensation: true,
                aliases: {
                  '@/*': [path.join(rootDir, '*')],
                },
                unstable_moduleResolution: {
                  type: 'commonJS',
                  rootDir: rootDir,
                },
                // ... other options
              },
            ],
          ],
        },
      },
    },
  },
};

Working with Metadata

Since SWC does not support receiving metadata after transformation, the process of extracting CSS styles is a bit tricky and is based on searching for a substring of metadata in the compiled application file and serializing it into JSON.

To extract metadate from compiled code, you need to add the following code to your build script:

let metadataStr = '[]';

const code = sourceCodeString.replace(
  /\/\/*__stylex_metadata_start__(?<metadata>.+)__stylex_metadata_end__/,
  (...args) => {
    metadataStr = args.at(-1)?.metadata.split('"__stylex_metadata_end__')[0];

    return '';
  }
);

const metadata = { stylex: [] };

try {
  metadata.stylex = JSON.parse(metadataStr);
} catch (e) {
  console.error('error parsing metadata', e);
}

Example of metadata:

[
  {
    "class_name": "x7z7khe",
    "style": {
      "rtl": null,
      "ltr": ".x7z7khe{padding:10px}"
    },
    "priority": 1000
  },
  {
    "class_name": "xrkmrrc",
    "style": {
      "rtl": null,
      "ltr": ".xrkmrrc{background-color:red}"
    },
    "priority": 3000
  }
]

Metadata can be used to extract CSS styles from the compiled code.

Example

Below is a simple example of input StyleX code:

import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  root: {
    padding: 10,
  },
  element: {
    backgroundColor: 'red',
  },
});

const styleProps = stylex.props(styles.root, styles.element);

Output code:

//__stylex_metadata_start__[{"class_name":"x7z7khe","style":{"rtl":null,"ltr":".x7z7khe{padding:10px}"},"priority":1000},{"class_name":"xrkmrrc","style":{"rtl":null,"ltr":".xrkmrrc{background-color:red}"},"priority":3000}]__stylex_metadata_end__
import * as stylex from '@stylexjs/stylex';
const styleProps = {
  className: 'x7z7khe xrkmrrc',
};

!IMPORTANT The current resolution of the exports field from package. json is only partially supported, so if you encounter problems, please open an issue with an attached link to reproduce the problem.

License

StyleX is MIT licensed. Stylex SWC plugin is also MIT licensed.

0.4.4-rc.3

8 months ago

0.4.4-rc.2

8 months ago

0.5.1-rc.1

7 months ago

0.4.4-rc.1

8 months ago

0.4.1-dev.1

8 months ago

0.5.1-rc.2

7 months ago

0.4.4-rc.5

8 months ago

0.4.1-rc.1

8 months ago

0.4.4-rc.4

8 months ago

0.4.1-rc.2

8 months ago

0.5.0-rc.2

7 months ago

0.4.0-rc.3

8 months ago

0.5.0-rc.1

7 months ago

0.4.0-rc.2

9 months ago

0.4.0-rc.1

9 months ago

0.5.0-rc.3

7 months ago

0.4.3-rc.1

8 months ago

0.4.3-rc.2

8 months ago

0.4.2-rc.1

8 months ago

0.4.4

8 months ago

0.4.2-rc.2

8 months ago

0.5.0

7 months ago

0.4.1

8 months ago

0.4.0

8 months ago

0.4.3

8 months ago

0.5.1

7 months ago

0.4.2

8 months ago

0.3.1-rc.2

9 months ago

0.3.1-rc.1

9 months ago

0.3.0

9 months ago

0.3.0-rc.5

9 months ago

0.3.0-rc.1

9 months ago

0.3.1

9 months ago

0.2.4

10 months ago

0.2.3

10 months ago

0.2.2

10 months ago

0.2.1

10 months ago

0.2.0

11 months ago

0.1.0

12 months ago