简体   繁体   中英

UglifyJs error with Gulp and webpack

I am currently discovering the module bundler "Webpack". I am trying to do something quite easy : I have a main.js entry and a bundle.js output. I use babel in order to translate in ES6.

My package.json :

{
  "name": "me",
  "version": "1.0.0",
  "description": "Builder",
  "main": "index.js",
  "scripts": {
    ...
  },
  "author": "me",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-preset-env": "^1.5.2",
    "babel-preset-es2015": "^6.24.1",
    "gulp": "^3.9.1",
    "lodash": "^4.17.4",
    "webpack": "^3.0.0",
    "webpack-stream": "^3.2.0"
  },
  "dependencies": {
    "jquery": "^3.2.1"
  }
}

Webpack.config.js :

var path = require('path');
var webpack = require('webpack');

module.exports = {
    entry: path.resolve(__dirname, 'js/main.js'),
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['es2015']
                    }
                }
            }
        ]
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin()
    ]
}

When I prompt webpack in the CLI, it works pretty well, a bundle.js file which is minified is created in the dist folder.

Now, I want to combine with Gulp

Here is my gulpfile.js :

var gulp = require('gulp');
var webpackS = require('webpack-stream');

gulp.task('default', function() {

  return gulp.src('./app/js/main.js')
    .pipe(webpackS( require('./app/webpack.config.js') ))
    .pipe(gulp.dest('./app/dist/'));

});

When I enter gulp in the CLI, I have this error :

stream.js:74
      throw er; // Unhandled stream error in pipe.
      ^
Error: bundle.js from UglifyJs
Unexpected token: name (_) [bundle.js:47,8]

However, when I remove the line new webpack.optimize.UglifyJsPlugin() from webpack.config.js and prompt gulp in the CLI it works perfectly !

I reinstalled all the npm packages but the problem is still here.

Does anybody have an idea ?

How do you run the Webpack config? I assume by webpack path-to-config/webpack.config.js ?

You have new webpack.optimize.UglifyJsPlugin() in your config, so by using webpack -p which executes the production mode, you get an error because UglifyJsPlugin is executed twice. I guess the same happens with your Gulp setup.

However, the error message points to "_" not defined, which could be related to lodash . Maybe the import can't be resolved by Gulp?

However, I would not mix Gulp and Webpack. See the discussion here: Gulp + Webpack or JUST Webpack? There is also an example using Webpack from within a gulp task.

I would like to suggest you to don't mix webpack and gulp , just create a script inside your package.json#scripts which runs webpack and then if you still want to exec webpack inside a gulp task you can do:

var exec = require('child_process').exec;

gulp.task('runWebpack', function (callback) {
  exec('npm run webpack', callback);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM