Skip to content

Get list of automatically injected helpers #7

Description

@niksy

This plugin has option to whitelist helpers, but what if it could detect which helpers are automatically injected and create whitelist from that?

I’ve made PoC based on Webpack 3 version of the plugin.

module.exports = function (compiler, pluginOptions) {
    compiler.plugin('compilation', function (compilation, params) {
        const injectedBabelHelpers = [];
        params.normalModuleFactory.plugin('parser', (parser) => {
            parser.plugin('call babelHelpers.*', ( expr ) => {
                injectedBabelHelpers.push(expr.callee.property.name);
            });
        });
        inject(compilation, pluginOptions, injectedBabelHelpers);
    });
};

function inject(compilation, pluginOptions, injectedBabelHelpers) {
    compilation.dependencyFactories.set(SingleEntryDependencyWrapper, new NullFactory());
    compilation.plugin('seal', function () {
        // Babel helpers whitelist is available here
        getChunks(this, pluginOptions.entries).forEach(chunk => injectHelpers(this, chunk.module, pluginOptions.whitelist));
    });
}

// …

It uses Webpack parser to get all babelHelpers expressions from AST and stores methods used in array. That array is then passed down to inject method, at first it’s empty, but in seal phase it’s filled with all the helpers inside compiled module. It may seem like a hack, but as I said, this is PoC and it could possibliy be made better. Final array can then be filtered for duplicates and used instead of pluginOptions.whitelist.

This should of course be another plugin option and it should be opt-in.

What do you think? Am I missing something here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions