@sanity/pkg-utils 5.1.2 → 5.1.3
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 +1 -1
- package/dist/_chunks-cjs/build.cjs +36 -16
- package/dist/_chunks-cjs/build.cjs.map +1 -1
- package/dist/_chunks-cjs/check.cjs +4 -4
- package/dist/_chunks-cjs/check.cjs.map +1 -1
- package/dist/_chunks-cjs/consoleSpy.cjs +65 -40
- package/dist/_chunks-cjs/consoleSpy.cjs.map +1 -1
- package/dist/_chunks-cjs/handleError.cjs +3 -3
- package/dist/_chunks-cjs/index.cjs +77 -40
- package/dist/_chunks-cjs/index.cjs.map +1 -1
- package/dist/_chunks-cjs/init.cjs +3 -3
- package/dist/_chunks-cjs/logger.cjs +3 -3
- package/dist/_chunks-cjs/watch.cjs +8 -8
- package/dist/_chunks-cjs/watch.cjs.map +1 -1
- package/dist/cli.cjs +3 -3
- package/dist/index.cjs +2 -2
- package/dist/index.d.cts +21 -10
- package/dist/index.d.ts +21 -10
- package/dist/index.js +164 -84
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/node/build.ts +1 -3
- package/src/node/check.ts +2 -3
- package/src/node/core/contexts/buildContext.ts +4 -5
- package/src/node/core/pkg/parseExports.ts +35 -13
- package/src/node/core/pkg/pkgExt.ts +6 -19
- package/src/node/core/pkg/validateExports.ts +6 -5
- package/src/node/resolveBuildContext.ts +11 -11
- package/src/node/resolveBuildTasks.ts +49 -7
- package/src/node/tasks/dts/buildTypes.ts +5 -1
- package/src/node/tasks/dts/getTargetPaths.ts +6 -2
- package/src/node/tasks/index.ts +3 -2
- package/src/node/tasks/rollup/index.ts +1 -0
- package/src/node/tasks/rollup/resolveRollupConfig.ts +22 -31
- package/src/node/tasks/rollup/rollupLegacyTask.ts +144 -0
- package/src/node/tasks/rollup/rollupTask.ts +2 -2
- package/src/node/tasks/types.ts +16 -5
- package/src/node/watch.ts +3 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/pkg-utils",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.3",
|
|
4
4
|
"description": "Simple utilities for modern npm packages.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity-io",
|
|
@@ -132,7 +132,7 @@
|
|
|
132
132
|
"typescript": "5.4.2",
|
|
133
133
|
"vitest": "^1.4.0",
|
|
134
134
|
"vitest-github-actions-reporter": "^0.11.1",
|
|
135
|
-
"@sanity/pkg-utils": "5.1.
|
|
135
|
+
"@sanity/pkg-utils": "5.1.3"
|
|
136
136
|
},
|
|
137
137
|
"peerDependencies": {
|
|
138
138
|
"typescript": "5.4.2"
|
package/src/node/build.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {loadConfig, loadPkgWithReporting} from './core'
|
|
2
2
|
import {createLogger} from './logger'
|
|
3
3
|
import {resolveBuildContext} from './resolveBuildContext'
|
|
4
4
|
import {resolveBuildTasks} from './resolveBuildTasks'
|
|
@@ -36,14 +36,12 @@ export async function build(options: {
|
|
|
36
36
|
const pkg = await loadPkgWithReporting({cwd, logger, strict})
|
|
37
37
|
|
|
38
38
|
const config = await loadConfig({cwd})
|
|
39
|
-
const extMap = getPkgExtMap({legacyExports: config?.legacyExports ?? false})
|
|
40
39
|
const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
|
|
41
40
|
|
|
42
41
|
const ctx = await resolveBuildContext({
|
|
43
42
|
config,
|
|
44
43
|
cwd,
|
|
45
44
|
emitDeclarationOnly,
|
|
46
|
-
extMap,
|
|
47
45
|
logger,
|
|
48
46
|
pkg,
|
|
49
47
|
strict,
|
package/src/node/check.ts
CHANGED
|
@@ -2,7 +2,7 @@ import esbuild, {type BuildFailure} from 'esbuild'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
|
|
4
4
|
import {createConsoleSpy} from './consoleSpy'
|
|
5
|
-
import {
|
|
5
|
+
import {loadConfig, loadPkgWithReporting} from './core'
|
|
6
6
|
import {fileExists} from './fileExists'
|
|
7
7
|
import {createLogger, type Logger} from './logger'
|
|
8
8
|
import {printPackageTree} from './printPackageTree'
|
|
@@ -19,9 +19,8 @@ export async function check(options: {
|
|
|
19
19
|
|
|
20
20
|
const pkg = await loadPkgWithReporting({cwd, logger, strict})
|
|
21
21
|
const config = await loadConfig({cwd})
|
|
22
|
-
const extMap = getPkgExtMap({legacyExports: config?.legacyExports ?? false})
|
|
23
22
|
const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
|
|
24
|
-
const ctx = await resolveBuildContext({config, cwd,
|
|
23
|
+
const ctx = await resolveBuildContext({config, cwd, logger, pkg, strict, tsconfig})
|
|
25
24
|
|
|
26
25
|
printPackageTree(ctx)
|
|
27
26
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import ts from 'typescript'
|
|
1
|
+
import type ts from 'typescript'
|
|
2
2
|
|
|
3
|
-
import {Logger} from '../../logger'
|
|
4
|
-
import {PkgConfigOptions, PkgExports, PkgRuntime} from '../config'
|
|
5
|
-
import {PackageJSON
|
|
3
|
+
import type {Logger} from '../../logger'
|
|
4
|
+
import type {PkgConfigOptions, PkgExports, PkgRuntime} from '../config'
|
|
5
|
+
import type {PackageJSON} from '../pkg'
|
|
6
6
|
|
|
7
7
|
/** @internal */
|
|
8
8
|
export interface BuildFile {
|
|
@@ -17,7 +17,6 @@ export interface BuildContext {
|
|
|
17
17
|
distPath: string
|
|
18
18
|
emitDeclarationOnly: boolean
|
|
19
19
|
exports: PkgExports | undefined
|
|
20
|
-
extMap: PkgExtMap
|
|
21
20
|
external: string[]
|
|
22
21
|
files: BuildFile[]
|
|
23
22
|
logger: Logger
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
+
import {legacyEnding} from '../../tasks/dts/getTargetPaths'
|
|
1
2
|
import type {PkgExport} from '../config'
|
|
2
3
|
import {isRecord} from '../isRecord'
|
|
3
|
-
import type {PkgExtMap} from './pkgExt'
|
|
4
4
|
import type {PackageJSON} from './types'
|
|
5
5
|
import {validateExports} from './validateExports'
|
|
6
6
|
|
|
7
7
|
/** @internal */
|
|
8
8
|
export function parseExports(options: {
|
|
9
|
-
extMap: PkgExtMap
|
|
10
9
|
pkg: PackageJSON
|
|
11
10
|
strict: boolean
|
|
11
|
+
legacyExports: boolean
|
|
12
12
|
}): (PkgExport & {_path: string})[] {
|
|
13
|
-
const {
|
|
13
|
+
const {pkg, strict, legacyExports} = options
|
|
14
14
|
|
|
15
15
|
const rootExport: PkgExport & {_path: string} = {
|
|
16
16
|
_exported: true,
|
|
@@ -30,17 +30,19 @@ export function parseExports(options: {
|
|
|
30
30
|
|
|
31
31
|
const errors: string[] = []
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
errors.push(
|
|
35
|
-
'package.json: `types` must be declared for the npm listing to show as a TypeScript module.',
|
|
36
|
-
)
|
|
37
|
-
}
|
|
33
|
+
// @TODO validate typesVersions when legacyExports is true
|
|
38
34
|
|
|
39
35
|
if (strict && 'typings' in pkg) {
|
|
40
36
|
errors.push('package.json: `typings` should be `types`')
|
|
41
37
|
}
|
|
42
38
|
|
|
43
39
|
if (pkg.exports) {
|
|
40
|
+
if (strict && !pkg.types && pkg.source?.endsWith('.ts')) {
|
|
41
|
+
errors.push(
|
|
42
|
+
'package.json: `types` must be declared for the npm listing to show as a TypeScript module.',
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
44
46
|
if (strict && !pkg.exports['./package.json']) {
|
|
45
47
|
errors.push('package.json: `exports["./package.json"] must be declared.')
|
|
46
48
|
}
|
|
@@ -66,10 +68,30 @@ export function parseExports(options: {
|
|
|
66
68
|
)
|
|
67
69
|
}
|
|
68
70
|
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
if (legacyExports) {
|
|
72
|
+
const indexLegacyExport = (
|
|
73
|
+
exportEntry.browser?.import ||
|
|
74
|
+
exportEntry.import ||
|
|
75
|
+
exportEntry.browser?.require ||
|
|
76
|
+
exportEntry.require ||
|
|
77
|
+
''
|
|
78
|
+
).replace(/(\.esm)?\.[mc]?js$/, legacyEnding)
|
|
79
|
+
|
|
80
|
+
if (indexLegacyExport !== rootExport.import) {
|
|
81
|
+
errors.push(
|
|
82
|
+
`package.json: "module" should be "${indexLegacyExport}" when "legacyExports" is true`,
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
if (
|
|
87
|
+
exportEntry.import &&
|
|
88
|
+
rootExport.import &&
|
|
89
|
+
exportEntry.import !== rootExport.import
|
|
90
|
+
) {
|
|
91
|
+
errors.push(
|
|
92
|
+
'package.json: mismatch between "module" and "exports.import" These must be equal.',
|
|
93
|
+
)
|
|
94
|
+
}
|
|
73
95
|
}
|
|
74
96
|
|
|
75
97
|
if (exportEntry.source && rootExport.source && exportEntry.source !== rootExport.source) {
|
|
@@ -96,7 +118,7 @@ export function parseExports(options: {
|
|
|
96
118
|
|
|
97
119
|
const _exports = [rootExport, ...extraExports]
|
|
98
120
|
|
|
99
|
-
errors.push(...validateExports(_exports, {
|
|
121
|
+
errors.push(...validateExports(_exports, {pkg}))
|
|
100
122
|
|
|
101
123
|
if (errors.length) {
|
|
102
124
|
throw new Error('\n- ' + errors.join('\n- '))
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import {legacyEnding} from '../../tasks/dts/getTargetPaths'
|
|
2
|
+
|
|
1
3
|
/** @internal */
|
|
2
4
|
export interface PkgExtMap {
|
|
3
5
|
commonjs: {commonjs: string; esm: string}
|
|
4
6
|
module: {commonjs: string; esm: string}
|
|
7
|
+
legacy: string
|
|
5
8
|
}
|
|
6
9
|
|
|
7
10
|
/** @internal */
|
|
8
|
-
export const
|
|
11
|
+
export const pkgExtMap: PkgExtMap = {
|
|
9
12
|
// pkg.type: "commonjs"
|
|
10
13
|
commonjs: {
|
|
11
14
|
commonjs: '.js',
|
|
@@ -17,22 +20,6 @@ export const DEFAULT_PKG_EXT_MAP: PkgExtMap = {
|
|
|
17
20
|
commonjs: '.cjs',
|
|
18
21
|
esm: '.js',
|
|
19
22
|
},
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
/** @internal */
|
|
23
|
-
export function getPkgExtMap(options: {legacyExports: boolean}): PkgExtMap {
|
|
24
|
-
const {legacyExports} = options
|
|
25
|
-
|
|
26
|
-
const ret = {...DEFAULT_PKG_EXT_MAP}
|
|
27
|
-
|
|
28
|
-
// Fall back to legacy file extensions for package.json with `"type": "commonjs"`
|
|
29
|
-
// NOTE: Not supported by Node 14+ and will be removed in a future version
|
|
30
|
-
if (legacyExports) {
|
|
31
|
-
ret['commonjs'] = {
|
|
32
|
-
commonjs: '.js',
|
|
33
|
-
esm: '.esm.js',
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return ret
|
|
23
|
+
// package.config.legacyExports: true
|
|
24
|
+
legacy: legacyEnding,
|
|
38
25
|
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {PkgExport} from '../config'
|
|
2
|
-
import {
|
|
3
|
-
import {PackageJSON} from './types'
|
|
1
|
+
import type {PkgExport} from '../config'
|
|
2
|
+
import {pkgExtMap as extMap} from './pkgExt'
|
|
3
|
+
import type {PackageJSON} from './types'
|
|
4
4
|
|
|
5
5
|
export function validateExports(
|
|
6
6
|
_exports: (PkgExport & {_path: string})[],
|
|
7
|
-
options: {
|
|
7
|
+
options: {pkg: PackageJSON},
|
|
8
8
|
): string[] {
|
|
9
|
-
const {
|
|
9
|
+
const {pkg} = options
|
|
10
10
|
const ext = extMap[pkg.type || 'commonjs']
|
|
11
11
|
|
|
12
12
|
const errors: string[] = []
|
|
13
13
|
|
|
14
|
+
// @TODO validate that no exports declare the legacy exports
|
|
14
15
|
for (const exp of _exports) {
|
|
15
16
|
if (exp.require && !exp.require.endsWith(ext.commonjs)) {
|
|
16
17
|
errors.push(
|
|
@@ -2,19 +2,18 @@ import browserslistToEsbuild from 'browserslist-to-esbuild'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
BuildContext,
|
|
5
|
+
type BuildContext,
|
|
6
6
|
DEFAULT_BROWSERSLIST_QUERY,
|
|
7
7
|
loadTSConfig,
|
|
8
|
-
PackageJSON,
|
|
8
|
+
type PackageJSON,
|
|
9
9
|
parseExports,
|
|
10
|
-
PkgConfigOptions,
|
|
11
|
-
PkgExports,
|
|
12
|
-
|
|
13
|
-
PkgRuntime,
|
|
10
|
+
type PkgConfigOptions,
|
|
11
|
+
type PkgExports,
|
|
12
|
+
type PkgRuntime,
|
|
14
13
|
resolveConfigProperty,
|
|
15
14
|
} from './core'
|
|
16
15
|
import {findCommonDirPath, pathContains} from './core/findCommonPath'
|
|
17
|
-
import {Logger} from './logger'
|
|
16
|
+
import type {Logger} from './logger'
|
|
18
17
|
import {resolveBrowserTarget} from './resolveBrowserTarget'
|
|
19
18
|
import {resolveNodeTarget} from './resolveNodeTarget'
|
|
20
19
|
|
|
@@ -22,7 +21,6 @@ export async function resolveBuildContext(options: {
|
|
|
22
21
|
config?: PkgConfigOptions
|
|
23
22
|
cwd: string
|
|
24
23
|
emitDeclarationOnly?: boolean
|
|
25
|
-
extMap: PkgExtMap
|
|
26
24
|
logger: Logger
|
|
27
25
|
pkg: PackageJSON
|
|
28
26
|
strict: boolean
|
|
@@ -32,7 +30,6 @@ export async function resolveBuildContext(options: {
|
|
|
32
30
|
config,
|
|
33
31
|
cwd,
|
|
34
32
|
emitDeclarationOnly = false,
|
|
35
|
-
extMap,
|
|
36
33
|
logger,
|
|
37
34
|
pkg,
|
|
38
35
|
strict,
|
|
@@ -76,7 +73,11 @@ export async function resolveBuildContext(options: {
|
|
|
76
73
|
'node': nodeTarget,
|
|
77
74
|
}
|
|
78
75
|
|
|
79
|
-
const parsedExports = parseExports({
|
|
76
|
+
const parsedExports = parseExports({
|
|
77
|
+
pkg,
|
|
78
|
+
strict,
|
|
79
|
+
legacyExports: config?.legacyExports ?? false,
|
|
80
|
+
}).reduce<PkgExports>((acc, x) => {
|
|
80
81
|
const {_path: exportPath, ...exportEntry} = x
|
|
81
82
|
|
|
82
83
|
return {...acc, [exportPath]: exportEntry}
|
|
@@ -146,7 +147,6 @@ export async function resolveBuildContext(options: {
|
|
|
146
147
|
emitDeclarationOnly,
|
|
147
148
|
exports,
|
|
148
149
|
external,
|
|
149
|
-
extMap,
|
|
150
150
|
files: [],
|
|
151
151
|
logger,
|
|
152
152
|
pkg,
|
|
@@ -2,8 +2,8 @@ import fs from 'fs'
|
|
|
2
2
|
import path from 'path'
|
|
3
3
|
|
|
4
4
|
import type {BuildContext, PkgExport, PkgFormat, PkgRuntime} from './core'
|
|
5
|
-
import type {BuildTask, DtsTask, RollupTask, RollupTaskEntry} from './tasks'
|
|
6
|
-
import {getTargetPaths} from './tasks/dts/getTargetPaths'
|
|
5
|
+
import type {BuildTask, DtsTask, RollupLegacyTask, RollupTask, RollupTaskEntry} from './tasks'
|
|
6
|
+
import {fileEnding, getTargetPaths, legacyEnding} from './tasks/dts/getTargetPaths'
|
|
7
7
|
|
|
8
8
|
/** @internal */
|
|
9
9
|
export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
|
|
@@ -23,6 +23,7 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
const rollupTasks: Record<string, RollupTask> = {}
|
|
26
|
+
const rollupLegacyTasks: Record<string, RollupLegacyTask> = {}
|
|
26
27
|
|
|
27
28
|
function addRollupTaskEntry(format: PkgFormat, runtime: PkgRuntime, entry: RollupTaskEntry) {
|
|
28
29
|
const buildId = `${format}:${runtime}`
|
|
@@ -41,6 +42,24 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
45
|
+
function addRollupLegacyTaskEntry(runtime: PkgRuntime, entry: RollupTaskEntry) {
|
|
46
|
+
const buildId = `esm:${runtime}`
|
|
47
|
+
|
|
48
|
+
if (rollupLegacyTasks[buildId]) {
|
|
49
|
+
rollupLegacyTasks[buildId].entries.push(entry)
|
|
50
|
+
} else {
|
|
51
|
+
rollupLegacyTasks[buildId] = {
|
|
52
|
+
type: 'build:legacy',
|
|
53
|
+
buildId,
|
|
54
|
+
entries: [entry],
|
|
55
|
+
runtime,
|
|
56
|
+
format: 'esm',
|
|
57
|
+
// @TODO set a different target here that is compatible with legacy bundlers and testing tools like brownfield jest
|
|
58
|
+
target: target[runtime],
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
44
63
|
// Parse `dts` tasks
|
|
45
64
|
for (const exp of exports) {
|
|
46
65
|
const importId = path.join(pkg.name, exp._path)
|
|
@@ -149,6 +168,25 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
|
|
|
149
168
|
})
|
|
150
169
|
}
|
|
151
170
|
|
|
171
|
+
// Create rollup bundles that legacy export files will re-export
|
|
172
|
+
if (config?.legacyExports) {
|
|
173
|
+
for (const exp of exports) {
|
|
174
|
+
const runtime = exp.browser?.import || exp.browser?.require ? 'browser' : ctx.runtime
|
|
175
|
+
const output = exp.browser?.import || exp.import || exp.browser?.require || exp.require
|
|
176
|
+
|
|
177
|
+
if (!output) continue
|
|
178
|
+
|
|
179
|
+
// Change the suffix to what the legacy exports will use
|
|
180
|
+
const legacyOutput = output.replace(fileEnding, legacyEnding)
|
|
181
|
+
|
|
182
|
+
addRollupLegacyTaskEntry(runtime, {
|
|
183
|
+
path: exp._path,
|
|
184
|
+
source: exp.browser?.source || exp.source,
|
|
185
|
+
output: legacyOutput,
|
|
186
|
+
})
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
152
190
|
for (const bundle of bundles) {
|
|
153
191
|
const idx = bundles.indexOf(bundle)
|
|
154
192
|
|
|
@@ -169,16 +207,20 @@ export function resolveBuildTasks(ctx: BuildContext): BuildTask[] {
|
|
|
169
207
|
}
|
|
170
208
|
}
|
|
171
209
|
|
|
172
|
-
tasks.push(...Object.values(rollupTasks))
|
|
210
|
+
tasks.push(...Object.values(rollupTasks), ...Object.values(rollupLegacyTasks))
|
|
173
211
|
|
|
174
212
|
// Write legacy exports files
|
|
175
213
|
if (config?.legacyExports) {
|
|
176
214
|
for (const exp of exports) {
|
|
177
215
|
if (exp._exported && exp._path !== '.') {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
216
|
+
const output = (
|
|
217
|
+
exp.browser?.import ||
|
|
218
|
+
exp.import ||
|
|
219
|
+
exp.browser?.require ||
|
|
220
|
+
exp.require ||
|
|
221
|
+
''
|
|
222
|
+
).replace(fileEnding, legacyEnding)
|
|
223
|
+
const relativeTargetPath = output.replace(/\.[^/.]+$/, '')
|
|
182
224
|
|
|
183
225
|
if (relativeTargetPath) {
|
|
184
226
|
fs.writeFileSync(
|
|
@@ -40,7 +40,11 @@ export async function buildTypes(options: {
|
|
|
40
40
|
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics)
|
|
41
41
|
|
|
42
42
|
for (const diagnostic of allDiagnostics) {
|
|
43
|
-
|
|
43
|
+
// @TODO match the import with known export paths
|
|
44
|
+
// Ignore TS2307 errors as they'll be captured elsewhere if they're actually a problem
|
|
45
|
+
if (diagnostic.code !== 2307) {
|
|
46
|
+
printDiagnostic({cwd, logger, diagnostic})
|
|
47
|
+
}
|
|
44
48
|
}
|
|
45
49
|
|
|
46
50
|
if (emitResult.emitSkipped) {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type {PackageJSON, PkgBundle, PkgExport} from '../../core'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const
|
|
3
|
+
/** @internal */
|
|
4
|
+
export const fileEnding = /\.[mc]?js$/
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const dtsEnding = '.d.ts'
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const legacyEnding = '.esm.js'
|
|
5
9
|
const mtsEnding = '.d.mts'
|
|
6
10
|
const ctsEnding = '.d.cts'
|
|
7
11
|
|
package/src/node/tasks/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {dtsTask, dtsWatchTask} from './dts'
|
|
2
|
-
import {rollupTask, rollupWatchTask} from './rollup'
|
|
3
|
-
import {BuildTaskHandlers, WatchTaskHandlers} from './types'
|
|
2
|
+
import {rollupLegacyTask, rollupTask, rollupWatchTask} from './rollup'
|
|
3
|
+
import type {BuildTaskHandlers, WatchTaskHandlers} from './types'
|
|
4
4
|
|
|
5
5
|
export * from './dts'
|
|
6
6
|
export * from './rollup'
|
|
@@ -10,6 +10,7 @@ export * from './types'
|
|
|
10
10
|
export const buildTaskHandlers: BuildTaskHandlers = {
|
|
11
11
|
'build:dts': dtsTask,
|
|
12
12
|
'build:js': rollupTask,
|
|
13
|
+
'build:legacy': rollupLegacyTask,
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
/** @internal */
|
|
@@ -9,8 +9,9 @@ import path from 'path'
|
|
|
9
9
|
import type {InputOptions, OutputOptions, Plugin} from 'rollup'
|
|
10
10
|
import esbuild from 'rollup-plugin-esbuild'
|
|
11
11
|
|
|
12
|
+
import {pkgExtMap as extMap} from '../../../node/core/pkg/pkgExt'
|
|
12
13
|
import {type BuildContext, resolveConfigProperty} from '../../core'
|
|
13
|
-
import type {RollupTask, RollupWatchTask} from '../types'
|
|
14
|
+
import type {RollupLegacyTask, RollupTask, RollupWatchTask} from '../types'
|
|
14
15
|
|
|
15
16
|
export interface RollupConfig {
|
|
16
17
|
inputOptions: InputOptions
|
|
@@ -20,11 +21,12 @@ export interface RollupConfig {
|
|
|
20
21
|
/** @internal */
|
|
21
22
|
export function resolveRollupConfig(
|
|
22
23
|
ctx: BuildContext,
|
|
23
|
-
buildTask: RollupTask | RollupWatchTask,
|
|
24
|
+
buildTask: RollupTask | RollupLegacyTask | RollupWatchTask,
|
|
24
25
|
): RollupConfig {
|
|
25
26
|
const {format, runtime, target} = buildTask
|
|
26
|
-
const {config, cwd, exports: _exports,
|
|
27
|
-
const
|
|
27
|
+
const {config, cwd, exports: _exports, external, distPath, logger, pkg, ts} = ctx
|
|
28
|
+
const isLegacyExports = buildTask.type === 'build:legacy'
|
|
29
|
+
const outputExt = isLegacyExports ? extMap.legacy : extMap[pkg.type || 'commonjs'][format]
|
|
28
30
|
const minify = config?.minify ?? false
|
|
29
31
|
const outDir = path.relative(cwd, distPath)
|
|
30
32
|
|
|
@@ -83,11 +85,9 @@ export function resolveRollupConfig(
|
|
|
83
85
|
entries: {...pathAliases},
|
|
84
86
|
}),
|
|
85
87
|
nodeResolve({
|
|
86
|
-
// mainFields: ['module', 'jsnext', 'main'],
|
|
87
88
|
browser: runtime === 'browser',
|
|
88
|
-
// exportConditions: [runtime === 'node' ? 'node' : 'browser'],
|
|
89
89
|
extensions: ['.cjs', '.mjs', '.js', '.jsx', '.json', '.node'],
|
|
90
|
-
preferBuiltins: true,
|
|
90
|
+
preferBuiltins: true,
|
|
91
91
|
}),
|
|
92
92
|
commonjs(),
|
|
93
93
|
json(),
|
|
@@ -107,7 +107,7 @@ export function resolveRollupConfig(
|
|
|
107
107
|
Array.isArray(config?.babel?.plugins) &&
|
|
108
108
|
getBabelOutputPlugin({
|
|
109
109
|
babelrc: false,
|
|
110
|
-
plugins: config
|
|
110
|
+
plugins: config.babel.plugins,
|
|
111
111
|
}),
|
|
112
112
|
minify &&
|
|
113
113
|
terser({
|
|
@@ -115,10 +115,10 @@ export function resolveRollupConfig(
|
|
|
115
115
|
output: {
|
|
116
116
|
comments: (_node, comment) => {
|
|
117
117
|
const text = comment.value
|
|
118
|
-
const
|
|
118
|
+
const cType = comment.type
|
|
119
119
|
|
|
120
120
|
// Check if this is a multiline comment
|
|
121
|
-
if (
|
|
121
|
+
if (cType === 'comment2') {
|
|
122
122
|
// Keep licensing comments
|
|
123
123
|
return /@preserve|@license|@cc_on/i.test(text)
|
|
124
124
|
}
|
|
@@ -135,6 +135,15 @@ export function resolveRollupConfig(
|
|
|
135
135
|
? defaultPlugins.concat(userPlugins)
|
|
136
136
|
: resolveConfigProperty(config?.rollup?.plugins, defaultPlugins)
|
|
137
137
|
|
|
138
|
+
const hashChunkFileNames = config?.rollup?.hashChunkFileNames ?? false
|
|
139
|
+
const chunksFolder = isLegacyExports
|
|
140
|
+
? '_legacy'
|
|
141
|
+
: hashChunkFileNames
|
|
142
|
+
? '_chunks'
|
|
143
|
+
: '_chunks-[format]'
|
|
144
|
+
const chunkFileNames = `${chunksFolder}/${hashChunkFileNames ? '[name]-[hash]' : '[name]'}${outputExt}`
|
|
145
|
+
const entryFileNames = isLegacyExports ? '[name].js' : `[name]${outputExt}`
|
|
146
|
+
|
|
138
147
|
return {
|
|
139
148
|
inputOptions: {
|
|
140
149
|
context: cwd,
|
|
@@ -192,31 +201,13 @@ export function resolveRollupConfig(
|
|
|
192
201
|
experimentalLogSideEffects: config?.rollup?.experimentalLogSideEffects,
|
|
193
202
|
},
|
|
194
203
|
outputOptions: {
|
|
195
|
-
chunkFileNames
|
|
196
|
-
const parts = outputExt.split('.')
|
|
197
|
-
const prefix = config?.rollup?.hashChunkFileNames ? '[name]-[hash]' : '[name]'
|
|
198
|
-
const folder = config?.rollup?.hashChunkFileNames ? '_chunks' : '_chunks-[format]'
|
|
199
|
-
|
|
200
|
-
if (parts.length === 3) {
|
|
201
|
-
return `${folder}/${prefix}.${parts[2]}`
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return `${folder}/${prefix}${outputExt}`
|
|
205
|
-
},
|
|
204
|
+
chunkFileNames,
|
|
206
205
|
compact: minify,
|
|
207
206
|
dir: outDir,
|
|
208
|
-
entryFileNames
|
|
209
|
-
const parts = outputExt.split('.')
|
|
210
|
-
|
|
211
|
-
if (parts.length === 3) {
|
|
212
|
-
return `[name].${parts[2]}`
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
return `[name]${outputExt}`
|
|
216
|
-
},
|
|
207
|
+
entryFileNames,
|
|
217
208
|
esModule: true,
|
|
218
209
|
format,
|
|
219
|
-
interop: '
|
|
210
|
+
interop: 'auto',
|
|
220
211
|
sourcemap: config?.sourcemap ?? true,
|
|
221
212
|
hoistTransitiveImports: false,
|
|
222
213
|
...config?.rollup?.output,
|