File tree Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Expand file tree Collapse file tree 3 files changed +33
-2
lines changed Original file line number Diff line number Diff line change
1
+ import coerce from 'semver/functions/coerce.js'
2
+ import satisfies from 'semver/functions/satisfies.js'
3
+ import type { ResolvedOptions } from '../options'
4
+
5
+ /**
6
+ * If the config includes the `cjs` format and
7
+ * one of its target >= node 23.0.0 / 22.12.0,
8
+ * warn the user about the deprecation of CommonJS.
9
+ */
10
+ export function warnLegacyCJS ( config : ResolvedOptions ) : void {
11
+ if ( ! config . format . includes ( 'cjs' ) || ! config . target ) {
12
+ return
13
+ }
14
+
15
+ const legacy = config . target . some ( ( t ) => {
16
+ const version = coerce ( t . split ( 'node' ) [ 1 ] )
17
+ return version && satisfies ( version , '>=23.0.0 || >=22.12.0' )
18
+ } )
19
+
20
+ if ( legacy ) {
21
+ config . logger . warnOnce (
22
+ 'We recommend using the ESM format instead of CommonJS.\n' +
23
+ 'The ESM format is compatible with modern platforms and runtimes, ' +
24
+ 'and most new libraries are now distributed only in ESM format.\n' +
25
+ 'Learn more at https://nodejs.org/en/learn/modules/publishing-a-package#how-did-we-get-here' ,
26
+ )
27
+ }
28
+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
11
11
import { exec } from 'tinyexec'
12
12
import treeKill from 'tree-kill'
13
13
import { attw } from './features/attw'
14
+ import { warnLegacyCJS } from './features/cjs'
14
15
import { cleanOutDir } from './features/clean'
15
16
import { copy } from './features/copy'
16
17
import { writeExports , type TsdownChunks } from './features/exports'
@@ -103,6 +104,8 @@ export async function buildSingle(
103
104
104
105
const { hooks, context } = await createHooks ( config )
105
106
107
+ warnLegacyCJS ( config )
108
+
106
109
await rebuild ( true )
107
110
if ( watch ) {
108
111
return ( ) => rebuild ( )
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ function format(msgs: any[]) {
30
30
return msgs . filter ( ( arg ) => arg !== undefined && arg !== false ) . join ( ' ' )
31
31
}
32
32
33
+ const warnedMessages = new Set < string > ( )
34
+
33
35
export function createLogger (
34
36
level : LogLevel = 'info' ,
35
37
{ customLogger, console = globalThis . console } : LoggerOptions = { } ,
@@ -46,8 +48,6 @@ export function createLogger(
46
48
console [ method ] ( msg )
47
49
}
48
50
49
- const warnedMessages = new Set < string > ( )
50
-
51
51
const logger : Logger = {
52
52
level,
53
53
You can’t perform that action at this time.
0 commit comments