forked from webpack-contrib/stylus-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (198 loc) · 6.73 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
var loaderUtils = require('loader-utils');
var stylus = require('stylus');
var path = require('path');
var fs = require('fs');
var when = require('when');
var whenNodefn = require('when/node/function');
var cloneDeep = require('lodash.clonedeep');
var CachedPathEvaluator = require('./lib/evaluator');
var PathCache = require('./lib/pathcache');
var resolver = require('./lib/resolver');
var globalImportsCaches = {};
module.exports = function(source) {
var self = this;
this.cacheable && this.cacheable();
var done = this.async();
var options = cloneDeep(loaderUtils.getOptions(this) || {});
options.dest = options.dest || '';
options.filename = options.filename || this.resourcePath;
options.Evaluator = CachedPathEvaluator;
var configKey, stylusOptions;
if (this.stylus) {
configKey = options.config || 'default';
stylusOptions = this.stylus[configKey] || {};
} else if (this.options) {
configKey = options.config || 'stylus';
stylusOptions = this.options[configKey] || {};
} else {
stylusOptions = {};
}
// Instead of assigning to options, we run them manually later so their side effects apply earlier for
// resolving paths.
var use = options.use || stylusOptions.use || [];
options.import = options.import || stylusOptions.import || [];
options.include = options.include || stylusOptions.include || [];
options.set = options.set || stylusOptions.set || {};
options.define = options.define || stylusOptions.define || {};
options.paths = options.paths || stylusOptions.paths;
if (options.sourceMap != null) {
options.sourcemap = options.sourceMap;
delete options.sourceMap;
}
else if (this.sourceMap) {
options.sourcemap = { comment: false };
}
var styl = stylus(source, options);
var paths = [path.dirname(options.filename)];
function needsArray(value) {
return (Array.isArray(value)) ? value : [value];
}
if (options.paths && !Array.isArray(options.paths)) {
paths = paths.concat(options.paths);
options.paths = [options.paths];
}
var manualImports = [];
Object.keys(options).forEach(function(key) {
var value = options[key];
if (key === 'use') {
needsArray(value).forEach(function(plugin) {
if (typeof plugin === 'function') {
styl.use(plugin);
} else {
throw new Error('Plugin should be a function');
}
});
} else if (key === 'set') {
for (var name in value) {
styl.set(name, value[name]);
}
} else if (key === 'define') {
for (var defineName in value) {
styl.define(defineName, value[defineName]);
}
} else if (key === 'include') {
needsArray(value).forEach(styl.include.bind(styl));
} else if (key === 'import') {
needsArray(value).forEach(function(stylusModule) {
manualImports.push(stylusModule);
});
} else {
styl.set(key, value);
if (key === 'resolve url' && value) {
styl.define('url', resolver());
}
}
});
var shouldCacheImports = stylusOptions.importsCache !== false;
var importsCache;
if (stylusOptions.importsCache !== false) {
if (typeof stylusOptions.importsCache === 'object') {
importsCache = stylusOptions.importsCache;
} else {
if(!globalImportsCaches[configKey]) globalImportsCaches[configKey] = {};
importsCache = globalImportsCaches[configKey];
}
}
// Use input file system's readFile if available. The normal webpack input
// file system is cached with entries purged when they are detected to be
// changed on disk by the watcher.
var readFile;
try {
var inputFileSystem = this._compiler.inputFileSystem;
readFile = inputFileSystem.readFile.bind(inputFileSystem);
} catch (error) {
readFile = fs.readFile;
}
var boundResolvers = PathCache.resolvers(options, this.resolve);
var pathCacheHelpers = {
resolvers: boundResolvers,
readFile: readFile,
};
// Use plugins here so that resolve related side effects can be used while we resolve imports.
(Array.isArray(use) ? use : [use]).forEach(styl.use, styl);
when
// Resolve manual imports like @import files.
.reduce(manualImports, function resolveManualImports(carry, filename) {
return PathCache.resolvers
.reduce(boundResolvers, path.dirname(options.filename), filename)
.then(function(paths) { return carry.concat(paths); });
}, [])
// Resolve dependencies of
.then(function(paths) {
paths.forEach(styl.import.bind(styl));
paths.forEach(self.addDependency);
var readFile = whenNodefn.lift(pathCacheHelpers.readFile);
return when.reduce(paths, function(cache, filepath) {
return readFile(filepath)
.then(function(source) {
return PathCache.createFromFile(
pathCacheHelpers, cache, source.toString(), filepath
);
});
}, {
contexts: {},
sources: {},
imports: importsCache,
});
})
.then(function(cache) {
return PathCache
.createFromFile(pathCacheHelpers, cache, source, options.filename);
})
.then(function(importPathCache) {
// CachedPathEvaluator will use this PathCache to find its dependencies.
options.cache = importPathCache;
importPathCache.allDeps().forEach(function(f) {
self.addDependency(path.normalize(f));
});
// var paths = importPathCache.origins;
styl.render(function(err, css) {
if (err) {
done(err);
} else {
if (styl.sourcemap) {
styl.sourcemap.sourcesContent = styl.sourcemap.sources.map(function (file) {
return importPathCache.sources[path.resolve(file)]
});
}
done(null, css, styl.sourcemap);
}
});
})
.catch(done);
};
var LoaderOptionsPlugin = require('webpack').LoaderOptionsPlugin;
// Webpack 2 plugin for setting options that'll be available to stylus-loader.
function OptionsPlugin(options) {
if (!LoaderOptionsPlugin) {
throw new Error(
'webpack.LoaderOptionPlugin is not available. A newer version of webpack is needed.'
);
}
var stylusOptions = {};
var test = options.test || /\.styl$/;
var include = options.include;
var exclude = options.exclude;
var loaderOptions = {
stylus: stylusOptions,
};
for (var key in options) {
if (['test', 'include', 'exclude'].indexOf(key) === -1) {
stylusOptions[key] = options[key];
}
}
if (test) {
loaderOptions.test = test;
}
if (include) {
loaderOptions.include = include;
}
if (exclude) {
loaderOptions.exclude = exclude;
}
this.plugin = new LoaderOptionsPlugin(loaderOptions);
};
module.exports.OptionsPlugin = OptionsPlugin;
OptionsPlugin.prototype.apply = function(compiler) {
this.plugin.apply(compiler);
};