diff --git a/rollup.config.ts b/rollup.config.ts index 5c85096c7..bee372da1 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -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: [ @@ -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, }, @@ -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}"], @@ -42,7 +120,7 @@ const classic = { treeshake, } satisfies RollupOptions; -const flat = { +const flatESM = { input: "src/flat.ts", output: [ @@ -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}"], @@ -72,4 +156,4 @@ const flat = { treeshake, } satisfies RollupOptions; -export default [classic, flat]; +export default [classicCJS, classicESM, flatCJS, flatESM]; diff --git a/src/utils/conditional-imports/ts-api-utils.ts b/src/utils/conditional-imports/cjs/ts-api-utils.ts similarity index 100% rename from src/utils/conditional-imports/ts-api-utils.ts rename to src/utils/conditional-imports/cjs/ts-api-utils.ts diff --git a/src/utils/conditional-imports/typescript.ts b/src/utils/conditional-imports/cjs/typescript.ts similarity index 100% rename from src/utils/conditional-imports/typescript.ts rename to src/utils/conditional-imports/cjs/typescript.ts diff --git a/src/utils/conditional-imports/esm/ts-api-utils.ts b/src/utils/conditional-imports/esm/ts-api-utils.ts new file mode 100644 index 000000000..4ec432184 --- /dev/null +++ b/src/utils/conditional-imports/esm/ts-api-utils.ts @@ -0,0 +1,10 @@ +import type tsApiUtils from "ts-api-utils"; + +import ts from "#eslint-plugin-functional/conditional-imports/typescript"; + +export default await ((): Promise => { + if (ts !== undefined) { + return import("ts-api-utils").catch(() => undefined); + } + return Promise.resolve(undefined); +})(); diff --git a/src/utils/conditional-imports/esm/typescript.ts b/src/utils/conditional-imports/esm/typescript.ts new file mode 100644 index 000000000..e6c1a5f3f --- /dev/null +++ b/src/utils/conditional-imports/esm/typescript.ts @@ -0,0 +1,4 @@ +// eslint-disable-next-line unicorn/import-style +export default await import("typescript") + .then((module) => module.default) + .catch(() => undefined); diff --git a/tsconfig.base.json b/tsconfig.base.json index 9a14633b9..436171c1d 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -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/*"] }