-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (56 loc) · 2.32 KB
/
index.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
var extend = require('lodash/extend');
var loaderUtils = require('loader-utils');
var path = require('path');
module.exports = function() {};
module.exports.pitch = function(remainingRequest, precedingRequest, data) {
if (this.cacheable) {
this.cacheable();
}
var addStylesOptions = extend({
singleton: true,
}, loaderUtils.parseQuery(this.query));
var valuesPath = loaderUtils.stringifyRequest(this, require.resolve('lodash/values'));
var replaceTokensPath = loaderUtils.stringifyRequest(this, path.join(__dirname, 'replace-tokens'));
var addStylesPath = loaderUtils.stringifyRequest(this, '!' + require.resolve('style-loader/addStyles'));
var contentPath = loaderUtils.stringifyRequest(this, "!!" + remainingRequest);
return [
"var styles = {};",
"var updater;",
// Utils
"var values = require(" + valuesPath + ");",
"var replaceTokens = require(" + replaceTokensPath + ");",
"var addStyles = require(" + addStylesPath + ");",
// Stylesheet template
"var styleTemplate = require(" + contentPath + ");",
"if (typeof styleTemplate === 'string') styleTemplate = [[module.id, styleTemplate, '']];",
// Update the inject styles on the page
"function update() {",
" var list = Array.prototype.concat.apply([], values(styles));",
" if (list.length && updater) {",
" updater(list, " + JSON.stringify(addStylesOptions) + ");",
" } else if (list.length) {",
" updater = addStyles(list, " + JSON.stringify(addStylesOptions) + ");",
" } else if (updater) {",
" updater();",
" updater = null;",
" }",
" return exports;",
"}",
// Add styles to the page for a specific ID
"function insert(id, tokens) {",
" styles[id] = replaceTokens(styleTemplate, tokens);",
" return update();",
"}",
// Remove styles from the page for a specific ID
"function remove(id) {",
" delete styles[id];",
" return update();",
"}",
"exports.update = update;",
"exports.insert = insert;",
"exports.remove = remove;",
"exports.styles = styles;",
// @TODO: Hot module reloading
""
].join('\n');
};