@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
126 lines (124 loc) • 4.03 kB
JavaScript
import { getAlias } from "../utils/alias.mjs";
import { getConfiguration } from "../configFile/getConfiguration.mjs";
import { getConfigEnvVars } from "../envVars/envVars.mjs";
import { BundleLogger } from "./logBundle.mjs";
import { isAbsolute, join, resolve } from "node:path";
import { mkdir, rm, writeFile } from "node:fs/promises";
import packageJSON from "@intlayer/types/package.json" with { type: "json" };
import { build } from "esbuild";
import { exec } from "node:child_process";
import { promisify } from "node:util";
//#region src/bundle/index.ts
const execAsync = promisify(exec);
const packageList = [
"next-intlayer",
"react-intlayer",
"vue-intlayer",
"svelte-intlayer",
"preact-intlayer",
"solid-intlayer",
"angular-intlayer",
"lit-intlayer",
"express-intlayer",
"hono-intlayer",
"fastify-intlayer",
"adonis-intlayer",
"vanilla-intlayer",
"intlayer"
];
const defaultVersion = packageJSON.version;
/**
* Bundle the application content using esbuild.
* It uses the Intlayer configuration to set up aliases and other esbuild options.
*
* @param options - Bundle options including entryPoint, outfile, and esbuild options.
* @returns The build result.
*/
const bundleIntlayer = async (options) => {
const { outfile = "intlayer-bundle.js", configOptions, bundlePackages = [...packageList], version = defaultVersion, ...esbuildOptions } = options;
const intlayerConfig = getConfiguration(configOptions);
const logger = new BundleLogger(intlayerConfig);
const alias = getAlias({
configuration: intlayerConfig,
formatter: (value) => resolve(process.cwd(), value)
});
const treeShakingDefines = getConfigEnvVars(intlayerConfig, (key) => `process.env.${key}`, (value) => `"${value}"`);
const intlayerBundlePlugin = {
name: "intlayer-bundle-plugin",
setup(build) {
const packagesRegex = new RegExp(`^(${bundlePackages.map((packages) => packages.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|")})(\\/.*)?$`);
build.onResolve({ filter: packagesRegex }, () => ({ external: false }));
build.onResolve({ filter: /^@intlayer\// }, () => ({ external: false }));
}
};
const tempDir = join(intlayerConfig.system.tempDir, "intlayer-bundle-tmp");
logger.setStatus("installing");
try {
await rm(tempDir, {
recursive: true,
force: true
});
await mkdir(tempDir, { recursive: true });
const deps = Object.fromEntries(bundlePackages.map((pkg) => [pkg, version]));
await writeFile(join(tempDir, "package.json"), JSON.stringify({
dependencies: deps,
type: "module"
}));
let pm = "npm install";
try {
await execAsync("bun --version");
pm = "bun install";
} catch {}
await execAsync(pm, { cwd: tempDir });
const buildOptions = {
bundle: true,
outfile: isAbsolute(outfile) ? outfile : join(process.cwd(), outfile),
absWorkingDir: tempDir,
platform: "browser",
conditions: [
"browser",
"module",
"import",
"default"
],
minify: true,
minifyIdentifiers: true,
treeShaking: true,
format: "iife",
ignoreAnnotations: true,
stdin: {
contents: bundlePackages.map((packageName) => {
const globalName = packageName.split("-").map((part) => `${part.charAt(0).toUpperCase()}${part.slice(1)}`).join("");
return `import * as ${globalName} from '${packageName}';\nif (typeof window !== 'undefined') { window['${globalName}'] = ${globalName}; }`;
}).join("\n"),
resolveDir: tempDir
},
define: {
"process.env": "{}",
...treeShakingDefines,
...esbuildOptions.define
},
alias: {
...alias,
...esbuildOptions.alias
},
...esbuildOptions,
plugins: [intlayerBundlePlugin, ...esbuildOptions.plugins || []]
};
logger.setStatus("bundling");
const result = await build(buildOptions);
logger.setStatus("success");
return result;
} catch (error) {
logger.setError(error);
throw error;
} finally {
await rm(tempDir, {
recursive: true,
force: true
});
}
};
//#endregion
export { bundleIntlayer, packageList };
//# sourceMappingURL=index.mjs.map