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
155 lines (152 loc) • 5.78 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) {
__defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
}
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
//#endregion
let node_fs = require("node:fs");
let node_path = require("node:path");
node_path = __toESM(node_path, 1);
let node_url = require("node:url");
//#region withYak/index.ts
const currentDir = typeof __dirname !== "undefined" ? __dirname : (0, node_path.dirname)((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
const addYak = (yakOptions, nextConfig) => {
const minify = yakOptions.minify ?? process.env.NODE_ENV === "production";
const yakPluginOptions = {
minify,
basePath: currentDir,
prefix: yakOptions.prefix,
displayNames: yakOptions.displayNames ?? !minify,
suppressDeprecationWarnings: yakOptions.experiments?.suppressDeprecationWarnings ?? false,
reactRefreshReg: true
};
const transpilation = yakOptions.experiments?.transpilationMode ?? "CssModule";
const cssExtension = transpilation === "CssModule" ? ".yak.module.css" : ".yak.css";
if (process.env.TURBOPACK === "1" || process.env.TURBOPACK === "auto") addYakTurbopack(nextConfig, yakOptions, {
...yakPluginOptions,
importMode: {
value: "data:text/css;base64,",
transpilation: "Css",
encoding: "Base64"
}
});
else addYakWebpack(nextConfig, yakOptions, {
...yakPluginOptions,
importMode: {
value: `./{{__BASE_NAME__}}${cssExtension}!=!./{{__BASE_NAME__}}?./{{__BASE_NAME__}}${cssExtension}`,
transpilation,
encoding: "None"
}
});
return nextConfig;
};
function addYakTurbopack(nextConfig, yakOptions, yakPluginOptions) {
const yakLoader = removeUndefinedRecursive({
loader: node_path.default.join(currentDir, "../loaders/turbo-loader.cjs"),
options: {
yakOptions,
yakPluginOptions
}
});
nextConfig.turbopack ||= {};
nextConfig.turbopack.rules ||= {};
const ruleKey = "*.{js,jsx,cjs,mjs,ts,tsx,cts,mts}";
const rule = {
loaders: [],
...nextConfig.turbopack.rules[ruleKey]
};
rule.loaders.push(yakLoader);
nextConfig.turbopack.rules[ruleKey] = rule;
const yakContext = resolveYakContext(yakOptions.contextPath, process.cwd());
if (yakContext) {
nextConfig.turbopack.resolveAlias ||= {};
nextConfig.turbopack.resolveAlias["next-yak/context/baseContext"] = `./${node_path.default.relative(process.cwd(), yakContext)}`;
}
}
function addYakWebpack(nextConfig, yakOptions, yakPluginOptions) {
nextConfig.experimental ||= {};
nextConfig.experimental.swcPlugins ||= [];
nextConfig.experimental.swcPlugins.push(["yak-swc", yakPluginOptions]);
const previousConfig = nextConfig.webpack;
nextConfig.webpack = (webpackConfig, options) => {
if (previousConfig) webpackConfig = previousConfig(webpackConfig, options);
webpackConfig.module.rules.push({
test: yakOptions.experiments?.transpilationMode === "Css" ? /\.yak\.css$/ : /\.yak\.module\.css$/,
loader: node_path.default.join(currentDir, "../loaders/webpack-loader.cjs"),
options: yakOptions
});
const yakContext = resolveYakContext(yakOptions.contextPath, webpackConfig.context || process.cwd());
if (yakContext) webpackConfig.resolve.alias["next-yak/context/baseContext"] = yakContext;
return webpackConfig;
};
}
function removeUndefinedRecursive(obj) {
if (typeof obj !== "object" || obj === null) return obj;
if (Array.isArray(obj)) {
const filtered = [];
for (let i = 0; i < obj.length; i++) {
const processed = removeUndefinedRecursive(obj[i]);
if (processed !== void 0) filtered.push(processed);
}
return filtered;
}
const newObj = {};
let hasChanges = false;
for (const key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = removeUndefinedRecursive(obj[key]);
if (value !== void 0) {
newObj[key] = value;
hasChanges = true;
}
}
return hasChanges ? newObj : obj;
}
function resolveYakContext(contextPath, cwd) {
const yakContext = contextPath ? node_path.default.resolve(cwd, contextPath) : node_path.default.resolve(cwd, "yak.context");
const extensions = [
"",
".ts",
".tsx",
".js",
".jsx"
];
for (const extension in extensions) {
const fileName = yakContext + extensions[extension];
if ((0, node_fs.existsSync)(fileName)) return fileName;
}
if (contextPath) throw new Error(`Could not find yak context file at ${yakContext}`);
}
const 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((config) => addYak(yakOptions, config)) : addYak(yakOptions, config);
};
return addYak(yakOptions, nextConfig);
};
//#endregion
exports.resolveYakContext = resolveYakContext;
exports.withYak = withYak;
//# sourceMappingURL=index.cjs.map