/
index.js
116 lines (91 loc) · 2.96 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
import { promises as fs } from 'fs';
import stylus from 'stylus';
import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';
import schema from './options.json';
import createEvaluator from './evaluator';
import { getStylusOptions } from './utils';
import resolver from './lib/resolver';
export default async function stylusLoader(source) {
const options = getOptions(this);
validateOptions(schema, options, {
name: 'Stylus Loader',
baseDataPath: 'options',
});
const callback = this.async();
const stylusOptions = getStylusOptions(this, options);
const useSourceMap =
typeof options.sourceMap === 'boolean' ? options.sourceMap : this.sourceMap;
if (useSourceMap) {
stylusOptions.sourcemap = {
content: true,
comment: false,
sourceRoot: this.rootContext,
};
}
const styl = stylus(source, stylusOptions);
if (typeof stylusOptions.include !== 'undefined') {
for (const included of stylusOptions.include) {
styl.include(included);
}
}
if (typeof stylusOptions.import !== 'undefined') {
for (const imported of stylusOptions.import) {
styl.import(imported);
}
}
if (typeof stylusOptions.resolveUrl !== 'undefined') {
stylusOptions.resolveUrl = {};
styl.define('url', resolver(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);
}
}
// include regular CSS on @import
if (stylusOptions.includeCSS) {
styl.set('include css', true);
}
const shouldUseWebpackImporter =
typeof options.webpackImporter === 'boolean'
? options.webpackImporter
: true;
if (shouldUseWebpackImporter) {
styl.set('Evaluator', await createEvaluator(source, stylusOptions, this));
}
// keep track of imported files (used by Stylus CLI watch mode)
// eslint-disable-next-line no-underscore-dangle
stylusOptions._imports = [];
// let stylus do its magic
styl.render(async (error, css) => {
if (error) {
this.addDependency(error.filename);
return callback(error);
}
// eslint-disable-next-line no-underscore-dangle
if (stylusOptions._imports.length) {
// eslint-disable-next-line no-underscore-dangle
for (const importData of stylusOptions._imports) {
this.addDependency(importData.path);
}
}
if (styl.sourcemap) {
delete styl.sourcemap.file;
// load source file contents into source map
if (stylusOptions.sourcemap && stylusOptions.sourcemap.content) {
try {
styl.sourcemap.sourcesContent = await Promise.all(
styl.sourcemap.sources.map((file) => fs.readFile(file, 'utf-8'))
);
} catch (e) {
return callback(e);
}
}
}
return callback(null, css, styl.sourcemap);
});
}