forked from systemjs/plugin-css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css-plugin-base.js
70 lines (59 loc) · 1.83 KB
/
css-plugin-base.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
/*
* Base CSS Plugin Class
*/
function CSSPluginBase(compileCSS) {
this.compileCSS = compileCSS;
this.translate = function(load, opts) {
var loader = this;
if (loader.builder && loader.buildCSS === false) {
load.metadata.build = false;
return;
}
var path = this._nodeRequire && this._nodeRequire('path');
return Promise.resolve(compileCSS.call(loader, load.source, load.address, load.metadata.loaderOptions || {}))
.then(function(result) {
load.metadata.style = result.css;
load.metadata.styleSourceMap = result.map;
if (result.moduleFormat)
load.metadata.format = result.moduleFormat;
return result.moduleSource || '';
});
};
}
var isWin = typeof process != 'undefined' && process.platform.match(/^win/);
function toFileURL(path) {
return 'file://' + (isWin ? '/' : '') + path.replace(/\\/g, '/');
}
var builderPromise;
function getBuilder(loader) {
if (builderPromise)
return builderPromise;
return builderPromise = loader['import']('./css-plugin-base-builder.js', module.id);
}
CSSPluginBase.prototype.bundle = function(loads, compileOpts, outputOpts) {
var loader = this;
return getBuilder(loader)
.then(function(builder) {
return builder.bundle.call(loader, loads, compileOpts, outputOpts);
});
};
CSSPluginBase.prototype.listAssets = function(loads, opts) {
var loader = this;
return getBuilder(loader)
.then(function(builder) {
return builder.listAssets.call(loader, loads, opts);
});
};
/*
* <style> injection browser plugin
*/
// NB hot reloading support here
CSSPluginBase.prototype.instantiate = function(load) {
if (this.builder)
return;
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = load.metadata.style;
document.head.appendChild(style);
};
module.exports = CSSPluginBase;