babel-plugin-query-css-module

1.0.3 • Public • Published

babel-plugin-query-css-module

A babel plugin solve the css-module global import styles problem.

When you use import '.global.less', but get styles modularized not what you'd expect, you need this plugin.

Effects:

// origin
import Test from './test.less';

// to
import Test from './test.less?cssModules';

Systerm Requirements

  • Babal.js 7+;
  • Node.js 10.14+;

Installation

npm i -D babel-plugin-query-css-module

Usage

babel.config.json:

// Without options
{
  "presets": ["babel-plugin-query-css-module"]
}

// With options
{
  "presets": [
    [
        "babel-plugin-query-css-module",
        {
            // Default: cssModules
            flag: 'css_modules' // The query name for resourceQuery match.
        }
    ]
  ]
}

Webpack settings for reference:

{
  module: {
    rules: [
      {
        test: /\.less$/,
        oneOf: [
          {
            resourceQuery: /cssModules/,
            use: [
              {
                loader: MiniCssExtractPlugin.loader,
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: true,
                  modules: {
                    localIdentName: '[local]___[hash:base64:5]',
                  },
                },
              },
              {
                loader: 'postcss-loader',
              },
              {
                loader: 'less-loader',
                options: {
                  lessOptions: {
                    javascriptEnabled: true,
                  },
                },
              },
            ],
          },
          {
            use: [
              {
                loader: MiniCssExtractPlugin.loader,
              },
              {
                loader: 'css-loader',
                options: {
                  sourceMap: true,
                },
              },
              {
                loader: 'postcss-loader',
              },
              {
                loader: 'less-loader',
                options: {
                  lessOptions: {
                    javascriptEnabled: true,
                  },
                },
              },
            ],
          },
        ],
      },
    ],
  },
}

References

https://github.com/umijs/umi/blob/3.x/packages/babel-plugin-auto-css-modules/src/index.ts

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.31latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.31
1.0.20
1.0.10
1.0.00
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i babel-plugin-query-css-module

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

5.31 kB

Total Files

4

Last publish

Collaborators

  • jerryni