@sanity/tsdown-config 0.7.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,3 +11,59 @@ import {defineConfig} from '@sanity/tsdown-config'
11
11
 
12
12
  export default defineConfig({tsconfig: 'tsconfig.dist.json'})
13
13
  ```
14
+
15
+ ## React Compiler
16
+
17
+ The same React Compiler feature as `@sanity/pkg-utils` is available. It runs
18
+ [`babel-plugin-react-compiler`](https://react.dev/learn/react-compiler) on the source files before
19
+ they are bundled, so published components are memoized automatically. The plugin needs to be
20
+ installed separately:
21
+
22
+ ```sh
23
+ pnpm add --save-dev babel-plugin-react-compiler
24
+ ```
25
+
26
+ Then enable it:
27
+
28
+ ```ts
29
+ import {defineConfig} from '@sanity/tsdown-config'
30
+
31
+ export default defineConfig({
32
+ tsconfig: 'tsconfig.dist.json',
33
+ reactCompiler: true,
34
+ })
35
+ ```
36
+
37
+ Pass an object to configure the compiler, using the same options as [`babel-plugin-react-compiler`](https://react.dev/reference/react-compiler/configuration):
38
+
39
+ ```ts
40
+ export default defineConfig({
41
+ tsconfig: 'tsconfig.dist.json',
42
+ reactCompiler: {target: '18'},
43
+ })
44
+ ```
45
+
46
+ ## styled-components
47
+
48
+ If your package uses `styled-components`, enable the same `styledComponents` transform that `@sanity/pkg-utils` has:
49
+
50
+ ```ts
51
+ import {defineConfig} from '@sanity/tsdown-config'
52
+
53
+ export default defineConfig({
54
+ tsconfig: 'tsconfig.dist.json',
55
+ styledComponents: true,
56
+ })
57
+ ```
58
+
59
+ It adds `displayName` (better debugging) and `componentId` (avoids SSR hydration mismatches) to your styled components, and minifies the CSS in tagged template literals.
60
+ Unlike `@sanity/pkg-utils` it doesn't require installing `babel-plugin-styled-components`, as it uses [oxc's native port](https://oxc.rs/docs/guide/usage/transformer/plugins.html#styled-components) of the babel plugin.
61
+
62
+ Pass an object to customize the transform, using the same options as [`babel-plugin-styled-components`](https://styled-components.com/docs/tooling#babel-plugin):
63
+
64
+ ```ts
65
+ export default defineConfig({
66
+ tsconfig: 'tsconfig.dist.json',
67
+ styledComponents: {namespace: 'my-package'},
68
+ })
69
+ ```
package/dist/index.d.mts CHANGED
@@ -1,4 +1,39 @@
1
1
  import { UserConfig } from "tsdown";
2
+ import { PluginOptions } from "babel-plugin-react-compiler";
3
+ /**
4
+ * Options for the `styled-components` transform, the same options as `babel-plugin-styled-components`.
5
+ * @public
6
+ */
7
+ interface StyledComponentsOptions {
8
+ /** @defaultValue true */
9
+ displayName?: boolean;
10
+ /**
11
+ * @defaultValue []
12
+ * @example ["\@xstyled/styled-components", "\@xstyled/styled-components/*"]
13
+ */
14
+ topLevelImportPaths?: string[];
15
+ /** @defaultValue true */
16
+ ssr?: boolean;
17
+ /** @defaultValue false */
18
+ fileName?: boolean;
19
+ /** @defaultValue ["index"] */
20
+ meaninglessFileNames?: string[];
21
+ /** @defaultValue true */
22
+ minify?: boolean;
23
+ /** @defaultValue false */
24
+ transpileTemplateLiterals?: boolean;
25
+ namespace?: string;
26
+ /** @defaultValue true */
27
+ pure?: boolean;
28
+ }
29
+ /**
30
+ * Options for the React Compiler, the same options as `babel-plugin-react-compiler`.
31
+ * The typings resolve in userland once `babel-plugin-react-compiler` (an optional peer
32
+ * dependency, required to use `reactCompiler`) is installed, and always match the installed
33
+ * version of the compiler.
34
+ * @public
35
+ */
36
+ type ReactCompilerOptions = Partial<PluginOptions>;
2
37
  /**
3
38
  * @public
4
39
  */
@@ -7,10 +42,28 @@ interface PackageOptions extends Pick<UserConfig, "tsconfig" | "entry" | "format
7
42
  * @defaultValue 'neutral'
8
43
  */
9
44
  platform?: UserConfig["platform"];
45
+ /**
46
+ * Runs `babel-plugin-react-compiler` on the source files before they are bundled, so published
47
+ * components are memoized automatically. Pass `true` to use the defaults, or an options object
48
+ * to configure the compiler (e.g. `{target: '18'}`).
49
+ * This is the same feature as the `babel: {reactCompiler: true}` and `reactCompilerOptions`
50
+ * options in `@sanity/pkg-utils`. Unlike `styledComponents` there's no oxc native port of the
51
+ * React Compiler yet, so `babel-plugin-react-compiler` needs to be installed.
52
+ * @defaultValue false
53
+ */
54
+ reactCompiler?: boolean | ReactCompilerOptions;
55
+ /**
56
+ * Applies the `styled-components` transform (`displayName`, `componentId`, CSS minification, etc)
57
+ * with the same defaults as the `babel: {styledComponents: true}` option in `@sanity/pkg-utils`.
58
+ * Unlike `@sanity/pkg-utils` it doesn't use `babel-plugin-styled-components`, but oxc's native port of it,
59
+ * so there's no need to install babel dependencies.
60
+ * @defaultValue false
61
+ */
62
+ styledComponents?: boolean | StyledComponentsOptions;
10
63
  }
11
64
  /**
12
65
  * @public
13
66
  */
14
- declare function defineConfig(options?: PackageOptions): UserConfig;
15
- export { PackageOptions, defineConfig };
67
+ declare function defineConfig(options?: PackageOptions): Promise<UserConfig>;
68
+ export { PackageOptions, ReactCompilerOptions, StyledComponentsOptions, defineConfig };
16
69
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;AAKA;;UAAiB,cAAA,SAAuB,IAAA,CAAK,UAAA;;;;EAI3C,QAAA,GAAW,UAAA;AAAA;;;;iBAMG,YAAA,CAAa,OAAA,GAAS,cAAA,GAAsB,UAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;AAOA;;;UAAiB,uBAAA;;EAEf,WAAA;;;;;EAKA,mBAAA;;EAEA,GAAA;EAWA;EATA,QAAA;EAmBF;EAjBE,oBAAA;;EAEA,MAAA;EAeyC;EAbzC,yBAAA;EACA,SAAA;;EAEA,IAAA;AAAA;;;;;;;;KAUU,oBAAA,GAAuB,OAAA,CAAQ,aAAA;;;;UAK1B,cAAA,SAAuB,IAAA,CAAK,UAAA;;;AAsBd;EAlB7B,QAAA,GAAW,UAAA;EAwBS;;;;;;;;;EAdpB,aAAA,aAA0B,oBAAA;;;AAc8C;;;;;EANxE,gBAAA,aAA6B,uBAAA;AAAA;;;;iBAMT,YAAA,CAAa,OAAA,GAAS,cAAA,GAAsB,OAAA,CAAQ,UAAA"}
package/dist/index.mjs CHANGED
@@ -2,24 +2,36 @@ import { defineConfig as defineConfig$1 } from "tsdown";
2
2
  /**
3
3
  * @public
4
4
  */
5
- function defineConfig(options = {}) {
6
- let { entry } = options, tsconfig = options.tsconfig ?? "tsconfig.json", platform = options.platform ?? "neutral";
5
+ async function defineConfig(options = {}) {
6
+ let { entry } = options, tsconfig = options.tsconfig ?? "tsconfig.json", platform = options.platform ?? "neutral", reactCompiler = options.reactCompiler ?? !1, styledComponents = options.styledComponents ?? !1, report = { gzip: !1 }, format = options.format ?? "esm", inputOptions = {
7
+ preserveEntrySignatures: "strict",
8
+ experimental: { attachDebugInfo: "none" },
9
+ ...styledComponents !== !1 && { transform: { plugins: { styledComponents: {
10
+ fileName: !1,
11
+ transpileTemplateLiterals: !1,
12
+ pure: !0,
13
+ cssProp: !1,
14
+ ...typeof styledComponents == "object" ? styledComponents : {}
15
+ } } } }
16
+ }, outputOptions = { hoistTransitiveImports: !1 }, exports = {
17
+ enabled: "local-only",
18
+ devExports: !0
19
+ }, plugins = [];
20
+ if (reactCompiler !== !1) {
21
+ let [{ default: pluginBabel }, { reactCompilerPreset }] = await Promise.all([import("@rolldown/plugin-babel"), import("@vitejs/plugin-react")]);
22
+ plugins.push(await pluginBabel({ presets: [reactCompilerPreset(typeof reactCompiler == "object" ? reactCompiler : {})] }));
23
+ }
7
24
  return defineConfig$1({
8
25
  entry,
9
- exports: {
10
- enabled: "local-only",
11
- devExports: !0
12
- },
13
- format: options.format ?? "esm",
26
+ exports,
27
+ format,
14
28
  hash: !1,
15
- inputOptions: {
16
- preserveEntrySignatures: "strict",
17
- experimental: { attachDebugInfo: "none" }
18
- },
19
- outputOptions: { hoistTransitiveImports: !1 },
29
+ inputOptions,
30
+ outputOptions,
20
31
  platform,
32
+ plugins,
21
33
  publint: !0,
22
- report: { gzip: !1 },
34
+ report,
23
35
  tsconfig,
24
36
  minify: {
25
37
  compress: !0,
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["defineTsdownConfig"],"sources":["../src/index.ts"],"sourcesContent":["import {defineConfig as defineTsdownConfig, type UserConfig} from 'tsdown'\n\n/**\n * @public\n */\nexport interface PackageOptions extends Pick<UserConfig, 'tsconfig' | 'entry' | 'format'> {\n /**\n * @defaultValue 'neutral'\n */\n platform?: UserConfig['platform']\n}\n\n/**\n * @public\n */\nexport function defineConfig(options: PackageOptions = {}): UserConfig {\n const {entry} = options\n const tsconfig = options.tsconfig ?? 'tsconfig.json'\n const platform = options.platform ?? 'neutral'\n const report = {gzip: false} as const satisfies UserConfig['report']\n const publint = true\n const hash = false\n const format = options.format ?? 'esm'\n const inputOptions = {\n preserveEntrySignatures: 'strict',\n experimental: {attachDebugInfo: 'none'},\n } as const satisfies UserConfig['inputOptions']\n const outputOptions = {\n hoistTransitiveImports: false,\n } as const satisfies UserConfig['outputOptions']\n const exports = {\n enabled: 'local-only',\n // @TODO use @sanity/parse-package-json to determine if devExports should be `true` or `source`\n devExports: true,\n } as const satisfies UserConfig['exports']\n\n return defineTsdownConfig({\n entry,\n exports,\n format,\n hash,\n inputOptions,\n outputOptions,\n platform,\n publint,\n report,\n tsconfig,\n minify: {compress: true, codegen: false, mangle: false},\n // Rely on tsdown's/rolldown's default tree-shaking (`moduleSideEffects: true`) rather than\n // customizing it. Previously this set the equivalent of `moduleSideEffects: 'no-external'`\n // (with a `.css` exemption), which stripped intentional side-effect-only imports of external\n // packages (e.g. `import 'react-time-ago/locale/en'`) from the output. The default preserves\n // those imports while still honoring `package.json` `sideEffects` fields for bundled modules.\n })\n}\n"],"mappings":";;;;AAeA,SAAgB,aAAa,UAA0B,CAAC,GAAe;CACrE,IAAM,EAAC,UAAS,SACV,WAAW,QAAQ,YAAY,iBAC/B,WAAW,QAAQ,YAAY;CAkBrC,OAAOA,eAAmB;EACxB;EACA,SAAA;GAPA,SAAS;GAET,YAAY;EAKN;EACN,QAjBa,QAAQ,UAAU;EAkB/B,MAAA;EACA,cAAA;GAjBA,yBAAyB;GACzB,cAAc,EAAC,iBAAiB,OAAM;EAgB3B;EACX,eAAA,EAdA,wBAAwB,GAcZ;EACZ;EACA,SAAA;EACA,QAAA,EA1Bc,MAAM,GA0Bf;EACL;EACA,QAAQ;GAAC,UAAU;GAAM,SAAS;GAAO,QAAQ;EAAK;CAMxD,CAAC;AACH"}
1
+ {"version":3,"file":"index.mjs","names":["defineTsdownConfig"],"sources":["../src/index.ts"],"sourcesContent":["import type {PluginOptions as ReactCompilerPluginOptions} from 'babel-plugin-react-compiler'\nimport {defineConfig as defineTsdownConfig, type Rolldown, type UserConfig} from 'tsdown'\n\n/**\n * Options for the `styled-components` transform, the same options as `babel-plugin-styled-components`.\n * @public\n */\nexport interface StyledComponentsOptions {\n /** @defaultValue true */\n displayName?: boolean\n /**\n * @defaultValue []\n * @example [\"\\@xstyled/styled-components\", \"\\@xstyled/styled-components/*\"]\n */\n topLevelImportPaths?: string[]\n /** @defaultValue true */\n ssr?: boolean\n /** @defaultValue false */\n fileName?: boolean\n /** @defaultValue [\"index\"] */\n meaninglessFileNames?: string[]\n /** @defaultValue true */\n minify?: boolean\n /** @defaultValue false */\n transpileTemplateLiterals?: boolean\n namespace?: string\n /** @defaultValue true */\n pure?: boolean\n}\n\n/**\n * Options for the React Compiler, the same options as `babel-plugin-react-compiler`.\n * The typings resolve in userland once `babel-plugin-react-compiler` (an optional peer\n * dependency, required to use `reactCompiler`) is installed, and always match the installed\n * version of the compiler.\n * @public\n */\nexport type ReactCompilerOptions = Partial<ReactCompilerPluginOptions>\n\n/**\n * @public\n */\nexport interface PackageOptions extends Pick<UserConfig, 'tsconfig' | 'entry' | 'format'> {\n /**\n * @defaultValue 'neutral'\n */\n platform?: UserConfig['platform']\n /**\n * Runs `babel-plugin-react-compiler` on the source files before they are bundled, so published\n * components are memoized automatically. Pass `true` to use the defaults, or an options object\n * to configure the compiler (e.g. `{target: '18'}`).\n * This is the same feature as the `babel: {reactCompiler: true}` and `reactCompilerOptions`\n * options in `@sanity/pkg-utils`. Unlike `styledComponents` there's no oxc native port of the\n * React Compiler yet, so `babel-plugin-react-compiler` needs to be installed.\n * @defaultValue false\n */\n reactCompiler?: boolean | ReactCompilerOptions\n /**\n * Applies the `styled-components` transform (`displayName`, `componentId`, CSS minification, etc)\n * with the same defaults as the `babel: {styledComponents: true}` option in `@sanity/pkg-utils`.\n * Unlike `@sanity/pkg-utils` it doesn't use `babel-plugin-styled-components`, but oxc's native port of it,\n * so there's no need to install babel dependencies.\n * @defaultValue false\n */\n styledComponents?: boolean | StyledComponentsOptions\n}\n\n/**\n * @public\n */\nexport async function defineConfig(options: PackageOptions = {}): Promise<UserConfig> {\n const {entry} = options\n const tsconfig = options.tsconfig ?? 'tsconfig.json'\n const platform = options.platform ?? 'neutral'\n const reactCompiler = options.reactCompiler ?? false\n const styledComponents = options.styledComponents ?? false\n const report = {gzip: false} as const satisfies UserConfig['report']\n const publint = true\n const hash = false\n const format = options.format ?? 'esm'\n const inputOptions = {\n preserveEntrySignatures: 'strict',\n experimental: {attachDebugInfo: 'none'},\n ...(styledComponents !== false && {\n transform: {\n plugins: {\n styledComponents: {\n // The same defaults as `babel: {styledComponents: true}` in `@sanity/pkg-utils`:\n // `fileName` is unnecessary, as the way we use styled-components in Sanity is usually by wrapping\n // `@sanity/ui` primitives, not declaring new ones like \"const Button = styled.button``\"\n fileName: false,\n // Native template literals take less space than this transpilation, and unlike\n // `babel-plugin-styled-components`, oxc doesn't add a `@__PURE__` annotation to the\n // transpiled call expression either, so enabling it wouldn't improve tree-shaking\n transpileTemplateLiterals: false,\n // Helps dead code elimination and tree-shaking, although oxc only annotates plain call\n // expressions so far, not tagged template expressions (https://github.com/rollup/rollup/issues/4035)\n pure: true,\n // Disabled, as tsdown tends to be used for npm publishing, while other tooling,\n // like `sanity dev`, `next dev`, etc are used for testing\n cssProp: false,\n ...(typeof styledComponents === 'object' ? styledComponents : {}),\n },\n },\n },\n }),\n } as const satisfies UserConfig['inputOptions']\n const outputOptions = {\n hoistTransitiveImports: false,\n } as const satisfies UserConfig['outputOptions']\n const exports = {\n enabled: 'local-only',\n // @TODO use @sanity/parse-package-json to determine if devExports should be `true` or `source`\n devExports: true,\n } as const satisfies UserConfig['exports']\n\n const plugins: Rolldown.Plugin[] = []\n if (reactCompiler !== false) {\n // Follows the official tsdown recipe for the React Compiler:\n // https://tsdown.dev/recipes/react-support#enabling-react-compiler\n // The plugins are lazy loaded so they're only paid for when the React Compiler is enabled.\n // `babel-plugin-react-compiler` itself is resolved by Babel from the consumer package during\n // the build, which is why it can be an optional peer dependency. Once rolldown ships its rust\n // port of the React Compiler this can be swapped out for an oxc transform, like `styledComponents`.\n const [{default: pluginBabel}, {reactCompilerPreset}] = await Promise.all([\n import('@rolldown/plugin-babel'),\n import('@vitejs/plugin-react'),\n ])\n plugins.push(\n // The plugin types don't match when `@rolldown/plugin-babel` resolves its `rolldown` peer\n // dependency to a different version than the one bundled with `tsdown`, so cast it:\n // https://tsdown.dev/advanced/plugins#rollup-plugins\n // oxlint-disable-next-line no-unsafe-type-assertion\n (await pluginBabel({\n presets: [reactCompilerPreset(typeof reactCompiler === 'object' ? reactCompiler : {})],\n })) as unknown as Rolldown.Plugin,\n )\n }\n\n return defineTsdownConfig({\n entry,\n exports,\n format,\n hash,\n inputOptions,\n outputOptions,\n platform,\n plugins,\n publint,\n report,\n tsconfig,\n minify: {compress: true, codegen: false, mangle: false},\n // Rely on tsdown's/rolldown's default tree-shaking (`moduleSideEffects: true`) rather than\n // customizing it. Previously this set the equivalent of `moduleSideEffects: 'no-external'`\n // (with a `.css` exemption), which stripped intentional side-effect-only imports of external\n // packages (e.g. `import 'react-time-ago/locale/en'`) from the output. The default preserves\n // those imports while still honoring `package.json` `sideEffects` fields for bundled modules.\n })\n}\n"],"mappings":";;;;AAsEA,eAAsB,aAAa,UAA0B,CAAC,GAAwB;CACpF,IAAM,EAAC,UAAS,SACV,WAAW,QAAQ,YAAY,iBAC/B,WAAW,QAAQ,YAAY,WAC/B,gBAAgB,QAAQ,iBAAiB,IACzC,mBAAmB,QAAQ,oBAAoB,IAC/C,SAAS,EAAC,MAAM,GAAK,GAGrB,SAAS,QAAQ,UAAU,OAC3B,eAAe;EACnB,yBAAyB;EACzB,cAAc,EAAC,iBAAiB,OAAM;EACtC,GAAI,qBAAqB,MAAS,EAChC,WAAW,EACT,SAAS,EACP,kBAAkB;GAIhB,UAAU;GAIV,2BAA2B;GAG3B,MAAM;GAGN,SAAS;GACT,GAAI,OAAO,oBAAqB,WAAW,mBAAmB,CAAC;EACjE,EACF,EACF,EACF;CACF,GACM,gBAAgB,EACpB,wBAAwB,GAC1B,GACM,UAAU;EACd,SAAS;EAET,YAAY;CACd,GAEM,UAA6B,CAAC;CACpC,IAAI,kBAAkB,IAAO;EAO3B,IAAM,CAAC,EAAC,SAAS,eAAc,EAAC,yBAAwB,MAAM,QAAQ,IAAI,CACxE,OAAO,2BACP,OAAO,uBACT,CAAC;EACD,QAAQ,KAKL,MAAM,YAAY,EACjB,SAAS,CAAC,oBAAoB,OAAO,iBAAkB,WAAW,gBAAgB,CAAC,CAAC,CAAC,EACvF,CAAC,CACH;CACF;CAEA,OAAOA,eAAmB;EACxB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,QAAQ;GAAC,UAAU;GAAM,SAAS;GAAO,QAAQ;EAAK;CAMxD,CAAC;AACH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/tsdown-config",
3
- "version": "0.7.3",
3
+ "version": "0.9.0",
4
4
  "description": "Shared configuration for tsdown",
5
5
  "keywords": [
6
6
  "sanity-io",
@@ -35,21 +35,32 @@
35
35
  "dist"
36
36
  ],
37
37
  "dependencies": {
38
+ "@babel/core": "^7.29.7",
39
+ "@rolldown/plugin-babel": "^0.2.3",
40
+ "@vitejs/plugin-react": "^6.0.3",
38
41
  "publint": "^0.3.21"
39
42
  },
40
43
  "devDependencies": {
44
+ "@types/babel__core": "^7.20.5",
41
45
  "@typescript/native-preview": "7.0.0-dev.20260421.2",
46
+ "babel-plugin-react-compiler": "^1.0.0",
42
47
  "tinyexec": "^1.2.4",
43
48
  "tsdown": "^0.22.3",
44
49
  "typescript": "6.0.3",
45
50
  "vitest": "^4.1.9",
46
- "@sanity/tsconfig": "^2.1.0",
47
- "@sanity/pkg-utils": "^10.8.2"
51
+ "@sanity/pkg-utils": "^10.9.0",
52
+ "@sanity/tsconfig": "^2.1.0"
48
53
  },
49
54
  "peerDependencies": {
55
+ "babel-plugin-react-compiler": "*",
50
56
  "tsdown": "0.22.x",
51
57
  "typescript": "5.8.x || 5.9.x || 6.0.x"
52
58
  },
59
+ "peerDependenciesMeta": {
60
+ "babel-plugin-react-compiler": {
61
+ "optional": true
62
+ }
63
+ },
53
64
  "engines": {
54
65
  "node": ">=20.19 <22 || >=22.12"
55
66
  },