This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
css-plugin-base-builder.js
193 lines (164 loc) · 6.42 KB
/
css-plugin-base-builder.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var fs = require('@node/fs');
var path = require('@node/path');
var postCssBundle = require('./postcss-bundle.js');
var postcss = postCssBundle.postcss;
var autoprefixer = postCssBundle.autoprefixer;
var cssnano = postCssBundle.cssnano;
var atImport = postCssBundle.atImport;
var atUrl = postCssBundle.atUrl;
var cssInject = "(function(c){if (typeof document == 'undefined') return; var d=document,a='appendChild',i='styleSheet',s=d.createElement('style');s.type='text/css';d.getElementsByTagName('head')[0][a](s);s[a](d.createTextNode(c));})";
var cssInjectSourceMaps = "(function(c){if (typeof document == 'undefined') return;var d=document,a='appendChild',s=d.createElement('link');s.rel='stylesheet';s.href=URL.createObjectURL(new Blob([c],{type:'text/css'}));d.getElementsByTagName('head')[0][a](s);})";
function escape(source) {
return source
.replace(/(["\\])/g, '\\$1')
.replace(/[\f]/g, "\\f")
.replace(/[\b]/g, "\\b")
.replace(/[\n]/g, "\\n")
.replace(/[\t]/g, "\\t")
.replace(/[\r]/g, "\\r")
.replace(/[\u2028]/g, "\\u2028")
.replace(/[\u2029]/g, "\\u2029");
}
var isWin = process.platform.match(/^win/);
function fromFileURL(url) {
return url.substr(7 + !!isWin).replace(/\//g, isWin ? '\\' : '/');
}
var listAssetsCnt = 0;
exports.listAssets = function(loads, opts) {
// count the number of plugin phases inheriting this plugin
listAssetsCnt++;
return loads.map(function(load) {
return {
url: load.address,
source: load.metadata.style,
sourceMap: load.metadata.styleSourceMap,
type: 'css'
};
});
};
var bundleCnt = 0;
var cssLoads = [];
exports.bundle = function(loads, compileOpts, outputOpts) {
// count the number of phases inheriting this plugin
// then apply reduction as a single process for the last one only
bundleCnt++;
cssLoads = cssLoads.concat(loads);
if (bundleCnt != listAssetsCnt)
return;
var loader = this;
// backwards compat with fileURL for rootURL
if (loader.rootURL && loader.rootURL.substr(0, 5) == 'file:')
loader.rootURL = fromFileURL(loader.rootURL);
if (loader.browserRootURL && loader.browserRootURL[loader.browserRootURL.length - 1] !== '/')
loader.browserRootURL += '/';
// reset for next
bundleCnt = listAssetsCnt = 0;
var baseURLPath = fromFileURL(loader.baseURL);
var inputFiles = {};
cssLoads.forEach(function(load) {
inputFiles[path.relative(baseURLPath, fromFileURL(load.address))] = {
source: load.metadata.style,
sourceMap: load.metadata.styleSourceMap
};
});
cssLoads = [];
var absRegEx = /^[a-z]+:/;
function absUrl(url) {
return url[0] !== '.' && (url.match(absRegEx) || url[0] === '/');
}
var cwd = process.cwd();
var postCssPlugins = [atImport({
resolve: function(fileName, dirname, opts) {
if (absUrl(fileName))
return fileName;
return path.relative(baseURLPath, path.join(dirname, fileName));
},
load: function(fileName, opts) {
if (absUrl(fileName))
return;
var file = inputFiles[fileName];
if (!file) {
console.log(fileName + ' not found! TODO: ensure these imports are normalized correctly.');
return;
}
var sourceMap = file.sourceMap;
if (sourceMap) {
if (typeof sourceMap === 'string')
sourceMap = JSON.parse(sourceMap);
// normalize input sources to use absolute file paths
sourceMap.sources = sourceMap.sources.map(source => {
if (source.match(absRegEx))
return source;
if (source[0] !== '/')
return path.resolve(path.dirname(path.resolve(baseURLPath, fileName)), source);
return source;
});
file.sourceMap = sourceMap;
}
return file;
}
}), atUrl({
url: function(fileName, decl, from, dirname, to, options, result) {
if ((absUrl(fileName) && (!loader.browserRootURL || fileName.charAt(0) !== '/')) || fileName.match(/^%23/))
return fileName;
// dirname may be renormalized to cwd
if (dirname.substr(0, cwd.length + 1) === cwd + path.sep)
dirname = path.resolve(baseURLPath, dirname.substr(cwd.length + 1));
if (loader.rootURL)
if (fileName.charAt(0) === '/') {
return (loader.browserRootURL || '/') + fileName.replace(/\\/g, '/').replace(/\//, '');
} else {
return (loader.browserRootURL || '/') + path.relative(loader.rootURL, path.join(dirname, fileName)).replace(/\\/g, '/');
}
else
return path.relative(baseURLPath, path.join(dirname, fileName)).replace(/\\/g, '/');
}
}), autoprefixer];
if (loader.cssNano !== false)
postCssPlugins.push(cssnano({
safe: true,
normalizeUrl: false
}));
return postcss(postCssPlugins)
.process(Object.keys(inputFiles).map(name => '@import "' + name.replace(/\\/g, '/') + '";').join('\n'), {
from: path.join(baseURLPath, '__.css'),
to: cwd + path.sep + '__.css',
map: {
inline: false,
sourcesContent: false
}
})
.then(function(result) {
var cssOutput = result.css;
// write a separate CSS file if necessary
if (loader.separateCSS) {
var outFile = path.resolve(outputOpts.outFile).replace(/\.js$/, '.css');
if (outputOpts.sourceMaps) {
fs.writeFileSync(outFile + '.map', result.map.toString());
cssOutput += '\n/*# sourceMappingURL=' + outFile.split(/[\\/]/).pop() + '.map*/';
}
fs.writeFileSync(outFile, cssOutput);
}
else {
// this can be disabled pending https://bugs.chromium.org/p/chromium/issues/detail?id=649679&can=2&q=css%20source%20maps
if (outputOpts.sourceMaps && loader.inlineCssSourceMaps) {
var sourceMap = JSON.parse(result.map.toString());
sourceMap.sources = sourceMap.sources.map(source => {
if (source.match(absRegEx))
return source;
if (source[0] !== '/')
source = path.resolve(baseURLPath, source);
if (loader.rootURL)
return (loader.browserRootURL || '/') + path.relative(loader.rootURL, source).replace(/\\/g, '/');
else
return path.relative(baseURLPath, source).replace(/\\/g, '/');
});
cssOutput += '\n/*# sourceMappingURL=data:application/json;base64,' + new Buffer(JSON.stringify(sourceMap)).toString('base64') + '*/';
return cssInjectSourceMaps + '\n("' + escape(cssOutput) + '");';
}
else {
return cssInject + '\n("' + escape(cssOutput) + '");';
}
}
});
};