@sanity/pkg-utils 1.13.0 → 1.15.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.
Files changed (46) hide show
  1. package/dist/_chunks/{_resolveBuildContext-ef196a3e.js → _resolveBuildContext-0e601cff.js} +3 -1
  2. package/dist/_chunks/_resolveBuildContext-0e601cff.js.map +1 -0
  3. package/dist/_chunks/{build-36a16057.js → build-3762711f.js} +7 -4
  4. package/dist/_chunks/build-3762711f.js.map +1 -0
  5. package/dist/_chunks/{buildAction-d4c2a5d2.js → buildAction-b4e23900.js} +5 -4
  6. package/dist/_chunks/buildAction-b4e23900.js.map +1 -0
  7. package/dist/_chunks/{check-7d728336.js → check-e60e8979.js} +6 -4
  8. package/dist/_chunks/check-e60e8979.js.map +1 -0
  9. package/dist/_chunks/{checkAction-2342707c.js → checkAction-6de69604.js} +3 -3
  10. package/dist/_chunks/{checkAction-2342707c.js.map → checkAction-6de69604.js.map} +1 -1
  11. package/dist/_chunks/{index-340fbb6a.js → index-00a76fd5.js} +101 -76
  12. package/dist/_chunks/index-00a76fd5.js.map +1 -0
  13. package/dist/_chunks/{watch-41c2ea71.js → watch-69b1e41c.js} +7 -4
  14. package/dist/_chunks/watch-69b1e41c.js.map +1 -0
  15. package/dist/_chunks/{watchAction-fea46c01.js → watchAction-72377ec8.js} +5 -4
  16. package/dist/_chunks/watchAction-72377ec8.js.map +1 -0
  17. package/dist/cli.js +6 -6
  18. package/dist/cli.js.map +1 -1
  19. package/dist/index.d.ts +48 -4
  20. package/dist/index.esm.js +121 -87
  21. package/dist/index.esm.js.map +1 -1
  22. package/dist/index.js +6 -5
  23. package/dist/index.js.map +1 -1
  24. package/package.json +1 -1
  25. package/src/cli/buildAction.ts +2 -1
  26. package/src/cli/index.ts +2 -0
  27. package/src/cli/watchAction.ts +2 -1
  28. package/src/node/_core/config/types.ts +12 -1
  29. package/src/node/_core/contexts/buildContext.ts +1 -0
  30. package/src/node/_resolveBuildContext.ts +3 -1
  31. package/src/node/_tasks/dts/_buildTypes.ts +5 -2
  32. package/src/node/_tasks/dts/_doExtract.ts +3 -2
  33. package/src/node/_tasks/dts/_extractTypes.ts +16 -85
  34. package/src/node/_tasks/dts/_getExtractMessagesConfig.ts +87 -0
  35. package/src/node/_tasks/dts/index.ts +1 -0
  36. package/src/node/_tasks/rollup/_resolveRollupConfig.ts +91 -77
  37. package/src/node/build.ts +27 -5
  38. package/src/node/check.ts +3 -2
  39. package/src/node/watch.ts +8 -3
  40. package/dist/_chunks/_resolveBuildContext-ef196a3e.js.map +0 -1
  41. package/dist/_chunks/build-36a16057.js.map +0 -1
  42. package/dist/_chunks/buildAction-d4c2a5d2.js.map +0 -1
  43. package/dist/_chunks/check-7d728336.js.map +0 -1
  44. package/dist/_chunks/index-340fbb6a.js.map +0 -1
  45. package/dist/_chunks/watch-41c2ea71.js.map +0 -1
  46. package/dist/_chunks/watchAction-fea46c01.js.map +0 -1
package/dist/index.js CHANGED
@@ -4,15 +4,15 @@ Object.defineProperty(exports, '__esModule', {
4
4
  value: true
5
5
  });
6
6
 
7
- var _resolveBuildContext = require('./_chunks/_resolveBuildContext-ef196a3e.js');
7
+ var _resolveBuildContext = require('./_chunks/_resolveBuildContext-0e601cff.js');
8
8
 
9
- var build = require('./_chunks/build-36a16057.js');
9
+ var build = require('./_chunks/build-3762711f.js');
10
10
 
11
- var index = require('./_chunks/index-340fbb6a.js');
11
+ var index = require('./_chunks/index-00a76fd5.js');
12
12
 
13
- var check = require('./_chunks/check-7d728336.js');
13
+ var check = require('./_chunks/check-e60e8979.js');
14
14
 
15
- var watch = require('./_chunks/watch-41c2ea71.js');
15
+ var watch = require('./_chunks/watch-69b1e41c.js');
16
16
 
17
17
  require('path');
18
18
 
@@ -98,6 +98,7 @@ exports.build = build.build;
98
98
  exports._buildTaskHandlers = index._buildTaskHandlers;
99
99
  exports._dtsTask = index._dtsTask;
100
100
  exports._dtsWatchTask = index._dtsWatchTask;
101
+ exports._getExtractMessagesConfig = index._getExtractMessagesConfig;
101
102
  exports._rollupTask = index._rollupTask;
102
103
  exports._rollupWatchTask = index._rollupWatchTask;
103
104
  exports._watchTaskHandlers = index._watchTaskHandlers;
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/node/_core/config/defineConfig.ts"],"sourcesContent":["import {PkgConfigOptions} from './types'\n\n/** @public */\nexport function defineConfig(configOptions: PkgConfigOptions): PkgConfigOptions {\n return configOptions\n}\n"],"names":["defineConfig","configOptions"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,SAASA,YAAT,CAAsBC,aAAtB,EAAqC;EAC1C,OAAOA,aAAP;AACF;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/node/_core/config/defineConfig.ts"],"sourcesContent":["import {PkgConfigOptions} from './types'\n\n/** @public */\nexport function defineConfig(configOptions: PkgConfigOptions): PkgConfigOptions {\n return configOptions\n}\n"],"names":["defineConfig","configOptions"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAO,SAASA,YAAT,CAAsBC,aAAtB,EAAqC;EAC1C,OAAOA,aAAP;AACF;;;;;;;;;;;;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/pkg-utils",
3
- "version": "1.13.0",
3
+ "version": "1.15.0",
4
4
  "author": "Sanity.io <hello@sanity.io>",
5
5
  "license": "MIT",
6
6
  "description": "Simple utilities for modern npm packages.",
@@ -1,10 +1,11 @@
1
1
  import {build} from '../node/build'
2
2
  import {_handleError} from './_handleError'
3
3
 
4
- export async function buildAction(options: {tsconfig?: string}): Promise<void> {
4
+ export async function buildAction(options: {strict?: boolean; tsconfig?: string}): Promise<void> {
5
5
  try {
6
6
  await build({
7
7
  cwd: process.cwd(),
8
+ strict: options.strict,
8
9
  tsconfig: options.tsconfig,
9
10
  })
10
11
  } catch (err) {
package/src/cli/index.ts CHANGED
@@ -16,6 +16,7 @@ cli
16
16
 
17
17
  cli
18
18
  .command('build', 'Build package')
19
+ .option('--strict', 'Strict mode')
19
20
  .option('--tsconfig [tsconfig]', '[string] tsconfig.json')
20
21
  .action(async (options) => {
21
22
  const {buildAction} = await import('./buildAction')
@@ -25,6 +26,7 @@ cli
25
26
 
26
27
  cli
27
28
  .command('watch', 'Watch package')
29
+ .option('--strict', 'Strict mode')
28
30
  .option('--tsconfig [tsconfig]', '[string] tsconfig.json')
29
31
  .action(async (options) => {
30
32
  const {watchAction} = await import('./watchAction')
@@ -1,10 +1,11 @@
1
1
  import {watch} from '../node/watch'
2
2
  import {_handleError} from './_handleError'
3
3
 
4
- export async function watchAction(options: {tsconfig?: string}): Promise<void> {
4
+ export async function watchAction(options: {strict?: boolean; tsconfig?: string}): Promise<void> {
5
5
  try {
6
6
  await watch({
7
7
  cwd: process.cwd(),
8
+ strict: options.strict,
8
9
  tsconfig: options.tsconfig,
9
10
  })
10
11
  } catch (err) {
@@ -56,15 +56,25 @@ export interface PkgExports {
56
56
  /** @public */
57
57
  export type PkgRuleLevel = 'error' | 'warn' | 'info' | 'off'
58
58
 
59
+ /** @public */
60
+ export interface TSDocCustomTag {
61
+ name: string
62
+ syntaxKind: 'block' | 'modifier'
63
+ allowMultiple?: boolean
64
+ }
65
+
59
66
  /** @public */
60
67
  export interface PkgConfigOptions {
61
68
  bundles?: PkgBundle[]
69
+ /** @alpha */
70
+ define?: Record<string, string | number | boolean | undefined | null>
62
71
  /**
63
72
  * Directory of distributed & bundled files.
64
73
  */
65
74
  dist?: string
66
75
  exports?: PkgConfigProperty<PkgExports>
67
76
  extract?: {
77
+ customTags?: TSDocCustomTag[]
68
78
  rules?: {
69
79
  'ae-forgotten-export'?: PkgRuleLevel
70
80
  'ae-incompatible-release-tags'?: PkgRuleLevel
@@ -102,7 +112,7 @@ export interface PkgConfigOptions {
102
112
  minify?: boolean
103
113
  /** @alpha */
104
114
  rollup?: {
105
- plugins?: RollupPlugin[]
115
+ plugins?: PkgConfigProperty<RollupPlugin[]>
106
116
  }
107
117
  /**
108
118
  * Default runtime of package exports
@@ -113,4 +123,5 @@ export interface PkgConfigOptions {
113
123
  * Directory of source files.
114
124
  */
115
125
  src?: string
126
+ tsconfig?: string
116
127
  }
@@ -25,6 +25,7 @@ export interface _BuildContext {
25
25
  }
26
26
  pkg: _PackageJSON
27
27
  runtime: PkgRuntime
28
+ strict: boolean
28
29
  target: Record<PkgRuntime, string[]>
29
30
  ts: {
30
31
  config?: ts.ParsedCommandLine
@@ -42,9 +42,10 @@ export async function _resolveBuildContext(options: {
42
42
  config?: PkgConfigOptions
43
43
  cwd: string
44
44
  pkg: _PackageJSON
45
+ strict: boolean
45
46
  tsconfig: string
46
47
  }): Promise<_BuildContext> {
47
- const {config, cwd, pkg, tsconfig: tsconfigPath} = options
48
+ const {config, cwd, pkg, strict, tsconfig: tsconfigPath} = options
48
49
  const logger = _createLogger()
49
50
  const tsconfig = await _loadTSConfig({cwd, tsconfigPath})
50
51
  const targetVersions = _resolveBrowserslistVersions(pkg.browserslist || _DEFAULTS.browserslist)
@@ -134,6 +135,7 @@ export async function _resolveBuildContext(options: {
134
135
  logger,
135
136
  pkg,
136
137
  runtime: config?.runtime ?? '*',
138
+ strict,
137
139
  target,
138
140
  ts: {
139
141
  config: tsconfig,
@@ -13,8 +13,9 @@ export async function _buildTypes(options: {
13
13
  cwd: string
14
14
  outDir: string
15
15
  tsconfig: ts.ParsedCommandLine
16
+ strict: boolean
16
17
  }): Promise<void> {
17
- const {outDir, tsconfig} = options
18
+ const {outDir, tsconfig, strict = false} = options
18
19
 
19
20
  const compilerOptions: ts.CompilerOptions = {
20
21
  ...tsconfig.options,
@@ -49,7 +50,9 @@ export async function _buildTypes(options: {
49
50
  if (errors.length) {
50
51
  console.error(ts.formatDiagnostics(errors, formatHost))
51
52
 
52
- throw new Error('failed to compile')
53
+ if (strict) {
54
+ throw new Error('failed to compile TypeScript definitions')
55
+ }
53
56
  }
54
57
  }
55
58
  }
@@ -19,7 +19,7 @@ export async function _doExtract(
19
19
  ctx: _BuildContext,
20
20
  task: _DtsTask | _DtsWatchTask
21
21
  ): Promise<_DtsResult> {
22
- const {config, cwd, files, ts} = ctx
22
+ const {config, cwd, files, strict, ts} = ctx
23
23
 
24
24
  if (!ts.config || !ts.configPath) {
25
25
  return {type: 'dts', messages: [], results: []}
@@ -33,7 +33,7 @@ export async function _doExtract(
33
33
 
34
34
  const tmpPath = path.resolve(outDir, '__tmp__')
35
35
 
36
- await _buildTypes({cwd, outDir: tmpPath, tsconfig: ts.config})
36
+ await _buildTypes({cwd, outDir: tmpPath, strict, tsconfig: ts.config})
37
37
 
38
38
  const messages: ExtractorMessage[] = []
39
39
 
@@ -50,6 +50,7 @@ export async function _doExtract(
50
50
  const targetPath = path.resolve(cwd, entry.targetPath)
51
51
 
52
52
  const result = await _extractTypes({
53
+ customTags: config?.extract?.customTags,
53
54
  cwd,
54
55
  exportPath,
55
56
  files,
@@ -3,25 +3,18 @@ import path from 'path'
3
3
  import {
4
4
  Extractor,
5
5
  ExtractorConfig,
6
- ExtractorLogLevel,
7
6
  ExtractorMessage,
8
7
  ExtractorResult,
9
- IExtractorMessagesConfig,
10
8
  } from '@microsoft/api-extractor'
11
9
  import mkdirp from 'mkdirp'
12
10
  import prettier from 'prettier'
13
- import {PkgRuleLevel, PkgConfigOptions, _BuildFile} from '../../_core'
11
+ import {PkgConfigOptions, _BuildFile} from '../../_core'
14
12
  import {_createApiExtractorConfig} from './_createApiExtractorConfig'
15
13
  import {_createTSDocConfig} from './_createTSDocConfig'
16
-
17
- const _LOG_LEVELS: Record<PkgRuleLevel, ExtractorLogLevel> = {
18
- error: 'error' as ExtractorLogLevel.Error,
19
- info: 'info' as ExtractorLogLevel.Info,
20
- off: 'none' as ExtractorLogLevel.None,
21
- warn: 'warning' as ExtractorLogLevel.Warning,
22
- }
14
+ import {_getExtractMessagesConfig} from './_getExtractMessagesConfig'
23
15
 
24
16
  export async function _extractTypes(options: {
17
+ customTags: NonNullable<PkgConfigOptions['extract']>['customTags']
25
18
  cwd: string
26
19
  distPath: string
27
20
  exportPath: string
@@ -32,90 +25,28 @@ export async function _extractTypes(options: {
32
25
  sourceTypesPath: string
33
26
  tsconfigPath: string
34
27
  }): Promise<{extractorResult: ExtractorResult; messages: ExtractorMessage[]}> {
35
- const {distPath, exportPath, files, filePath, projectPath, rules, sourceTypesPath, tsconfigPath} =
36
- options
28
+ const {
29
+ customTags,
30
+ distPath,
31
+ exportPath,
32
+ files,
33
+ filePath,
34
+ projectPath,
35
+ rules,
36
+ sourceTypesPath,
37
+ tsconfigPath,
38
+ } = options
37
39
 
38
40
  const tsdocConfigFile = await _createTSDocConfig({
39
- customTags: [],
41
+ customTags: customTags || [],
40
42
  })
41
43
 
42
- function _ruleToLogLevel(
43
- key: keyof NonNullable<NonNullable<PkgConfigOptions['extract']>['rules']>,
44
- defaultLevel?: ExtractorLogLevel
45
- ) {
46
- const r = rules?.[key]
47
-
48
- return (r ? _LOG_LEVELS[r] : defaultLevel || 'warning') as ExtractorLogLevel
49
- }
50
-
51
- const messagesConfig: IExtractorMessagesConfig = {
52
- compilerMessageReporting: {
53
- default: {
54
- logLevel: 'warning' as ExtractorLogLevel,
55
- },
56
- },
57
-
58
- extractorMessageReporting: {
59
- default: {
60
- logLevel: 'warning' as ExtractorLogLevel,
61
- addToApiReportFile: false,
62
- },
63
-
64
- 'ae-forgotten-export': {
65
- logLevel: _ruleToLogLevel('ae-forgotten-export', 'error' as ExtractorLogLevel),
66
- addToApiReportFile: false,
67
- },
68
-
69
- 'ae-incompatible-release-tags': {
70
- logLevel: _ruleToLogLevel('ae-incompatible-release-tags', 'error' as ExtractorLogLevel),
71
- addToApiReportFile: false,
72
- },
73
-
74
- 'ae-internal-missing-underscore': {
75
- logLevel: _ruleToLogLevel('ae-internal-missing-underscore'),
76
- addToApiReportFile: false,
77
- },
78
-
79
- 'ae-missing-release-tag': {
80
- logLevel: _ruleToLogLevel('ae-missing-release-tag', 'error' as ExtractorLogLevel),
81
- addToApiReportFile: false,
82
- },
83
-
84
- 'ae-wrong-input-file-type': {
85
- logLevel: 'none' as ExtractorLogLevel,
86
- addToApiReportFile: false,
87
- },
88
- },
89
-
90
- tsdocMessageReporting: {
91
- default: {
92
- logLevel: 'warning' as ExtractorLogLevel,
93
- addToApiReportFile: false,
94
- },
95
-
96
- 'tsdoc-link-tag-unescaped-text': {
97
- logLevel: _ruleToLogLevel('tsdoc-link-tag-unescaped-text', 'warning' as ExtractorLogLevel),
98
- addToApiReportFile: false,
99
- },
100
-
101
- 'tsdoc-undefined-tag': {
102
- logLevel: _ruleToLogLevel('tsdoc-undefined-tag', 'error' as ExtractorLogLevel),
103
- addToApiReportFile: false,
104
- },
105
-
106
- 'tsdoc-unsupported-tag': {
107
- logLevel: _ruleToLogLevel('tsdoc-unsupported-tag', 'none' as ExtractorLogLevel),
108
- addToApiReportFile: false,
109
- },
110
- },
111
- }
112
-
113
44
  const extractorConfig: ExtractorConfig = ExtractorConfig.prepare({
114
45
  configObject: _createApiExtractorConfig({
115
46
  distPath,
116
47
  exportPath,
117
48
  filePath,
118
- messages: messagesConfig,
49
+ messages: _getExtractMessagesConfig({rules}),
119
50
  projectFolder: projectPath,
120
51
  mainEntryPointFilePath: sourceTypesPath,
121
52
  tsconfigPath,
@@ -0,0 +1,87 @@
1
+ import {ExtractorLogLevel, IExtractorMessagesConfig} from '@microsoft/api-extractor'
2
+ import {PkgRuleLevel, PkgConfigOptions} from '../../_core'
3
+
4
+ const _LOG_LEVELS: Record<PkgRuleLevel, ExtractorLogLevel> = {
5
+ error: 'error' as ExtractorLogLevel.Error,
6
+ info: 'info' as ExtractorLogLevel.Info,
7
+ off: 'none' as ExtractorLogLevel.None,
8
+ warn: 'warning' as ExtractorLogLevel.Warning,
9
+ }
10
+
11
+ /** @internal */
12
+ export function _getExtractMessagesConfig(options: {
13
+ rules: NonNullable<PkgConfigOptions['extract']>['rules']
14
+ }): IExtractorMessagesConfig {
15
+ const {rules} = options
16
+
17
+ function _ruleToLogLevel(
18
+ key: keyof NonNullable<NonNullable<PkgConfigOptions['extract']>['rules']>,
19
+ defaultLevel?: ExtractorLogLevel
20
+ ) {
21
+ const r = rules?.[key]
22
+
23
+ return (r ? _LOG_LEVELS[r] : defaultLevel || 'warning') as ExtractorLogLevel
24
+ }
25
+
26
+ return {
27
+ compilerMessageReporting: {
28
+ default: {
29
+ logLevel: 'warning' as ExtractorLogLevel,
30
+ },
31
+ },
32
+
33
+ extractorMessageReporting: {
34
+ default: {
35
+ logLevel: 'warning' as ExtractorLogLevel,
36
+ addToApiReportFile: false,
37
+ },
38
+
39
+ 'ae-forgotten-export': {
40
+ logLevel: _ruleToLogLevel('ae-forgotten-export', 'error' as ExtractorLogLevel),
41
+ addToApiReportFile: false,
42
+ },
43
+
44
+ 'ae-incompatible-release-tags': {
45
+ logLevel: _ruleToLogLevel('ae-incompatible-release-tags', 'error' as ExtractorLogLevel),
46
+ addToApiReportFile: false,
47
+ },
48
+
49
+ 'ae-internal-missing-underscore': {
50
+ logLevel: _ruleToLogLevel('ae-internal-missing-underscore'),
51
+ addToApiReportFile: false,
52
+ },
53
+
54
+ 'ae-missing-release-tag': {
55
+ logLevel: _ruleToLogLevel('ae-missing-release-tag', 'error' as ExtractorLogLevel),
56
+ addToApiReportFile: false,
57
+ },
58
+
59
+ 'ae-wrong-input-file-type': {
60
+ logLevel: 'none' as ExtractorLogLevel,
61
+ addToApiReportFile: false,
62
+ },
63
+ },
64
+
65
+ tsdocMessageReporting: {
66
+ default: {
67
+ logLevel: 'warning' as ExtractorLogLevel,
68
+ addToApiReportFile: false,
69
+ },
70
+
71
+ 'tsdoc-link-tag-unescaped-text': {
72
+ logLevel: _ruleToLogLevel('tsdoc-link-tag-unescaped-text', 'warning' as ExtractorLogLevel),
73
+ addToApiReportFile: false,
74
+ },
75
+
76
+ 'tsdoc-undefined-tag': {
77
+ logLevel: _ruleToLogLevel('tsdoc-undefined-tag', 'error' as ExtractorLogLevel),
78
+ addToApiReportFile: false,
79
+ },
80
+
81
+ 'tsdoc-unsupported-tag': {
82
+ logLevel: _ruleToLogLevel('tsdoc-unsupported-tag', 'none' as ExtractorLogLevel),
83
+ addToApiReportFile: false,
84
+ },
85
+ },
86
+ }
87
+ }
@@ -1,3 +1,4 @@
1
1
  export * from './_dtsTask'
2
2
  export * from './_dtsWatchTask'
3
+ export * from './_getExtractMessagesConfig'
3
4
  export * from './_types'
@@ -5,10 +5,10 @@ import commonjs from '@rollup/plugin-commonjs'
5
5
  import json from '@rollup/plugin-json'
6
6
  import {nodeResolve} from '@rollup/plugin-node-resolve'
7
7
  import replace from '@rollup/plugin-replace'
8
- import {InputOptions, OutputOptions} from 'rollup'
8
+ import {InputOptions, OutputOptions, Plugin} from 'rollup'
9
9
  import esbuild from 'rollup-plugin-esbuild'
10
10
  import {terser} from 'rollup-plugin-terser'
11
- import {_BuildContext, _DEFAULTS, _MODULE_EXT} from '../../_core'
11
+ import {_BuildContext, _DEFAULTS, _MODULE_EXT, _resolveConfigProperty} from '../../_core'
12
12
  import {_RollupTask, _RollupWatchTask} from '../_types'
13
13
 
14
14
  export interface _RollupConfig {
@@ -45,6 +45,94 @@ export function _resolveRollupConfig(
45
45
 
46
46
  const sourcePaths = _exports && Object.values(_exports).map((e) => path.resolve(cwd, e.source))
47
47
 
48
+ const replacements = Object.fromEntries(
49
+ Object.entries(config?.define || {}).map(([key, val]) => [key, JSON.stringify(val)])
50
+ )
51
+
52
+ const defaultPlugins = [
53
+ replace({
54
+ preventAssignment: true,
55
+ values: {
56
+ ...(pkg.name === '@sanity/pkg-utils'
57
+ ? {...replacements}
58
+ : {
59
+ 'process.env.PKG_FILE_PATH': (arg) => {
60
+ const sourcePath = './' + path.relative(cwd, arg)
61
+ const entry = entries.find((e) => e.source === sourcePath)
62
+
63
+ if (!entry) {
64
+ // eslint-disable-next-line no-console
65
+ console.error(`could not find source entry: ${sourcePath}`)
66
+
67
+ return 'null'
68
+ }
69
+
70
+ return JSON.stringify(
71
+ path.relative(cwd, path.resolve(outDir, entry.name + outputExt))
72
+ )
73
+ },
74
+ 'process.env.PKG_FORMAT': JSON.stringify(format),
75
+ 'process.env.PKG_RUNTIME': JSON.stringify(runtime),
76
+ 'process.env.PKG_VERSION': JSON.stringify(process.env.PKG_VERSION || pkg.version),
77
+ ...replacements,
78
+ }),
79
+ },
80
+ }),
81
+ alias({
82
+ entries: {...pathAliases},
83
+ }),
84
+ nodeResolve({
85
+ // mainFields: ['module', 'jsnext', 'main'],
86
+ browser: runtime === 'browser',
87
+ // exportConditions: [runtime === 'node' ? 'node' : 'browser'],
88
+ extensions: ['.cjs', '.mjs', '.js', '.jsx', '.json', '.node'],
89
+ preferBuiltins: true, // runtime === 'node',
90
+ }),
91
+ commonjs({
92
+ esmExternals: false,
93
+ extensions: ['.js'],
94
+ // include: /\/node_modules\//,
95
+ // requireReturnsDefault: 'namespace',
96
+ }),
97
+ json(),
98
+ esbuild({
99
+ jsx: config?.jsx ?? 'automatic',
100
+ jsxFactory: config?.jsxFactory ?? 'createElement',
101
+ jsxFragment: config?.jsxFragment ?? 'Fragment',
102
+ jsxImportSource: config?.jsxImportSource ?? 'react',
103
+ target,
104
+ tsconfig: ctx.ts.configPath || 'tsconfig.json',
105
+ }),
106
+ getBabelOutputPlugin({
107
+ babelrc: false,
108
+ plugins: ['@babel/plugin-proposal-object-rest-spread'],
109
+ presets: [['@babel/preset-env', {targets: pkg.browserslist || _DEFAULTS.browserslist}]],
110
+ }),
111
+ minify &&
112
+ terser({
113
+ output: {
114
+ comments: (_node, comment) => {
115
+ const text = comment.value
116
+ const type = comment.type
117
+
118
+ // Check if this is a multiline comment
119
+ if (type == 'comment2') {
120
+ // Keep licensing comments
121
+ return /@preserve|@license|@cc_on/i.test(text)
122
+ }
123
+
124
+ return false
125
+ },
126
+ },
127
+ }),
128
+ ].filter(Boolean) as Plugin[]
129
+
130
+ const userPlugins = config?.rollup?.plugins
131
+
132
+ const plugins = Array.isArray(userPlugins)
133
+ ? defaultPlugins.concat(userPlugins)
134
+ : _resolveConfigProperty(config?.rollup?.plugins, defaultPlugins)
135
+
48
136
  return {
49
137
  inputOptions: {
50
138
  context: cwd,
@@ -92,81 +180,7 @@ export function _resolveRollupConfig(
92
180
  }
93
181
  },
94
182
 
95
- plugins: [
96
- replace({
97
- preventAssignment: true,
98
- values: {
99
- ...(pkg.name === '@sanity/pkg-utils'
100
- ? {}
101
- : {
102
- 'process.env.PKG_FILE_PATH': (arg) => {
103
- const sourcePath = './' + path.relative(cwd, arg)
104
- const entry = entries.find((e) => e.source === sourcePath)
105
-
106
- if (!entry) {
107
- throw new Error(`could not find source entry: ${sourcePath}`)
108
- }
109
-
110
- return JSON.stringify(
111
- path.relative(cwd, path.resolve(outDir, entry.name + outputExt))
112
- )
113
- },
114
- 'process.env.PKG_FORMAT': JSON.stringify(format),
115
- 'process.env.PKG_RUNTIME': JSON.stringify(runtime),
116
- 'process.env.PKG_VERSION': JSON.stringify(process.env.PKG_VERSION || pkg.version),
117
- }),
118
- },
119
- }),
120
- alias({
121
- entries: {...pathAliases},
122
- }),
123
- nodeResolve({
124
- // mainFields: ['module', 'jsnext', 'main'],
125
- browser: runtime === 'browser',
126
- // exportConditions: [runtime === 'node' ? 'node' : 'browser'],
127
- extensions: ['.cjs', '.mjs', '.js', '.jsx', '.json', '.node'],
128
- preferBuiltins: true, // runtime === 'node',
129
- }),
130
- commonjs({
131
- esmExternals: false,
132
- extensions: ['.js'],
133
- // include: /\/node_modules\//,
134
- // requireReturnsDefault: 'namespace',
135
- }),
136
- json(),
137
- esbuild({
138
- jsx: config?.jsx ?? 'automatic',
139
- jsxFactory: config?.jsxFactory ?? 'createElement',
140
- jsxFragment: config?.jsxFragment ?? 'Fragment',
141
- jsxImportSource: config?.jsxImportSource ?? 'react',
142
- target,
143
- tsconfig: ctx.ts.configPath || 'tsconfig.json',
144
- }),
145
- getBabelOutputPlugin({
146
- babelrc: false,
147
- plugins: ['@babel/plugin-proposal-object-rest-spread'],
148
- presets: [['@babel/preset-env', {targets: pkg.browserslist || _DEFAULTS.browserslist}]],
149
- }),
150
- minify &&
151
- terser({
152
- output: {
153
- comments: (_node, comment) => {
154
- const text = comment.value
155
- const type = comment.type
156
-
157
- // Check if this is a multiline comment
158
- if (type == 'comment2') {
159
- // Keep licensing comments
160
- return /@preserve|@license|@cc_on/i.test(text)
161
- }
162
-
163
- return false
164
- },
165
- },
166
- }),
167
-
168
- ...(config?.rollup?.plugins || []),
169
- ].filter(Boolean),
183
+ plugins,
170
184
 
171
185
  treeshake: {
172
186
  propertyReadSideEffects: false,
package/src/node/build.ts CHANGED
@@ -4,14 +4,36 @@ import {_resolveBuildTasks} from './_resolveBuildTasks'
4
4
  import {_spinner} from './_spinner'
5
5
  import {_BuildTask, _TaskHandler, _buildTaskHandlers} from './_tasks'
6
6
 
7
- /** @public */
8
- export async function build(options: {cwd: string; tsconfig?: string}): Promise<void> {
9
- const {cwd, tsconfig = 'tsconfig.json'} = options
7
+ /**
8
+ * Build the distribution files of a npm package.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * import {build} from '@sanity/pkg-utils'
13
+ *
14
+ * build({
15
+ * cwd: process.cwd(),
16
+ * tsconfig: 'tsconfig.dist.json,
17
+ * }).then(() => {
18
+ * console.log('successfully built')
19
+ * }).catch((err) => {
20
+ * console.log(`build error: ${err.message}`)
21
+ * })
22
+ * ```
23
+ *
24
+ * @public
25
+ */
26
+ export async function build(options: {
27
+ cwd: string
28
+ strict?: boolean
29
+ tsconfig?: string
30
+ }): Promise<void> {
31
+ const {cwd, strict = false, tsconfig: tsconfigOption} = options
10
32
 
11
33
  const pkg = await _loadPkgWithReporting({cwd})
12
34
  const config = await _loadConfig({cwd})
13
-
14
- const ctx = await _resolveBuildContext({config, cwd, pkg, tsconfig})
35
+ const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
36
+ const ctx = await _resolveBuildContext({config, cwd, pkg, strict, tsconfig})
15
37
 
16
38
  const buildTasks = _resolveBuildTasks(ctx)
17
39
 
package/src/node/check.ts CHANGED
@@ -10,11 +10,12 @@ export async function check(options: {
10
10
  strict?: boolean
11
11
  tsconfig?: string
12
12
  }): Promise<void> {
13
- const {cwd, strict, tsconfig = 'tsconfig.json'} = options
13
+ const {cwd, strict = false, tsconfig: tsconfigOption} = options
14
14
 
15
15
  const pkg = await _loadPkgWithReporting({cwd})
16
16
  const config = await _loadConfig({cwd})
17
- const ctx = await _resolveBuildContext({config, cwd, pkg, tsconfig})
17
+ const tsconfig = tsconfigOption || config?.tsconfig || 'tsconfig.json'
18
+ const ctx = await _resolveBuildContext({config, cwd, pkg, strict, tsconfig})
18
19
  const {logger} = ctx
19
20
 
20
21
  _printPackageTree(ctx)