Webpack Minified和Unminified Js和Css

时间:2017-02-18 22:40:03

标签: javascript webpack

这是我想要的文件结构:

- dist
  - js
    - jPlayer.js
    - jPlayer.min.js
  - css
    - skins
      - sleek.css
      - sleek.min.css

这是我的webpack.config.js

import ExtractTextPlugin from 'extract-text-webpack-plugin';
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import autoprefixer from 'autoprefixer';
import webpack from 'webpack';

const debug = process.env.NODE_ENV !== 'production';

export default {
  context: __dirname,
  entry: {
    'jPlayer.js': './build.js',
    'jPlayer.min.js': './build.js',
    'sleek.css': './src/less/skins/sleek.less',
    'sleek.min.css': './src/less/skins/sleek.less'
  },
  output: {
    path: './dist/',
    filename: '[name]',
  },
  devServer: {
    historyApiFallback: true,
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['transform-class-properties', 'transform-decorators-legacy'],
        },
      },
      {
        test: /\.less$/,
        loader: ExtractTextPlugin.extract('style-loader',
          'css-loader?importLoaders=1' +
          '!postcss-loader' +
          '!less-loader'),
      },
      {
        test: /\.(woff2?|eot|ttf|svg)(\?[\s\S]+)?$/,
        loader: 'url-loader?limit=100000',
      },
      {
        test: /\.(jpg)$/,
        loader: 'file-loader',
      },
    ],
  },
  plugins: [
    new ExtractTextPlugin('[name]'),
    new webpack.optimize.UglifyJsPlugin({
      include: /\.min\.js$/,
    }),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.min\.css$/
    })
  ],
  postcss: () => [autoprefixer],
  resolve: {
    extensions: ['', '.js', '.jsx'],
  },
};

目前输出

- dist
  - jPlayer.js
  - jPlayer.min.js
  - sleek.css
  - sleek.min.css

UglifyJs还会不断修改我的sleek.css文件:

enter image description here

我该怎么做?

这不是我想要的。

2 个答案:

答案 0 :(得分:0)

尝试将output配置更改为:

output: {
    path: './dist/',
    filename: 'js/[name]',
}

ExtractTextPlugin的配置为:

new ExtractTextPlugin('./css/[name].css')

答案 1 :(得分:0)

UglifyJS就是这里的问题,出于某种原因它与css文件混淆,即使它的JS名称只是愚蠢。

BabiliPlugin工作得更好,因为它没有弄乱css文件。

import ExtractTextPlugin from 'extract-text-webpack-plugin';
import OptimizeCssAssetsPlugin from 'optimize-css-assets-webpack-plugin';
import autoprefixer from 'autoprefixer';
import BabiliPlugin from 'babili-webpack-plugin';

export default {
  context: __dirname,
  entry: {
    'js/jPlayer.js': './src/index.js',
    'js/jPlayer.min.js': './src/index.js',
    'css/skins/sleek.css': './src/less/skins/sleek.less',
    'css/skins/sleek.min.css': './src/less/skins/sleek.less',
  },
  output: {
    path: './dist/',
    filename: '[name]',
  },
  devServer: {
    historyApiFallback: true,
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['transform-class-properties', 'transform-decorators-legacy'],
        },
      },
      {
        test: /\.less$/,
        loader: ExtractTextPlugin.extract('style-loader',
          'css-loader?importLoaders=1' +
          '!postcss-loader' +
          '!less-loader'),
      },
      {
        test: /\.(woff2?|eot|ttf|svg)(\?[\s\S]+)?$/,
        loader: 'url-loader?limit=100000',
      },
      {
        test: /\.jpg$/,
        loader: 'file-loader',
      },
    ],
  },
  plugins: [
    new ExtractTextPlugin('[name]'),
    new OptimizeCssAssetsPlugin({
      assetNameRegExp: /\.min\.css$/,
    }),
    new BabiliPlugin({}, {
      test: /\.min\.js$/,
    }),
  ],
  postcss: () => [autoprefixer],
  resolve: {
    extensions: ['', '.js', '.jsx'],
  },
};