使用webpack构建时自动插入babel-polyfill

时间:2017-01-26 06:05:27

标签: webpack babeljs babel-polyfill

我有以下webpack配置:

const path = require('path');

const webpack = require('webpack');


function NoOpPlugin(options) {
    // Setup the plugin instance with options...
}
NoOpPlugin.prototype.apply = function(compiler) {
    compiler.plugin('done', function(){});
};


module.exports = {
    entry: {
        main: __dirname + '/src/public/mainapp/app.js'
    },
    output: {
        path: __dirname + "/assets/dist",
        filename: '[name].min.js',
    },
    externals: {
        window: 'window'
    },
    module: {
        loaders: [
            {
                test: /\.modernizrrc.js$/,
                loader: "modernizr"
            },
            {
                test: /\.modernizrrc(\.json)?$/,
                loader: "modernizr!json"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    "presets": [
                        ["env", {
                            "targets": {
                                "browsers": [
                                    "> 1%",
                                    "last 2 versions"
                                ]
                            },
                            "useBuiltIns": true
                        }]
                    ],
                    "plugins": [
                        "inferno",
                        "transform-decorators-legacy",
                        "transform-object-rest-spread"
                    ]
                }
            },
            {
                test: /\.js$/,
                loader: "eslint-loader",
                exclude: /node_modules/
            }
        ]
    },
    resolve: {
        alias: {
            modernizr$: path.resolve(__dirname, ".modernizrrc")
        }
    },
    plugins: [
        new webpack.DefinePlugin({
            "process.env": {
                BROWSER: JSON.stringify(true),
                NODE_ENV: JSON.stringify(process.env.NODE_ENV || '')
            }
        }),
        process.env.NODE_ENV === 'production' ? new webpack.optimize.UglifyJsPlugin() : new NoOpPlugin()
    ]
}

如果我将import 'babel-polyfill';放在app.js文件的开头,babel-preset-env会将其转换为仅使用我需要的polyfill。

我希望webpack插入polyfill而不必修改我的源代码。我知道我可以将我的入口点更改为[' babel-polyfill',__ dirname +' /src/public/mainapp/app.js'],但随后每个polyfill都会添加到输出中我是否需要它。有什么方法可以做到这一点,同时仍然只选择我需要的polyfill?

0 个答案:

没有答案