next-intlayer
Version:
Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.
214 lines • 8.2 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);
var withIntlayer_exports = {};
__export(withIntlayer_exports, {
withIntlayer: () => withIntlayer
});
module.exports = __toCommonJS(withIntlayer_exports);
var import_chokidar = require("@intlayer/chokidar");
var import_config = require("@intlayer/config");
var import_webpack = require("@intlayer/webpack");
var import_deepmerge = __toESM(require("deepmerge"));
var import_fast_glob = __toESM(require("fast-glob"));
var import_path = require("path");
var import_compareVersion = require('./compareVersion.cjs');
var import_getNextVertion = require('./getNextVertion.cjs');
const isTurbopackEnabled = process.env.npm_lifecycle_script?.includes("--turbo");
const nextVersion = (0, import_getNextVertion.getNextVersion)();
const isGteNext13 = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "13.0.0");
const isGteNext15 = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "15.0.0");
const isTurbopackStable = (0, import_compareVersion.compareVersions)(nextVersion, "\u2265", "15.3.0");
const getIsSwcPluginAvailable = () => {
try {
import_config.ESMxCJSRequire.resolve("@intlayer/swc");
return true;
} catch (e) {
return false;
}
};
const getPruneConfig = (intlayerConfig) => {
const { optimize, traversePattern, importMode } = intlayerConfig.build;
const { dictionariesDir, dynamicDictionariesDir, mainDir, baseDir } = intlayerConfig.content;
if (!optimize) return {};
if (!isGteNext13) return {};
const isSwcPluginAvailable = getIsSwcPluginAvailable();
if (!isSwcPluginAvailable) return {};
const logger = (0, import_config.getAppLogger)(intlayerConfig);
(0, import_chokidar.runOnce)(
(0, import_path.join)(baseDir, ".next", "cache", "intlayer-prune-plugin-enabled.lock"),
() => logger("Intlayer prune plugin is enabled"),
1e3 * 10
// 10 seconds
);
const dictionariesEntryPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
const dynamicDictionariesEntryPath = (0, import_path.join)(
mainDir,
"dynamic_dictionaries.mjs"
);
const filesListPattern = import_fast_glob.default.sync(traversePattern, {
cwd: baseDir
}).map((file) => (0, import_path.join)(baseDir, file));
const filesList = [
...filesListPattern,
dictionariesEntryPath
// should add dictionariesEntryPath to replace it by a empty object if import made dynamic
];
return {
experimental: {
swcPlugins: [
[
import_config.ESMxCJSRequire.resolve("@intlayer/swc"),
{
dictionariesDir,
dictionariesEntryPath,
dynamicDictionariesDir,
dynamicDictionariesEntryPath,
importMode,
filesList,
replaceDictionaryEntry: false
}
]
]
}
};
};
const withIntlayer = async (nextConfig = {}) => {
if (typeof nextConfig !== "object") {
nextConfig = {};
}
const intlayerConfig = (0, import_config.getConfiguration)();
const sentinelPath = (0, import_path.join)(
intlayerConfig.content.baseDir,
".next",
"cache",
"intlayer-prepared.lock"
);
await (0, import_chokidar.runOnce)(
sentinelPath,
async () => await (0, import_chokidar.prepareIntlayer)(intlayerConfig)
);
const { mainDir, configDir, baseDir } = intlayerConfig.content;
const dictionariesPath = (0, import_path.join)(mainDir, "dictionaries.mjs");
const relativeDictionariesPath = (0, import_path.relative)(baseDir, dictionariesPath);
const unmergedDictionariesPath = (0, import_path.join)(mainDir, "unmerged_dictionaries.mjs");
const relativeUnmergedDictionariesPath = (0, import_path.relative)(
baseDir,
unmergedDictionariesPath
);
const configurationPath = (0, import_path.join)(configDir, "configuration.json");
const relativeConfigurationPath = (0, import_path.relative)(baseDir, configurationPath);
const turboConfig = {
resolveAlias: {
// prefix by './' to consider the path as relative to the project root. This is necessary for turbopack to work correctly.
// Normalize the path to avoid issues with the path separator on Windows
"@intlayer/dictionaries-entry": (0, import_config.normalizePath)(
`./${relativeDictionariesPath}`
),
"@intlayer/unmerged-dictionaries-entry": (0, import_config.normalizePath)(
`./${relativeUnmergedDictionariesPath}`
),
"@intlayer/config/built": (0, import_config.normalizePath)(`./${relativeConfigurationPath}`)
},
rules: {
"*.node": {
as: "*.node",
loaders: ["node-loader"]
}
}
};
const serverExternalPackages = [
"esbuild",
"module",
"fs",
"chokidar",
"fsevents"
];
const newConfig = {
// Only add `serverExternalPackages` if Next.js is v15+
...isGteNext15 ? {
// only for Next ≥15
serverExternalPackages
} : {
// only for Next ≥13 and <15.3
...isGteNext13 && {
serverComponentsExternalPackages: serverExternalPackages
}
},
...isTurbopackEnabled && {
...isGteNext15 && isTurbopackStable ? {
// only for Next ≥15.3
turbopack: turboConfig
} : {
experimental: {
// only for Next ≥13 and <15.3
turbo: turboConfig
}
}
},
webpack: (config, options) => {
if (typeof nextConfig.webpack === "function") {
config = nextConfig.webpack(config, options);
}
config.resolve.alias = {
...config.resolve.alias,
"@intlayer/dictionaries-entry": (0, import_path.resolve)(relativeDictionariesPath),
"@intlayer/unmerged-dictionaries-entry": (0, import_path.resolve)(
relativeUnmergedDictionariesPath
),
"@intlayer/config/built": (0, import_path.resolve)(relativeConfigurationPath)
};
config.externals.push({
esbuild: "esbuild",
module: "module",
fs: "fs",
chokidar: "chokidar",
fsevents: "fsevents"
});
config.module.rules.push({
test: /\.node$/,
loader: "node-loader"
});
const { isServer, nextRuntime } = options;
const isBuildCommand = process.env.npm_lifecycle_event === "build" || process.argv.some((arg) => arg === "build");
if (!isBuildCommand && isServer && nextRuntime === "nodejs") {
config.plugins.push(new import_webpack.IntlayerPlugin());
}
return config;
}
};
const pruneConfig = getPruneConfig(intlayerConfig);
const intlayerNextConfig = (0, import_deepmerge.default)(pruneConfig, newConfig);
const result = (0, import_deepmerge.default)(nextConfig, intlayerNextConfig);
return result;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
withIntlayer
});
//# sourceMappingURL=withIntlayer.cjs.map