webpack 2 怎么提取文件下的css

//webpack

var path = require('path');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
  entry: './app/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  },
  module:{
      rules:[{
          test:/\.css$/,
          use:ExtractTextPlugin.extract({
              use:'css-loader'
          })
      }]
  },
  plugins:[
      new ExtractTextPlugin('styles.css'),
  ]
};

//index

<html>
  <head>
    <title>webpack 2 demo</title>
    <link rel="stylesheet" type="text/css" href="dist/styles.css"/>
    <style type="text/css">
        h1{
            background: black;
        }
    </style>
  </head>
  <body>
      <div class='container'>
          <h1>webpack</h1>
      </div>
      <script src="dist/bundle.js"></script>
  </body>
</html>

//package
{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack --config webpack.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^0.28.1",
    "extract-text-webpack-plugin": "^2.1.0",
    "style-loader": "^0.17.0",
    "webpack": "^2.4.1"
  },
  "dependencies": {
    "lodash": "^4.17.4"
  }
}

我的配置只能提取import引入的css 不能提取内联的 求大神指点

阅读 4.4k
4 个回答

你的样式写在css里面的才能提取出来,因为你test 就是匹配的css

之余内联样式,是提取不出来的,那个会被test js所匹配打包的

只能提取import的css,内联的没有办法

你以为test这一个选项是干嘛用的?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进