Webpack - 导出SASS(.scss)文件

时间:2016-08-05 20:01:20

标签: css sass webpack

我有一个包,我希望将我的SASS变量导出到其他包中使用它。目前我的所有.scss文件都已编译并放入/dist/main.css文件中。我的webpack配置:

var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: ['./src/index.js'],
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel'
      },
      {
        test:   /\.(scss|sass|css)$/,
        loader: ExtractTextPlugin.extract("style", "css!sass")
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=10000&name=fonts/[hash].[ext]'
      },
      {
        test: /\.scss$/, loader: 'style!css!sass!sass-resources'
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  output: {
    path: __dirname + '/build',
    publicPath: '/',
    filename: 'index.js',
    library: 'Supernova',
    libraryTarget: 'umd'
  },
  externals: {
    'react': 'react',
    'react-dom': 'react-dom'
  },
  plugins: [
    new ExtractTextPlugin("[name].css")
  ]
};

我的目标是创建一个类似bootstrap-sass的包。

2 个答案:

答案 0 :(得分:1)

我强烈建议您使用webpack-merge分隔出Sass配置,以便其他软件包使用它。对于您当前的配置,我会做三件事:

  1. webpack-merge添加到您的项目(npm i --save-dev webpack-merge)。
  2. 将您的Sass配置放入一个名为webpack.sass-config.js的单独文件中。它包含以下内容:

    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    exports.config = function(options) {
      return {
        module: {
          loaders: [
            {
                test:   /\.(scss|sass|css)$/,
                loader: ExtractTextPlugin.extract("style", "css!sass")
            },
            {
                test: /\.scss$/, loader: 'style!css!sass!sass-resources'
            }
          ]
        },
        plugins: [
          new ExtractTextPlugin("[name].css")
        ]
      }
    }
    
    // Side note: returning a function instead of a plain object lets
    // you pass optional parameters from your main config file. This
    // is useful if you want to make something like your compiled css
    // file name different for another Webpack project without having
    // to edit your Sass configuration file.
    
  3. 将您的webpack.config.js更新为以下内容:

    var merge = require('webpack-merge');
    
    // import your separated Sass configuration
    var sassConfig = require('webpack.sass-config');
    
    // Define your common config for entry, output, JSX, fonts, etc.
    var common = {
      entry: ['./src/index.js'],
      module: {
        loaders: [
          {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel'
          },
          {
            test: /\.(png|woff|woff2|eot|ttf|svg)$/,
            loader: 'url-loader?limit=10000&name=fonts/[hash].[ext]'
          }
        ]
      },
      resolve: {
        extensions: ['', '.js', '.jsx']
      },
      output: {
        path: __dirname + '/build',
        publicPath: '/',
        filename: 'index.js',
        library: 'Supernova',
        libraryTarget: 'umd'
      },
      externals: {
        'react': 'react',
        'react-dom': 'react-dom'
      }
    };
    
    // Merge your common config and Sass config
    var config = merge(
        common,
        sassConfig.config()
    );
    
    // Export the merged configuration
    modules.exports = config;
    
  4. 显然,这可以远远超出你的Sass配置。我使用webpack-merge将我的开发配置与生产配置分开。 This article关于Survive JS是如何充分利用Webpack设置的绝佳资源。

答案 1 :(得分:1)

如果要将在sass文件中使用的变量提供给已发布程序包的使用者,则需要查看一些针对node-sass的特殊配置。

当前(以及您发布此信息的时间),node-sass支持使用javascript:https://github.com/sass/node-sass#functions--v300---experimental

编写自己的自定义sass函数。

这是未经测试的,但是我们前一段时间在一家我工作过的公司中做到了……要做您想做的事,您需要类似的东西:

src/
  your-package.js
  your-styles.scss

tools/
  constants/
    colours.js

  webpack/
    ...
    base.sass.js
    base.js
    development.js
    production.js

  sass/
    functions/
      colours.js

# tools/webpack/base.sass.js

const Config = require('webpack-config').default

import {
  signature as ColourSignature,
  handler as ColourHandler
} from '@tools/sass/functions/colours

module.exports = new Config()
  .merge({
    module: {
      rules: [
        {
          test: /\.scss$/,
          use: [
            ...
            { loader: 'sass-loader',
              options: {
                sourceMap: true,
                functions: {
                  [ColourSignature]: ColourHandler
                }
              }
            },
          ]
        }
      ]
    }
  })

# src/your-package.js

import Colours from '@tools/constants/colours'
import "./your-styles.scss"

export default YourAwesomeComponent {
  static Colours = Colours
}

export const colours = Colours

# src/your-styles.scss

.your-awesome-component { 
  background-color: ColourGet(veganvomit, sobrightithurts);
}
# tools/sass/functions/colour.js

import Colours from '@tools/constants/colours'

export signature = 'ColourGet($name, $shade: default)'
export handler = function(name, shade) {
  const colour = Colours[name]

  if (!colour) return 

  if (typeof colour === 'string') return colour

  return colour[shade]
}
# tools/sass/constants/colours.js

export default {

  veganvomit: {
    sobrightithurts: "darkkhaki",
    light: "#D2691E",
    default: "#8B4513",
    somethingsomethingsomethingdarkside: "#000"
  }

}

因此,现在您发布软件包时,他们可以从默认导出YourAwesomeClass.Colours访问sass变量,也可以直接从'您的真棒软件包'中导入import {颜色}