MCPcopy
hub / github.com/kuma-ui/kuma-ui / kumaUiLoader

Function kumaUiLoader

packages/webpack-plugin/src/loader.ts:25–98  ·  view source on GitHub ↗
(
  source: Buffer,
)

Source from the content-addressed store, hash-verified

23};
24
25const kumaUiLoader: RawLoaderDefinitionFunction<Options> = function (
26 source: Buffer,
27) {
28 // tell Webpack this loader is async
29 const callback = this.async();
30 const id = this.resourcePath;
31 const { plugin, outputDir, wasm } = this.getOptions();
32
33 if (plugin.config) {
34 // enable automatic rebuild for static theme props
35 // <Box color={"colors.red.100"} />
36 this.addDependency(plugin.config);
37 const userTheme = getUserTheme(plugin.config);
38 if (userTheme) {
39 theme.setUserTheme(userTheme);
40 }
41 }
42
43 if (
44 id.includes("/node_modules/") ||
45 id.includes("@kuma-ui/core") ||
46 !/\.(t|j)(s|sx)?$/.test(id)
47 ) {
48 callback(null, source);
49 return;
50 }
51
52 const outputPath = this._compiler?.options.output.path;
53 if (!outputPath) throw Error("output path is not correctly set");
54 const result = compileSync({ code: source.toString(), id, wasm });
55 if (!result || !result.code) {
56 callback(null, source);
57 return;
58 }
59
60 const css = result.css || "";
61
62 if (css) {
63 /**
64 * This is a temporary workaround to enable HMR (Hot Module Replacement) in Next.js client components.
65 * Currently, Client Component doesn't account for virtual files, so we need to emit an actual CSS file.
66 * TODO: Address and fix this issue.
67 */
68 if (outputDir && process.env.NODE_ENV !== "production") {
69 const codePrefix = fileLoader(css, {
70 context: this,
71 outputDir: outputDir,
72 });
73 callback(null, `${result.code}\n${codePrefix};`);
74 return;
75 }
76
77 const params = new URLSearchParams({ [CSS_PARAM_NAME]: css });
78
79 const importCSS = `import ${JSON.stringify(
80 `${this.utils.contextify(
81 this.context,
82 emptyCssExtractionFile,

Callers

nothing calls this directly

Calls 5

getUserThemeFunction · 0.90
compileSyncFunction · 0.90
callbackFunction · 0.85
fileLoaderFunction · 0.85
setUserThemeMethod · 0.80

Tested by

no test coverage detected