簡體   English   中英

Webpack:將CSS提取到其自己的包中

[英]Webpack: extract css to its own bundle

我在一個項目上使用webpack。 我使用樣式加載器,因此可以import "my.css"

可以將CSS與javascript捆綁在一起,並在組件安裝好后以<style>標記呈現。

但是,我想保留該導入語法,並使webpack為每個入口點構建一個CSS捆綁包。

因此,webpack輸出應為[name].bundle.js AND [name].bundle.css 這是我可以實現的嗎?

var config = {
  entry: {
    'admin': APP_DIR + '/admin.entry.jsx',
    'public': APP_DIR + '/public.entry.jsx'
  },
  output: {
    path: BUILD_DIR,
    filename: '[name].bundle.js'
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json']
  },
  plugins: [],
  devtool: 'cheap-source-map',
  module: {
    loaders: [{
        test: /(\/index)?\.jsx?/,
        include: APP_DIR,
        loader: 'babel-loader'
      },
      {
        test: /\.scss$/,
        loaders: [
          'style-loader',
          'css-loader',
          'sass-loader',
          {
            loader: 'sass-resources-loader',
            options: {
              resources: ['./src/styles/constants.scss']
            },
          }
        ]
      }
    ],
  }
};

與此babel.rc一起:

{
  "presets" : ["es2015", "react"],
  "plugins": ["transform-decorators-legacy", "babel-plugin-root-import"]
}

是的,您需要使用extract-text-webpack-plugin 您可能只想在生產配置上執行此操作。 配置看起來像這樣:

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

module.exports = {
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          //resolve-url-loader may be chained before sass-loader if necessary
          use: ['css-loader', 'sass-loader']
        })
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('style.css')
    //if you want to pass in options, you can do so:
    //new ExtractTextPlugin({
    //  filename: 'style.css'
    //})
  ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM