MCPcopy
hub / github.com/webpack/sass-loader / getCompileFn

Function getCompileFn

src/utils.js:650–705  ·  view source on GitHub ↗

* Verifies that the implementation and version of Sass is supported by this loader. * @template {SassImplementation} T * @param {LoaderContext} loaderContext loader context * @param {T} implementation sass implementation * @param {ApiType=} apiType api type * @returns {(sassOptions: SassOptions

(loaderContext, implementation, apiType = "auto")

Source from the content-addressed store, hash-verified

648 * @returns {(sassOptions: SassOptions & { data: string }) => Promise<CompileResult>} compile function
649 */
650function getCompileFn(loaderContext, implementation, apiType = "auto") {
651 const { initAsyncCompiler } = implementation;
652
653 if (
654 apiType === "modern-compiler" ||
655 (apiType === "auto" && typeof initAsyncCompiler === "function")
656 ) {
657 return async (
658 /** @type {SassOptions & { data: string }} */ sassOptions,
659 ) => {
660 const webpackCompiler =
661 /** @type {LoaderContext & { _compiler?: import("webpack").Compiler }} */
662 (loaderContext)._compiler;
663 const { data, ...rest } = sassOptions;
664
665 // Some people can run the loader in a multi-threading way;
666 // there is no webpack compiler object in such case.
667 if (webpackCompiler && initAsyncCompiler) {
668 if (!sassModernCompilers.has(webpackCompiler)) {
669 // Create a long-running compiler process that can be reused
670 // for compiling individual files.
671 const compiler = await initAsyncCompiler();
672
673 // Check again because awaiting the initialization function
674 // introduces a race condition.
675 if (!sassModernCompilers.has(webpackCompiler)) {
676 sassModernCompilers.set(webpackCompiler, compiler);
677 webpackCompiler.hooks.shutdown.tap("sass-loader", () => {
678 compiler.dispose();
679 });
680 } else {
681 compiler.dispose();
682 }
683 }
684
685 return /** @type {AsyncCompiler} */ (
686 sassModernCompilers.get(webpackCompiler)
687 ).compileStringAsync(/** @type {string} */ (data), rest);
688 }
689
690 return implementation.compileStringAsync(
691 /** @type {string} */ (data),
692 rest,
693 );
694 };
695 }
696
697 return (/** @type {SassOptions & { data: string }} */ sassOptions) => {
698 const { data, ...rest } = sassOptions;
699
700 return implementation.compileStringAsync(
701 /** @type {string} */ (data),
702 rest,
703 );
704 };
705}
706
707const ABSOLUTE_SCHEME = /^[A-Za-z0-9+\-.]+:/;

Callers 1

loaderFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…