UNPKG

rollup-plugin-sass

Version:
68 lines 3.21 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.processRenderResponse = exports.INSERT_STYLE_ID = void 0; const path_1 = __importDefault(require("path")); const pluginutils_1 = require("@rollup/pluginutils"); const helpers_1 = require("./helpers"); exports.INSERT_STYLE_ID = '___$insertStyle'; const processRenderResponse = (rollupOptions, fileId, state, inCss) => { if (!inCss) return Promise.resolve(); const { processor } = rollupOptions; return (Promise.resolve() .then(() => !(0, helpers_1.isFunction)(processor) ? inCss + '' : processor(inCss, fileId)) .then((result) => { if (!(0, helpers_1.isObject)(result)) { return [result, {}]; } if (!(0, helpers_1.isString)(result.css)) { throw new Error('You need to return the styles using the `css` property. ' + 'See https://github.com/elycruz/rollup-plugin-sass#processor'); } if (result.cssModules && !(0, helpers_1.isObject)(result.cssModules)) { throw new Error('You need to provide a js object as `cssModules` property. ' + 'See https://github.com/elycruz/rollup-plugin-sass#processor'); } const { css, cssModules } = result, namedExports = __rest(result, ["css", "cssModules"]); return [css, namedExports, cssModules]; }) .then(([resolvedCss, namedExports, cssModules]) => { const { styleMaps } = state; styleMaps[fileId].content = resolvedCss; let defaultExport = `""`; let cssCode = JSON.stringify(resolvedCss); const imports = []; if (rollupOptions.insert) { imports.push(`import ${exports.INSERT_STYLE_ID} from '${exports.INSERT_STYLE_ID}';`); cssCode = `${exports.INSERT_STYLE_ID}(${cssCode})`; defaultExport = cssCode; } else if (!rollupOptions.output) { defaultExport = cssCode; } const variableName = (0, pluginutils_1.makeLegalIdentifier)(path_1.default.basename(fileId, path_1.default.extname(fileId))); const codeOutput = [ ...imports, `const ${variableName} = ${defaultExport}`, `export default ${cssModules ? JSON.stringify(cssModules) : variableName}`, ...Object.entries(namedExports).map(([n, v]) => `export const ${n} = ${JSON.stringify(v)}`), ]; return codeOutput.join(';\n'); })); }; exports.processRenderResponse = processRenderResponse; //# sourceMappingURL=processRenderResponse.js.map