1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = stylusLoader;
|
---|
7 |
|
---|
8 | var _path = _interopRequireDefault(require("path"));
|
---|
9 |
|
---|
10 | var _options = _interopRequireDefault(require("./options.json"));
|
---|
11 |
|
---|
12 | var _utils = require("./utils");
|
---|
13 |
|
---|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
15 |
|
---|
16 | async function stylusLoader(source) {
|
---|
17 | const options = this.getOptions(_options.default);
|
---|
18 | const callback = this.async();
|
---|
19 | const implementation = (0, _utils.getStylusImplementation)(this, options.implementation);
|
---|
20 |
|
---|
21 | if (!implementation) {
|
---|
22 | callback();
|
---|
23 | return;
|
---|
24 | }
|
---|
25 |
|
---|
26 | let data = source;
|
---|
27 |
|
---|
28 | if (typeof options.additionalData !== "undefined") {
|
---|
29 | data = typeof options.additionalData === "function" ? await options.additionalData(data, this) : `${options.additionalData}\n${data}`;
|
---|
30 | }
|
---|
31 |
|
---|
32 | const stylusOptions = (0, _utils.getStylusOptions)(this, options);
|
---|
33 | const styl = implementation(data, stylusOptions); // include regular CSS on @import
|
---|
34 |
|
---|
35 | if (stylusOptions.includeCSS) {
|
---|
36 | styl.set("include css", true);
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (stylusOptions.hoistAtrules) {
|
---|
40 | styl.set("hoist atrules", true);
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (stylusOptions.lineNumbers) {
|
---|
44 | styl.set("linenos", true);
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (stylusOptions.disableCache) {
|
---|
48 | styl.set("cache", false);
|
---|
49 | }
|
---|
50 |
|
---|
51 | const useSourceMap = typeof options.sourceMap === "boolean" ? options.sourceMap : this.sourceMap;
|
---|
52 |
|
---|
53 | if (stylusOptions.sourcemap || useSourceMap) {
|
---|
54 | styl.set("sourcemap", useSourceMap ? {
|
---|
55 | comment: false,
|
---|
56 | sourceRoot: stylusOptions.dest,
|
---|
57 | basePath: this.rootContext
|
---|
58 | } : stylusOptions.sourcemap);
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (typeof stylusOptions.use !== "undefined" && stylusOptions.use.length > 0) {
|
---|
62 | let {
|
---|
63 | length
|
---|
64 | } = stylusOptions.use; // eslint-disable-next-line no-plusplus
|
---|
65 |
|
---|
66 | while (length--) {
|
---|
67 | let [item] = stylusOptions.use.splice(length, 1);
|
---|
68 |
|
---|
69 | if (typeof item === "string") {
|
---|
70 | try {
|
---|
71 | const resolved = require.resolve(item); // eslint-disable-next-line import/no-dynamic-require, global-require
|
---|
72 |
|
---|
73 |
|
---|
74 | item = require(resolved)(stylusOptions);
|
---|
75 | } catch (error) {
|
---|
76 | callback(`Failed to load "${item}" Stylus plugin. Are you sure it's installed?\n${error}`);
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | styl.use(item);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (typeof stylusOptions.import !== "undefined") {
|
---|
86 | for (const imported of stylusOptions.import) {
|
---|
87 | styl.import(imported);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | if (typeof stylusOptions.include !== "undefined") {
|
---|
92 | for (const included of stylusOptions.include) {
|
---|
93 | styl.include(included);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (stylusOptions.resolveURL !== false) {
|
---|
98 | styl.define("url", (0, _utils.urlResolver)(stylusOptions.resolveURL));
|
---|
99 | }
|
---|
100 |
|
---|
101 | const shouldUseWebpackImporter = typeof options.webpackImporter === "boolean" ? options.webpackImporter : true;
|
---|
102 |
|
---|
103 | if (shouldUseWebpackImporter) {
|
---|
104 | styl.set("Evaluator", await (0, _utils.createEvaluator)(this, source, stylusOptions));
|
---|
105 | }
|
---|
106 |
|
---|
107 | if (typeof stylusOptions.define !== "undefined") {
|
---|
108 | const definitions = Array.isArray(stylusOptions.define) ? stylusOptions.define : Object.entries(stylusOptions.define);
|
---|
109 |
|
---|
110 | for (const defined of definitions) {
|
---|
111 | styl.define(...defined);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | styl.render(async (error, css) => {
|
---|
116 | if (error) {
|
---|
117 | if (error.filename) {
|
---|
118 | this.addDependency(_path.default.normalize(error.filename));
|
---|
119 | }
|
---|
120 |
|
---|
121 | callback(error);
|
---|
122 | return;
|
---|
123 | } // eslint-disable-next-line no-underscore-dangle
|
---|
124 |
|
---|
125 |
|
---|
126 | if (stylusOptions._imports.length > 0) {
|
---|
127 | // eslint-disable-next-line no-underscore-dangle
|
---|
128 | for (const importData of stylusOptions._imports) {
|
---|
129 | if (_path.default.isAbsolute(importData.path)) {
|
---|
130 | this.addDependency(_path.default.normalize(importData.path));
|
---|
131 | } else {
|
---|
132 | this.addDependency(_path.default.resolve(process.cwd(), importData.path));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | let map = styl.sourcemap;
|
---|
138 |
|
---|
139 | if (map && useSourceMap) {
|
---|
140 | map = (0, _utils.normalizeSourceMap)(map, stylusOptions.dest);
|
---|
141 |
|
---|
142 | try {
|
---|
143 | map.sourcesContent = await Promise.all(map.sources.map(async file => (await (0, _utils.readFile)(this.fs, file)).toString()));
|
---|
144 | } catch (fsError) {
|
---|
145 | callback(fsError);
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | callback(null, css, map);
|
---|
151 | });
|
---|
152 | } |
---|