如何使用webpack合并和压缩文件

37

2
我最终做的是在 entry 中列出我想要压缩的所有代码,就像这样:entry:{ vendor: ['file.js', 'file2.js', 'file3.js'] } - Binh Phan
这对我不起作用...它只导出最后一个文件...我不知道webpack对第一个文件做了什么... - Albert James Teddy
4个回答

30
将多个CSS合并成单个文件可以使用extract-text-webpack-pluginwebpack-merge-and-include-globally

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

使用webpack-merge-and-include-globally可以将多个JavaScript文件合并为单个文件,而无需使用AMD或CommonJS包装器。或者,您可以使用expose-loader将这些包装模块公开为全局范围。

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

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

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')
      ]
    })
  ]
};

3

现在它不支持webpack 3 :( 有其他替代方案吗? - Sahil Purav
似乎它不再受支持了。 - karoluS

-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


-16

12
目前我的项目是一个 HTML 文件,里面有很多的 script 标签。我只想提供 JS 文件列表并使用 webpack 进行合并和压缩。 - Binh Phan
7
我一定要把几个完全独立的文件(shims)连接成一个文件。不要告诉我“你不需要连接文件”。 - MaxXx1313

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接