UNPKG

svelte-preprocess

Version:

A Svelte preprocessor wrapper with baked-in support for commonly used preprocessors

62 lines (61 loc) 2.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.transformer = void 0; const postcss_1 = __importDefault(require("postcss")); async function process({ options: { plugins = [], parser, syntax } = {}, content, filename, sourceMap, }) { const { css, map, messages } = await (0, postcss_1.default)(plugins).process(content, { from: filename, to: filename, map: { prev: sourceMap, inline: false }, parser, syntax, }); const dependencies = messages.reduce((acc, msg) => { // istanbul ignore if if (msg.type !== 'dependency') return acc; acc.push(msg.file); return acc; }, []); return { code: css, map, dependencies }; } async function getConfigFromFile(options) { try { /** If not, look for a postcss config file */ const { default: postcssLoadConfig } = await import(`postcss-load-config`); const loadedConfig = await postcssLoadConfig(options, options === null || options === void 0 ? void 0 : options.configFilePath); return { error: null, config: { plugins: loadedConfig.plugins, // `postcss-load-config` puts all other props in a `options` object ...loadedConfig.options, }, }; } catch (e) { return { config: null, error: e, }; } } /** Adapted from https://github.com/TehShrike/svelte-preprocess-postcss */ const transformer = async ({ content, filename, options = {}, map, }) => { let fileConfig = null; if (!options.plugins) { fileConfig = await getConfigFromFile(options); options = { ...options, ...fileConfig.config }; } if (options.plugins || options.syntax || options.parser) { return process({ options, content, filename, sourceMap: map }); } if ((fileConfig === null || fileConfig === void 0 ? void 0 : fileConfig.error) != null) { console.error(`[svelte-preprocess] PostCSS configuration was not passed or is invalid. If you expect to load it from a file make sure to install "postcss-load-config" and try again.\n\n${fileConfig.error}`); } return { code: content, map, dependencies: [] }; }; exports.transformer = transformer;