jsxstyle-loader

A webpack loader that extracts out static styles defined on jsxstyle components

Renamed to jsxstyle-webpack-plugin
Last updated 2 years ago by meyer .
Apache-2.0 · Original npm · Tarball · package.json
$ cnpm install jsxstyle-loader 
SYNC missed versions from official npm registry.

jsxstyle-loader

jsxstyle-loader is a webpack loader that extracts static style props from jsxstyle components into a separate CSS file.

Don’t know what jsxstyle is? Check out the jsxstyle README for more information.

Getting Started

  1. Add a new rule object for jsxstyle-loader to your webpack config, below any other JS loaders.

    jsxstyle-loader relies on untranspiled JSX to be present in order to extract styles. Since webpack loaders run from right to left and bottom to top, jsxstyle-loader should be placed at the end of your list of JS loaders.

  2. Add jsxstyle-loader’s companion plugin to the plugins section of your webpack config.

  3. Ensure your webpack config contains a loader that handles .css files.

When you’re done, the relevant parts of your webpack config should look like this:

const JsxstyleLoaderPlugin = require('jsxstyle-loader/plugin');

module.exports = {
  // ...
  plugins: [new JsxstyleLoaderPlugin()],
  // ...
  module: {
    rules: [
      // ...
      {
        test: /\.js$/,
        use: [
          // any loaders that transpile JSX should go above jsxstyle-loader
          {
            loader: 'your-cool-js-loader',
          },
          // jsxstyle-loader goes at the end
          {
            loader: 'jsxstyle-loader',
          },
        ],
      },
      {
        test: /\.css$/,
        use: 'your-cool-css-loader',
      },
      // ...
    ],
  },
};

Loader Options

styleGroups

By default, jsxstyle-loader will extract all static style props on a jsxstyle component into one class. This can lead to CSS classes that contain a lot of common style declarations. A good CSS minifier should help with this, but if you want a bit more control over how styles are grouped into CSS classes, you can provide an array of CSS style objects. When jsxstyle-loader encounters a component that contains all styles in a style object, those styles will be extracted into a separate class name.

For example, with the following loader config:

{
  loader: 'jsxstyle-loader',
  options: {
    styleGroups: [
      {
        display: 'block',
      },
      {
        marginLeft: 15,
        marginRight: 15,
      },
    ],
  },
}

...and a jsxstyle component that looks like this:

import { Block } from 'jsxstyle';

<Block backgroundColor="blue" marginLeft={15} marginRight={15} padding={20} />;

...the styles on this component will be extracted into three separate classes:

._x0 {
  display: block;
}
._x1 {
  margin-left: 15px;
  margin-right: 15px;
}
._x2 {
  background-color: blue;
  padding: 20px;
}

Without the styleGroups parameter, all five style props would be extracted into one class.

namedStyleGroups

The namedStyleGroups config option is just like the styleGroups config option, with one key difference: it is expected to be an object of CSS style objects, not an array. The key of the CSS style object will be used as the class name if all props and values are present on a jsxstyle component.

whitelistedModules

The whitelistedModules config option allows you to add modules to the evaluation context. For example, with the following loader config, any prop on a jsxstyle component that references a value from ./LayoutConstants.js will be assumed to be evaluatable:

{
  loader: 'jsxstyle-loader',
  options: {
    whitelistedModules: [
      require.resolve('./LayoutConstants'),
    ],
  },
}

Note: the modules you specify as whitelistedModules will not be transpiled, so make sure they’re in a format that’s compatible with your version of node.

parserPlugins

jsxstyle-loader uses babylon to parse javascript into an AST. By default, jsxstyle-loader is preconfigured with most of babylon’s plugins enabled, but if you need to enable additional plugins, you can specify an array of plugins with the parserPlugins option.

You can see a list of all available plugins in the babylon README.

classNameFormat

Out of the box, jsxstyle-loader will use a non-deterministic class naming scheme. Because webpack’s module iteration order is not guaranteed, class names will differ slightly between builds of the same code. If you need class names to remain the same each time the same code is bundled, set the classNameFormat option to hash in your loader config. Class names will be generated using a content-based hash.

liteMode

// look ma, no jsxstyle import
<block color="red">This text will be red</block>

Instead of importing components from jsxstyle or jsxstyle/preact, don’t import anything and just use the dash-case version of the component name as if it’s a valid DOM element. When jsxstyle-loader encounters one of these dash-case elements, it’ll treat it like the PascalCased equivalent component imported from jsxstyle or jsxstyle/preact.

To enable this feature, set liteMode in your loader options to either 'react' or 'preact'.

FAQs

Can I use jsxstyle-loader with Flow?

Yes! Flow parsing is automatically enabled for any non-Typescript files.

Can I use jsxstyle-loader with Typescript?

Yes! Take a look at the TypeScript example and issue #82 for some context. You’ll need to make a few configuration changes:

  1. Set jsx to preserve in the compilerOptions section of your tsconfig.json file.
  2. Ensure jsxstyle-loader runs after ts-loader. Webpack loaders run from bottom to top, to jsxstyle-loader needs to be placed before ts-loader in your webpack config.
  3. Add a loader that transpiles JSX, since ts-loader is now set to preserve JSX.

It’s not working ????

  1. Make sure the loader object test regex matches JS files that use jsxstyle.

  2. jsxstyle-loader relies on JSX still being around, so make sure it runs before babel-loader does its thing.

  3. jsxstyle-loader only supports destructured require/import syntax:

    // Cool!
    import { Block } from 'jsxstyle';
    <Block />;
    
    // Neat!
    const { Block } = require('jsxstyle');
    <Block />;
    
    // Nope :(
    const Block = require('jsxstyle').Block;
    <Block />;
    

What are “static style props”?

Simply put, static style props are props whose values can be evaluated at build time. By default, this consists of any literal type (string, number, null) as well as any variables provided to the evaluation context. The evaluation context is derived from the prop’s current scope.

For example, the fontSize prop in the following component will be marked as evaluatable and will be extracted as 42:

import { Block } from 'jsxstyle';

const bestNumber = 42;
<Block fontSize={bestNumber}>hello</Block>;

Any modules marked as whitelisted with the whitelistedModules config option will also be added to the evaluation context.

If the value of a style prop is a ternary and both sides can be evaluated, the prop will be extracted and the ternary condition will be moved to the className.

If the value of a prop is a simple logical expression with the && operator, it will be converted to a ternary with a null alternate.

Inline styles… are bad.

See the jsxstyle FAQs.

Does it work with hot reloading?

It sure does, but using it in development will only cause confusion, since what you will see in the developer tools is the transformed JS. jsxstyle-loader is a production optimisation.

Any caveats?

One big one: CSS class names are not de-duplicated across components. Any CSS minifier that combines identical class names will handle deduplication.

Contributing

Got an idea for jsxstyle-loader? Did you encounter a bug? Open an issue and let’s talk it through. PRs welcome too!

Current Tags

  • 1.1.2-0                                ...           disable-csstype (2 years ago)
  • 1.1.1                                ...           latest (2 years ago)

35 Versions

  • 1.1.2-0                                ...           2 years ago
  • 1.1.1 [deprecated]           ...           2 years ago
  • 1.1.0 [deprecated]           ...           2 years ago
  • 1.0.16 [deprecated]           ...           2 years ago
  • 1.0.15 [deprecated]           ...           2 years ago
  • 1.0.14 [deprecated]           ...           2 years ago
  • 1.0.13 [deprecated]           ...           2 years ago
  • 1.0.12 [deprecated]           ...           2 years ago
  • 1.0.11 [deprecated]           ...           2 years ago
  • 1.0.10 [deprecated]           ...           2 years ago
  • 1.0.9 [deprecated]           ...           2 years ago
  • 1.0.8 [deprecated]           ...           2 years ago
  • 1.0.7 [deprecated]           ...           2 years ago
  • 1.0.6 [deprecated]           ...           2 years ago
  • 1.0.5 [deprecated]           ...           2 years ago
  • 1.0.4 [deprecated]           ...           2 years ago
  • 1.0.3 [deprecated]           ...           2 years ago
  • 1.0.2 [deprecated]           ...           2 years ago
  • 1.0.1 [deprecated]           ...           2 years ago
  • 1.0.0 [deprecated]           ...           2 years ago
  • 1.0.0-rc.4                                ...           2 years ago
  • 1.0.0-rc.3                                ...           2 years ago
  • 1.0.0-rc.2 [deprecated]           ...           2 years ago
  • 1.0.0-rc.1 [deprecated]           ...           2 years ago
  • 1.0.0-rc.0                                ...           2 years ago
  • 1.0.0-beta.10                                ...           2 years ago
  • 1.0.0-beta.9                                ...           2 years ago
  • 1.0.0-beta.8                                ...           2 years ago
  • 1.0.0-beta.7                                ...           2 years ago
  • 1.0.0-beta.6                                ...           2 years ago
  • 1.0.0-beta.5                                ...           2 years ago
  • 1.0.0-beta.4 [deprecated]           ...           2 years ago
  • 1.0.0-beta.3 [deprecated]           ...           2 years ago
  • 1.0.0-beta.2 [deprecated]           ...           2 years ago
  • 1.0.0-beta.1 [deprecated]           ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (12)
Dev Dependencies (1)

Copyright 2014 - 2016 © taobao.org |