UNPKG

next-intlayer

Version:

Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.

163 lines 5.3 kB
import { cleanOutputDir } from "@intlayer/chokidar"; import { ESMxCJSRequire, getAppLogger, getConfiguration } from "@intlayer/config"; import { IntlayerPlugin } from "@intlayer/webpack"; import merge from "deepmerge"; import fg from "fast-glob"; import { join, relative, resolve } from "path"; import { compareVersions } from "./compareVersion.mjs"; import { getNextVersion } from "./getNextVertion.mjs"; cleanOutputDir(); const isTurbopackEnabled = process.env.npm_lifecycle_script?.includes("--turbo"); const nextVersion = getNextVersion(); const isGteNext13 = compareVersions(nextVersion, "\u2265", "13.0.0"); const isGteNext15 = compareVersions(nextVersion, "\u2265", "15.0.0"); const isTurbopackStable = compareVersions(nextVersion, "\u2265", "15.3.0"); const getIsSwcPluginAvailable = () => { try { ESMxCJSRequire.resolve("@intlayer/swc"); return true; } catch (e) { return false; } }; const getPruneConfig = (intlayerConfig) => { const { optimize, traversePattern, activateDynamicImport } = intlayerConfig.build; const { dictionariesDir, dynamicDictionariesDir, mainDir, baseDir } = intlayerConfig.content; if (!optimize) return {}; if (!isGteNext13) return {}; const isSwcPluginAvailable = getIsSwcPluginAvailable(); if (!isSwcPluginAvailable) return {}; const logger = getAppLogger(intlayerConfig); logger("Intlayer prune plugin is enabled"); const dictionariesEntryPath = join(mainDir, "dictionaries.mjs"); const dynamicDictionariesEntryPath = join( mainDir, "dynamic_dictionaries.mjs" ); const filesListPattern = fg.sync(traversePattern, { cwd: baseDir }).map((file) => join(baseDir, file)); const filesList = [ ...filesListPattern, dictionariesEntryPath // should add dictionariesEntryPath to replace it by a empty object if import made dynamic ]; return { experimental: { swcPlugins: [ [ ESMxCJSRequire.resolve("@intlayer/swc"), { dictionariesDir, dictionariesEntryPath, dynamicDictionariesDir, dynamicDictionariesEntryPath, activateDynamicImport, filesList, replaceDictionaryEntry: false } ] ] } }; }; const withIntlayer = (nextConfig = {}) => { if (typeof nextConfig !== "object") { nextConfig = {}; } const intlayerConfig = getConfiguration(); const { mainDir, configDir, baseDir } = intlayerConfig.content; const dictionariesPath = join(mainDir, "dictionaries.mjs"); const relativeDictionariesPath = relative(baseDir, dictionariesPath); const unmergedDictionariesPath = join(mainDir, "unmerged_dictionaries.mjs"); const relativeUnmergedDictionariesPath = relative( baseDir, unmergedDictionariesPath ); const configurationPath = join(configDir, "configuration.json"); const relativeConfigurationPath = relative(baseDir, configurationPath); const turboConfig = { resolveAlias: { // "prefix by './' to consider the path as relative to the project root. This is necessary for turbo to work correctly." "@intlayer/dictionaries-entry": `./${relativeDictionariesPath}`, "@intlayer/unmerged-dictionaries-entry": `./${relativeUnmergedDictionariesPath}`, "@intlayer/config/built": `./${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": resolve(relativeDictionariesPath), "@intlayer/unmerged-dictionaries-entry": resolve( relativeUnmergedDictionariesPath ), "@intlayer/config/built": 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; if (isServer && nextRuntime === "nodejs") { config.plugins.push(new IntlayerPlugin()); } return config; } }; const pruneConfig = getPruneConfig(intlayerConfig); const intlayerNextConfig = merge(pruneConfig, newConfig); return merge(nextConfig, intlayerNextConfig); }; export { withIntlayer }; //# sourceMappingURL=withIntlayer.mjs.map