-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex.js
312 lines (253 loc) · 8.67 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Some css-modules-loader-code dependencies use Promise so we'll provide it for older node versions
if (!global.Promise) { global.Promise = require('promise-polyfill'); }
var fs = require('fs');
var path = require('path');
var Cmify = require('./cmify');
var Core = require('css-modules-loader-core');
var FileSystemLoader = require('./file-system-loader');
var assign = require('object-assign');
var stringHash = require('string-hash');
var ReadableStream = require('stream').Readable;
var through = require('through2');
/*
Custom `generateScopedName` function for `postcss-modules-scope`.
Short names consisting of source hash and line number.
*/
function generateShortName (name, filename, css) {
// first occurrence of the name
// TODO: better match with regex
var i = css.indexOf('.' + name);
var numLines = css.substr(0, i).split(/[\r\n]/).length;
var hash = stringHash(css).toString(36).substr(0, 5);
return '_' + name + '_' + hash + '_' + numLines;
}
/*
Custom `generateScopedName` function for `postcss-modules-scope`.
Appends a hash of the css source.
*/
function generateLongName (name, filename) {
var sanitisedPath = filename.replace(/\.[^\.\/\\]+$/, '')
.replace(/[\W_]+/g, '_')
.replace(/^_|_$/g, '');
return '_' + sanitisedPath + '__' + name;
}
/*
Get the default plugins and apply options.
*/
function getDefaultPlugins (options) {
var scope = Core.scope;
var customNameFunc = options.generateScopedName;
var defaultNameFunc = process.env.NODE_ENV === 'production' ?
generateShortName :
generateLongName;
scope.generateScopedName = customNameFunc || defaultNameFunc;
return [
Core.values
, Core.localByDefault
, Core.extractImports
, scope
];
}
/*
Normalize the manifest paths so that they are always relative
to the project root directory.
*/
function normalizeManifestPaths (tokensByFile, rootDir) {
var output = {};
var rootDirLength = rootDir.length + 1;
Object.keys(tokensByFile).forEach(function (filename) {
var normalizedFilename = filename.substr(rootDirLength);
output[normalizedFilename] = tokensByFile[filename];
});
return output;
}
// caches
//
// persist these for as long as the process is running. #32
// keep track of all tokens so we can avoid duplicates
var tokensByFile = {};
// we need a separate loader for each entry point
var loadersByFile = {};
module.exports = function (browserify, options) {
options = options || {};
// if no root directory is specified, assume the cwd
var rootDir = options.rootDir || options.d || browserify._options.basedir;
if (rootDir) { rootDir = path.resolve(rootDir); }
if (!rootDir) { rootDir = process.cwd(); }
var transformOpts = {};
if (options.global) {
transformOpts.global = true;
}
var cssOutFilename = options.output || options.o;
var jsonOutFilename = options.json || options.jsonOutput;
transformOpts.cssOutFilename = cssOutFilename;
// PostCSS plugins passed to FileSystemLoader
var plugins = options.use || options.u;
if (!plugins) {
plugins = getDefaultPlugins(options);
}
else {
if (typeof plugins === 'string') {
plugins = [plugins];
}
}
var postcssBefore = options.postcssBefore || options.before || [];
var postcssAfter = options.postcssAfter || options.after || [];
plugins = (Array.isArray(postcssBefore) ? postcssBefore : [postcssBefore]).concat(plugins).concat(postcssAfter);
// load plugins by name (if a string is used)
plugins = plugins.map(function requirePlugin (name) {
// assume not strings are already required plugins
if (typeof name !== 'string') {
return name;
}
var plugin = module.parent.require(name);
// custom scoped name generation
if (name === 'postcss-modules-scope') {
options[name] = options[name] || {};
if (!options[name].generateScopedName) {
options[name].generateScopedName = generateLongName;
}
}
if (name in options) {
plugin = plugin(options[name]);
}
else {
plugin = plugin.postcss || plugin();
}
return plugin;
});
// create a loader for this entry file
var loader = loadersByFile[cssOutFilename];
if (!loader) {
loader = loadersByFile[cssOutFilename] = new FileSystemLoader(rootDir, plugins);
}
function createCssModuleSource (filename, cb) {
// reset the `tokensByFile` cache
var relFilename = path.relative(rootDir, filename);
tokensByFile[filename] = loader.tokensByFile[filename] = null;
loader.fetch(relFilename, '/').then(function (tokens) {
var deps = loader.deps.dependenciesOf(filename);
var output = deps.map(function (f) {
return 'require("' + f + '")';
});
output.push('module.exports = ' + JSON.stringify(tokens));
var isValid = true;
var isUndefined = /\bundefined\b/;
Object.keys(tokens).forEach(function (k) {
if (isUndefined.test(tokens[k])) {
isValid = false;
}
});
if (!isValid) {
var err = 'Composition in ' + filename + ' contains an undefined reference';
console.error(err);
output.push('console.error("' + err + '");');
}
assign(tokensByFile, loader.tokensByFile);
return cb(null, output.join('\n'));
}).catch(function (err) {
return cb(err);
});
}
// TODO: clean this up so there's less scope crossing
Cmify.prototype._flush = function (callback) {
var self = this;
var filename = this._filename;
// only handle .css files
if (!this.isCssFile(filename)) { return callback(); }
createCssModuleSource(filename, function (err, source) {
if (err) {
self.push('console.error("' + err + '");');
self.emit('error', err);
return callback();
}
self.push(source);
return callback();
});
};
browserify.transform(Cmify, transformOpts);
// ----
function invalidateFile (filename) {
Object.keys(loadersByFile).forEach(function (loaderKey) {
var loader = loadersByFile[loaderKey];
// clear the token cache for the changed file
tokensByFile[filename] = loader.tokensByFile[filename] = null;
// also clear the cache for any modules depending on this one
try {
var deps = loader.deps.dependantsOf(filename);
deps.forEach(function (dep) {
tokensByFile[dep] = loader.tokensByFile[dep] = null;
});
}
catch (err) {}
});
}
function addHooks () {
browserify.pipeline.get('deps').push(through.obj(function write (row, enc, next) {
// css modules need to be regenerated at this point
// (so that imported @value updates are carried through hmr)
var self = this;
if (!/\.css$/.test(row.id)) {
return next(null, row)
}
invalidateFile(row.id)
return createCssModuleSource(row.id, function (err, source) {
if (err) {
row.source = 'console.error("' + err + '");';
self.emit('error', err);
return next(null, row)
}
row.source = source
next(null, row)
})
}, function end (done) {
done();
}));
browserify.pipeline.get('pack').push(through(function write (row, enc, next) {
next(null, row)
}, function end (cb) {
// on each bundle, create a new stream b/c the old one might have ended
var compiledCssStream = new ReadableStream();
compiledCssStream._read = function () {};
browserify.emit('css stream', compiledCssStream);
// Combine the collected sources for a single bundle into a single CSS file
var self = this;
var loader = loadersByFile[cssOutFilename];
var css = loader.finalSource;
// end the output stream
compiledCssStream.push(css);
compiledCssStream.push(null);
const writes = [];
// write the css file
if (cssOutFilename) {
writes.push(writeFile(cssOutFilename, css));
}
// write the classname manifest
if (jsonOutFilename) {
writes.push(writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir))));
}
Promise.all(writes)
.then(function () { cb(); })
.catch(function (err) { self.emit('error', err); cb() })
}));
}
browserify.on('reset', addHooks);
addHooks();
browserify.on('update', function (files) {
// invalidate cache of any changed css modules
files.forEach(invalidateFile);
})
return browserify;
};
function writeFile(filename, content) {
return new Promise(function (resolve, reject) {
fs.writeFile(filename, content, function (err) {
if (err)
reject(err);
else
resolve();
});
});
}
module.exports.generateShortName = generateShortName;
module.exports.generateLongName = generateLongName;