@rushstack/trace-import 0.6.14 → 0.7.1
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/CHANGELOG.json +52 -0
- package/CHANGELOG.md +15 -1
- package/bin/trace-import +1 -1
- package/lib-esm/TraceImportCommandLineParser.js +67 -0
- package/lib-esm/TraceImportCommandLineParser.js.map +1 -0
- package/lib-esm/start.js +14 -0
- package/lib-esm/start.js.map +1 -0
- package/lib-esm/traceImport.js +272 -0
- package/lib-esm/traceImport.js.map +1 -0
- package/package.json +25 -5
- /package/{lib → lib-commonjs}/TraceImportCommandLineParser.js +0 -0
- /package/{lib → lib-commonjs}/TraceImportCommandLineParser.js.map +0 -0
- /package/{lib → lib-commonjs}/start.js +0 -0
- /package/{lib → lib-commonjs}/start.js.map +0 -0
- /package/{lib → lib-commonjs}/traceImport.js +0 -0
- /package/{lib → lib-commonjs}/traceImport.js.map +0 -0
- /package/{lib → lib-dts}/TraceImportCommandLineParser.d.ts +0 -0
- /package/{lib → lib-dts}/TraceImportCommandLineParser.d.ts.map +0 -0
- /package/{lib → lib-dts}/start.d.ts +0 -0
- /package/{lib → lib-dts}/start.d.ts.map +0 -0
- /package/{lib → lib-dts}/traceImport.d.ts +0 -0
- /package/{lib → lib-dts}/traceImport.d.ts.map +0 -0
package/CHANGELOG.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/trace-import",
|
|
3
3
|
"entries": [
|
|
4
|
+
{
|
|
5
|
+
"version": "0.7.1",
|
|
6
|
+
"tag": "@rushstack/trace-import_v0.7.1",
|
|
7
|
+
"date": "Fri, 20 Feb 2026 00:15:04 GMT",
|
|
8
|
+
"comments": {
|
|
9
|
+
"patch": [
|
|
10
|
+
{
|
|
11
|
+
"comment": "Add `\"node\"` condition before `\"import\"` in the `\"exports\"` map so that Node.js uses the CJS output (which handles extensionless imports), while bundlers still use ESM via `\"import\"`. Fixes https://github.com/microsoft/rushstack/issues/5644."
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"dependency": [
|
|
15
|
+
{
|
|
16
|
+
"comment": "Updating dependency \"@rushstack/node-core-library\" to `5.20.1`"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"comment": "Updating dependency \"@rushstack/terminal\" to `0.22.1`"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"comment": "Updating dependency \"@rushstack/ts-command-line\" to `5.3.1`"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"comment": "Updating dependency \"@rushstack/heft\" to `1.2.1`"
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"version": "0.7.0",
|
|
32
|
+
"tag": "@rushstack/trace-import_v0.7.0",
|
|
33
|
+
"date": "Thu, 19 Feb 2026 00:04:53 GMT",
|
|
34
|
+
"comments": {
|
|
35
|
+
"minor": [
|
|
36
|
+
{
|
|
37
|
+
"comment": "Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `\"exports\"` field in `package.json`."
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
"dependency": [
|
|
41
|
+
{
|
|
42
|
+
"comment": "Updating dependency \"@rushstack/node-core-library\" to `5.20.0`"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"comment": "Updating dependency \"@rushstack/terminal\" to `0.22.0`"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"comment": "Updating dependency \"@rushstack/ts-command-line\" to `5.3.0`"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"comment": "Updating dependency \"@rushstack/heft\" to `1.2.0`"
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
},
|
|
4
56
|
{
|
|
5
57
|
"version": "0.6.14",
|
|
6
58
|
"tag": "@rushstack/trace-import_v0.6.14",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
# Change Log - @rushstack/trace-import
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Fri, 20 Feb 2026 00:15:04 GMT and should not be manually modified.
|
|
4
|
+
|
|
5
|
+
## 0.7.1
|
|
6
|
+
Fri, 20 Feb 2026 00:15:04 GMT
|
|
7
|
+
|
|
8
|
+
### Patches
|
|
9
|
+
|
|
10
|
+
- Add `"node"` condition before `"import"` in the `"exports"` map so that Node.js uses the CJS output (which handles extensionless imports), while bundlers still use ESM via `"import"`. Fixes https://github.com/microsoft/rushstack/issues/5644.
|
|
11
|
+
|
|
12
|
+
## 0.7.0
|
|
13
|
+
Thu, 19 Feb 2026 00:04:53 GMT
|
|
14
|
+
|
|
15
|
+
### Minor changes
|
|
16
|
+
|
|
17
|
+
- Normalize package layout. CommonJS is now under `lib-commonjs`, DTS is now under `lib-dts`, and ESM is now under `lib-esm`. Imports to `lib` still work as before, handled by the `"exports"` field in `package.json`.
|
|
4
18
|
|
|
5
19
|
## 0.6.14
|
|
6
20
|
Sat, 07 Feb 2026 01:13:26 GMT
|
package/bin/trace-import
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
require('../lib/start.js');
|
|
2
|
+
require('../lib-commonjs/start.js');
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { CommandLineParser } from '@rushstack/ts-command-line';
|
|
4
|
+
import { InternalError } from '@rushstack/node-core-library';
|
|
5
|
+
import { Colorize } from '@rushstack/terminal';
|
|
6
|
+
import { traceImport } from './traceImport';
|
|
7
|
+
export class TraceImportCommandLineParser extends CommandLineParser {
|
|
8
|
+
constructor() {
|
|
9
|
+
super({
|
|
10
|
+
toolFilename: 'trace-import',
|
|
11
|
+
toolDescription: 'This tool analyzes import module paths, to determine the resolved target folder. ' +
|
|
12
|
+
'For example, if the "semver" NPM package is installed, "trace-import --path semver/index" will ' +
|
|
13
|
+
'print output equivalent to the Node.js require.resolve() API. ' +
|
|
14
|
+
'If "@types/semver" is installed, then "trace-import --resolution-type ts --path semver/index" will ' +
|
|
15
|
+
'print the .d.ts file path that would be resolved by a TypeScript import statement.'
|
|
16
|
+
});
|
|
17
|
+
this._debugParameter = this.defineFlagParameter({
|
|
18
|
+
parameterLongName: '--debug',
|
|
19
|
+
parameterShortName: '-d',
|
|
20
|
+
description: 'Show the full call stack if an error occurs while executing the tool'
|
|
21
|
+
});
|
|
22
|
+
this._pathParameter = this.defineStringParameter({
|
|
23
|
+
parameterLongName: '--path',
|
|
24
|
+
parameterShortName: '-p',
|
|
25
|
+
description: 'The import module path to be analyzed. For example, ' +
|
|
26
|
+
'"example" in expressions such as: require("example"); require.resolve("example"); import { Thing } from "example";',
|
|
27
|
+
argumentName: 'IMPORT_PATH',
|
|
28
|
+
required: true
|
|
29
|
+
});
|
|
30
|
+
this._baseFolderParameter = this.defineStringParameter({
|
|
31
|
+
parameterLongName: '--base-folder',
|
|
32
|
+
parameterShortName: '-b',
|
|
33
|
+
description: 'The "--path" string will be resolved as if the import statement appeared in a script located in this folder. ' +
|
|
34
|
+
'If omitted, the current working directory is used.',
|
|
35
|
+
argumentName: 'FOLDER_PATH'
|
|
36
|
+
});
|
|
37
|
+
this._resolutionTypeParameter = this.defineChoiceParameter({
|
|
38
|
+
parameterLongName: '--resolution-type',
|
|
39
|
+
parameterShortName: '-t',
|
|
40
|
+
description: 'The type of module resolution to perform: ' +
|
|
41
|
+
'"cjs" for CommonJS, "es" for ES modules, or "ts" for TypeScript typings',
|
|
42
|
+
alternatives: ['cjs', 'es', 'ts'],
|
|
43
|
+
defaultValue: 'cjs'
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
async onExecuteAsync() {
|
|
47
|
+
if (this._debugParameter.value) {
|
|
48
|
+
InternalError.breakInDebugger = true;
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
traceImport({
|
|
52
|
+
importPath: this._pathParameter.value,
|
|
53
|
+
baseFolder: this._baseFolderParameter.value,
|
|
54
|
+
resolutionType: this._resolutionTypeParameter.value
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (this._debugParameter.value) {
|
|
59
|
+
console.error('\n' + error.stack);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
console.error('\n' + Colorize.red('ERROR: ' + error.message.trim()));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=TraceImportCommandLineParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TraceImportCommandLineParser.js","sourceRoot":"","sources":["../src/TraceImportCommandLineParser.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EACL,iBAAiB,EAKlB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAC7D,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,OAAO,EAAuB,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjE,MAAM,OAAO,4BAA6B,SAAQ,iBAAiB;IAMjE;QACE,KAAK,CAAC;YACJ,YAAY,EAAE,cAAc;YAC5B,eAAe,EACb,oFAAoF;gBACpF,iGAAiG;gBACjG,iEAAiE;gBACjE,qGAAqG;gBACrG,oFAAoF;SACvF,CAAC,CAAC;QAEH,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,mBAAmB,CAAC;YAC9C,iBAAiB,EAAE,SAAS;YAC5B,kBAAkB,EAAE,IAAI;YACxB,WAAW,EAAE,sEAAsE;SACpF,CAAC,CAAC;QAEH,IAAI,CAAC,c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,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;YACvE,CAAC;QACH,CAAC;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport {\n CommandLineParser,\n type CommandLineFlagParameter,\n type CommandLineStringParameter,\n type IRequiredCommandLineStringParameter,\n type IRequiredCommandLineChoiceParameter\n} from '@rushstack/ts-command-line';\nimport { InternalError } from '@rushstack/node-core-library';\nimport { Colorize } from '@rushstack/terminal';\n\nimport { type ResolutionType, traceImport } from './traceImport';\n\nexport class TraceImportCommandLineParser extends CommandLineParser {\n private readonly _debugParameter: CommandLineFlagParameter;\n private readonly _pathParameter: IRequiredCommandLineStringParameter;\n private readonly _baseFolderParameter: CommandLineStringParameter;\n private readonly _resolutionTypeParameter: IRequiredCommandLineChoiceParameter<ResolutionType>;\n\n public constructor() {\n super({\n toolFilename: 'trace-import',\n toolDescription:\n 'This tool analyzes import module paths, to determine the resolved target folder. ' +\n 'For example, if the \"semver\" NPM package is installed, \"trace-import --path semver/index\" will ' +\n 'print output equivalent to the Node.js require.resolve() API. ' +\n 'If \"@types/semver\" is installed, then \"trace-import --resolution-type ts --path semver/index\" will ' +\n 'print the .d.ts file path that would be resolved by a TypeScript import statement.'\n });\n\n this._debugParameter = this.defineFlagParameter({\n parameterLongName: '--debug',\n parameterShortName: '-d',\n description: 'Show the full call stack if an error occurs while executing the tool'\n });\n\n this._pathParameter = this.defineStringParameter({\n parameterLongName: '--path',\n parameterShortName: '-p',\n description:\n 'The import module path to be analyzed. For example, ' +\n '\"example\" in expressions such as: require(\"example\"); require.resolve(\"example\"); import { Thing } from \"example\";',\n argumentName: 'IMPORT_PATH',\n required: true\n });\n\n this._baseFolderParameter = this.defineStringParameter({\n parameterLongName: '--base-folder',\n parameterShortName: '-b',\n description:\n 'The \"--path\" string will be resolved as if the import statement appeared in a script located in this folder. ' +\n 'If omitted, the current working directory is used.',\n argumentName: 'FOLDER_PATH'\n });\n\n this._resolutionTypeParameter = this.defineChoiceParameter<ResolutionType>({\n parameterLongName: '--resolution-type',\n parameterShortName: '-t',\n description:\n 'The type of module resolution to perform: ' +\n '\"cjs\" for CommonJS, \"es\" for ES modules, or \"ts\" for TypeScript typings',\n alternatives: ['cjs', 'es', 'ts'],\n defaultValue: 'cjs'\n });\n }\n\n protected override async onExecuteAsync(): Promise<void> {\n if (this._debugParameter.value) {\n InternalError.breakInDebugger = true;\n }\n try {\n traceImport({\n importPath: this._pathParameter.value,\n baseFolder: this._baseFolderParameter.value,\n resolutionType: this._resolutionTypeParameter.value\n });\n } catch (error) {\n if (this._debugParameter.value) {\n console.error('\\n' + error.stack);\n } else {\n console.error('\\n' + Colorize.red('ERROR: ' + error.message.trim()));\n }\n }\n }\n}\n"]}
|
package/lib-esm/start.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import { PackageJsonLookup } from '@rushstack/node-core-library';
|
|
4
|
+
import { Colorize } from '@rushstack/terminal';
|
|
5
|
+
import { TraceImportCommandLineParser } from './TraceImportCommandLineParser';
|
|
6
|
+
const toolVersion = PackageJsonLookup.loadOwnPackageJson(__dirname).version;
|
|
7
|
+
console.log();
|
|
8
|
+
console.log(Colorize.bold(`trace-import ${toolVersion}`) + ' - ' + Colorize.cyan('https://rushstack.io'));
|
|
9
|
+
console.log();
|
|
10
|
+
const commandLine = new TraceImportCommandLineParser();
|
|
11
|
+
commandLine.executeAsync().catch((error) => {
|
|
12
|
+
console.error(error);
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=start.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"start.js","sourceRoot":"","sources":["../src/start.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,2DAA2D;AAE3D,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAE/C,OAAO,EAAE,4BAA4B,EAAE,MAAM,gCAAgC,CAAC;AAE9E,MAAM,WAAW,GAAW,iBAAiB,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC;AAEpF,OAAO,CAAC,GAAG,EAAE,CAAC;AACd,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,gBAAgB,WAAW,EAAE,CAAC,GAAG,KAAK,GAAG,QAAQ,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAC,CAAC;AAC1G,OAAO,CAAC,GAAG,EAAE,CAAC;AAEd,MAAM,WAAW,GAAiC,IAAI,4BAA4B,EAAE,CAAC;AACrF,WAAW,CAAC,YAAY,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACzC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { PackageJsonLookup } from '@rushstack/node-core-library';\nimport { Colorize } from '@rushstack/terminal';\n\nimport { TraceImportCommandLineParser } from './TraceImportCommandLineParser';\n\nconst toolVersion: string = PackageJsonLookup.loadOwnPackageJson(__dirname).version;\n\nconsole.log();\nconsole.log(Colorize.bold(`trace-import ${toolVersion}`) + ' - ' + Colorize.cyan('https://rushstack.io'));\nconsole.log();\n\nconst commandLine: TraceImportCommandLineParser = new TraceImportCommandLineParser();\ncommandLine.executeAsync().catch((error) => {\n console.error(error);\n});\n"]}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|
2
|
+
// See LICENSE in the project root for license information.
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import * as process from 'node:process';
|
|
5
|
+
import * as Resolve from 'resolve';
|
|
6
|
+
import { Colorize } from '@rushstack/terminal';
|
|
7
|
+
import { FileSystem, JsonFile, PackageName } from '@rushstack/node-core-library';
|
|
8
|
+
const jsExtensions = ['.js', '.cjs', '.jsx', '.json'];
|
|
9
|
+
const tsExtensions = ['.d.ts', '.ts', '.tsx', '.json'];
|
|
10
|
+
// Somewhat loosely matches inputs such as:
|
|
11
|
+
// my-package/path/to/file.js
|
|
12
|
+
// [group 1 ][group 2 ]
|
|
13
|
+
//
|
|
14
|
+
// @scope/my-package/path/to/file.js
|
|
15
|
+
// [group 1 ][group 2 ]
|
|
16
|
+
//
|
|
17
|
+
// @scope/my-package
|
|
18
|
+
// [group 1 ]
|
|
19
|
+
const packageImportPathRegExp = /^((?:@[a-z0-9_][a-z0-9\-_\.]*\/)?[a-z0-9_][a-z0-9\-_\.]*)(\/.*)?$/i;
|
|
20
|
+
function logInputField(title, value) {
|
|
21
|
+
console.log(Colorize.cyan(title.padEnd(25)) + value);
|
|
22
|
+
}
|
|
23
|
+
function logOutputField(title, value) {
|
|
24
|
+
console.log(Colorize.green(title.padEnd(25)) + value);
|
|
25
|
+
}
|
|
26
|
+
function traceTypeScriptPackage(options) {
|
|
27
|
+
const { packageSubpath, packageFolder, packageJson, atTypes, warnings } = options;
|
|
28
|
+
// For example, if we started with importFullPath="semver/index",
|
|
29
|
+
// here we may get normalizedImportFullPath="@types/semver/index"
|
|
30
|
+
const normalizedImportFullPath = packageJson.name + packageSubpath;
|
|
31
|
+
// First try to resolve the .js main index
|
|
32
|
+
let cjsTargetPath = undefined;
|
|
33
|
+
try {
|
|
34
|
+
cjsTargetPath = Resolve.sync(normalizedImportFullPath, {
|
|
35
|
+
basedir: packageFolder,
|
|
36
|
+
preserveSymlinks: false,
|
|
37
|
+
extensions: jsExtensions
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
// not found
|
|
42
|
+
}
|
|
43
|
+
const mainIndexTitle = atTypes ? '@types main index:' : 'Main index:';
|
|
44
|
+
if (cjsTargetPath) {
|
|
45
|
+
const parsedPath = path.parse(cjsTargetPath);
|
|
46
|
+
// Is the resolved .js extension okay?
|
|
47
|
+
if (tsExtensions.indexOf(parsedPath.ext.toLocaleLowerCase()) >= 0) {
|
|
48
|
+
logOutputField(mainIndexTitle, '(inferred from .js main index)');
|
|
49
|
+
console.log();
|
|
50
|
+
logOutputField('Target path:', cjsTargetPath);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
// Try to replace the file extension
|
|
54
|
+
const dtsTargetPath = path.join(parsedPath.dir, parsedPath.name + '.d.ts');
|
|
55
|
+
if (FileSystem.exists(dtsTargetPath)) {
|
|
56
|
+
logOutputField(mainIndexTitle, '(inferred from .js entry point)');
|
|
57
|
+
console.log();
|
|
58
|
+
logOutputField('Target path:', dtsTargetPath);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!packageSubpath) {
|
|
63
|
+
// Try importing the "types"/"typings" main index:
|
|
64
|
+
if (packageJson.types) {
|
|
65
|
+
logOutputField(mainIndexTitle, `"types": ${JSON.stringify(packageJson.types)}`);
|
|
66
|
+
console.log();
|
|
67
|
+
logOutputField('Target path:', path.join(packageFolder, packageJson.types));
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
if (packageJson.typings) {
|
|
71
|
+
logOutputField(mainIndexTitle, `"typings": ${JSON.stringify(packageJson.typings)}`);
|
|
72
|
+
console.log();
|
|
73
|
+
logOutputField('Target path:', path.join(packageFolder, packageJson.typings));
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
if (atTypes) {
|
|
77
|
+
warnings.push('The @types package does not define "types" or "typings" field.');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
// Try importing the .d.ts file directly
|
|
82
|
+
let dtsTargetPath = undefined;
|
|
83
|
+
try {
|
|
84
|
+
dtsTargetPath = Resolve.sync(normalizedImportFullPath, {
|
|
85
|
+
basedir: packageFolder,
|
|
86
|
+
preserveSymlinks: false,
|
|
87
|
+
extensions: tsExtensions
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
// not found
|
|
92
|
+
}
|
|
93
|
+
if (dtsTargetPath) {
|
|
94
|
+
console.log();
|
|
95
|
+
logOutputField('Target path:', dtsTargetPath);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
function traceImportInner(options, warnings) {
|
|
102
|
+
let baseFolder;
|
|
103
|
+
if (options.baseFolder) {
|
|
104
|
+
baseFolder = path.resolve(options.baseFolder);
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
baseFolder = process.cwd();
|
|
108
|
+
}
|
|
109
|
+
const importFullPath = options.importPath.trim();
|
|
110
|
+
if (!importFullPath) {
|
|
111
|
+
throw new Error(`Invalid import path syntax: ${JSON.stringify(importFullPath)}`);
|
|
112
|
+
}
|
|
113
|
+
const match = packageImportPathRegExp.exec(importFullPath);
|
|
114
|
+
logInputField('Base folder:', baseFolder);
|
|
115
|
+
if (match) {
|
|
116
|
+
const importPackageName = match[1];
|
|
117
|
+
const packageSubpath = match[2];
|
|
118
|
+
const packageSubpathWithoutSlash = packageSubpath
|
|
119
|
+
? packageSubpath.substring(1)
|
|
120
|
+
: undefined;
|
|
121
|
+
logInputField('Package name:', importPackageName);
|
|
122
|
+
logInputField('Package subpath:', packageSubpathWithoutSlash || '(not specified)');
|
|
123
|
+
console.log('\nResolving...\n');
|
|
124
|
+
// Resolve the NPM package first
|
|
125
|
+
let packageFolder;
|
|
126
|
+
let packageJson = undefined;
|
|
127
|
+
{
|
|
128
|
+
let resolvedPackageJsonPath;
|
|
129
|
+
try {
|
|
130
|
+
resolvedPackageJsonPath = Resolve.sync(`${importPackageName}/package.json`, {
|
|
131
|
+
basedir: baseFolder,
|
|
132
|
+
preserveSymlinks: false
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
// Could not find NPM package
|
|
137
|
+
}
|
|
138
|
+
if (resolvedPackageJsonPath) {
|
|
139
|
+
packageFolder = path.dirname(resolvedPackageJsonPath);
|
|
140
|
+
logOutputField('Package folder:', packageFolder);
|
|
141
|
+
packageJson = JsonFile.load(resolvedPackageJsonPath);
|
|
142
|
+
logOutputField('package.json:', `${packageJson.name || '(missing name)'} (${packageJson.version || 'missing version'})`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// Also try to resolve the @types package
|
|
146
|
+
let atTypesPackageFolder = undefined;
|
|
147
|
+
let atTypesPackageJson = undefined;
|
|
148
|
+
if (options.resolutionType === 'ts') {
|
|
149
|
+
if (!importPackageName.startsWith('@types/')) {
|
|
150
|
+
const parsedPackageName = PackageName.parse(importPackageName);
|
|
151
|
+
let atTypesPackageName;
|
|
152
|
+
if (parsedPackageName.scope) {
|
|
153
|
+
atTypesPackageName = `@types/${parsedPackageName.scope}__${parsedPackageName.unscopedName}`;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
atTypesPackageName = `@types/${parsedPackageName.unscopedName}`;
|
|
157
|
+
}
|
|
158
|
+
let atTypesPackageJsonPath;
|
|
159
|
+
try {
|
|
160
|
+
atTypesPackageJsonPath = Resolve.sync(`${atTypesPackageName}/package.json`, {
|
|
161
|
+
basedir: baseFolder,
|
|
162
|
+
preserveSymlinks: false
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
// Unable to resolve @types package
|
|
167
|
+
}
|
|
168
|
+
if (atTypesPackageJsonPath) {
|
|
169
|
+
atTypesPackageFolder = path.dirname(atTypesPackageJsonPath);
|
|
170
|
+
logOutputField('@types folder:', atTypesPackageFolder);
|
|
171
|
+
atTypesPackageJson = JsonFile.load(atTypesPackageJsonPath);
|
|
172
|
+
logOutputField('@types package.json:', `${atTypesPackageJson.name || '(missing name)'} (${atTypesPackageJson.version || 'missing version'})`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
switch (options.resolutionType) {
|
|
177
|
+
case 'cjs':
|
|
178
|
+
{
|
|
179
|
+
if (!packageFolder || !packageJson) {
|
|
180
|
+
throw new Error(`Cannot find package "${importPackageName}" from "${baseFolder}".`);
|
|
181
|
+
}
|
|
182
|
+
if (!packageSubpath) {
|
|
183
|
+
if (packageJson.main) {
|
|
184
|
+
logOutputField('Main index:', `"main": ${JSON.stringify(packageJson.main)}`);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
logOutputField('Main index:', '(none)');
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
let targetPath;
|
|
191
|
+
try {
|
|
192
|
+
targetPath = Resolve.sync(importFullPath, {
|
|
193
|
+
basedir: packageFolder,
|
|
194
|
+
preserveSymlinks: false,
|
|
195
|
+
extensions: jsExtensions
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
// Are we importing the main index?
|
|
200
|
+
if (packageSubpath) {
|
|
201
|
+
throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
console.log('\nThis package does not define a main index.');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
console.log();
|
|
209
|
+
logOutputField('Target path:', targetPath);
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
case 'ts':
|
|
213
|
+
if (!packageFolder || (!packageJson && !atTypesPackageFolder && !atTypesPackageJson)) {
|
|
214
|
+
throw new Error(`Cannot find package "${importPackageName}" from "${baseFolder}".`);
|
|
215
|
+
}
|
|
216
|
+
if (packageFolder && packageJson) {
|
|
217
|
+
if (traceTypeScriptPackage({ packageSubpath, packageFolder, packageJson, warnings })) {
|
|
218
|
+
if (atTypesPackageFolder) {
|
|
219
|
+
warnings.push('An @types package was found but not used.');
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (atTypesPackageFolder && atTypesPackageJson) {
|
|
225
|
+
if (traceTypeScriptPackage({
|
|
226
|
+
packageSubpath,
|
|
227
|
+
packageFolder: atTypesPackageFolder,
|
|
228
|
+
packageJson: atTypesPackageJson,
|
|
229
|
+
warnings,
|
|
230
|
+
atTypes: true
|
|
231
|
+
})) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);
|
|
236
|
+
default:
|
|
237
|
+
throw new Error(`The "${options.resolutionType}" resolution type is not implemented yet`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
logInputField('Import path:', importFullPath);
|
|
242
|
+
console.log(`\nThe import path does not appear to reference an NPM package.`);
|
|
243
|
+
console.log('Resolving...\n');
|
|
244
|
+
let targetPath;
|
|
245
|
+
try {
|
|
246
|
+
targetPath = Resolve.sync(importFullPath, {
|
|
247
|
+
basedir: baseFolder,
|
|
248
|
+
preserveSymlinks: false,
|
|
249
|
+
extensions: options.resolutionType === 'ts' ? tsExtensions : jsExtensions
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
throw new Error(`Unable to resolve the import path: ${importFullPath}`);
|
|
254
|
+
}
|
|
255
|
+
logOutputField('Target path:', targetPath);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
export function traceImport(options) {
|
|
259
|
+
const warnings = [];
|
|
260
|
+
try {
|
|
261
|
+
traceImportInner(options, warnings);
|
|
262
|
+
}
|
|
263
|
+
finally {
|
|
264
|
+
if (warnings.length) {
|
|
265
|
+
console.log();
|
|
266
|
+
for (const warning of warnings) {
|
|
267
|
+
console.log(Colorize.yellow('Warning: ' + warning));
|
|
268
|
+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,KAAK,CAAC,QAAQ,OAAO,CAAC,cAAc,0CAA0C,CAAC,CAAC;QAC9F,CAAC;IACH,CAAC;SAAM,CAAC;QACN,aAAa,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;QAC9C,OAAO,CAAC,GAAG,CAAC,gEAAgE,CAAC,CAAC;QAC9E,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;QAE9B,IAAI,UAAkB,CAAC;QACvB,IAAI,CAAC;YACH,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;gBACxC,OAAO,EAAE,UAAU;gBACnB,gBAAgB,EAAE,KAAK;gBACvB,UAAU,EAAE,OAAO,CAAC,cAAc,KAAK,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,YAAY;aAC1E,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,sCAAsC,cAAc,EAAE,CAAC,CAAC;QAC1E,CAAC;QAED,cAAc,CAAC,cAAc,EAAE,UAAU,CAAC,CAAC;IAC7C,CAAC;AACH,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,OAAwB;IAClD,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,CAAC;QACH,gBAAgB,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACtC,CAAC;YAAS,CAAC;QACT,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;YACpB,OAAO,CAAC,GAAG,EAAE,CAAC;YACd,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;gBAC/B,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,GAAG,OAAO,CAAC,CAAC,CAAC;YACtD,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport * as path from 'node:path';\nimport * as process from 'node:process';\n\nimport * as Resolve from 'resolve';\n\nimport { Colorize } from '@rushstack/terminal';\nimport {\n FileSystem,\n type IPackageJson,\n type IParsedPackageName,\n JsonFile,\n PackageName\n} from '@rushstack/node-core-library';\n\nconst jsExtensions: string[] = ['.js', '.cjs', '.jsx', '.json'];\nconst tsExtensions: string[] = ['.d.ts', '.ts', '.tsx', '.json'];\n\nexport type ResolutionType = 'cjs' | 'es' | 'ts';\n\ninterface IExecuteOptions {\n importPath: string;\n baseFolder: string | undefined;\n resolutionType: ResolutionType;\n}\n\n// Somewhat loosely matches inputs such as:\n// my-package/path/to/file.js\n// [group 1 ][group 2 ]\n//\n// @scope/my-package/path/to/file.js\n// [group 1 ][group 2 ]\n//\n// @scope/my-package\n// [group 1 ]\nconst packageImportPathRegExp: RegExp = /^((?:@[a-z0-9_][a-z0-9\\-_\\.]*\\/)?[a-z0-9_][a-z0-9\\-_\\.]*)(\\/.*)?$/i;\n\nfunction logInputField(title: string, value: string): void {\n console.log(Colorize.cyan(title.padEnd(25)) + value);\n}\n\nfunction logOutputField(title: string, value: string): void {\n console.log(Colorize.green(title.padEnd(25)) + value);\n}\n\nfunction traceTypeScriptPackage(options: {\n packageSubpath: string;\n packageFolder: string;\n packageJson: IPackageJson;\n warnings: string[];\n atTypes?: boolean;\n}): boolean {\n const { packageSubpath, packageFolder, packageJson, atTypes, warnings } = options;\n\n // For example, if we started with importFullPath=\"semver/index\",\n // here we may get normalizedImportFullPath=\"@types/semver/index\"\n const normalizedImportFullPath: string = packageJson.name + packageSubpath;\n\n // First try to resolve the .js main index\n let cjsTargetPath: string | undefined = undefined;\n try {\n cjsTargetPath = Resolve.sync(normalizedImportFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: jsExtensions\n });\n } catch (error) {\n // not found\n }\n\n const mainIndexTitle: string = atTypes ? '@types main index:' : 'Main index:';\n\n if (cjsTargetPath) {\n const parsedPath: path.ParsedPath = path.parse(cjsTargetPath);\n\n // Is the resolved .js extension okay?\n if (tsExtensions.indexOf(parsedPath.ext.toLocaleLowerCase()) >= 0) {\n logOutputField(mainIndexTitle, '(inferred from .js main index)');\n console.log();\n logOutputField('Target path:', cjsTargetPath);\n return true;\n }\n\n // Try to replace the file extension\n const dtsTargetPath: string = path.join(parsedPath.dir, parsedPath.name + '.d.ts');\n if (FileSystem.exists(dtsTargetPath)) {\n logOutputField(mainIndexTitle, '(inferred from .js entry point)');\n console.log();\n logOutputField('Target path:', dtsTargetPath);\n return true;\n }\n }\n\n if (!packageSubpath) {\n // Try importing the \"types\"/\"typings\" main index:\n\n if (packageJson.types) {\n logOutputField(mainIndexTitle, `\"types\": ${JSON.stringify(packageJson.types)}`);\n console.log();\n logOutputField('Target path:', path.join(packageFolder, packageJson.types));\n return true;\n }\n\n if (packageJson.typings) {\n logOutputField(mainIndexTitle, `\"typings\": ${JSON.stringify(packageJson.typings)}`);\n console.log();\n logOutputField('Target path:', path.join(packageFolder, packageJson.typings));\n return true;\n }\n\n if (atTypes) {\n warnings.push('The @types package does not define \"types\" or \"typings\" field.');\n }\n } else {\n // Try importing the .d.ts file directly\n let dtsTargetPath: string | undefined = undefined;\n try {\n dtsTargetPath = Resolve.sync(normalizedImportFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: tsExtensions\n });\n } catch (error) {\n // not found\n }\n\n if (dtsTargetPath) {\n console.log();\n logOutputField('Target path:', dtsTargetPath);\n return true;\n }\n }\n\n return false;\n}\n\nfunction traceImportInner(options: IExecuteOptions, warnings: string[]): void {\n let baseFolder: string;\n if (options.baseFolder) {\n baseFolder = path.resolve(options.baseFolder);\n } else {\n baseFolder = process.cwd();\n }\n\n const importFullPath: string = options.importPath.trim();\n if (!importFullPath) {\n throw new Error(`Invalid import path syntax: ${JSON.stringify(importFullPath)}`);\n }\n const match: RegExpExecArray | null = packageImportPathRegExp.exec(importFullPath);\n\n logInputField('Base folder:', baseFolder);\n\n if (match) {\n const importPackageName: string = match[1];\n const packageSubpath: string | undefined = match[2];\n const packageSubpathWithoutSlash: string | undefined = packageSubpath\n ? packageSubpath.substring(1)\n : undefined;\n\n logInputField('Package name:', importPackageName);\n logInputField('Package subpath:', packageSubpathWithoutSlash || '(not specified)');\n\n console.log('\\nResolving...\\n');\n\n // Resolve the NPM package first\n let packageFolder: string | undefined;\n let packageJson: IPackageJson | undefined = undefined;\n {\n let resolvedPackageJsonPath: string | undefined;\n try {\n resolvedPackageJsonPath = Resolve.sync(`${importPackageName}/package.json`, {\n basedir: baseFolder,\n preserveSymlinks: false\n });\n } catch (e) {\n // Could not find NPM package\n }\n\n if (resolvedPackageJsonPath) {\n packageFolder = path.dirname(resolvedPackageJsonPath);\n logOutputField('Package folder:', packageFolder);\n\n packageJson = JsonFile.load(resolvedPackageJsonPath) as IPackageJson;\n logOutputField(\n 'package.json:',\n `${packageJson.name || '(missing name)'} (${packageJson.version || 'missing version'})`\n );\n }\n }\n\n // Also try to resolve the @types package\n let atTypesPackageFolder: string | undefined = undefined;\n let atTypesPackageJson: IPackageJson | undefined = undefined;\n\n if (options.resolutionType === 'ts') {\n if (!importPackageName.startsWith('@types/')) {\n const parsedPackageName: IParsedPackageName = PackageName.parse(importPackageName);\n let atTypesPackageName: string;\n if (parsedPackageName.scope) {\n atTypesPackageName = `@types/${parsedPackageName.scope}__${parsedPackageName.unscopedName}`;\n } else {\n atTypesPackageName = `@types/${parsedPackageName.unscopedName}`;\n }\n\n let atTypesPackageJsonPath: string | undefined;\n try {\n atTypesPackageJsonPath = Resolve.sync(`${atTypesPackageName}/package.json`, {\n basedir: baseFolder,\n preserveSymlinks: false\n });\n } catch (e) {\n // Unable to resolve @types package\n }\n\n if (atTypesPackageJsonPath) {\n atTypesPackageFolder = path.dirname(atTypesPackageJsonPath);\n logOutputField('@types folder:', atTypesPackageFolder);\n\n atTypesPackageJson = JsonFile.load(atTypesPackageJsonPath) as IPackageJson;\n logOutputField(\n '@types package.json:',\n `${atTypesPackageJson.name || '(missing name)'} (${\n atTypesPackageJson.version || 'missing version'\n })`\n );\n }\n }\n }\n\n switch (options.resolutionType) {\n case 'cjs':\n {\n if (!packageFolder || !packageJson) {\n throw new Error(`Cannot find package \"${importPackageName}\" from \"${baseFolder}\".`);\n }\n\n if (!packageSubpath) {\n if (packageJson.main) {\n logOutputField('Main index:', `\"main\": ${JSON.stringify(packageJson.main)}`);\n } else {\n logOutputField('Main index:', '(none)');\n }\n }\n\n let targetPath: string;\n try {\n targetPath = Resolve.sync(importFullPath, {\n basedir: packageFolder,\n preserveSymlinks: false,\n extensions: jsExtensions\n });\n } catch (error) {\n // Are we importing the main index?\n if (packageSubpath) {\n throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);\n } else {\n console.log('\\nThis package does not define a main index.');\n return;\n }\n }\n console.log();\n logOutputField('Target path:', targetPath);\n }\n break;\n case 'ts':\n if (!packageFolder || (!packageJson && !atTypesPackageFolder && !atTypesPackageJson)) {\n throw new Error(`Cannot find package \"${importPackageName}\" from \"${baseFolder}\".`);\n }\n\n if (packageFolder && packageJson) {\n if (traceTypeScriptPackage({ packageSubpath, packageFolder, packageJson, warnings })) {\n if (atTypesPackageFolder) {\n warnings.push('An @types package was found but not used.');\n }\n\n return;\n }\n }\n\n if (atTypesPackageFolder && atTypesPackageJson) {\n if (\n traceTypeScriptPackage({\n packageSubpath,\n packageFolder: atTypesPackageFolder,\n packageJson: atTypesPackageJson,\n warnings,\n atTypes: true\n })\n ) {\n return;\n }\n }\n\n throw new Error(`Unable to resolve the module subpath: ...${packageSubpath}`);\n default:\n throw new Error(`The \"${options.resolutionType}\" resolution type is not implemented yet`);\n }\n } else {\n logInputField('Import path:', importFullPath);\n console.log(`\\nThe import path does not appear to reference an NPM package.`);\n console.log('Resolving...\\n');\n\n let targetPath: string;\n try {\n targetPath = Resolve.sync(importFullPath, {\n basedir: baseFolder,\n preserveSymlinks: false,\n extensions: options.resolutionType === 'ts' ? tsExtensions : jsExtensions\n });\n } catch (error) {\n throw new Error(`Unable to resolve the import path: ${importFullPath}`);\n }\n\n logOutputField('Target path:', targetPath);\n }\n}\n\nexport function traceImport(options: IExecuteOptions): void {\n const warnings: string[] = [];\n try {\n traceImportInner(options, warnings);\n } finally {\n if (warnings.length) {\n console.log();\n for (const warning of warnings) {\n console.log(Colorize.yellow('Warning: ' + warning));\n }\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rushstack/trace-import",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "CLI tool for understanding how require() and \"import\" statements get resolved",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,17 +15,37 @@
|
|
|
15
15
|
"resolve": "~1.22.1",
|
|
16
16
|
"semver": "~7.5.4",
|
|
17
17
|
"typescript": "~5.8.2",
|
|
18
|
-
"@rushstack/node-core-library": "5.
|
|
19
|
-
"@rushstack/terminal": "0.
|
|
20
|
-
"@rushstack/ts-command-line": "5.
|
|
18
|
+
"@rushstack/node-core-library": "5.20.1",
|
|
19
|
+
"@rushstack/terminal": "0.22.1",
|
|
20
|
+
"@rushstack/ts-command-line": "5.3.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/resolve": "1.20.2",
|
|
24
24
|
"@types/semver": "7.5.0",
|
|
25
25
|
"eslint": "~9.37.0",
|
|
26
|
-
"@rushstack/heft": "1.1
|
|
26
|
+
"@rushstack/heft": "1.2.1",
|
|
27
27
|
"local-node-rig": "1.0.0"
|
|
28
28
|
},
|
|
29
|
+
"exports": {
|
|
30
|
+
"./lib/*": {
|
|
31
|
+
"types": "./lib-dts/*.d.ts",
|
|
32
|
+
"node": "./lib-commonjs/*.js",
|
|
33
|
+
"import": "./lib-esm/*.js",
|
|
34
|
+
"require": "./lib-commonjs/*.js"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"typesVersions": {
|
|
39
|
+
"*": {
|
|
40
|
+
"lib/*": [
|
|
41
|
+
"lib-dts/*"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"sideEffects": [
|
|
46
|
+
"lib-commonjs/start.js",
|
|
47
|
+
"lib-esm/start.js"
|
|
48
|
+
],
|
|
29
49
|
"scripts": {
|
|
30
50
|
"start": "node lib/start",
|
|
31
51
|
"build": "heft build --clean",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|