this.files.forEach(function (file) {
            var polyfiller = autopolyfiller(options.browsers)
                .include(options.include)
                .exclude(options.exclude);

            file.src
                .filter(function (filepath) {
                    if (!grunt.file.exists(filepath)) {
                        grunt.log.warn('Source file "' + filepath + '" not found.');
                        return false;
                    } else {
                        return true;
                    }
                })
                .forEach(function (filepath) {
                    // Read file source.
                    polyfiller.add(grunt.file.read(filepath));
                });

            // Write the destination file.
            grunt.file.write(file.dest, polyfiller.toString());

            // Print a success message.
            grunt.log.writeln('File "' + file.dest + '" created.');
        });
            compilation.plugin("optimize-tree", function (chunks, modules, callback) {
                var loader = compilation.modules.filter(function (module) {
                    return module.rawRequest === "__polyfill_service_loader__";
                })[0];

                if ( !loader ) {
                    // `__load_polyfills__()` not called, nothing to do.
                    return callback();
                }

                if ( !polyfills ) {
                    polyfills = modules
                        .filter(function (module) {
                            return Boolean(module._source);
                        })
                        .reduce(function (polyfiller, module) {
                            return polyfiller.add(module._source._value);
                        }, autopolyfiller())
                        .polyfills;
                }

                polyfillServiceUrl = getPolyfillUrl(options, polyfills);
                compilation.rebuildModule(loader, callback);
            });
Example #3
0
module.exports = function(source, sourceMap) {
    var polyfills;

    try {
        var query = loaderUtils.parseQuery(this.query);
        // array of browsers
        var browsers = query.browsers || [];
        // array of excluded polyfills
        var exclude = query.exclude || [];
        // array of included polyfills
        var include = query.include || [];
        // use custom parser
        var customParse = query.withParser || [
                require('acorn'),
                query.parserOptions || {}
            ];
        // use custom polyfills
        var uses = query.use || [];

        // array of polyfills names
        var prePolyfills = autopolyfiller(browsers);

        if (customParse.length) {
            prePolyfills = prePolyfills.withParser.apply(prePolyfills, customParse);
        }
        uses.forEach(function(use) {
            prePolyfills.use(use);
        });

        polyfills = prePolyfills.exclude(exclude).include(include).add(source).polyfills;

        if (this.cacheable) {
            this.cacheable();
        }

    }
    catch (err) {
        throw new AutopolyfillerLoaderError(err.name, 'Can\'t get polyfills list (' + err.message + ')', err);
    }
    if (polyfills.length) {
        var inject = '\n/* injects from autopolyfiller-loader */\n';

        try {
            // append require()s with absoluted paths to neccessary polyfills
            polyfills.forEach(function(polyfill) {
                var path = getPolyfillPath(polyfill);
                var test = getPolyfillDetect(polyfill);
                inject += 'if (!(' + test + ')) require(' + JSON.stringify(path) + ');';
                inject += '\n';
            });

            inject += '\n';
        }
        catch (err) {
            throw new AutopolyfillerLoaderError(err.name, 'Can\'t get polyfill\'s source (' + err.message + ')', err);
        }

        // support existing SourceMap
        // https://github.com/mozilla/source-map#sourcenode
        // https://github.com/webpack/imports-loader/blob/master/index.js#L34-L44
        // https://webpack.github.io/docs/loaders.html#writing-a-loader
        if (sourceMap) {
            var currentRequest = loaderUtils.getCurrentRequest(this);
            var SourceNode = SourceMap.SourceNode;
            var SourceMapConsumer = SourceMap.SourceMapConsumer;
            var sourceMapConsumer = new SourceMapConsumer(sourceMap);
            var node = SourceNode.fromStringWithSourceMap(source, sourceMapConsumer);

            node.prepend(inject);

            var result = node.toStringWithSourceMap({
                file: currentRequest
            });

            return void this.callback(null, result.code, result.map.toJSON());
        }

        // prepend collected inject at the top of file
        return inject + source;
    }

    // return the original source and sourceMap
    if (sourceMap) {
        return void this.callback(null, source, sourceMap);
    }

    // return the original source
    return source;
};