@lwrjs/legacy-npm-module-provider 0.10.8
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/LICENSE +10 -0
- package/build/cjs/index.cjs +89 -0
- package/build/cjs/resolveNpmModules.cjs +140 -0
- package/build/es/index.d.ts +19 -0
- package/build/es/index.js +66 -0
- package/build/es/resolveNpmModules.d.ts +5 -0
- package/build/es/resolveNpmModules.js +127 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
MIT LICENSE
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Salesforce.com, Inc.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
7
|
+
|
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
9
|
+
|
|
10
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
+
};
|
|
12
|
+
var __exportStar = (target, module2, desc) => {
|
|
13
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(module2))
|
|
15
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
+
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
var __toModule = (module2) => {
|
|
21
|
+
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// packages/@lwrjs/legacy-npm-module-provider/src/index.ts
|
|
25
|
+
__markAsModule(exports);
|
|
26
|
+
__export(exports, {
|
|
27
|
+
default: () => src_default
|
|
28
|
+
});
|
|
29
|
+
var import_path = __toModule(require("path"));
|
|
30
|
+
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
31
|
+
var import_resolveNpmModules = __toModule(require("./resolveNpmModules.cjs"));
|
|
32
|
+
var LegacyNpmModuleProvider = class {
|
|
33
|
+
constructor(providerConfig = {}, {config: {cacheDir}}) {
|
|
34
|
+
this.name = "legacy-npm-module-provider";
|
|
35
|
+
this.moduleEntryVersionCache = new Map();
|
|
36
|
+
this.webModulesCacheDir = import_path.default.join(cacheDir, "web_modules");
|
|
37
|
+
this.providerConfig = providerConfig;
|
|
38
|
+
}
|
|
39
|
+
async getModule(moduleId) {
|
|
40
|
+
const moduleSource = await this.getModuleSource(moduleId);
|
|
41
|
+
if (!moduleSource) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
...moduleSource,
|
|
46
|
+
compiledSource: moduleSource.originalSource
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
async getModuleSource(moduleId) {
|
|
50
|
+
const moduleEntry = await this.getModuleEntry(moduleId);
|
|
51
|
+
if (!moduleEntry) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const {namespace, name} = moduleId;
|
|
55
|
+
const {id, specifier, entry, version} = moduleEntry;
|
|
56
|
+
const originalSource = (0, import_shared_utils.readFile)(entry);
|
|
57
|
+
const ownHash = (0, import_shared_utils.hashContent)(originalSource);
|
|
58
|
+
return {
|
|
59
|
+
id,
|
|
60
|
+
namespace,
|
|
61
|
+
name: name || specifier,
|
|
62
|
+
version,
|
|
63
|
+
specifier,
|
|
64
|
+
moduleEntry,
|
|
65
|
+
ownHash,
|
|
66
|
+
originalSource
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async getModuleEntry({specifier, version}) {
|
|
70
|
+
if (version && this.moduleEntryVersionCache.has(specifier)) {
|
|
71
|
+
const versions = this.moduleEntryVersionCache.get(specifier);
|
|
72
|
+
if (versions.has(version)) {
|
|
73
|
+
return versions.get(version);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const moduleEntry = await (0, import_resolveNpmModules.resolveNpmModuleSpecifierDeprecated)(specifier, this.webModulesCacheDir, this.providerConfig);
|
|
77
|
+
if (!moduleEntry) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
let specifierVersions = this.moduleEntryVersionCache.get(specifier);
|
|
81
|
+
if (!specifierVersions) {
|
|
82
|
+
specifierVersions = new Map();
|
|
83
|
+
this.moduleEntryVersionCache.set(specifier, specifierVersions);
|
|
84
|
+
}
|
|
85
|
+
specifierVersions.set(moduleEntry.version, moduleEntry);
|
|
86
|
+
return moduleEntry;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
var src_default = LegacyNpmModuleProvider;
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, {get: all[name], enumerable: true});
|
|
11
|
+
};
|
|
12
|
+
var __exportStar = (target, module2, desc) => {
|
|
13
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(module2))
|
|
15
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
16
|
+
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
|
|
17
|
+
}
|
|
18
|
+
return target;
|
|
19
|
+
};
|
|
20
|
+
var __toModule = (module2) => {
|
|
21
|
+
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// packages/@lwrjs/legacy-npm-module-provider/src/resolveNpmModules.ts
|
|
25
|
+
__markAsModule(exports);
|
|
26
|
+
__export(exports, {
|
|
27
|
+
resolveNpmModuleSpecifierBlock: () => resolveNpmModuleSpecifierBlock,
|
|
28
|
+
resolveNpmModuleSpecifierDeprecated: () => resolveNpmModuleSpecifierDeprecated
|
|
29
|
+
});
|
|
30
|
+
var import_path = __toModule(require("path"));
|
|
31
|
+
var import_esinstall = __toModule(require("esinstall"));
|
|
32
|
+
var import_fs = __toModule(require("fs"));
|
|
33
|
+
var import_resolve = __toModule(require("resolve"));
|
|
34
|
+
var import_shared_utils = __toModule(require("@lwrjs/shared-utils"));
|
|
35
|
+
function getPkgVersion(specifier) {
|
|
36
|
+
const pkgPath = import_resolve.default.sync((0, import_path.join)(specifier, "/package.json"));
|
|
37
|
+
return JSON.parse((0, import_fs.readFileSync)(pkgPath, "utf-8")).version;
|
|
38
|
+
}
|
|
39
|
+
function readModuleMapping(moduleMappingPath) {
|
|
40
|
+
return (0, import_fs.existsSync)(moduleMappingPath) ? JSON.parse((0, import_fs.readFileSync)(moduleMappingPath, "utf-8")) : {imports: {}};
|
|
41
|
+
}
|
|
42
|
+
function writeModuleMapping(loc, importMap) {
|
|
43
|
+
(0, import_fs.writeFileSync)(loc, JSON.stringify(importMap, void 0, 2), {encoding: "utf-8"});
|
|
44
|
+
}
|
|
45
|
+
async function resolveNpmModuleSpecifierBlock(specifier, dest, config) {
|
|
46
|
+
try {
|
|
47
|
+
const tmpFolder = (0, import_path.join)(dest, "__tmp", specifier.replace(/[/|@]/g, "_"));
|
|
48
|
+
(0, import_fs.mkdirSync)(tmpFolder, {recursive: true});
|
|
49
|
+
const moduleMappingPath = (0, import_path.join)(dest, "import-map-cache.json");
|
|
50
|
+
const currentModuleMapping = readModuleMapping(moduleMappingPath);
|
|
51
|
+
const version = getPkgVersion(specifier);
|
|
52
|
+
const cacheKey = `${specifier}@${version}`;
|
|
53
|
+
if (currentModuleMapping.imports[cacheKey]) {
|
|
54
|
+
return {
|
|
55
|
+
id: cacheKey,
|
|
56
|
+
specifier,
|
|
57
|
+
entry: currentModuleMapping.imports[cacheKey],
|
|
58
|
+
version
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const external = config.external?.filter((e) => e !== specifier) || [];
|
|
62
|
+
const polyfillNode = config.polyfillNode ?? true;
|
|
63
|
+
await (0, import_esinstall.install)([specifier], {
|
|
64
|
+
dest: tmpFolder,
|
|
65
|
+
importMap: currentModuleMapping,
|
|
66
|
+
logger: {
|
|
67
|
+
debug: () => void 0,
|
|
68
|
+
log: () => void 0,
|
|
69
|
+
warn: () => void 0,
|
|
70
|
+
error: () => void 0
|
|
71
|
+
},
|
|
72
|
+
polyfillNode,
|
|
73
|
+
external
|
|
74
|
+
});
|
|
75
|
+
const moduleMapping = readModuleMapping((0, import_path.join)(tmpFolder, "import-map.json"));
|
|
76
|
+
const moduleRelativePath = moduleMapping.imports[specifier];
|
|
77
|
+
const moduleAbsPath = (0, import_path.join)(tmpFolder, moduleRelativePath);
|
|
78
|
+
const moduleEntry = (0, import_path.join)(dest, moduleRelativePath);
|
|
79
|
+
(0, import_fs.mkdirSync)((0, import_path.dirname)(moduleEntry), {recursive: true});
|
|
80
|
+
(0, import_fs.copyFileSync)(moduleAbsPath, moduleEntry);
|
|
81
|
+
const mergedMapping = {
|
|
82
|
+
imports: {
|
|
83
|
+
...currentModuleMapping.imports,
|
|
84
|
+
[cacheKey]: moduleEntry
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
writeModuleMapping(moduleMappingPath, mergedMapping);
|
|
88
|
+
return {
|
|
89
|
+
id: cacheKey,
|
|
90
|
+
entry: moduleEntry,
|
|
91
|
+
specifier,
|
|
92
|
+
version
|
|
93
|
+
};
|
|
94
|
+
} catch (err) {
|
|
95
|
+
import_shared_utils.logger.error(`[npm-module-provider][esintsall] resolveNpmModules: ${specifier} ${dest}`, {err});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
var Queue = class {
|
|
99
|
+
constructor() {
|
|
100
|
+
this.queue = [];
|
|
101
|
+
this.workingOnPromise = false;
|
|
102
|
+
this.pendingPromise = false;
|
|
103
|
+
}
|
|
104
|
+
enqueue(promise) {
|
|
105
|
+
return new Promise((resolve2, reject) => {
|
|
106
|
+
this.queue.push({promise, resolve: resolve2, reject});
|
|
107
|
+
this.dequeue();
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
dequeue() {
|
|
111
|
+
if (this.workingOnPromise) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
const item = this.queue.shift();
|
|
115
|
+
if (!item) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
this.workingOnPromise = true;
|
|
119
|
+
try {
|
|
120
|
+
item.promise().then((value) => {
|
|
121
|
+
this.workingOnPromise = false;
|
|
122
|
+
item.resolve(value);
|
|
123
|
+
this.dequeue();
|
|
124
|
+
}).catch((err) => {
|
|
125
|
+
this.workingOnPromise = false;
|
|
126
|
+
item.reject(err);
|
|
127
|
+
this.dequeue();
|
|
128
|
+
});
|
|
129
|
+
} catch (err) {
|
|
130
|
+
this.workingOnPromise = false;
|
|
131
|
+
item.reject(err);
|
|
132
|
+
this.dequeue();
|
|
133
|
+
}
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var RESOLVER_QUEUE = new Queue();
|
|
138
|
+
async function resolveNpmModuleSpecifierDeprecated(specifier, dest, config) {
|
|
139
|
+
return RESOLVER_QUEUE.enqueue(resolveNpmModuleSpecifierBlock.bind(null, specifier, dest, config));
|
|
140
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AbstractModuleId, ModuleCompiled, ModuleEntry, ModuleProvider, ModuleSource, ProviderContext } from '@lwrjs/types';
|
|
2
|
+
export interface LegacyNpmModuleProviderConfig {
|
|
3
|
+
external?: string[];
|
|
4
|
+
polyfillNode?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated please use @lwrjs/npm-module-provider
|
|
8
|
+
*/
|
|
9
|
+
export default class LegacyNpmModuleProvider implements ModuleProvider {
|
|
10
|
+
name: string;
|
|
11
|
+
private providerConfig;
|
|
12
|
+
private webModulesCacheDir;
|
|
13
|
+
private moduleEntryVersionCache;
|
|
14
|
+
constructor(providerConfig: LegacyNpmModuleProviderConfig | undefined, { config: { cacheDir } }: ProviderContext);
|
|
15
|
+
getModule(moduleId: AbstractModuleId): Promise<ModuleCompiled | undefined>;
|
|
16
|
+
getModuleSource(moduleId: AbstractModuleId): Promise<ModuleSource | undefined>;
|
|
17
|
+
getModuleEntry({ specifier, version }: AbstractModuleId): Promise<ModuleEntry | undefined>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { hashContent, readFile } from '@lwrjs/shared-utils';
|
|
3
|
+
import { resolveNpmModuleSpecifierDeprecated } from './resolveNpmModules.js';
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated please use @lwrjs/npm-module-provider
|
|
6
|
+
*/
|
|
7
|
+
export default class LegacyNpmModuleProvider {
|
|
8
|
+
constructor(providerConfig = {}, { config: { cacheDir } }) {
|
|
9
|
+
this.name = 'legacy-npm-module-provider';
|
|
10
|
+
this.moduleEntryVersionCache = new Map();
|
|
11
|
+
this.webModulesCacheDir = path.join(cacheDir, 'web_modules');
|
|
12
|
+
this.providerConfig = providerConfig;
|
|
13
|
+
}
|
|
14
|
+
async getModule(moduleId) {
|
|
15
|
+
const moduleSource = await this.getModuleSource(moduleId);
|
|
16
|
+
if (!moduleSource) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
...moduleSource,
|
|
21
|
+
compiledSource: moduleSource.originalSource,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
async getModuleSource(moduleId) {
|
|
25
|
+
const moduleEntry = await this.getModuleEntry(moduleId);
|
|
26
|
+
if (!moduleEntry) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const { namespace, name } = moduleId;
|
|
30
|
+
const { id, specifier, entry, version } = moduleEntry;
|
|
31
|
+
const originalSource = readFile(entry);
|
|
32
|
+
const ownHash = hashContent(originalSource);
|
|
33
|
+
return {
|
|
34
|
+
id,
|
|
35
|
+
namespace,
|
|
36
|
+
name: name || specifier,
|
|
37
|
+
version,
|
|
38
|
+
specifier,
|
|
39
|
+
moduleEntry,
|
|
40
|
+
ownHash,
|
|
41
|
+
originalSource,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
async getModuleEntry({ specifier, version }) {
|
|
45
|
+
// Check cache
|
|
46
|
+
if (version && this.moduleEntryVersionCache.has(specifier)) {
|
|
47
|
+
const versions = this.moduleEntryVersionCache.get(specifier);
|
|
48
|
+
if (versions.has(version)) {
|
|
49
|
+
return versions.get(version);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const moduleEntry = await resolveNpmModuleSpecifierDeprecated(specifier, this.webModulesCacheDir, this.providerConfig);
|
|
53
|
+
if (!moduleEntry) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
// Store in cache
|
|
57
|
+
let specifierVersions = this.moduleEntryVersionCache.get(specifier);
|
|
58
|
+
if (!specifierVersions) {
|
|
59
|
+
specifierVersions = new Map();
|
|
60
|
+
this.moduleEntryVersionCache.set(specifier, specifierVersions);
|
|
61
|
+
}
|
|
62
|
+
specifierVersions.set(moduleEntry.version, moduleEntry);
|
|
63
|
+
return moduleEntry;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FsModuleEntry } from '@lwrjs/types';
|
|
2
|
+
import type { LegacyNpmModuleProviderConfig } from './index';
|
|
3
|
+
export declare function resolveNpmModuleSpecifierBlock(specifier: string, dest: string, config: LegacyNpmModuleProviderConfig): Promise<FsModuleEntry | undefined>;
|
|
4
|
+
export declare function resolveNpmModuleSpecifierDeprecated(specifier: string, dest: string, config: LegacyNpmModuleProviderConfig): Promise<FsModuleEntry | undefined>;
|
|
5
|
+
//# sourceMappingURL=resolveNpmModules.d.ts.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import { dirname, join } from 'path';
|
|
3
|
+
import { install } from 'esinstall';
|
|
4
|
+
import { readFileSync, copyFileSync, mkdirSync, existsSync, writeFileSync } from 'fs';
|
|
5
|
+
import resolve from 'resolve';
|
|
6
|
+
import { logger } from '@lwrjs/shared-utils';
|
|
7
|
+
function getPkgVersion(specifier) {
|
|
8
|
+
const pkgPath = resolve.sync(join(specifier, '/package.json'));
|
|
9
|
+
return JSON.parse(readFileSync(pkgPath, 'utf-8')).version;
|
|
10
|
+
}
|
|
11
|
+
function readModuleMapping(moduleMappingPath) {
|
|
12
|
+
return existsSync(moduleMappingPath)
|
|
13
|
+
? JSON.parse(readFileSync(moduleMappingPath, 'utf-8'))
|
|
14
|
+
: { imports: {} };
|
|
15
|
+
}
|
|
16
|
+
function writeModuleMapping(loc, importMap) {
|
|
17
|
+
writeFileSync(loc, JSON.stringify(importMap, undefined, 2), { encoding: 'utf-8' });
|
|
18
|
+
}
|
|
19
|
+
export async function resolveNpmModuleSpecifierBlock(specifier, dest, config) {
|
|
20
|
+
try {
|
|
21
|
+
// TODO: file an issue on snowpack so they don't wipe the folder every-time
|
|
22
|
+
// in the mean time we have to do hackery that will be likely to break when upgrading
|
|
23
|
+
const tmpFolder = join(dest, '__tmp', specifier.replace(/[/|@]/g, '_'));
|
|
24
|
+
mkdirSync(tmpFolder, { recursive: true });
|
|
25
|
+
const moduleMappingPath = join(dest, 'import-map-cache.json');
|
|
26
|
+
const currentModuleMapping = readModuleMapping(moduleMappingPath);
|
|
27
|
+
const version = getPkgVersion(specifier);
|
|
28
|
+
const cacheKey = `${specifier}@${version}`;
|
|
29
|
+
if (currentModuleMapping.imports[cacheKey]) {
|
|
30
|
+
return {
|
|
31
|
+
id: cacheKey,
|
|
32
|
+
specifier,
|
|
33
|
+
entry: currentModuleMapping.imports[cacheKey],
|
|
34
|
+
version,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const external = config.external?.filter((e) => e !== specifier) || [];
|
|
38
|
+
const polyfillNode = config.polyfillNode ?? true;
|
|
39
|
+
// Unfortunately esinstall APIs its not very programatic instead its file oriented...
|
|
40
|
+
await install([specifier], {
|
|
41
|
+
dest: tmpFolder,
|
|
42
|
+
importMap: currentModuleMapping,
|
|
43
|
+
logger: {
|
|
44
|
+
debug: () => void 0,
|
|
45
|
+
log: () => void 0,
|
|
46
|
+
warn: () => void 0,
|
|
47
|
+
error: () => void 0,
|
|
48
|
+
},
|
|
49
|
+
polyfillNode,
|
|
50
|
+
external,
|
|
51
|
+
});
|
|
52
|
+
// esinstall produces a new module mapping, read it to get the generated file
|
|
53
|
+
const moduleMapping = readModuleMapping(join(tmpFolder, 'import-map.json'));
|
|
54
|
+
const moduleRelativePath = moduleMapping.imports[specifier];
|
|
55
|
+
const moduleAbsPath = join(tmpFolder, moduleRelativePath);
|
|
56
|
+
const moduleEntry = join(dest, moduleRelativePath);
|
|
57
|
+
// create module's namespaced dir if it's namespaced, i.e.
|
|
58
|
+
// no namespace: 'utils' -> web_modules/utils.js
|
|
59
|
+
// namespace: '@mynamespace/utils' -> web_modules/@mynamespace/utils.js
|
|
60
|
+
mkdirSync(dirname(moduleEntry), { recursive: true });
|
|
61
|
+
// copy from _tmp to main folder and update the importMap for caching
|
|
62
|
+
copyFileSync(moduleAbsPath, moduleEntry);
|
|
63
|
+
const mergedMapping = {
|
|
64
|
+
imports: {
|
|
65
|
+
...currentModuleMapping.imports,
|
|
66
|
+
[cacheKey]: moduleEntry,
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
writeModuleMapping(moduleMappingPath, mergedMapping);
|
|
70
|
+
return {
|
|
71
|
+
id: cacheKey,
|
|
72
|
+
entry: moduleEntry,
|
|
73
|
+
specifier,
|
|
74
|
+
version,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
logger.error(`[npm-module-provider][esintsall] resolveNpmModules: ${specifier} ${dest}`, { err });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
class Queue {
|
|
82
|
+
constructor() {
|
|
83
|
+
this.queue = [];
|
|
84
|
+
this.workingOnPromise = false;
|
|
85
|
+
this.pendingPromise = false;
|
|
86
|
+
}
|
|
87
|
+
enqueue(promise) {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
this.queue.push({ promise, resolve, reject });
|
|
90
|
+
this.dequeue();
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
dequeue() {
|
|
94
|
+
if (this.workingOnPromise) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const item = this.queue.shift();
|
|
98
|
+
if (!item) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
this.workingOnPromise = true;
|
|
102
|
+
try {
|
|
103
|
+
item.promise()
|
|
104
|
+
.then((value) => {
|
|
105
|
+
this.workingOnPromise = false;
|
|
106
|
+
item.resolve(value);
|
|
107
|
+
this.dequeue();
|
|
108
|
+
})
|
|
109
|
+
.catch((err) => {
|
|
110
|
+
this.workingOnPromise = false;
|
|
111
|
+
item.reject(err);
|
|
112
|
+
this.dequeue();
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
this.workingOnPromise = false;
|
|
117
|
+
item.reject(err);
|
|
118
|
+
this.dequeue();
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const RESOLVER_QUEUE = new Queue();
|
|
124
|
+
export async function resolveNpmModuleSpecifierDeprecated(specifier, dest, config) {
|
|
125
|
+
return RESOLVER_QUEUE.enqueue(resolveNpmModuleSpecifierBlock.bind(null, specifier, dest, config));
|
|
126
|
+
}
|
|
127
|
+
//# sourceMappingURL=resolveNpmModules.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lwrjs/legacy-npm-module-provider",
|
|
3
|
+
"description": "DEPRECATED: This package provides our legacy npm-module-provider functionality. Use at your own risk.",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"version": "0.10.8",
|
|
9
|
+
"homepage": "https://developer.salesforce.com/docs/platform/lwr/overview",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/salesforce-experience-platform-emu/lwr.git",
|
|
13
|
+
"directory": "packages/@lwrjs/legacy-npm-module-provider"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/salesforce-experience-platform-emu/lwr/issues"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"types": "build/es/index.d.ts",
|
|
20
|
+
"main": "build/cjs/index.cjs",
|
|
21
|
+
"module": "build/es/index.js",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": "./build/es/index.js",
|
|
25
|
+
"require": "./build/cjs/index.cjs"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"build/**/*.js",
|
|
30
|
+
"build/**/*.cjs",
|
|
31
|
+
"build/**/*.d.ts"
|
|
32
|
+
],
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -b",
|
|
35
|
+
"clean": "rm -rf build node_modules",
|
|
36
|
+
"test": "jest"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@lwrjs/types": "0.10.8",
|
|
40
|
+
"jest": "^26.6.3",
|
|
41
|
+
"ts-jest": "^26.5.6",
|
|
42
|
+
"typescript": "^4.9.5"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@lwrjs/shared-utils": "0.10.8",
|
|
46
|
+
"esinstall": "^1.1.7"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=16.0.0"
|
|
50
|
+
},
|
|
51
|
+
"volta": {
|
|
52
|
+
"extends": "../../../package.json"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "2c09d011be6fe83186aa3d1358b2aa9814a02008"
|
|
55
|
+
}
|