forked from css-modules/css-modulesify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
233 lines (184 loc) · 6.27 KB
/
Copy pathindex.js
File metadata and controls
233 lines (184 loc) · 6.27 KB
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
var fs = require('fs');
var path = require('path');
var through = require('through');
var Core = require('css-modules-loader-core');
var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader');
var assign = require('object-assign');
var stringHash = require('string-hash');
var ReadableStream = require('stream').Readable;
/*
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
// TOOD: 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.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;
}
var cssExt = /\.css$/;
// caches
//
// persist these for as long as the process is running. #32
// keep track of css files visited
var filenames = [];
// keep track of all tokens so we can avoid duplicates
var tokensByFile = {};
// keep track of all source files for later builds: when
// using watchify, not all files will be caught on subsequent
// bundles
var sourceByFile = {};
module.exports = function (browserify, options) {
options = options || {};
// if no root directory is specified, assume the cwd
var rootDir = options.rootDir || options.d;
if (rootDir) { rootDir = path.resolve(rootDir); }
if (!rootDir) { rootDir = process.cwd(); }
var cssOutFilename = options.output || options.o;
var jsonOutFilename = options.json || options.jsonOutput;
var sourceKey = cssOutFilename;
// keying our source caches by the name of our output file means we can
// isolate css compilation of seperate bundles that are running in parallel
sourceByFile[sourceKey] = sourceByFile[sourceKey] || {};
// PostCSS plugins passed to FileSystemLoader
var plugins = options.use || options.u;
if (!plugins) {
plugins = getDefaultPlugins(options);
}
else {
if (typeof plugins === 'string') {
plugins = [plugins];
}
}
var postcssAfter = options.postcssAfter || options.after || [];
plugins = plugins.concat(postcssAfter);
// load plugins by name (if a string is used)
plugins = plugins.map(function requirePlugin (name) {
// assume functions are already required plugins
if (typeof name === 'function') {
return name;
}
var plugin = require(require.resolve(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;
});
// the compiled CSS stream needs to be avalible to the transform,
// but re-created on each bundle call.
var compiledCssStream;
function transform (filename) {
// only handle .css files
if (!cssExt.test(filename)) {
return through();
}
// collect visited filenames
filenames.push(filename);
var loader = new FileSystemLoader(rootDir, plugins);
return through(function noop () {}, function end () {
var self = this;
loader.fetch(path.relative(rootDir, filename), '/').then(function (tokens) {
var output = 'module.exports = ' + JSON.stringify(tokens);
assign(tokensByFile, loader.tokensByFile);
// store this file's source to be written out to disk later
sourceByFile[sourceKey][filename] = loader.finalSource;
compiledCssStream.push(loader.finalSource);
self.queue(output);
self.queue(null);
}, function (err) {
self.emit('error', err);
});
});
}
browserify.transform(transform, {
global: true
});
browserify.on('bundle', function (bundle) {
// on each bundle, create a new stream b/c the old one might have ended
compiledCssStream = new ReadableStream();
compiledCssStream._read = function () {};
bundle.emit('css stream', compiledCssStream);
bundle.on('end', function () {
// Combine the collected sources for a single bundle into a single CSS file
var files = Object.keys(sourceByFile[sourceKey]);
var css;
// end the output stream
compiledCssStream.push(null);
// write the css file
if (cssOutFilename) {
css = files.map(function (file) {
return sourceByFile[sourceKey][file];
}).join('\n');
fs.writeFile(cssOutFilename, css, function (err) {
if (err) {
browserify.emit('error', err);
}
});
}
// write the classname manifest
if (jsonOutFilename) {
fs.writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)), function (err) {
if (err) {
browserify.emit('error', err);
}
});
}
// reset the `tokensByFile` cache
tokensByFile = {};
});
});
return browserify;
};
module.exports.generateShortName = generateShortName;
module.exports.generateLongName = generateLongName;