forked from akanix42/meteor-css-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
58 lines (52 loc) · 2.47 KB
/
plugin.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
/* globals Plugin */
import CssModulesBuildPlugin from './css-modules-build-plugin';
import pluginOptionsWrapper from './options';
import ImportPathHelpers from './helpers/import-path-helpers';
const pluginOptions = pluginOptionsWrapper.options;
ImportPathHelpers.init(Plugin);
if (pluginOptions.extensions.indexOf('css') === -1) {
registerCompiler();
} else {
monkeyPatchToHandleCssExtension();
}
global.cssModules = global.cssModules || {};
global.cssModules.compiler = new CssModulesBuildPlugin(Plugin);
function registerCompiler() {
Plugin.registerCompiler({
extensions: pluginOptions.extensions,
archMatching: pluginOptions.specificArchitecture,
filenames: pluginOptions.filenames
}, function() {
return new CssModulesBuildPlugin(Plugin);
});
}
/**
* Monkey patch _registerSourceProcessor and SourceProcessorSet.merge so we can block the default CSS compiler
*/
function monkeyPatchToHandleCssExtension() {
const registerSourceProcessor = Plugin._registerSourceProcessor;
Plugin._registerSourceProcessor = function(options, factory, { sourceProcessorSet, methodName, featurePackage }) {
const buildPluginMerge = sourceProcessorSet.constructor.prototype.merge;
sourceProcessorSet.constructor.prototype.merge = function(otherSet, options) {
/* If a css plugin handler has already been added,
* don't merge the meteor package, which only includes the 'css' package
*/
if (otherSet._myPackageDisplayName !== 'meteor' || !('css' in this._byExtension)) {
/* If we're using CSS modules inside of a package, it's possible that Meteor's CSS build plugin got merged
* before we could block it. In that case, unregister the Meteor CSS processor.
*/
if (('css' in this._byExtension) && 'css' in otherSet._byExtension) { // && sp.isopack.displayName() === 'meteor')
const previouslyRegisteredSourceProcessor = this._byExtension.css[0];
if (previouslyRegisteredSourceProcessor.isopack.displayName() === 'meteor' && otherSet._myPackageDisplayName === 'nathantreid:css-modules') {
this.allSourceProcessors.splice(this.allSourceProcessors.indexOf(previouslyRegisteredSourceProcessor), 1);
delete this._byExtension.css;
}
}
buildPluginMerge.call(this, ...arguments);
}
};
registerSourceProcessor(options, factory, { sourceProcessorSet, methodName, featurePackage });
};
registerCompiler();
Plugin._registerSourceProcessor = registerSourceProcessor;
}