Skip to content

Instantly share code, notes, and snippets.

@nathanhammond
Last active February 11, 2024 23:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanhammond/d45697aa0908197d52ea31edc1d0e7c7 to your computer and use it in GitHub Desktop.
Save nathanhammond/d45697aa0908197d52ea31edc1d0e7c7 to your computer and use it in GitHub Desktop.
Funnel which includes only the modules identified by rollup.
var path = require('path');
var Funnel = require('broccoli-funnel');
var Plugin = require('broccoli-plugin');
var rollup = require('rollup').rollup;
var amdNameResolver = require('amd-name-resolver').moduleResolve;
var existsSync = require('exists-sync');
// Create a subclass RollupFunnel derived from Plugin
RollupFunnel.prototype = Object.create(Plugin.prototype);
RollupFunnel.prototype.constructor = RollupFunnel;
function RollupFunnel(inputNodes, options) {
if (!(this instanceof RollupFunnel)) {
return new RollupFunnel(inputNodes, options);
}
this.originalInput = inputNodes;
options = options || {};
Plugin.call(this, [inputNodes], {
annotation: options.annotation
});
this.options = options;
}
RollupFunnel.prototype._copy = Funnel.prototype._copy;
RollupFunnel.prototype.build = function() {
var base = this.inputPaths[0];
var modules = [];
var rollupOptions = {
entry: this.options.rollup.entry,
dest: 'foo.js',
plugins: [
{
resolveId: function(importee, importer) {
var moduleName;
// This will only ever be the entry point.
if (!importer) {
moduleName = importee.replace(base, '');
modules.push(moduleName);
return path.join(base, importee);
}
// Link in the global paths.
moduleName = amdNameResolver(importee, importer).replace(base, '').replace(/^\//, '');
var modulePath = path.join(base, moduleName + '.js');
if (existsSync(modulePath)) {
modules.push(moduleName + '.js');
return modulePath;
}
}
}
]
};
return rollup(rollupOptions).then(function() {
modules.map(function(module) {
var inputPath = path.join(this.inputPaths[0], module);
var outputPath = path.join(this.outputPath, module);
this._copy(inputPath, outputPath);
}, this);
}.bind(this));
};
module.exports = RollupFunnel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment