@ms-cloudpack/package-utilities 1.2.0 → 2.0.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/CHANGELOG.json +52 -1
- package/CHANGELOG.md +19 -2
- package/lib/addExportsMapEntry.d.ts +37 -7
- package/lib/addExportsMapEntry.js +35 -50
- package/lib/addExportsMapEntry.js.map +1 -1
- package/lib/addExportsMapEntry.test.js +107 -20
- package/lib/addExportsMapEntry.test.js.map +1 -1
- package/lib/createExportsMap.js +29 -47
- package/lib/createExportsMap.js.map +1 -1
- package/lib/createExportsMap.test.js +158 -46
- package/lib/createExportsMap.test.js.map +1 -1
- package/lib/createImportMap.test.js +27 -25
- package/lib/createImportMap.test.js.map +1 -1
- package/lib/findFileInPackage.d.ts +20 -0
- package/lib/findFileInPackage.js +71 -0
- package/lib/findFileInPackage.js.map +1 -0
- package/lib/{resolveImportFromPackage.test.d.ts → findFileInPackage.test.d.ts} +0 -0
- package/lib/findFileInPackage.test.js +95 -0
- package/lib/findFileInPackage.test.js.map +1 -0
- package/lib/findResolveMapEntry.d.ts +5 -1
- package/lib/findResolveMapEntry.js +4 -1
- package/lib/findResolveMapEntry.js.map +1 -1
- package/lib/findResolveMapEntry.test.js +19 -3
- package/lib/findResolveMapEntry.test.js.map +1 -1
- package/lib/index.d.ts +1 -2
- package/lib/index.js +0 -1
- package/lib/index.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +3 -3
- package/lib/resolveImportFromPackage.test.js +0 -163
- package/lib/resolveImportFromPackage.test.js.map +0 -1
- package/lib/resolveImportFromPackagePath.d.ts +0 -27
- package/lib/resolveImportFromPackagePath.js +0 -126
- package/lib/resolveImportFromPackagePath.js.map +0 -1
package/CHANGELOG.json
CHANGED
|
@@ -2,7 +2,58 @@
|
|
|
2
2
|
"name": "@ms-cloudpack/package-utilities",
|
|
3
3
|
"entries": [
|
|
4
4
|
{
|
|
5
|
-
"date": "Wed,
|
|
5
|
+
"date": "Wed, 16 Nov 2022 08:11:17 GMT",
|
|
6
|
+
"tag": "@ms-cloudpack/package-utilities_v2.0.0",
|
|
7
|
+
"version": "2.0.0",
|
|
8
|
+
"comments": {
|
|
9
|
+
"major": [
|
|
10
|
+
{
|
|
11
|
+
"author": "dake.3601@gmail.com",
|
|
12
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
13
|
+
"commit": "589b4c2a5b784aa17faf8078e31f24e4e27b3e84",
|
|
14
|
+
"comment": "Added exclude scoped packages option to findResolveMapEntry function."
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"date": "Tue, 15 Nov 2022 08:13:12 GMT",
|
|
21
|
+
"tag": "@ms-cloudpack/package-utilities_v1.2.1",
|
|
22
|
+
"version": "1.2.1",
|
|
23
|
+
"comments": {
|
|
24
|
+
"patch": [
|
|
25
|
+
{
|
|
26
|
+
"author": "dzearing@microsoft.com",
|
|
27
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
28
|
+
"commit": "a71972309b615d683a5a3bac7a85c981fb6e89af",
|
|
29
|
+
"comment": "When packages have browser/main/module entries which don't exist on disk, fall back to others that might exist."
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"author": "beachball",
|
|
33
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
34
|
+
"comment": "Bump @ms-cloudpack/path-utilities to v1.1.1",
|
|
35
|
+
"commit": "a71972309b615d683a5a3bac7a85c981fb6e89af"
|
|
36
|
+
}
|
|
37
|
+
]
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"date": "Sat, 05 Nov 2022 08:11:41 GMT",
|
|
42
|
+
"tag": "@ms-cloudpack/package-utilities_v1.2.0",
|
|
43
|
+
"version": "1.2.0",
|
|
44
|
+
"comments": {
|
|
45
|
+
"none": [
|
|
46
|
+
{
|
|
47
|
+
"author": "email not defined",
|
|
48
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
49
|
+
"commit": "12e604f31bccd24b3826c9e7390580a97dd6e4ce",
|
|
50
|
+
"comment": "Update devDependency @babel/types to v7.20.2"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"date": "Wed, 02 Nov 2022 08:11:20 GMT",
|
|
6
57
|
"tag": "@ms-cloudpack/package-utilities_v1.2.0",
|
|
7
58
|
"version": "1.2.0",
|
|
8
59
|
"comments": {
|
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
# Change Log - @ms-cloudpack/package-utilities
|
|
2
2
|
|
|
3
|
-
This log was last generated on Wed,
|
|
3
|
+
This log was last generated on Wed, 16 Nov 2022 08:11:17 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
+
## 2.0.0
|
|
8
|
+
|
|
9
|
+
Wed, 16 Nov 2022 08:11:17 GMT
|
|
10
|
+
|
|
11
|
+
### Major changes
|
|
12
|
+
|
|
13
|
+
- Added exclude scoped packages option to findResolveMapEntry function. (dake.3601@gmail.com)
|
|
14
|
+
|
|
15
|
+
## 1.2.1
|
|
16
|
+
|
|
17
|
+
Tue, 15 Nov 2022 08:13:12 GMT
|
|
18
|
+
|
|
19
|
+
### Patches
|
|
20
|
+
|
|
21
|
+
- When packages have browser/main/module entries which don't exist on disk, fall back to others that might exist. (dzearing@microsoft.com)
|
|
22
|
+
- Bump @ms-cloudpack/path-utilities to v1.1.1
|
|
23
|
+
|
|
7
24
|
## 1.2.0
|
|
8
25
|
|
|
9
|
-
Wed, 02 Nov 2022 08:
|
|
26
|
+
Wed, 02 Nov 2022 08:11:20 GMT
|
|
10
27
|
|
|
11
28
|
### Minor changes
|
|
12
29
|
|
|
@@ -1,13 +1,43 @@
|
|
|
1
1
|
import type { PackageDefinitions } from './PackageDefinitions.js';
|
|
2
|
+
import type { PackageJson } from 'type-fest';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* Options fo addExportsMapEntry.
|
|
4
5
|
*/
|
|
5
|
-
export
|
|
6
|
-
|
|
6
|
+
export interface AddExportsMapEntryOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The exports map to add the entry to.
|
|
9
|
+
*/
|
|
10
|
+
exports: PackageJson.Exports;
|
|
11
|
+
/**
|
|
12
|
+
* The package path to add the entry for. This is used to resolve relative paths.
|
|
13
|
+
*/
|
|
7
14
|
packagePath: string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The import path to add the entry for. This is used as the key in the exports map. Defaults
|
|
17
|
+
* to "." being the package import.
|
|
18
|
+
*/
|
|
19
|
+
importPath?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The relative path to the physical file location, representing the "key" in the exports map. Can be a partial
|
|
22
|
+
* location, in which case we will attempt to search for the file and associated source/d.ts files. Defaults to
|
|
23
|
+
* assuming `index.js`.
|
|
24
|
+
*/
|
|
25
|
+
filePath?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Optional types path. If not provided, types will be discovered for internal packages that have source files.
|
|
28
|
+
* This is useful mainly when converting the default import from a package.json that has explicit typings listed.
|
|
29
|
+
*/
|
|
11
30
|
typesPath?: string;
|
|
31
|
+
/**
|
|
32
|
+
* The condition to add the entry for. (Example: "import", "require", "browser", "types") Defaults to "default"
|
|
33
|
+
*/
|
|
34
|
+
condition?: string;
|
|
35
|
+
/**
|
|
36
|
+
* The package definitions cache to use to resolve the file path.
|
|
37
|
+
*/
|
|
12
38
|
packages?: PackageDefinitions;
|
|
13
|
-
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Given an exports map and details about an import path, adds the entry.
|
|
42
|
+
*/
|
|
43
|
+
export declare function addExportsMapEntry(options: AddExportsMapEntryOptions): Promise<void>;
|
|
@@ -1,62 +1,47 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isFile, sourceToIntermediatePath } from '@ms-cloudpack/path-utilities';
|
|
3
|
-
import { isExternalPackage } from './isExternalPackage.js';
|
|
4
|
-
import { resolveImportFromPackagePath } from './resolveImportFromPackagePath.js';
|
|
1
|
+
import { findFileInPackage } from './findFileInPackage.js';
|
|
5
2
|
import { safeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
6
3
|
/**
|
|
7
4
|
* Given an exports map and details about an import path, adds the entry.
|
|
8
5
|
*/
|
|
9
6
|
export async function addExportsMapEntry(options) {
|
|
10
|
-
const { exports,
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
intermediatePath = await resolveImportFromPackagePath(packagePath, entryPath || importPath, {
|
|
25
|
-
packages,
|
|
26
|
-
preferSource: false,
|
|
27
|
-
ignoreExportsMap: true,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
// If at this point we haven't found what we're looking for, don't change anything.
|
|
31
|
-
if (!intermediatePath) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
// Discover d.ts if available.
|
|
35
|
-
if (!resolvedTypesPath) {
|
|
36
|
-
const isTypeScriptSource = sourcePath && ['.ts', '.tsx', '.mts', '.cts'].indexOf(path.extname(sourcePath)) >= 0;
|
|
37
|
-
const potentialTypesPath = path.extname(intermediatePath) === '.js' && intermediatePath.replace('.js', '.d.ts');
|
|
38
|
-
if (potentialTypesPath &&
|
|
39
|
-
((isInternal && isTypeScriptSource) || (!isInternal && isFile(path.join(packagePath, potentialTypesPath))))) {
|
|
40
|
-
resolvedTypesPath = potentialTypesPath;
|
|
7
|
+
const { exports, packagePath, condition = 'default' } = options;
|
|
8
|
+
const importPath = makeSafeImportKey(options.importPath || '.');
|
|
9
|
+
const { filePath, sourcePath, typesPath = options.typesPath ? safeRelativePath(options.typesPath) : undefined, } = await findFileInPackage({
|
|
10
|
+
packagePath,
|
|
11
|
+
filePath: options.filePath || options.importPath || '',
|
|
12
|
+
});
|
|
13
|
+
// Create a local for casting.
|
|
14
|
+
const localExports = exports;
|
|
15
|
+
// Only do work if a file path was provided.
|
|
16
|
+
if (filePath) {
|
|
17
|
+
let exportsEntry = (localExports[importPath] || (localExports[importPath] = {}));
|
|
18
|
+
// Promote string-based exports into objects with default conditions.
|
|
19
|
+
if (typeof exportsEntry === 'string') {
|
|
20
|
+
exportsEntry = exports[importPath] = { default: exportsEntry };
|
|
41
21
|
}
|
|
22
|
+
if (typesPath) {
|
|
23
|
+
exportsEntry['types'] = typesPath;
|
|
24
|
+
}
|
|
25
|
+
if (sourcePath) {
|
|
26
|
+
exportsEntry['source'] = sourcePath;
|
|
27
|
+
}
|
|
28
|
+
// Add the entry for the condition.
|
|
29
|
+
exportsEntry[condition] = filePath;
|
|
42
30
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
exportSet = exports[safeImportPath] = {
|
|
48
|
-
default: exportSet,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
else if (!exportSet) {
|
|
52
|
-
exportSet = exports[safeImportPath] = {};
|
|
31
|
+
}
|
|
32
|
+
function makeSafeImportKey(key) {
|
|
33
|
+
if (!key || key === '' || key === '.') {
|
|
34
|
+
return '.';
|
|
53
35
|
}
|
|
54
|
-
if
|
|
55
|
-
|
|
36
|
+
// if the key starts with a slash, add a dot
|
|
37
|
+
if (key.startsWith('/')) {
|
|
38
|
+
key = '.' + key;
|
|
56
39
|
}
|
|
57
|
-
|
|
58
|
-
if (
|
|
59
|
-
|
|
40
|
+
// if the key doesn't start with "./", add it
|
|
41
|
+
if (!key.startsWith('./')) {
|
|
42
|
+
key = './' + key;
|
|
60
43
|
}
|
|
44
|
+
// Remove the .js extension.
|
|
45
|
+
return key.replace(/\.js$/, '');
|
|
61
46
|
}
|
|
62
47
|
//# sourceMappingURL=addExportsMapEntry.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addExportsMapEntry.js","sourceRoot":"","sources":["../src/addExportsMapEntry.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"addExportsMapEntry.js","sourceRoot":"","sources":["../src/addExportsMapEntry.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAE3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AA8CrE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,OAAkC;IACzE,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,SAAS,GAAG,SAAS,EAAE,GAAG,OAAO,CAAC;IAChE,MAAM,UAAU,GAAG,iBAAiB,CAAC,OAAO,CAAC,UAAU,IAAI,GAAG,CAAC,CAAC;IAChE,MAAM,EACJ,QAAQ,EACR,UAAU,EACV,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,gBAAgB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,GAChF,GAAG,MAAM,iBAAiB,CAAC;QAC1B,WAAW;QACX,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,OAAO,CAAC,UAAU,IAAI,EAAE;KACvD,CAAC,CAAC;IAEH,8BAA8B;IAC9B,MAAM,YAAY,GAAG,OAAiD,CAAC;IAEvE,4CAA4C;IAC5C,IAAI,QAAQ,EAAE;QACZ,IAAI,YAAY,GAAG,CAAC,YAAY,CAAC,UAAU,MAAvB,YAAY,CAAC,UAAU,IAAM,EAAE,EAAC,CAAC;QAErD,qEAAqE;QACrE,IAAI,OAAO,YAAY,KAAK,QAAQ,EAAE;YACpC,YAAY,GAAI,OAAkD,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;SAC5G;QAED,IAAI,SAAS,EAAE;YACb,YAAY,CAAC,OAAO,CAAC,GAAG,SAAS,CAAC;SACnC;QAED,IAAI,UAAU,EAAE;YACd,YAAY,CAAC,QAAQ,CAAC,GAAG,UAAU,CAAC;SACrC;QAED,mCAAmC;QACnC,YAAY,CAAC,SAAS,CAAC,GAAG,QAAQ,CAAC;KACpC;AACH,CAAC;AAED,SAAS,iBAAiB,CAAC,GAAW;IACpC,IAAI,CAAC,GAAG,IAAI,GAAG,KAAK,EAAE,IAAI,GAAG,KAAK,GAAG,EAAE;QACrC,OAAO,GAAG,CAAC;KACZ;IAED,4CAA4C;IAC5C,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;QACvB,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC;KACjB;IAED,6CAA6C;IAC7C,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;QACzB,GAAG,GAAG,IAAI,GAAG,GAAG,CAAC;KAClB;IAED,4BAA4B;IAC5B,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAClC,CAAC"}
|
|
@@ -1,38 +1,89 @@
|
|
|
1
1
|
import { describe, expect, it } from '@jest/globals';
|
|
2
2
|
import { addExportsMapEntry } from './addExportsMapEntry.js';
|
|
3
|
-
import { testProject1Path } from './testPaths.js';
|
|
4
3
|
import { createTestFileStructure } from '@ms-cloudpack/test-utilities';
|
|
5
4
|
import path from 'path';
|
|
6
|
-
describe('addExportsMapEntry', () => {
|
|
7
|
-
it('can
|
|
5
|
+
describe('await addExportsMapEntry', () => {
|
|
6
|
+
it('can ignore entries that have no physical file', async () => {
|
|
8
7
|
const exportsMap = {};
|
|
8
|
+
const packagePath = await createTestFileStructure({});
|
|
9
9
|
await addExportsMapEntry({
|
|
10
10
|
exports: exportsMap,
|
|
11
|
-
packagePath
|
|
11
|
+
packagePath,
|
|
12
|
+
filePath: './lib/foo-sub1',
|
|
13
|
+
});
|
|
14
|
+
expect(exportsMap).toEqual({});
|
|
15
|
+
});
|
|
16
|
+
it('can add an entry with only an importPath', async () => {
|
|
17
|
+
const exportsMap = {};
|
|
18
|
+
const packagePath = await createTestFileStructure({
|
|
19
|
+
'lib/foo-sub1.js': '',
|
|
20
|
+
});
|
|
21
|
+
await addExportsMapEntry({
|
|
22
|
+
exports: exportsMap,
|
|
23
|
+
packagePath,
|
|
12
24
|
importPath: './lib/foo-sub1',
|
|
13
25
|
});
|
|
14
26
|
expect(exportsMap).toMatchInlineSnapshot(`
|
|
15
27
|
{
|
|
16
28
|
"./lib/foo-sub1": {
|
|
17
29
|
"default": "./lib/foo-sub1.js",
|
|
18
|
-
"source": "./src/foo-sub1.ts",
|
|
19
|
-
"types": "./lib/foo-sub1.d.ts",
|
|
20
30
|
},
|
|
21
31
|
}
|
|
22
32
|
`);
|
|
23
33
|
});
|
|
24
|
-
it('can add a
|
|
34
|
+
it('can add an entry with only a file path', async () => {
|
|
25
35
|
const exportsMap = {};
|
|
36
|
+
const packagePath = await createTestFileStructure({
|
|
37
|
+
'lib/foo-sub1.js': '',
|
|
38
|
+
});
|
|
26
39
|
await addExportsMapEntry({
|
|
27
40
|
exports: exportsMap,
|
|
28
|
-
packagePath
|
|
29
|
-
|
|
41
|
+
packagePath,
|
|
42
|
+
filePath: './lib/foo-sub1',
|
|
30
43
|
});
|
|
31
44
|
expect(exportsMap).toMatchInlineSnapshot(`
|
|
32
45
|
{
|
|
33
|
-
"
|
|
46
|
+
".": {
|
|
47
|
+
"default": "./lib/foo-sub1.js",
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
`);
|
|
51
|
+
});
|
|
52
|
+
it('can add an entry with both import and file paths', async () => {
|
|
53
|
+
const exportsMap = {};
|
|
54
|
+
const packagePath = await createTestFileStructure({
|
|
55
|
+
'lib/foo-sub1.js': '',
|
|
56
|
+
});
|
|
57
|
+
await addExportsMapEntry({
|
|
58
|
+
exports: exportsMap,
|
|
59
|
+
packagePath,
|
|
60
|
+
importPath: './foo',
|
|
61
|
+
filePath: './lib/foo-sub1',
|
|
62
|
+
});
|
|
63
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
64
|
+
{
|
|
65
|
+
"./foo": {
|
|
66
|
+
"default": "./lib/foo-sub1.js",
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
`);
|
|
70
|
+
});
|
|
71
|
+
it('can add an entry that has source backing it', async () => {
|
|
72
|
+
const exportsMap = {};
|
|
73
|
+
const packagePath = await createTestFileStructure({
|
|
74
|
+
'src/foo-sub1.tsx': '',
|
|
75
|
+
});
|
|
76
|
+
await addExportsMapEntry({
|
|
77
|
+
exports: exportsMap,
|
|
78
|
+
packagePath,
|
|
79
|
+
importPath: '.',
|
|
80
|
+
filePath: './lib/foo-sub1',
|
|
81
|
+
});
|
|
82
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
83
|
+
{
|
|
84
|
+
".": {
|
|
34
85
|
"default": "./lib/foo-sub1.js",
|
|
35
|
-
"source": "./src/foo-sub1.
|
|
86
|
+
"source": "./src/foo-sub1.tsx",
|
|
36
87
|
"types": "./lib/foo-sub1.d.ts",
|
|
37
88
|
},
|
|
38
89
|
}
|
|
@@ -42,25 +93,28 @@ describe('addExportsMapEntry', () => {
|
|
|
42
93
|
const exportsMap = {
|
|
43
94
|
'.': './lib/index.js',
|
|
44
95
|
};
|
|
96
|
+
const packagePath = await createTestFileStructure({
|
|
97
|
+
'src/foo.tsx': '',
|
|
98
|
+
});
|
|
45
99
|
await addExportsMapEntry({
|
|
46
100
|
exports: exportsMap,
|
|
47
|
-
packagePath
|
|
48
|
-
importPath: './lib/foo
|
|
101
|
+
packagePath,
|
|
102
|
+
importPath: './lib/foo',
|
|
49
103
|
});
|
|
50
104
|
expect(exportsMap).toMatchInlineSnapshot(`
|
|
51
105
|
{
|
|
52
106
|
".": "./lib/index.js",
|
|
53
|
-
"./lib/foo
|
|
54
|
-
"default": "./lib/foo
|
|
55
|
-
"source": "./src/foo
|
|
56
|
-
"types": "./lib/foo
|
|
107
|
+
"./lib/foo": {
|
|
108
|
+
"default": "./lib/foo.js",
|
|
109
|
+
"source": "./src/foo.tsx",
|
|
110
|
+
"types": "./lib/foo.d.ts",
|
|
57
111
|
},
|
|
58
112
|
}
|
|
59
113
|
`);
|
|
60
114
|
});
|
|
61
115
|
it('can add an entry to an external package like the "debug" package', async () => {
|
|
62
116
|
const exportsMap = {};
|
|
63
|
-
const
|
|
117
|
+
const repoPath = await createTestFileStructure({
|
|
64
118
|
'node_modules/debug/package.json': {
|
|
65
119
|
name: 'debug',
|
|
66
120
|
main: './src/index.js',
|
|
@@ -69,15 +123,48 @@ describe('addExportsMapEntry', () => {
|
|
|
69
123
|
'node_modules/debug/src/index.js': '',
|
|
70
124
|
'node_modules/debug/src/browser.js': '',
|
|
71
125
|
});
|
|
126
|
+
const packagePath = path.join(repoPath, 'node_modules/debug');
|
|
127
|
+
await addExportsMapEntry({
|
|
128
|
+
exports: exportsMap,
|
|
129
|
+
packagePath,
|
|
130
|
+
importPath: '.',
|
|
131
|
+
filePath: './src/index.js',
|
|
132
|
+
condition: 'require',
|
|
133
|
+
});
|
|
134
|
+
await addExportsMapEntry({
|
|
135
|
+
exports: exportsMap,
|
|
136
|
+
packagePath,
|
|
137
|
+
importPath: '.',
|
|
138
|
+
filePath: './src/browser.js',
|
|
139
|
+
condition: 'browser',
|
|
140
|
+
});
|
|
141
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
142
|
+
{
|
|
143
|
+
".": {
|
|
144
|
+
"browser": "./src/browser.js",
|
|
145
|
+
"require": "./src/index.js",
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
`);
|
|
149
|
+
});
|
|
150
|
+
it('can add typings specified in input', async () => {
|
|
151
|
+
const exportsMap = {};
|
|
152
|
+
const packagePath = await createTestFileStructure({
|
|
153
|
+
'index.js': '',
|
|
154
|
+
'typings/index.d.ts': '',
|
|
155
|
+
});
|
|
72
156
|
await addExportsMapEntry({
|
|
73
157
|
exports: exportsMap,
|
|
74
|
-
packagePath
|
|
158
|
+
packagePath,
|
|
75
159
|
importPath: '.',
|
|
160
|
+
filePath: './index.js',
|
|
161
|
+
typesPath: './typings/index.d.ts',
|
|
76
162
|
});
|
|
77
163
|
expect(exportsMap).toMatchInlineSnapshot(`
|
|
78
164
|
{
|
|
79
165
|
".": {
|
|
80
|
-
"default": "./
|
|
166
|
+
"default": "./index.js",
|
|
167
|
+
"types": "./typings/index.d.ts",
|
|
81
168
|
},
|
|
82
169
|
}
|
|
83
170
|
`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addExportsMapEntry.test.js","sourceRoot":"","sources":["../src/addExportsMapEntry.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"addExportsMapEntry.test.js","sourceRoot":"","sources":["../src/addExportsMapEntry.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACxC,EAAE,CAAC,+CAA+C,EAAE,KAAK,IAAI,EAAE;QAC7D,MAAM,UAAU,GAAG,EAAE,CAAC;QACtB,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC,EAAE,CAAC,CAAC;QAEtD,MAAM,kBAAkB,CAAC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW;YACX,QAAQ,EAAE,gBAAgB;SAC3B,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC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AC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW;YACX,UAAU,EAAE,GAAG;YACf,QAAQ,EAAE,gBAAgB;YAC1B,SAAS,EAAE,SAAS;SACrB,CAAC,CAAC;QAEH,MAAM,kBAAkB,CAAC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW;YACX,UAAU,EAAE,GAAG;YACf,QAAQ,EAAE,kBAAkB;YAC5B,SAAS,EAAE,SAAS;SACrB,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC;;;;;;;KAOxC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,UAAU,GAA2C,EAAE,CAAC;QAC9D,MAAM,WAAW,GAAG,MAAM,uBAAuB,CAAC;YAChD,UAAU,EAAE,EAAE;YACd,oBAAoB,EAAE,EAAE;SACzB,CAAC,CAAC;QAEH,MAAM,kBAAkB,CAAC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW;YACX,UAAU,EAAE,GAAG;YACf,QAAQ,EAAE,YAAY;YACtB,SAAS,EAAE,sBAAsB;SAClC,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC;;;;;;;KAOxC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib/createExportsMap.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
|
-
import { isFile } from '@ms-cloudpack/path-utilities';
|
|
3
1
|
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
4
2
|
import { addExportsMapEntry } from './addExportsMapEntry.js';
|
|
3
|
+
import { safeRelativePath } from '@ms-cloudpack/path-string-parsing';
|
|
5
4
|
/**
|
|
6
5
|
* Given a package path, generates an export map for the package.
|
|
7
6
|
*/
|
|
@@ -15,60 +14,43 @@ export async function createExportsMap(packagePath, options = {}) {
|
|
|
15
14
|
if (definition.exports) {
|
|
16
15
|
return definition.exports;
|
|
17
16
|
}
|
|
18
|
-
const { main, module,
|
|
19
|
-
const browserString = typeof browser === 'string' ? browser : undefined;
|
|
20
|
-
const rootIndex = isFile(path.join(packagePath, 'index.js')) ? './index.js' : undefined;
|
|
17
|
+
const { browser, main, module, types, typings, type } = definition;
|
|
21
18
|
const exports = {};
|
|
22
|
-
const typesPath =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
19
|
+
const typesPath = types || typings;
|
|
20
|
+
await addExportsMapEntry({
|
|
21
|
+
exports,
|
|
22
|
+
packagePath,
|
|
23
|
+
filePath: 'index.js',
|
|
24
|
+
typesPath,
|
|
25
|
+
condition: type === 'module' ? 'import' : 'default',
|
|
26
|
+
});
|
|
27
|
+
main &&
|
|
28
|
+
(await addExportsMapEntry({
|
|
31
29
|
exports,
|
|
32
|
-
importPath: '.',
|
|
33
30
|
packagePath,
|
|
34
|
-
|
|
35
|
-
entryPath: browserString || module || main || rootIndex,
|
|
31
|
+
filePath: main,
|
|
36
32
|
typesPath,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
const exportUrl = mapValue.import || mapValue.default;
|
|
44
|
-
if (browser[exportUrl]) {
|
|
45
|
-
mapValue['browser'] = browser[exportUrl] || '';
|
|
46
|
-
}
|
|
47
|
-
}
|
|
33
|
+
condition: type === 'module' ? 'import' : 'default',
|
|
34
|
+
}));
|
|
35
|
+
module && (await addExportsMapEntry({ exports, packagePath, filePath: module, typesPath, condition: 'import' }));
|
|
36
|
+
if (browser) {
|
|
37
|
+
if (typeof definition.browser === 'string') {
|
|
38
|
+
await addExportsMapEntry({ exports, packagePath, filePath: definition.browser, condition: 'browser' });
|
|
48
39
|
}
|
|
49
|
-
else {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
else if (typeof definition.browser === 'object') {
|
|
41
|
+
for (const [key, value] of Object.entries(definition.browser)) {
|
|
42
|
+
if (value) {
|
|
43
|
+
await addExportsMapEntry({
|
|
44
|
+
exports,
|
|
45
|
+
packagePath,
|
|
46
|
+
importPath: safeRelativePath(module || main) === safeRelativePath(key) ? '.' : key,
|
|
47
|
+
filePath: value,
|
|
48
|
+
condition: 'browser',
|
|
49
|
+
});
|
|
56
50
|
}
|
|
57
51
|
}
|
|
58
52
|
}
|
|
59
53
|
}
|
|
60
54
|
return exports;
|
|
61
55
|
}
|
|
62
|
-
function makeSafeImportKey(key) {
|
|
63
|
-
// if the key starts with a slash, add a dot
|
|
64
|
-
if (key.startsWith('/')) {
|
|
65
|
-
key = '.' + key;
|
|
66
|
-
}
|
|
67
|
-
// if the key doesn't start with "./", add it
|
|
68
|
-
if (!key.startsWith('./')) {
|
|
69
|
-
key = './' + key;
|
|
70
|
-
}
|
|
71
|
-
// Remove the .js extension.
|
|
72
|
-
return key.replace(/\.js$/, '');
|
|
73
|
-
}
|
|
74
56
|
//# sourceMappingURL=createExportsMap.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createExportsMap.js","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createExportsMap.js","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AAErE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,WAAmB,EACnB,UAA6C,EAAE;IAE/C,MAAM,EAAE,QAAQ,GAAG,kBAAkB,CAAC,WAAW,EAAE,EAA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