Skip to content

Commit

Permalink
Fix type errors from webpack.loadModule
Browse files Browse the repository at this point in the history
  • Loading branch information
Mad-Kat committed Jan 7, 2025
1 parent d1eda5e commit e300c1e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
32 changes: 27 additions & 5 deletions packages/next-yak/loaders/css-loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { LoaderContext } from "webpack";
import { resolveCrossFileConstant } from "./lib/resolveCrossFileSelectors.js";
import { relative } from "path";
import type { LoaderContext } from "webpack";
import type { YakConfigOptions } from "../withYak/index.js";
import { resolveCrossFileConstant } from "./lib/resolveCrossFileSelectors.js";

/**
* Transform typescript to css
Expand All @@ -22,6 +22,11 @@ export default async function cssExtractLoader(
if (err) {
return callback(err);
}
if (!source) {
return callback(
new Error(`Source code for ${this.resourcePath} is empty`),
);
}
const { experiments } = this.getOptions();
const debugLog = createDebugLogger(this, experiments?.debug);

Expand All @@ -36,8 +41,22 @@ export default async function cssExtractLoader(
});
}

function extractCss(code: string): string {
const codeParts = code.split("/*YAK Extracted CSS:\n");
function extractCss(code: string | Buffer<ArrayBufferLike>): string {
let codeString: string;

if (typeof code === "string") {
codeString = code;
} else if (code instanceof Buffer) {
codeString = code.toString("utf-8");
} else if (code instanceof ArrayBuffer) {
codeString = new TextDecoder("utf-8").decode(code);
} else {
throw new Error(
"Invalid input type: code must be string, Buffer, or ArrayBuffer",
);
}

const codeParts = codeString.split("/*YAK Extracted CSS:\n");
let result = "";
for (let i = 1; i < codeParts.length; i++) {
const codeUntilEnd = codeParts[i].split("*/")[0];
Expand All @@ -59,7 +78,10 @@ function createDebugLogger(
return () => {};
}
const debugType = debugOptions === true ? "ts" : debugOptions.type;
return (messageType: "ts" | "css" | "css resolved", message: string) => {
return (
messageType: "ts" | "css" | "css resolved",
message: string | Buffer<ArrayBufferLike> | undefined,
) => {
if (messageType === debugType || debugType === "all") {
console.log(
"🐮 Yak",
Expand Down
18 changes: 15 additions & 3 deletions packages/next-yak/loaders/lib/resolveCrossFileSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from "path";
import babel from "@babel/core";
import path from "path";
// @ts-expect-error - this is used by babel directly so we ignore that it is not typed
import babelPlugin from "@babel/plugin-syntax-typescript";
import type { Compilation, LoaderContext } from "webpack";
import { getCssModuleLocalIdent } from "next/dist/build/webpack/config/blocks/css/loaders/getCssModuleLocalIdent.js";
import type { Compilation, LoaderContext } from "webpack";

const yakCssImportRegex =
// Make mixin and selector non optional once we dropped support for the babel plugin
Expand Down Expand Up @@ -227,7 +227,19 @@ async function parseFile(
const tranformedSource = new Promise<string>((resolve, reject) => {
loader.loadModule(filePath, (err, source) => {
if (err) return reject(err);
resolve(source || "");
let sourceString: string;
if (typeof source === "string") {
sourceString = source;
} else if (source instanceof Buffer) {
sourceString = source.toString("utf-8");
} else if (source instanceof ArrayBuffer) {
sourceString = new TextDecoder("utf-8").decode(source);
} else {
throw new Error(
"Invalid input type: code must be string, Buffer, or ArrayBuffer",
);
}
resolve(sourceString || "");
});
});

Expand Down

0 comments on commit e300c1e

Please sign in to comment.