-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathindex.js
172 lines (136 loc) · 3.8 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
import path from "path";
import schema from "./options.json";
import {
getStylusOptions,
createEvaluator,
urlResolver,
readFile,
normalizeSourceMap,
getStylusImplementation,
} from "./utils";
export default async function stylusLoader(source) {
const options = this.getOptions(schema);
const callback = this.async();
let implementation;
try {
implementation = getStylusImplementation(this, options.implementation);
} catch (error) {
callback(error);
return;
}
if (!implementation) {
callback(
new Error(
`The Stylus implementation "${options.implementation}" not found`,
),
);
return;
}
let data = source;
if (typeof options.additionalData !== "undefined") {
data =
typeof options.additionalData === "function"
? await options.additionalData(data, this)
: `${options.additionalData}\n${data}`;
}
let stylusOptions;
try {
stylusOptions = getStylusOptions(this, options);
} catch (err) {
callback(err);
return;
}
const styl = implementation(data, stylusOptions);
// include regular CSS on @import
if (stylusOptions.includeCSS) {
styl.set("include css", true);
}
if (stylusOptions.hoistAtrules) {
styl.set("hoist atrules", true);
}
if (stylusOptions.lineNumbers) {
styl.set("linenos", true);
}
if (stylusOptions.disableCache) {
styl.set("cache", false);
}
const useSourceMap =
typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
if (useSourceMap || stylusOptions.sourcemap) {
styl.set(
"sourcemap",
useSourceMap
? {
comment: false,
sourceRoot: stylusOptions.dest,
basePath: this.rootContext,
}
: stylusOptions.sourcemap,
);
}
if (typeof stylusOptions.import !== "undefined") {
for (const imported of stylusOptions.import) {
styl.import(imported);
}
}
if (typeof stylusOptions.include !== "undefined") {
for (const included of stylusOptions.include) {
styl.include(included);
}
}
if (stylusOptions.resolveURL !== false) {
styl.define("url", urlResolver(stylusOptions.resolveURL));
}
const shouldUseWebpackImporter =
typeof options.webpackImporter === "boolean"
? options.webpackImporter
: true;
if (shouldUseWebpackImporter) {
styl.set("Evaluator", await createEvaluator(this, source, stylusOptions));
}
if (typeof stylusOptions.define !== "undefined") {
const definitions = Array.isArray(stylusOptions.define)
? stylusOptions.define
: Object.entries(stylusOptions.define);
for (const defined of definitions) {
styl.define(...defined);
}
}
styl.render(async (error, css) => {
if (error) {
if (error.filename) {
this.addDependency(path.normalize(error.filename));
}
const obj = new Error(error.message, { cause: error });
obj.stack = null;
callback(obj);
return;
}
// eslint-disable-next-line no-underscore-dangle
if (stylusOptions._imports.length > 0) {
// eslint-disable-next-line no-underscore-dangle
for (const importData of stylusOptions._imports) {
if (path.isAbsolute(importData.path)) {
this.addDependency(path.normalize(importData.path));
} else {
this.addDependency(path.resolve(process.cwd(), importData.path));
}
}
}
let map = styl.sourcemap;
if (map && useSourceMap) {
map = normalizeSourceMap(map, stylusOptions.dest);
try {
map.sourcesContent = await Promise.all(
map.sources.map(async (file) =>
(await readFile(this.fs, file)).toString(),
),
);
} catch (fsError) {
callback(fsError);
return;
}
}
callback(null, css, map);
});
}