@alifd/next-theme-loader

1.3.1 • Public • Published

@alifd/next-theme-loader

A webpack loader for injecting scss variables of theme package to scss file.

npm package

Install

npm install babel-plugin-import --save-dev
npm install @alifd/next-theme-loader --save-dev
npm install @alifd/next-theme-webpack-plugin --save-dev

Usage

webpack 2+

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ThemePlugin = require('@alifd/next-theme-webpack-plugin');

module.exports = {
  entry: {
    index: './src/index.jsx'
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  devtool: 'inline-source-map',
  module: {
    rules: [{
      test: /\.jsx?$/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            'env',
            'react',
            'stage-0'
          ],
          plugins: [
            'add-module-exports',
            'transform-decorators-legacy',
            ['babel-plugin-import', { style: true }]
          ]
        }
      },
      exclude: /node_modules/
    }, {
      test: /\.css$/,
      use: ExtractTextPlugin.extract({
        use: 'css-loader'
      })
    }, {
      test: /\.scss$/,
      use: ExtractTextPlugin.extract({
        use: [
          'css-loader',
          'fast-sass-loader',
          {
            loader: '@alifd/next-theme-loader',
            options: {
              theme: '@alifd/theme-package', /* variables only modified (optional)*/
              modifyVars: {'$css-prefix': 'my-'} /* custom variable (optional) */
            }
          }
        ]
      })
    }]
  },
  plugins: [
    new ThemePlugin({ theme: '@alifd/theme-package' }),
    new ExtractTextPlugin('[name].css')
  ]
};

webpack 1

webpack.config.js

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const ThemePlugin = require('@alifd/next-theme-webpack-plugin');

module.exports = {
  entry: {
    index: './src/index.jsx'
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js'
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devtool: 'inline-source-map',
  module: {
    loaders: [{
      test: /\.jsx?$/,
      loader: 'babel',
      query: {
        presets: [
          'es2015',
          'react',
          'stage-0'
        ],
        plugins: [
          'add-module-exports',
          'transform-decorators-legacy',
          ['babel-plugin-import', { style: true }]
        ]
      },
      exclude: /node_modules/
    }, {
      test: /\.css$/,
      loaders: ExtractTextPlugin.extract('css')
    }, {
      test: /\.scss$/,
      loader: ExtractTextPlugin.extract('css!fast-sass!@alifd/next-theme-loader?theme=@alifd/theme-package')
    }]
  },
  plugins: [
    new ThemePlugin({ theme: '@alifd/theme-package' }),
    new ExtractTextPlugin('[name].css')
  ]
};

Query

  • theme(String): theme package(eg: @alifd/theme-1) which come from https://fusion.design
  • base(String): package with all variables (eg: @alifd/next)
  • modifyVars(String/Object): inject some variables, such as: $css-prefix or $icon-font-path, value may be the following two types
    • { '$css-prefix': '"my-"' }
    • path.join(__dirname, 'variable.scss'), it should be an absolute path

Note: if you want to set modifyVars to object under webpack1, you should write as below:

{
  test: /\.scss$/,
  loader: ExtractTextPlugin.extract(`css!sass!@alifd/next-theme-loader?${JSON.stringify({ theme: '@alifd/theme-package', modifyVars: { '$css-prefix': '"my-"', '$icon-font-path': '"//xxx.com"' } })}`)
}

Package Sidebar

Install

npm i @alifd/next-theme-loader

Weekly Downloads

9

Version

1.3.1

License

MIT

Unpacked Size

6.96 kB

Total Files

4

Last publish

Collaborators

  • zyliang96
  • sunyuan20
  • jiaqiu_chen
  • soberz
  • whatever_lll
  • john-watson
  • eternalsky
  • mark-ck
  • lakerswgq
  • no-repeat
  • wwsun
  • aboutblank
  • bindoon
  • tao1991123
  • vicerwang
  • myronliu347
  • jdkahn
  • fusion-bot
  • sobear
  • lianmin
  • clarkxia
  • quanyin
  • jerryyxu
  • yellow2dong
  • alvinhui
  • andevery
  • luhengchang228
  • yuysmile
  • fd.xy
  • rax-publisher