forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BundleExtensionsPlugin.js
84 lines (74 loc) · 3.01 KB
/
BundleExtensionsPlugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// this is how we do the magic for making sure extensions in plugins get applied
// to canvas modules. It depends upon conventional file system names (
// some file in the plugin has the same name as the coffeescript file it extends
// in canvas)
/*
# given app/coffeescripts/foo.coffee in canvas-lms, if you want to
# monkey patch it from your plugin, create
# app/coffeescripts/extensions/foo.coffee (in your plugin) like so:
#
# define ->
# (Foo) ->
# Foo::zomg = -> "i added this method"
# Foo
#
# and that's it, no changes required in canvas-lms, no plugin
# bundles, etc.
#
# note that Foo is not an explicit dependency, it magically figures
# it out. also note that your module should return a function that
# accepts and returns Foo. this function will magically wrap around
# Foo so you can do stuff to it anytime somebody requires "foo" as
# per usual.
*/
var glob = require("glob");
var loadExtensionsMap = function(){
var pluginExtensionsPattern = __dirname + "/../gems/plugins/*/app/coffeescripts/extensions/**/*.coffee";
var pluginExtensions = glob.sync(pluginExtensionsPattern, []);
var extensionsMap = {};
var extensionPartsRegexp = /plugins\/([^/]*)\/app\/coffeescripts\/extensions\/(.*)\.coffee/;
pluginExtensions.forEach(function(extension){
var extractions = extension.match(extensionPartsRegexp);
var pluginName = extractions[1];
var fileName = extractions[2];
if(extensionsMap[fileName] === undefined){
extensionsMap[fileName] = [];
}
extensionsMap[fileName].push(pluginName);
});
return extensionsMap;
};
// this is all the extensions that we can find in gems/plugins
var extensions = loadExtensionsMap();
var requireUndextendedRegexp = /^unextended!/;
var extensionRequirementRegexp = /\/extensions\//;
var BundleExtensionsPlugin = function(){};
BundleExtensionsPlugin.prototype.apply = function(compiler){
compiler.plugin("normal-module-factory", function(nmf) {
nmf.plugin("before-resolve", function(result, callback) {
var addLoadersFor = [];
// if we're resolving an extension, we don't want to try to
// extend the extension itself, so skip the check and move on
if(!extensionRequirementRegexp.test(result.request)){
Object.keys(extensions).forEach(function(key){
if(result.request.indexOf(key) > -1){
if(requireUndextendedRegexp.test(result.request)){
// skip, unextended loader means we really want the original
} else {
// we're trying to resolve a file that has an extension in at least one plugin,
// so we'll set the flag that tells us to add the withExtensions loader
// down below
addLoadersFor = extensions[key];
}
}
});
if(addLoadersFor.length > 0){
var newRequest = "withExtensions?" + addLoadersFor.join(",") + "!" +result.request;
result.request = newRequest;
}
}
return callback(null, result);
});
});
};
module.exports = BundleExtensionsPlugin;