37

I want to be able to minify and concatenate files to 1 single file without using grunt How to concatenate and minify multiple CSS and JavaScript files with Grunt.js (0.3.x) can I achieve this with just webpack? I tried many different combinations but the issue is some of the libraries I use assuming that it's AMD or CommonJS format so I keep on getting errors.

Community
  • 1
  • 1
Binh Phan
  • 381
  • 1
  • 3
  • 3
  • 2
    What i ended up doing was list all the code I want to minify in entry like this `entry:{ vendor: ['file.js', 'file2.js', 'file3.js'] }` – Binh Phan Feb 23 '16 at 18:09
  • This does not work for me... it only export the last file... I do not know what webpack does with the first ones... – Albert James Teddy Mar 10 '17 at 13:12

4 Answers4

30

Merge multiple CSS into single file can done using extract-text-webpack-plugin or webpack-merge-and-include-globally.

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

To merge multiple JavaScripts into single file without AMD or CommonJS wrapper can be done using webpack-merge-and-include-globally. Alternatively, you can expose those wrapped modules as global scope using expose-loader.

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

Example using 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')
      ]
    })
  ]
};
Desmond Lua
  • 6,142
  • 4
  • 37
  • 50
3

try this plugin, aims to concat and minify js without webpack:

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

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
xlhuang
  • 221
  • 2
  • 9
-9

yes you can minify it with webpack looks like this

    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}
};

this is example minify and concat for angular2 webpack

maybe you can read it https://github.com/petehunt/webpack-howto first

devnode
  • 17
-16
  1. You don't need to concatenate files while using Webpack, because Webpack does this by default.

    Webpack will generate a bundle file which includes all files that you have required in your project.

  2. Webpack has built-in UglifyJs support (http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin)

Community
  • 1
  • 1
gucheen
  • 17
  • 5
  • 12
    Gucheen my project right now is an html file with the whole bunch of script tags I just want give lists of of js files and have webpack concatenation and minify it. – Binh Phan Feb 18 '16 at 02:41
  • 7
    I definitely want to concatenate several completely independent files (shims) into one. Don't tell me "You don't need to concatenate files". – MaxXx1313 Mar 30 '17 at 19:44