next-yak
Version:
next-yak is a CSS-in-JS solution tailored for Next.js that seamlessly combines the expressive power of styled-components syntax with efficient build-time extraction of CSS using Next.js's built-in CSS configuration
69 lines • 2.31 kB
JavaScript
// withYak/index.ts
import { existsSync } from "node:fs";
import path, { dirname } from "node:path";
import { fileURLToPath } from "node:url";
var currentDir = typeof __dirname !== "undefined" ? __dirname : dirname(fileURLToPath(import.meta.url));
var addYak = (yakOptions, nextConfig) => {
const previousConfig = nextConfig.webpack;
const minify = yakOptions.minify !== void 0 ? yakOptions.minify : process.env.NODE_ENV === "production";
nextConfig.experimental ||= {};
nextConfig.experimental.swcPlugins ||= [];
nextConfig.experimental.swcPlugins.push([
"yak-swc",
{
minify,
basePath: currentDir,
prefix: yakOptions.prefix,
displayNames: yakOptions.displayNames ?? !minify
}
]);
nextConfig.webpack = (webpackConfig, options) => {
if (previousConfig) {
webpackConfig = previousConfig(webpackConfig, options);
}
webpackConfig.module.rules.push({
test: /\.yak\.module\.css$/,
loader: path.join(currentDir, "../loaders/css-loader.js"),
options: yakOptions
});
const yakContext = resolveYakContext(
yakOptions.contextPath,
webpackConfig.context || process.cwd()
);
if (yakContext) {
webpackConfig.resolve.alias["next-yak/context/baseContext"] = yakContext;
}
return webpackConfig;
};
return nextConfig;
};
function resolveYakContext(contextPath, cwd) {
const yakContext = contextPath ? path.resolve(cwd, contextPath) : path.resolve(cwd, "yak.context");
const extensions = ["", ".ts", ".tsx", ".js", ".jsx"];
for (const extension in extensions) {
const fileName = yakContext + extensions[extension];
if (existsSync(fileName)) {
return fileName;
}
}
if (contextPath) {
throw new Error(`Could not find yak context file at ${yakContext}`);
}
}
var withYak = (maybeYakOptions, nextConfig) => {
if (nextConfig === void 0) {
return withYak({}, maybeYakOptions);
}
const yakOptions = maybeYakOptions;
if (typeof nextConfig === "function") {
return (...args) => {
const config = nextConfig(...args);
return config instanceof Promise ? config.then((config2) => addYak(yakOptions, config2)) : addYak(yakOptions, config);
};
}
return addYak(yakOptions, nextConfig);
};
export {
withYak
};
//# sourceMappingURL=index.js.map