I can't use ES6 in webpack config file

后端 未结 2 597
小鲜肉
小鲜肉 2021-01-19 17:44

Is possible to use ES6 (especially import - instead of require) in webpack config file?

I have e.g.

import webpack from \'webpack\';
<
2条回答
  •  旧巷少年郎
    2021-01-19 18:12

    You could use gulp and babel/register like this:

    var gulp = require('gulp');
    var webpack = require('webpack');
    var gutil = require('gutil');
    var babel = require('babel/register');
    var config = require(path.join('../..', 'webpack.config.es6.js'));
    
    gulp.task('webpack-es6-test', function(done){
       webpack(config).run(onBuild(done));
    });
    
    function onBuild(done) {
        return function(err, stats) {
            if (err) {
                gutil.log('Error', err);
                if (done) {
                    done();
                }
            } else {
                Object.keys(stats.compilation.assets).forEach(function(key) {
                    gutil.log('Webpack: output ', gutil.colors.green(key));
                });
                gutil.log('Webpack: ', gutil.colors.blue('finished ', stats.compilation.name));
                if (done) {
                    done();
                }
            }
        }
    }
    

    ...and your webpack config can have any es6. Tested and works for me.

提交回复
热议问题