Skip to content

Commit

Permalink
fix: added target options to esbuild and swc
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed May 25, 2024
1 parent 1075a5a commit 5d3100b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 48 deletions.
49 changes: 2 additions & 47 deletions packages/packem/src/create-bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const build = async (
declaration: false,
dependencies: [],
devDependencies: [],
emitCJS: false,
emitESM: true,
entries: [],
externals: [...Module.builtinModules, ...Module.builtinModules.map((m) => `node:${m}`)],
failOnWarn: true,
Expand Down Expand Up @@ -161,8 +163,6 @@ const build = async (
// the `import` and the `(`.
include: /\bimport\s*[(/]/,
},
emitCJS: false,
emitESM: true,
esbuild: {
charset: "utf8",
include: /\.[jt]sx?$/,
Expand Down Expand Up @@ -353,51 +353,6 @@ const build = async (

ensureDirSync(options.outDir);

if (options.transformerName === "esbuild" && options.rollup.esbuild) {
if (tsconfig?.config.compilerOptions?.target?.toLowerCase() === "es3") {
logger.warn(
[
"ES3 target is not supported by esbuild, so ES5 will be used instead..",
"Please set 'target' option in tsconfig to at least ES5 to disable this error",
].join(" "),
);

// eslint-disable-next-line no-param-reassign
tsconfig.config.compilerOptions.target = "es5";
options.rollup.esbuild.target = "es5";
}

if (options.rollup.esbuild.jsx === "preserve") {
let message = "Packem does not support 'preserve' jsx option. Please use 'transform' or 'automatic' instead.";

if (tsconfig?.config.compilerOptions?.jsx) {
message =
"Packem does not support '" +
tsconfig.config.compilerOptions.jsx +
"' jsx option. Please change it to 'react' or 'react-jsx' or 'react-jsxdev' instead.";
}

throw new Error(message);
}

// Add node target to esbuild target
if (options.rollup.esbuild.target) {
const targets = arrayify(options.rollup.esbuild.target);

if (!targets.some((t) => t.startsWith("node"))) {
options.rollup.esbuild.target = [options.target, ...targets];
}
} else {
options.rollup.esbuild.target = [options.target];
}

if (tsconfig?.config.compilerOptions?.target === "es5") {
options.rollup.esbuild.keepNames = false;

logger.debug("Disabling keepNames because target is set to es5");
}
}

if (options.rollup.resolve && options.rollup.resolve.preferBuiltins === true) {
options.rollup.polyfillNode = false;

Expand Down
59 changes: 59 additions & 0 deletions packages/packem/src/rollup/get-rollup-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { visualizer as visualizerPlugin } from "rollup-plugin-visualizer";
import { DEFAULT_EXTENSIONS } from "../constants";
import type { BuildContext, InternalBuildOptions } from "../types";
import arrayIncludes from "../utils/array-includes";
import arrayify from "../utils/arrayify";
import getPackageName from "../utils/get-package-name";
import { cjsInterop as cjsInteropPlugin } from "./plugins/cjs-interop";
import { copyPlugin } from "./plugins/copy";
Expand Down Expand Up @@ -49,6 +50,48 @@ const getTransformerConfig = (
throw new Error("No esbuild options found in your configuration.");
}

if (context.tsconfig?.config.compilerOptions?.target?.toLowerCase() === "es3") {
context.logger.warn(
[
"ES3 target is not supported by esbuild, so ES5 will be used instead..",
"Please set 'target' option in tsconfig to at least ES5 to disable this error",
].join(" "),
);

context.tsconfig.config.compilerOptions.target = "es5";
context.options.rollup.esbuild.target = "es5";
}

if (context.options.rollup.esbuild.jsx === "preserve") {
let message = "Packem does not support 'preserve' jsx option. Please use 'transform' or 'automatic' instead.";

if (context.tsconfig?.config.compilerOptions?.jsx) {
message =
"Packem does not support '" +
context.tsconfig.config.compilerOptions.jsx +
"' jsx option. Please change it to 'react' or 'react-jsx' or 'react-jsxdev' instead.";
}

throw new Error(message);
}

// Add node target to esbuild target
if (context.options.rollup.esbuild.target) {
const targets = arrayify(context.options.rollup.esbuild.target);

if (!targets.some((t) => t.startsWith("node"))) {
context.options.rollup.esbuild.target = [...new Set([...arrayify(context.options.target), ...targets])];
}
} else {
context.options.rollup.esbuild.target = arrayify(context.options.target);
}

if (context.tsconfig?.config.compilerOptions?.target === "es5") {
context.options.rollup.esbuild.keepNames = false;

context.logger.debug("Disabling keepNames because target is set to es5");
}

return {
minify: context.options.minify,
sourceMap: context.options.sourcemap,
Expand All @@ -62,6 +105,21 @@ const getTransformerConfig = (
throw new Error("No swc options found in your configuration.");
}

if (typeof context.options.rollup.swc.env !== "object") {
context.options.rollup.swc.env = {};
}

// Add node target to esbuild target
if (context.options.rollup.swc.env.targets) {
const targets = arrayify(context.options.rollup.swc.env.targets);

if (!targets.some((t) => t.startsWith("node"))) {
context.options.rollup.swc.env.targets = [...new Set([...arrayify(context.options.target), ...targets])];
}
} else {
context.options.rollup.swc.env.targets = arrayify(context.options.target);
}

return {
minify: context.options.minify,
...context.options.rollup.swc,
Expand Down Expand Up @@ -283,6 +341,7 @@ export const getRollupOptions = async (context: BuildContext): Promise<RollupOpt
context.options.rollup.resolve &&
nodeResolvePlugin({
extensions: DEFAULT_EXTENSIONS,
rootDir: context.rootDir,
...context.options.rollup.resolve,
}),

Expand Down
27 changes: 26 additions & 1 deletion packages/packem/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ export type BuildEntry = {
outDir?: string;
};

type Target =
| "chrome"
| "deno"
| "edge"
| "firefox"
| "hermes"
| "ie"
| "ios"
| "node"
| "opera"
| "rhino"
| "safari"
| "esnext"
| "es5"
| "es6"
| "es2015"
| "es2016"
| "es2017"
| "es2018"
| "es2019"
| "es2020"
| "es2021"
| "es2022"
| "es2023";

export interface BuildOptions {
alias: Record<string, string>;
cjsInterop?: boolean;
Expand Down Expand Up @@ -120,7 +145,7 @@ export interface BuildOptions {
sourcemap: boolean;
stub: boolean;
stubOptions: { jiti: Omit<JITIOptions, "onError" | "transform"> };
target: string;
target: Target | Target[];
transformer?: (config: SwcPluginConfig | SucrasePluginConfig | EsbuildPluginConfig) => Plugin;
}

Expand Down

0 comments on commit 5d3100b

Please sign in to comment.