Skip to content

Commit

Permalink
fix: esm conditional imports
Browse files Browse the repository at this point in the history
fix #791
  • Loading branch information
RebeccaStevens committed Mar 24, 2024
1 parent 7607b7c commit a31b512
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 12 deletions.
106 changes: 95 additions & 11 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,43 @@ const treeshake = {
unknownGlobalSideEffects: false,
} satisfies RollupOptions["treeshake"];

const classic = {
const classicCJS = {
input: "src/classic.ts",

output: [
{
file: pkg.exports["."].require,
format: "cjs",
sourcemap: false,
},
],

plugins: [
rollupPluginAutoExternal(),
rollupPluginTs({
transpileOnly: true,
tsconfig: {
fileName: "tsconfig.build.json",
hook: (resolvedConfig) => ({
...resolvedConfig,
paths: {
...resolvedConfig.paths,
"#eslint-plugin-functional/conditional-imports/*": [
"src/utils/conditional-imports/cjs/*",
],
},
}),
},
}),
rollupPluginDeassert({
include: ["**/*.{js,ts}"],
}),
],

treeshake,
} satisfies RollupOptions;

const classicESM = {
input: "src/classic.ts",

output: [
Expand All @@ -21,8 +57,39 @@ const classic = {
format: "esm",
sourcemap: false,
},
],

plugins: [
rollupPluginAutoExternal(),
rollupPluginTs({
transpileOnly: true,
tsconfig: {
fileName: "tsconfig.build.json",
hook: (resolvedConfig) => ({
...resolvedConfig,
paths: {
...resolvedConfig.paths,
"#eslint-plugin-functional/conditional-imports/*": [
"src/utils/conditional-imports/esm/*",
],
},
}),
},
}),
rollupPluginDeassert({
include: ["**/*.{js,ts}"],
}),
],

treeshake,
} satisfies RollupOptions;

const flatCJS = {
input: "src/flat.ts",

output: [
{
file: pkg.exports["."].require,
file: pkg.exports["./flat"].require,
format: "cjs",
sourcemap: false,
},
Expand All @@ -32,7 +99,18 @@ const classic = {
rollupPluginAutoExternal(),
rollupPluginTs({
transpileOnly: true,
tsconfig: "tsconfig.build.json",
tsconfig: {
fileName: "tsconfig.build.json",
hook: (resolvedConfig) => ({
...resolvedConfig,
paths: {
...resolvedConfig.paths,
"#eslint-plugin-functional/conditional-imports/*": [
"src/utils/conditional-imports/cjs/*",
],
},
}),
},
}),
rollupPluginDeassert({
include: ["**/*.{js,ts}"],
Expand All @@ -42,7 +120,7 @@ const classic = {
treeshake,
} satisfies RollupOptions;

const flat = {
const flatESM = {
input: "src/flat.ts",

output: [
Expand All @@ -51,18 +129,24 @@ const flat = {
format: "esm",
sourcemap: false,
},
{
file: pkg.exports["./flat"].require,
format: "cjs",
sourcemap: false,
},
],

plugins: [
rollupPluginAutoExternal(),
rollupPluginTs({
transpileOnly: true,
tsconfig: "tsconfig.build.json",
tsconfig: {
fileName: "tsconfig.build.json",
hook: (resolvedConfig) => ({
...resolvedConfig,
paths: {
...resolvedConfig.paths,
"#eslint-plugin-functional/conditional-imports/*": [
"src/utils/conditional-imports/esm/*",
],
},
}),
},
}),
rollupPluginDeassert({
include: ["**/*.{js,ts}"],
Expand All @@ -72,4 +156,4 @@ const flat = {
treeshake,
} satisfies RollupOptions;

export default [classic, flat];
export default [classicCJS, classicESM, flatCJS, flatESM];
File renamed without changes.
10 changes: 10 additions & 0 deletions src/utils/conditional-imports/esm/ts-api-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type tsApiUtils from "ts-api-utils";

import ts from "#eslint-plugin-functional/conditional-imports/typescript";

export default await ((): Promise<typeof tsApiUtils | undefined> => {
if (ts !== undefined) {
return import("ts-api-utils").catch(() => undefined);
}
return Promise.resolve(undefined);
})();
4 changes: 4 additions & 0 deletions src/utils/conditional-imports/esm/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line unicorn/import-style
export default await import("typescript")
.then((module) => module.default)
.catch(() => undefined);
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"#eslint-plugin-functional/settings": ["src/settings"],
"#eslint-plugin-functional/utils/*": ["src/utils/*"],
"#eslint-plugin-functional/conditional-imports/*": [
"src/utils/conditional-imports/*"
"src/utils/conditional-imports/cjs/*"
],
"#eslint-plugin-functional/tests/*": ["tests/*"]
}
Expand Down

0 comments on commit a31b512

Please sign in to comment.