鍍金池/ 問答/HTML/ webpack 貌似把style-loader打進(jìn)去了。。。

webpack 貌似把style-loader打進(jìn)去了。。。

這是我的 webpack 配置

const path = require('path')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const pkg = require('../package.json')
const webpack = require('webpack')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

module.exports = {
  context: path.resolve(__dirname, '../'),
  mode: 'development',
  target: 'web',
  entry: {
    app: './src/main.js'
  },
  output: {
    filename: 'js/[name].bundle.js',
    path: path.resolve('./dist'),
    publicPath: '/'
  },
  resolve: {
    // modules: [
    //   path.resolve('./src'),
    //   'node_modules'
    // ],
    alias: {
      '@': path.resolve('./src')
    },
    extensions: ['.js', '.vue', '.json']
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: [path.resolve('./src')],
        loader: 'babel-loader'
      },
      {
        test: /\.js$/,
        include: [path.resolve('./src')],
        enforce: 'pre',
        loader: 'eslint-loader',
        options: {
          formatter: require('eslint-friendly-formatter'),
          emitError: true
        }
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: 'style-loader',
            options: {
              sourceMap: true
            }
          },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
              importLoaders: 1
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true
            }
          }
        ]
      },
      {
        test: /\.(png|jpe?g|gif|svg)$/,
        loader: 'url-loader',
        options: {
          limit: 8192,
          name: 'images/[name].[hash:5].[ext]'
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10240,
          name: 'fonts/[name].[hash:5].[ext]'
        }
      }
    ]
  },
  devServer: {
    contentBase: path.resolve('static'),
    publicPath: '/',
    compress: true,
    hot: true,
    open: true,
    overlay: true,
    port: 9999,
    clientLogLevel: 'warning',
    proxy: {}
  },
  plugins: [
    new CleanWebpackPlugin([path.resolve('./dist')], {
      root: path.resolve('./')
    }),
    new HtmlWebpackPlugin({
      title: pkg.name,
      filename: 'index.html',
      template: path.resolve('index.html')
    }),
    new webpack.HotModuleReplacementPlugin(),
    new BundleAnalyzerPlugin()
  ],
  devtool: 'cheap-eval-source-map'
}

在打包后用 analyzer 分析的時(shí)候?yàn)槭裁磿?huì)把 style-loader 和 css-loader 也打進(jìn)去了。。。。導(dǎo)致文件特別大

回答
編輯回答
胭脂淚

你沒有使用插件把 css 單獨(dú)打成一個(gè)文件,所以樣式是通過 js 動(dòng)態(tài)創(chuàng)建 style 標(biāo)簽插入頁面的,你覺得這部分代碼是哪來的?就是 style-loader!所以這種方式是會(huì)把 style-loader 打進(jìn)去來完成動(dòng)態(tài)創(chuàng)建 style 標(biāo)簽引入樣式的功能。你可以選擇使用 mini-css-extract-plugin 插件將 css 單獨(dú)提取為一個(gè)文件來避免此問題,這一步都是產(chǎn)品模式下配置的。此外,在開發(fā)模式下分析 bundle 沒什么意義。

2018年4月14日 17:12
編輯回答
還吻

你的package.json 貼一下

2017年1月24日 03:29
編輯回答
逗婦惱

在pkg.json里面是不是把style-loader和cssloader放dependence里面了?這兩個(gè)東西應(yīng)該在dev-dependece里面。
另外,你webpack里面配置的mode就是'development',所以打進(jìn)去也理所應(yīng)當(dāng)。


看到你貼的pkg.json了,大兄弟,你只有個(gè)devdependence,而且webpack里面配置的mode就是'development',打包當(dāng)然就打包這個(gè)啦。

建議你去學(xué)學(xué)webpack基礎(chǔ)的東西。不然你不會(huì)明白的。

2017年4月13日 19:04