Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 117 118 | 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import path from "path";
import { makeLegalIdentifier } from "@rollup/pluginutils";
import {
RollupPluginSassOptions,
RollupPluginSassState,
RollupPluginSassProcessorFnOutput,
} from "../types";
import { isFunction, isObject, isString } from "./helpers";
import { computeContentHash } from "../../crypto";
export const INSERT_STYLE_ID = "___$insertStyle";
export const processRenderResponse = (
rollupOptions: Pick<
RollupPluginSassOptions,
"insert" | "processor" | "output" | "shouldExtract" | "getExtractNameAndUrl"
>,
fileId: string,
state: RollupPluginSassState,
inCss: string,
) => {
Iif (!inCss) return Promise.resolve();
const {
processor,
shouldExtract = () => false,
getExtractNameAndUrl = (url) => ({ name: url, url }),
} = rollupOptions;
return (
Promise.resolve()
.then(() =>
!isFunction(processor) ? inCss + "" : processor(inCss, fileId),
)
// Gather output requirements
.then(
(
result: Partial<RollupPluginSassProcessorFnOutput>,
): [string, Record<string, unknown>, Record<string, string>?] => {
Eif (!isObject(result)) {
return [result, {}];
}
if (!isString(result.css)) {
/** @todo consider using rollup utils to throw this error */
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 && !isObject(result.cssModules)) {
/** @todo consider using rollup utils to throw this error */
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, ...namedExports } = result;
return [css, namedExports, cssModules];
},
)
// Compose output
.then(async ([resolvedCss, namedExports, cssModules]) => {
const { styleMaps } = state;
// Update bundle tracking entry with resolved content
styleMaps[fileId].content = resolvedCss;
let defaultExport = `""`;
let cssCode = JSON.stringify(resolvedCss);
const imports: string[] = [];
if (rollupOptions.insert) {
/**
* Include import using {@link INSERT_STYLE_ID} as source.
* It will be resolved to insert style function using `resolvedID` and `load` hooks;
* e.g., the path will completely replaced, and re-generated (as a relative path)
* by rollup.
*/
imports.push(`import ${INSERT_STYLE_ID} from '${INSERT_STYLE_ID}';`);
Iif (shouldExtract(fileId, resolvedCss)) {
const contentHash = await computeContentHash(resolvedCss);
const { url } = getExtractNameAndUrl(fileId, contentHash);
cssCode = `${INSERT_STYLE_ID}("${url}","link")`;
} else {
cssCode = `${INSERT_STYLE_ID}(${cssCode},"style")`;
}
defaultExport = cssCode;
} else Eif (!rollupOptions.output) {
defaultExport = cssCode;
}
const variableName = makeLegalIdentifier(
path.basename(fileId, path.extname(fileId)),
);
const codeOutput: string[] = [
...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");
})
); // @note do not `catch` here - let error propagate to rollup level
};
|