@ms-cloudpack/package-utilities 0.13.1 → 0.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.
- package/CHANGELOG.json +46 -1
- package/CHANGELOG.md +18 -2
- package/lib/PackageDefinitions.d.ts +1 -0
- package/lib/PackageDefinitions.js +2 -1
- package/lib/PackageDefinitions.js.map +1 -1
- package/lib/addExportsMapEntry.d.ts +13 -0
- package/lib/addExportsMapEntry.js +41 -0
- package/lib/addExportsMapEntry.js.map +1 -0
- package/lib/{evaluateImportsFromEntries.test.d.ts → addExportsMapEntry.test.d.ts} +0 -0
- package/lib/addExportsMapEntry.test.js +55 -0
- package/lib/addExportsMapEntry.test.js.map +1 -0
- package/lib/createExportsMap.d.ts +8 -0
- package/lib/createExportsMap.js +74 -0
- package/lib/createExportsMap.js.map +1 -0
- package/lib/{generateExportsMap.test.d.ts → createExportsMap.test.d.ts} +0 -0
- package/lib/createExportsMap.test.js +145 -0
- package/lib/createExportsMap.test.js.map +1 -0
- package/lib/createImportMap.d.ts +7 -2
- package/lib/createImportMap.js +49 -29
- package/lib/createImportMap.js.map +1 -1
- package/lib/createImportMap.test.js +44 -157
- package/lib/createImportMap.test.js.map +1 -1
- package/lib/createResolveMap.d.ts +2 -0
- package/lib/createResolveMap.js +4 -5
- package/lib/createResolveMap.js.map +1 -1
- package/lib/createResolveMap.test.js +6 -5
- package/lib/createResolveMap.test.js.map +1 -1
- package/lib/findResolveMapEntry.d.ts +6 -0
- package/lib/findResolveMapEntry.js +12 -0
- package/lib/findResolveMapEntry.js.map +1 -0
- package/lib/{getFlattenedExportsMap.test.d.ts → findResolveMapEntry.test.d.ts} +0 -0
- package/lib/findResolveMapEntry.test.js +52 -0
- package/lib/findResolveMapEntry.test.js.map +1 -0
- package/lib/{getFlattenedExportsMap.d.ts → flattenExportsMap.d.ts} +4 -1
- package/lib/flattenExportsMap.js +67 -0
- package/lib/flattenExportsMap.js.map +1 -0
- package/lib/flattenExportsMap.test.d.ts +1 -0
- package/lib/flattenExportsMap.test.js +185 -0
- package/lib/flattenExportsMap.test.js.map +1 -0
- package/lib/getExportPathFromEntry.js +2 -1
- package/lib/getExportPathFromEntry.js.map +1 -1
- package/lib/getExportPathFromEntry.test.js +0 -8
- package/lib/getExportPathFromEntry.test.js.map +1 -1
- package/lib/getExportsMap.d.ts +5 -0
- package/lib/getExportsMap.js +11 -0
- package/lib/getExportsMap.js.map +1 -0
- package/lib/index.d.ts +10 -7
- package/lib/index.js +15 -6
- package/lib/index.js.map +1 -1
- package/lib/parseImportString.d.ts +3 -3
- package/lib/parseImportString.js +4 -4
- package/lib/parseImportString.js.map +1 -1
- package/lib/parseImportString.test.js +21 -3
- package/lib/parseImportString.test.js.map +1 -1
- package/lib/resolveImportFromPackage.test.js +7 -18
- package/lib/resolveImportFromPackage.test.js.map +1 -1
- package/lib/resolveImportFromPackagePath.d.ts +10 -5
- package/lib/resolveImportFromPackagePath.js +58 -47
- package/lib/resolveImportFromPackagePath.js.map +1 -1
- package/lib/resolvePackageEntries.d.ts +8 -2
- package/lib/resolvePackageEntries.js +8 -37
- package/lib/resolvePackageEntries.js.map +1 -1
- package/lib/resolvePackageEntries.test.js +76 -77
- package/lib/resolvePackageEntries.test.js.map +1 -1
- package/lib/safeRelativePath.d.ts +4 -0
- package/lib/safeRelativePath.js +19 -0
- package/lib/safeRelativePath.js.map +1 -0
- package/lib/safeRelativePath.test.d.ts +1 -0
- package/lib/safeRelativePath.test.js +17 -0
- package/lib/safeRelativePath.test.js.map +1 -0
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +2 -2
- package/lib/evaluateImportsFromEntries.d.ts +0 -31
- package/lib/evaluateImportsFromEntries.js +0 -151
- package/lib/evaluateImportsFromEntries.js.map +0 -1
- package/lib/evaluateImportsFromEntries.test.js +0 -39
- package/lib/evaluateImportsFromEntries.test.js.map +0 -1
- package/lib/generateExportsMap.d.ts +0 -9
- package/lib/generateExportsMap.js +0 -80
- package/lib/generateExportsMap.js.map +0 -1
- package/lib/generateExportsMap.test.js +0 -100
- package/lib/generateExportsMap.test.js.map +0 -1
- package/lib/getFlattenedExportsMap.js +0 -77
- package/lib/getFlattenedExportsMap.js.map +0 -1
- package/lib/getFlattenedExportsMap.test.js +0 -270
- package/lib/getFlattenedExportsMap.test.js.map +0 -1
package/CHANGELOG.json
CHANGED
|
@@ -2,7 +2,52 @@
|
|
|
2
2
|
"name": "@ms-cloudpack/package-utilities",
|
|
3
3
|
"entries": [
|
|
4
4
|
{
|
|
5
|
-
"date": "
|
|
5
|
+
"date": "Wed, 12 Oct 2022 08:10:23 GMT",
|
|
6
|
+
"tag": "@ms-cloudpack/package-utilities_v0.15.0",
|
|
7
|
+
"version": "0.15.0",
|
|
8
|
+
"comments": {
|
|
9
|
+
"minor": [
|
|
10
|
+
{
|
|
11
|
+
"author": "dzearing@microsoft.com",
|
|
12
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
13
|
+
"commit": "03233b20287555cc8994203f05be01cb8a3fb225",
|
|
14
|
+
"comment": "Overhauling import/export map utilities."
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"date": "Tue, 11 Oct 2022 08:16:37 GMT",
|
|
21
|
+
"tag": "@ms-cloudpack/package-utilities_v0.14.0",
|
|
22
|
+
"version": "0.14.0",
|
|
23
|
+
"comments": {
|
|
24
|
+
"none": [
|
|
25
|
+
{
|
|
26
|
+
"author": "email not defined",
|
|
27
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
28
|
+
"commit": "eaac5bbb36b4cc8105df8146bbe9366b0ed2e7ed",
|
|
29
|
+
"comment": "Update babel monorepo to v7.19.4"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"date": "Wed, 05 Oct 2022 19:39:20 GMT",
|
|
36
|
+
"tag": "@ms-cloudpack/package-utilities_v0.14.0",
|
|
37
|
+
"version": "0.14.0",
|
|
38
|
+
"comments": {
|
|
39
|
+
"minor": [
|
|
40
|
+
{
|
|
41
|
+
"author": "dzearing@microsoft.com",
|
|
42
|
+
"package": "@ms-cloudpack/package-utilities",
|
|
43
|
+
"commit": "a5958df7dbb6455200dee17b7201273b7bb8155a",
|
|
44
|
+
"comment": "Overhaul of import/export map utilities. We now have getExportsMap, createExportsMap, flattenExportsMap, and createImportMap. Also removing some of the deprecated apis."
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"date": "Thu, 29 Sep 2022 17:46:30 GMT",
|
|
6
51
|
"tag": "@ms-cloudpack/package-utilities_v0.13.1",
|
|
7
52
|
"version": "0.13.1",
|
|
8
53
|
"comments": {
|
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,28 @@
|
|
|
1
1
|
# Change Log - @ms-cloudpack/package-utilities
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Wed, 12 Oct 2022 08:10:23 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
+
## 0.15.0
|
|
8
|
+
|
|
9
|
+
Wed, 12 Oct 2022 08:10:23 GMT
|
|
10
|
+
|
|
11
|
+
### Minor changes
|
|
12
|
+
|
|
13
|
+
- Overhauling import/export map utilities. (dzearing@microsoft.com)
|
|
14
|
+
|
|
15
|
+
## 0.14.0
|
|
16
|
+
|
|
17
|
+
Wed, 05 Oct 2022 19:39:20 GMT
|
|
18
|
+
|
|
19
|
+
### Minor changes
|
|
20
|
+
|
|
21
|
+
- Overhaul of import/export map utilities. We now have getExportsMap, createExportsMap, flattenExportsMap, and createImportMap. Also removing some of the deprecated apis. (dzearing@microsoft.com)
|
|
22
|
+
|
|
7
23
|
## 0.13.1
|
|
8
24
|
|
|
9
|
-
Thu, 29 Sep 2022 17:46:
|
|
25
|
+
Thu, 29 Sep 2022 17:46:30 GMT
|
|
10
26
|
|
|
11
27
|
### Patches
|
|
12
28
|
|
|
@@ -4,6 +4,7 @@ export declare class PackageDefinitions {
|
|
|
4
4
|
static getInstance(): PackageDefinitions;
|
|
5
5
|
private _cache;
|
|
6
6
|
private _transforms;
|
|
7
|
+
constructor(definitionMap?: Record<string, PackageJson>);
|
|
7
8
|
get(packagePath: string, options?: {
|
|
8
9
|
refresh: boolean;
|
|
9
10
|
}): Promise<PackageJson | undefined>;
|
|
@@ -3,9 +3,10 @@ import { applyOverrides } from '@ms-cloudpack/package-overrides';
|
|
|
3
3
|
import { readJson } from '@ms-cloudpack/json-utilities';
|
|
4
4
|
let _singleton;
|
|
5
5
|
export class PackageDefinitions {
|
|
6
|
-
constructor() {
|
|
6
|
+
constructor(definitionMap = {}) {
|
|
7
7
|
this._cache = {};
|
|
8
8
|
this._transforms = [applyOverrides];
|
|
9
|
+
this._cache = definitionMap;
|
|
9
10
|
}
|
|
10
11
|
static getInstance() {
|
|
11
12
|
if (!_singleton) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PackageDefinitions.js","sourceRoot":"","sources":["../src/PackageDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAGxD,IAAI,UAA0C,CAAC;AAG/C,MAAM,OAAO,kBAAkB;
|
|
1
|
+
{"version":3,"file":"PackageDefinitions.js","sourceRoot":"","sources":["../src/PackageDefinitions.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AAGxD,IAAI,UAA0C,CAAC;AAG/C,MAAM,OAAO,kBAAkB;IAW7B,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
|
3
|
+
* Given an exports map and details about an import path, adds the entry.
|
|
4
|
+
*/
|
|
5
|
+
export declare function addExportsMapEntry(options: {
|
|
6
|
+
exports: Record<string, Record<string, string>>;
|
|
7
|
+
packagePath: string;
|
|
8
|
+
importPath: string;
|
|
9
|
+
entryPath?: string;
|
|
10
|
+
condition?: string;
|
|
11
|
+
typesPath?: string;
|
|
12
|
+
packages?: PackageDefinitions;
|
|
13
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { isFile, sourceToIntermediatePath } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import { isExternalPackage } from './isExternalPackage.js';
|
|
4
|
+
import { resolveImportFromPackagePath } from './resolveImportFromPackagePath.js';
|
|
5
|
+
import { safeRelativePath } from './safeRelativePath.js';
|
|
6
|
+
/**
|
|
7
|
+
* Given an exports map and details about an import path, adds the entry.
|
|
8
|
+
*/
|
|
9
|
+
export async function addExportsMapEntry(options) {
|
|
10
|
+
const { exports, packages, packagePath, importPath, entryPath, typesPath, condition = 'default' } = options;
|
|
11
|
+
const isInternal = !isExternalPackage(packagePath);
|
|
12
|
+
const sourcePath = await resolveImportFromPackagePath(packagePath, entryPath || importPath, {
|
|
13
|
+
packages,
|
|
14
|
+
preferSource: isInternal,
|
|
15
|
+
ignoreExportsMap: true,
|
|
16
|
+
});
|
|
17
|
+
const intermediatePath = sourcePath && sourceToIntermediatePath(sourcePath);
|
|
18
|
+
let resolvedTypesPath = typesPath;
|
|
19
|
+
// If at this point we haven't found what we're looking for, throw an exception.
|
|
20
|
+
if (!intermediatePath) {
|
|
21
|
+
throw new Error(`Unable to resolve import path ${importPath} in package ${packagePath}`);
|
|
22
|
+
}
|
|
23
|
+
// Discover d.ts if available.
|
|
24
|
+
if (!resolvedTypesPath) {
|
|
25
|
+
const isTypeScriptSource = sourcePath && ['.ts', '.tsx', '.mts', '.cts'].indexOf(path.extname(sourcePath)) >= 0;
|
|
26
|
+
const potentialTypesPath = path.extname(intermediatePath) === '.js' && intermediatePath.replace('.js', '.d.ts');
|
|
27
|
+
if (potentialTypesPath &&
|
|
28
|
+
((isInternal && isTypeScriptSource) || (!isInternal && isFile(path.join(packagePath, potentialTypesPath))))) {
|
|
29
|
+
resolvedTypesPath = potentialTypesPath;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const exportSet = (exports[importPath] || (exports[importPath] = {}));
|
|
33
|
+
if (resolvedTypesPath) {
|
|
34
|
+
exportSet.types = safeRelativePath(resolvedTypesPath);
|
|
35
|
+
}
|
|
36
|
+
exportSet[condition] = intermediatePath;
|
|
37
|
+
if (sourcePath && sourcePath !== intermediatePath) {
|
|
38
|
+
exportSet.source = safeRelativePath(sourcePath);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=addExportsMapEntry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addExportsMapEntry.js","sourceRoot":"","sources":["../src/addExportsMapEntry.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,wBAAwB,EAAE,MAAM,8BAA8B,CAAC;AAEhF,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,OAQxC;IACC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,GAAG,SAAS,EAAE,GAAG,OAAO,CAAC;IAC5G,MAAM,UAAU,GAAG,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;IACnD,MAAM,UAAU,GAAG,MAAM,4BAA4B,CAAC,WAAW,EAAE,SAA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CAAC,MAAM,GAAG,gBAAgB,CAAC,UAAU,CAAC,CAAC;KACjD;AACH,CAAC"}
|
|
File without changes
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals';
|
|
2
|
+
import { addExportsMapEntry } from './addExportsMapEntry.js';
|
|
3
|
+
import { testProject1Path } from './testPaths.js';
|
|
4
|
+
describe('addExportsMapEntry', () => {
|
|
5
|
+
it('can add an entry to an empty exports map', async () => {
|
|
6
|
+
const exportsMap = {};
|
|
7
|
+
await addExportsMapEntry({
|
|
8
|
+
exports: exportsMap,
|
|
9
|
+
packagePath: testProject1Path,
|
|
10
|
+
importPath: './lib/foo-sub1',
|
|
11
|
+
});
|
|
12
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
13
|
+
{
|
|
14
|
+
"./lib/foo-sub1": {
|
|
15
|
+
"default": "./lib/foo-sub1.js",
|
|
16
|
+
"source": "./src/foo-sub1.ts",
|
|
17
|
+
"types": "./lib/foo-sub1.d.ts",
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
`);
|
|
21
|
+
});
|
|
22
|
+
it(' can add an entry to an existing exports map', async () => {
|
|
23
|
+
const exportsMap = {
|
|
24
|
+
'.': {
|
|
25
|
+
default: './lib/index.js',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
await addExportsMapEntry({
|
|
29
|
+
exports: exportsMap,
|
|
30
|
+
packagePath: testProject1Path,
|
|
31
|
+
importPath: './lib/foo-sub1',
|
|
32
|
+
});
|
|
33
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
34
|
+
{
|
|
35
|
+
".": {
|
|
36
|
+
"default": "./lib/index.js",
|
|
37
|
+
},
|
|
38
|
+
"./lib/foo-sub1": {
|
|
39
|
+
"default": "./lib/foo-sub1.js",
|
|
40
|
+
"source": "./src/foo-sub1.ts",
|
|
41
|
+
"types": "./lib/foo-sub1.d.ts",
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
`);
|
|
45
|
+
});
|
|
46
|
+
it('can throw when the entry does not exist', async () => {
|
|
47
|
+
const exportsMap = {};
|
|
48
|
+
await expect(addExportsMapEntry({
|
|
49
|
+
exports: exportsMap,
|
|
50
|
+
packagePath: testProject1Path,
|
|
51
|
+
importPath: './lib/foo-sub2',
|
|
52
|
+
})).rejects.toThrow();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=addExportsMapEntry.test.js.map
|
|
@@ -0,0 +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,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAElD,QAAQ,CAAC,oBAAoB,EAAE,GAAG,EAAE;IAClC,EAAE,CAAC,0CAA0C,EAAE,KAAK,IAAI,EAAE;QACxD,MAAM,UAAU,GAAG,EAAE,CAAC;QAEtB,MAAM,kBAAkB,CAAC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW,EAAE,gBAAgB;YAC7B,UAAU,EAAE,gBAAgB;SAC7B,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;KAQxC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,8CAA8C,EAAE,KAAK,IAAI,EAAE;QAC5D,MAAM,UAAU,GAAG;YACjB,GAAG,EAAE;gBACH,OAAO,EAAE,gBAAgB;aAC1B;SACF,CAAC;QAEF,MAAM,kBAAkB,CAAC;YACvB,OAAO,EAAE,UAAU;YACnB,WAAW,EAAE,gBAAgB;YAC7B,UAAU,EAAE,gBAAgB;SAC7B,CAAC,CAAC;QAEH,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;;;;KAWxC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yCAAyC,EAAE,KAAK,IAAI,EAAE;QACvD,MAAM,UAAU,GAAG,EAAE,CAAC;QAEtB,MAAM,MAAM,CACV,kBAAkB,CAAC;YACjB,OAAO,EAAE,UAAU;YACnB,WAAW,EAAE,gBAAgB;YAC7B,UAAU,EAAE,gBAAgB;SAC7B,CAAC,CACH,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;IACtB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PackageJson } from 'type-fest';
|
|
2
|
+
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
3
|
+
/**
|
|
4
|
+
* Given a package path, generates an export map for the package.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createExportsMap(packagePath: string, options?: {
|
|
7
|
+
packages?: PackageDefinitions;
|
|
8
|
+
}): Promise<PackageJson.Exports>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { isFile } from '@ms-cloudpack/path-utilities';
|
|
3
|
+
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
4
|
+
import { addExportsMapEntry } from './addExportsMapEntry.js';
|
|
5
|
+
/**
|
|
6
|
+
* Given a package path, generates an export map for the package.
|
|
7
|
+
*/
|
|
8
|
+
export async function createExportsMap(packagePath, options = {}) {
|
|
9
|
+
const { packages = PackageDefinitions.getInstance() } = options;
|
|
10
|
+
const definition = await packages.get(packagePath);
|
|
11
|
+
if (!definition) {
|
|
12
|
+
throw new Error(`Package definition not found for ${packagePath}`);
|
|
13
|
+
}
|
|
14
|
+
// Don't create an exports map for a definition that already has one.
|
|
15
|
+
if (definition.exports) {
|
|
16
|
+
return definition.exports;
|
|
17
|
+
}
|
|
18
|
+
const { main, module, browser } = definition;
|
|
19
|
+
const browserString = typeof browser === 'string' ? browser : undefined;
|
|
20
|
+
const rootIndex = isFile(path.join(packagePath, 'index.js')) ? './index.js' : undefined;
|
|
21
|
+
const exports = {};
|
|
22
|
+
const typesPath = definition.types || definition.typings;
|
|
23
|
+
const hasPackageEntry = browserString || module || main || rootIndex;
|
|
24
|
+
if (!hasPackageEntry) {
|
|
25
|
+
if (!browser) {
|
|
26
|
+
return exports;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
await addExportsMapEntry({
|
|
31
|
+
exports,
|
|
32
|
+
importPath: '.',
|
|
33
|
+
packagePath,
|
|
34
|
+
condition: 'default',
|
|
35
|
+
entryPath: browserString || module || main || rootIndex,
|
|
36
|
+
typesPath,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (typeof browser === 'object') {
|
|
40
|
+
// If we have an entry defined in the package, iterate through exports and update things.
|
|
41
|
+
if (hasPackageEntry) {
|
|
42
|
+
for (const mapValue of Object.values(exports)) {
|
|
43
|
+
const exportUrl = mapValue.import || mapValue.default;
|
|
44
|
+
if (browser[exportUrl]) {
|
|
45
|
+
mapValue['browser'] = browser[exportUrl] || '';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// When all we have are browser entries, attempt to make exports out of them.
|
|
51
|
+
for (const [key, value] of Object.entries(browser)) {
|
|
52
|
+
if (typeof value === 'string' && path.extname(value) === '.js') {
|
|
53
|
+
exports[makeSafeImportKey(key)] = {
|
|
54
|
+
browser: value,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return exports;
|
|
61
|
+
}
|
|
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
|
+
//# sourceMappingURL=createExportsMap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createExportsMap.js","sourceRoot":"","sources":["../src/createExportsMap.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,EAAE,MAAM,EAAE,MAAM,8BAA8B,CAAC;AACtD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,WAAmB,EACnB,UAA6C,EAAE;IAE/C,MAAM,EAAE,QAAQ,GAAG,kBAAkB,CAAC,WAAW,EAAE,EAAE,GAAG,OAAO,CAAC;IAChE,MAAM,UAAU,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IAEnD,IAAI,CAAC,UAAU,EAAE;QACf,MAAM,IAAI,KAAK,CAAC,oCAAoC,WAAW,EAAE,CAAC,CAAC;KACpE;IAED,qEAAqE;IACrE,IAAI,UAAU,CAAC,OAAO,EAAE;QACtB,OAAO,UAAU,CAAC,OAAO,CAAC;KAC3B;IAED,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;IAC7C,MAAM,aAAa,GAAG,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IACxE,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;IACxF,MAAM,OAAO,GAA2C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A4C;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"}
|
|
File without changes
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { describe, it, expect } from '@jest/globals';
|
|
2
|
+
import { createExportsMap } from './createExportsMap.js';
|
|
3
|
+
import { testScenariosPath } from './testPaths.js';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
6
|
+
import { createTestFileStructure } from '@ms-cloudpack/test-utilities';
|
|
7
|
+
const testGenerateExportsPath = path.join(testScenariosPath, 'test-generate-exports');
|
|
8
|
+
describe('createExportsMap', () => {
|
|
9
|
+
it('can return a blank import map for something with nothing to export', async () => {
|
|
10
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'no-exports'))).toEqual({});
|
|
11
|
+
});
|
|
12
|
+
it('can generate an exports map for a package with an implicit index.js file', async () => {
|
|
13
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'implicit-export'))).toMatchInlineSnapshot(`
|
|
14
|
+
{
|
|
15
|
+
".": {
|
|
16
|
+
"default": "./index.js",
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
`);
|
|
20
|
+
});
|
|
21
|
+
it('can generate an exports map for a package with a main export as esm', async () => {
|
|
22
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'main-export-esm'))).toMatchInlineSnapshot(`
|
|
23
|
+
{
|
|
24
|
+
".": {
|
|
25
|
+
"default": "./main.js",
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
`);
|
|
29
|
+
});
|
|
30
|
+
it('can generate an exports map for a package with a main export as cjs', async () => {
|
|
31
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'main-export-cjs'))).toMatchInlineSnapshot(`
|
|
32
|
+
{
|
|
33
|
+
".": {
|
|
34
|
+
"default": "./main.js",
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
`);
|
|
38
|
+
});
|
|
39
|
+
it('can generate an exports map for a package with a main export as cjs and types', async () => {
|
|
40
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'main-export-cjs-types'))).toMatchInlineSnapshot(`
|
|
41
|
+
{
|
|
42
|
+
".": {
|
|
43
|
+
"default": "./main.js",
|
|
44
|
+
"types": "./index.d.ts",
|
|
45
|
+
},
|
|
46
|
+
}
|
|
47
|
+
`);
|
|
48
|
+
});
|
|
49
|
+
it('can generate an exports map for a package with a module export', async () => {
|
|
50
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'module-export'))).toMatchInlineSnapshot(`
|
|
51
|
+
{
|
|
52
|
+
".": {
|
|
53
|
+
"default": "./module.js",
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
`);
|
|
57
|
+
});
|
|
58
|
+
it('can generate an exports map for a package with a browser export', async () => {
|
|
59
|
+
const testPath = await createTestFileStructure({
|
|
60
|
+
'package.json': {
|
|
61
|
+
name: 'browser-export',
|
|
62
|
+
main: 'main.js',
|
|
63
|
+
module: 'module.js',
|
|
64
|
+
browser: 'browser.js',
|
|
65
|
+
},
|
|
66
|
+
'main.js': '',
|
|
67
|
+
'module.js': '',
|
|
68
|
+
'browser.js': '',
|
|
69
|
+
});
|
|
70
|
+
expect(await createExportsMap(testPath)).toMatchInlineSnapshot(`
|
|
71
|
+
{
|
|
72
|
+
".": {
|
|
73
|
+
"default": "./browser.js",
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
`);
|
|
77
|
+
});
|
|
78
|
+
it('can generate an exports map for a package with a type/source exports', async () => {
|
|
79
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'sources-export'))).toMatchInlineSnapshot(`
|
|
80
|
+
{
|
|
81
|
+
".": {
|
|
82
|
+
"default": "./lib/index.js",
|
|
83
|
+
"source": "./src/index.tsx",
|
|
84
|
+
"types": "./lib/index.d.ts",
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
`);
|
|
88
|
+
});
|
|
89
|
+
it('can generate an exports map for an internal unbuilt package', async () => {
|
|
90
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'main-export-unbuilt'))).toMatchInlineSnapshot(`
|
|
91
|
+
{
|
|
92
|
+
".": {
|
|
93
|
+
"default": "./lib/index.js",
|
|
94
|
+
"source": "./src/index.ts",
|
|
95
|
+
"types": "./lib/index.d.ts",
|
|
96
|
+
},
|
|
97
|
+
}
|
|
98
|
+
`);
|
|
99
|
+
});
|
|
100
|
+
it('can generate an exports map for an external package with implicit type exports', async () => {
|
|
101
|
+
expect(await createExportsMap(path.join(testGenerateExportsPath, 'node_modules/ext-esm-types')))
|
|
102
|
+
.toMatchInlineSnapshot(`
|
|
103
|
+
{
|
|
104
|
+
".": {
|
|
105
|
+
"default": "./lib/main.js",
|
|
106
|
+
"types": "./lib/main.d.ts",
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
`);
|
|
110
|
+
});
|
|
111
|
+
it(`can find exports from browser-only packages like uuid@3.4.0`, async () => {
|
|
112
|
+
const packages = new PackageDefinitions();
|
|
113
|
+
const testPath = await createTestFileStructure({
|
|
114
|
+
'package.json': {
|
|
115
|
+
name: 'uuid',
|
|
116
|
+
browser: {
|
|
117
|
+
'./lib/rng.js': './lib/rng-browser.js',
|
|
118
|
+
'./lib/sha1.js': './lib/sha1-browser.js',
|
|
119
|
+
'./lib/md5.js': './lib/md5-browser.js',
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
'lib/rng.js': '',
|
|
123
|
+
'lib/sha1.js': '',
|
|
124
|
+
'lib/md5.js': '',
|
|
125
|
+
});
|
|
126
|
+
const exportsMap = (await createExportsMap(testPath, { packages }));
|
|
127
|
+
expect(typeof exportsMap).toEqual('object');
|
|
128
|
+
expect(exportsMap?.['.']).toBeUndefined();
|
|
129
|
+
expect(exportsMap).toMatchInlineSnapshot(`
|
|
130
|
+
{
|
|
131
|
+
"./lib/md5": {
|
|
132
|
+
"browser": "./lib/md5-browser.js",
|
|
133
|
+
},
|
|
134
|
+
"./lib/rng": {
|
|
135
|
+
"browser": "./lib/rng-browser.js",
|
|
136
|
+
},
|
|
137
|
+
"./lib/sha1": {
|
|
138
|
+
"browser": "./lib/sha1-browser.js",
|
|
139
|
+
},
|
|
140
|
+
}
|
|
141
|
+
`);
|
|
142
|
+
expect(Object.keys(exportsMap).length).toBeGreaterThan(0);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=createExportsMap.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createExportsMap.test.js","sourceRoot":"","sources":["../src/createExportsMap.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,MAAM,uBAAuB,GAAG,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,uBAAuB,CAAC,CAAC;AAEtF,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,EAAE,CAAC,oEAAoE,EAAE,KAAK,IAAI,EAAE;QAClF,MAAM,CAAC,MAAM,gBAAgB,CAAC,IAAI,CAAC,IAAI,CA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MAAM,uBAAuB,CAAC;YAC7C,cAAc,EAAE;gBACd,IAAI,EAAE,MAAM;gBACZ,OAAO,EAAE;oBACP,cAAc,EAAE,sBAAsB;oBACtC,eAAe,EAAE,uBAAuB;oBACxC,cAAc,EAAE,sBAAsB;iBACvC;aACF;YACD,YAAY,EAAE,EAAE;YAChB,aAAa,EAAE,EAAE;YACjB,YAAY,EAAE,EAAE;SACjB,CAAC,CAAC;QAEH,MAAM,UAAU,GAAG,CAAC,MAAM,gBAAgB,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,CAAC,CAA2C,CAAC;QAC9G,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;QAC5C,MAAM,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;QAC1C,MAAM,CAAC,UAAU,CAAC,CAAC,qBAAqB,CAAC;;;;;;;;;;;;KAYxC,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAA0D,CAAC,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAC5G,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/lib/createImportMap.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { ResolveMap } from './createResolveMap.js';
|
|
2
|
-
import
|
|
2
|
+
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
3
3
|
export interface ImportMap {
|
|
4
4
|
imports: Record<string, string>;
|
|
5
5
|
scopes?: Record<string, Record<string, string>>;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Given a resolve ma and a bundleServerUrl, returns an import map to be used in the browser.
|
|
9
|
+
*/
|
|
10
|
+
export declare function createImportMap(resolveMap: ResolveMap, bundleServerUrl: string, options?: {
|
|
11
|
+
packages?: PackageDefinitions;
|
|
12
|
+
}): Promise<ImportMap>;
|
package/lib/createImportMap.js
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
1
1
|
import { PackageDefinitions } from './PackageDefinitions.js';
|
|
2
2
|
import { merge } from 'merge';
|
|
3
|
-
import {
|
|
4
|
-
import fs from 'fs';
|
|
3
|
+
import { flattenExportsMap } from './flattenExportsMap.js';
|
|
5
4
|
import path from 'path';
|
|
6
|
-
|
|
5
|
+
import { getExportsMap } from './getExportsMap.js';
|
|
6
|
+
import { isExternalPackage } from './isExternalPackage.js';
|
|
7
|
+
/**
|
|
8
|
+
* Given a resolve ma and a bundleServerUrl, returns an import map to be used in the browser.
|
|
9
|
+
*/
|
|
10
|
+
export async function createImportMap(resolveMap, bundleServerUrl, options = {}) {
|
|
7
11
|
var _a, _b;
|
|
12
|
+
const { packages = PackageDefinitions.getInstance() } = options;
|
|
8
13
|
const importMap = {
|
|
9
14
|
imports: {},
|
|
10
15
|
};
|
|
11
16
|
for (const resolveEntry of Object.values(resolveMap)) {
|
|
12
|
-
merge(importMap.imports, await getImportMapFromEntry(resolveEntry, bundleServerUrl,
|
|
17
|
+
merge(importMap.imports, await getImportMapFromEntry(resolveEntry, bundleServerUrl, packages));
|
|
13
18
|
if (resolveEntry.scopedVersions) {
|
|
14
19
|
// Initialize scopes
|
|
15
20
|
importMap.scopes ?? (importMap.scopes = {});
|
|
16
21
|
for (const scopedEntry of Object.values(resolveEntry.scopedVersions)) {
|
|
17
|
-
const importMapForEntry = await getImportMapFromEntry(scopedEntry, bundleServerUrl,
|
|
22
|
+
const importMapForEntry = await getImportMapFromEntry(scopedEntry, bundleServerUrl, packages);
|
|
18
23
|
for (const requiredById of Object.keys(scopedEntry.requiredBy)) {
|
|
19
24
|
const scopes = ((_a = importMap.scopes)[_b = `${bundleServerUrl}/${requiredById}/`] ?? (_a[_b] = {}));
|
|
20
25
|
merge(scopes, importMapForEntry);
|
|
@@ -24,35 +29,50 @@ export async function createImportMap(resolveMap, bundleServerUrl, packageDefini
|
|
|
24
29
|
}
|
|
25
30
|
return importMap;
|
|
26
31
|
}
|
|
27
|
-
async function getImportMapFromEntry(entry, bundleServerUrl,
|
|
32
|
+
async function getImportMapFromEntry(entry, bundleServerUrl, packages) {
|
|
28
33
|
const importMap = {};
|
|
29
|
-
const definition =
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
34
|
+
const definition = await packages.get(entry.path);
|
|
35
|
+
const exportsMap = await getExportsMap(entry.path, { packages });
|
|
36
|
+
if (!definition ||
|
|
37
|
+
!definition.name ||
|
|
38
|
+
!definition.version ||
|
|
39
|
+
!exportsMap ||
|
|
40
|
+
Object.keys(exportsMap).length === 0 ||
|
|
41
|
+
definition.name.startsWith('@types/')) {
|
|
42
|
+
return importMap;
|
|
43
|
+
}
|
|
44
|
+
// Get both the unbundled entries from the exports map
|
|
45
|
+
const unbundledExportsMap = flattenExportsMap(exportsMap, {
|
|
46
|
+
conditions: ['browser-esm'],
|
|
47
|
+
requiredConditions: ['browser-esm'],
|
|
48
|
+
});
|
|
49
|
+
const bundledExportsMap = flattenExportsMap(exportsMap);
|
|
50
|
+
const packagePrefix = `${definition.name}@${definition.version}/`;
|
|
51
|
+
const baseUrl = `${bundleServerUrl}/${packagePrefix}`;
|
|
52
|
+
// Add the bundled entries to the import map, but prefer unbundled if the entry exists.
|
|
53
|
+
for (const [importPath, bundledFilePath] of Object.entries(bundledExportsMap)) {
|
|
54
|
+
const isExternal = isExternalPackage(entry.path);
|
|
55
|
+
const unbundledFilePath = isExternal ? unbundledExportsMap[importPath] : undefined;
|
|
56
|
+
let resolvedFilePath = unbundledFilePath ? `unbundled/${unbundledFilePath}` : `bundled/${bundledFilePath}`;
|
|
57
|
+
const ext = path.extname(resolvedFilePath);
|
|
58
|
+
// For bundled files:
|
|
59
|
+
if (!unbundledFilePath) {
|
|
60
|
+
// Ignore .d.ts entries
|
|
61
|
+
if (resolvedFilePath.endsWith('.d.ts')) {
|
|
62
|
+
continue;
|
|
45
63
|
}
|
|
46
|
-
|
|
47
|
-
|
|
64
|
+
// We always expect responses to come back as javascript (.js extensions.)
|
|
65
|
+
if (ext !== '.js') {
|
|
66
|
+
resolvedFilePath = path.join(path.dirname(resolvedFilePath), path.basename(resolvedFilePath, ext) + (ext === '.mjs' ? '.js' : ext + '.js'));
|
|
48
67
|
}
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
.join(path.dirname(exportPath), path.basename(exportPath, path.extname(exportPath)) + ext)
|
|
52
|
-
.replace('+', '_'), baseUrl).href;
|
|
68
|
+
// Plus signs should be replaced with underscores.
|
|
69
|
+
resolvedFilePath = resolvedFilePath.replace(/\+/g, '_');
|
|
53
70
|
}
|
|
54
|
-
importMap[definition.name
|
|
71
|
+
importMap[importPath.replace('.', `${definition.name}`)] = new URL(resolvedFilePath, baseUrl).href;
|
|
55
72
|
}
|
|
73
|
+
// Finally add a fallback for all other unexpected bundle entries. (Note, this should point to a script
|
|
74
|
+
// which logs this as an error to the user and offers a way to add the entry to exports overrides.)
|
|
75
|
+
// importMap[definition.name + '/'] = baseUrl + 'missing/';
|
|
56
76
|
return importMap;
|
|
57
77
|
}
|
|
58
78
|
//# sourceMappingURL=createImportMap.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createImportMap.js","sourceRoot":"","sources":["../src/createImportMap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"createImportMap.js","sourceRoot":"","sources":["../src/createImportMap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,OAA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