UNPKG

stylus-loader

Version:
143 lines (138 loc) 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = stylusLoader; var _nodePath = _interopRequireDefault(require("node:path")); var _options = _interopRequireDefault(require("./options.json")); var _utils = require("./utils.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** @typedef {import("webpack").LoaderContext<LoaderOptions>} LoaderContext */ /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("./utils.js").LoaderOptions} LoaderOptions */ /** @typedef {import("./utils.js").StylusError} StylusError */ /** @typedef {import("./utils.js").StylusOptions} StylusOptions */ /** @typedef {import("./utils.js").EXPECTED_ANY} EXPECTED_ANY */ /** * The stylus-loader makes `Stylus` available to webpack modules. * @this {LoaderContext} * @param {string} source source * @returns {Promise<void>} loader result */ async function stylusLoader(source) { const options = this.getOptions(/** @type {Schema} */_options.default); const callback = this.async(); let implementation; try { implementation = await (0, _utils.getStylusImplementation)(this, options.implementation); } catch (error) { callback(/** @type {Error} */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 = await (0, _utils.getStylusOptions)(this, options); } catch (error) { callback(/** @type {Error} */error); 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", (0, _utils.urlResolver)(stylusOptions.resolveURL)); } const shouldUseWebpackImporter = typeof options.webpackImporter === "boolean" ? options.webpackImporter : true; if (shouldUseWebpackImporter) { styl.set("Evaluator", await (0, _utils.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(...(/** @type {[string, EXPECTED_ANY]} */defined)); } } styl.render( /** * @param {StylusError | null} error error * @param {string} css css * @returns {Promise<void>} render result */ async (error, css) => { if (error) { if (error.filename) { this.addDependency(_nodePath.default.normalize(error.filename)); } const obj = new Error(error.message, { cause: error }); obj.stack = /** @type {EXPECTED_ANY} */null; callback(obj); return; } if (stylusOptions._imports && stylusOptions._imports.length > 0) { for (const importData of stylusOptions._imports) { if (_nodePath.default.isAbsolute(importData.path)) { this.addDependency(_nodePath.default.normalize(_nodePath.default.sep === "\\" ? importData.path.replace(/^\/\/\?\//, "") : importData.path)); } else { this.addDependency(_nodePath.default.resolve(process.cwd(), importData.path)); } } } // @ts-expect-error no types are shipped for this let map = styl.sourcemap; if (map && useSourceMap) { map = (0, _utils.normalizeSourceMap)(map, /** @type {string} */stylusOptions.dest); try { map.sourcesContent = await Promise.all(map.sources.map( /** * @param {string} file file * @returns {Promise<string>} file contents */ async file => (await (0, _utils.readFile)(this.fs, file)).toString())); } catch (err) { callback(/** @type {Error} */err); return; } } callback(null, css, map); }); }module.exports = exports.default; module.exports.default = exports.default;