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
105 lines (104 loc) • 4.06 kB
JavaScript
;
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 __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// withYak/index.ts
var index_exports = {};
__export(index_exports, {
withYak: () => withYak
});
module.exports = __toCommonJS(index_exports);
var import_node_fs = require("fs");
var import_node_path = __toESM(require("path"), 1);
var import_node_url = require("url");
var import_meta = {};
var currentDir = typeof __dirname !== "undefined" ? __dirname : (0, import_node_path.dirname)((0, import_node_url.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: import_node_path.default.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 ? import_node_path.default.resolve(cwd, contextPath) : import_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, import_node_fs.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);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
withYak
});
//# sourceMappingURL=index.cjs.map