如何使用webpack连接和缩小文件

时间:2016-02-18 02:12:45

标签: javascript webpack minify

我希望能够在不使用grunt How to concatenate and minify multiple CSS and JavaScript files with Grunt.js (0.3.x)的情况下将文件缩小并连接到单个文件 我可以用webpack实现这个目标吗?我尝试了很多不同的组合,但问题是我使用的一些库,假设它是AMD或CommonJS格式,所以我一直在收到错误。

4 个答案:

答案 0 :(得分:8)

可以使用 extract-text-webpack-plugin webpack-merge-and-include-global 将多个CSS合并到单个文件中。

https://code.luasoftware.com/tutorials/webpack/merge-multiple-css-into-single-file/

要在没有AMD或CommonJS包装器的情况下将多个JavaScripts合并到单个文件中,可以使用 webpack-merge-and-include-global 来完成。或者,您可以使用 expose-loader 将这些包装的模块公开为全局范围。

https://code.luasoftware.com/tutorials/webpack/merge-multiple-javascript-into-single-file-for-global-scope/

使用 webpack-merge-and-include-global 的示例。

const path = require('path');
const MergeIntoSingleFilePlugin = require('webpack-merge-and-include-globally');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new MergeIntoSingleFilePlugin({
      "bundle.js": [
        path.resolve(__dirname, 'src/util.js'),
        path.resolve(__dirname, 'src/index.js')
      ],
      "bundle.css": [
        path.resolve(__dirname, 'src/css/main.css'),
        path.resolve(__dirname, 'src/css/local.css')
      ]
    })
  ]
};

答案 1 :(得分:3)

试试这个插件,目的是在没有webpack的情况下连接和缩小js:

https://github.com/hxlniada/webpack-concat-plugin

答案 2 :(得分:-8)

  1. 使用Webpack时不需要连接文件,因为默认情况下Webpack会这样做。

    Webpack将生成一个包文件,其中包含项目中所需的所有文件。

  2. Webpack内置了UglifyJs支持(https://github.com/krisrak/html5-cordova-samples/tree/master/file

答案 3 :(得分:-9)

是的,您可以使用webpack缩小它,如下所示

    module.exports = {
  // static data for index.html
  metadata: metadata,
  // for faster builds use 'eval'
  devtool: 'source-map',
  debug: true,

  entry: {
    'vendor': './src/vendor.ts',
    'main': './src/main.ts' // our angular app
  },

  // Config for our build files
  output: {
    path: root('dist'),
    filename: '[name].bundle.js',
    sourceMapFilename: '[name].map',
    chunkFilename: '[id].chunk.js'
  },

  resolve: {
    // ensure loader extensions match
    extensions: ['','.ts','.js','.json','.css','.html','.jade']
  },

  module: {
    preLoaders: [{ test: /\.ts$/, loader: 'tslint-loader', exclude: [/node_modules/] }],
    loaders: [
      // Support for .ts files.
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        query: {
          'ignoreDiagnostics': [
            2403, // 2403 -> Subsequent variable declarations
            2300, // 2300 -> Duplicate identifier
            2374, // 2374 -> Duplicate number index signature
            2375  // 2375 -> Duplicate string index signature
          ]
        },
        exclude: [ /\.(spec|e2e)\.ts$/, /node_modules\/(?!(ng2-.+))/ ]
      },

      // Support for *.json files.
      { test: /\.json$/,  loader: 'json-loader' },

      // Support for CSS as raw text
      { test: /\.css$/,   loader: 'raw-loader' },

      // support for .html as raw text
      { test: /\.html$/,  loader: 'raw-loader' },

      // support for .jade as raw text
      { test: /\.jade$/,  loader: 'jade' }

      // if you add a loader include the resolve file extension above
    ]
  },

  plugins: [
    new CommonsChunkPlugin({ name: 'vendor', filename: 'vendor.bundle.js', minChunks: Infinity }),
    // new CommonsChunkPlugin({ name: 'common', filename: 'common.js', minChunks: 2, chunks: ['main', 'vendor'] }),
    // static assets
    new CopyWebpackPlugin([ { from: 'src/assets', to: 'assets' } ]),
    // generating html
    new HtmlWebpackPlugin({ template: 'src/index.html', inject: false }),
    // replace
    new DefinePlugin({
      'process.env': {
        'ENV': JSON.stringify(metadata.ENV),
        'NODE_ENV': JSON.stringify(metadata.ENV)
      }
    })
  ],

  // Other module loader config
  tslint: {
    emitErrors: false,
    failOnHint: false
  },
  // our Webpack Development Server config
  devServer: {
    port: metadata.port,
    host: metadata.host,
    historyApiFallback: true,
    watchOptions: { aggregateTimeout: 300, poll: 1000 }
  },
  // we need this due to problems with es6-shim
  node: {global: 'window', progress: false, crypto: 'empty', module: false, clearImmediate: false, setImmediate: false}
};

这是angular2 webpack

的缩小和连续示例

也许你可以阅读它 https://github.com/petehunt/webpack-howto 第一