Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get list of automatically injected helpers #7

Open
niksy opened this issue Jul 11, 2018 · 0 comments
Open

Get list of automatically injected helpers #7

niksy opened this issue Jul 11, 2018 · 0 comments

Comments

@niksy
Copy link

niksy commented Jul 11, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant