Skip to content

Commit 6d90f2d

Browse files
author
Jamie Thompson
authored
feat: add support for npm alias in subdependencies (codesandbox#3730)
Fixes codesandbox#3727
1 parent 180859a commit 6d90f2d

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

packages/app/src/sandbox/eval/npm/fetch-npm-module.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,22 +178,38 @@ const getFetchProtocol = (depVersion: string, useFallback = false) => {
178178
// Strips the version of a path, eg. test/1.3.0 -> test
179179
const ALIAS_REGEX = /\/\d*\.\d*\.\d*.*?(\/|$)/;
180180

181+
/*
182+
* Resolve name and version from npm aliases
183+
* e.g. "my-react": "npm:[email protected]
184+
*/
185+
const resolveNPMAlias = (name: string, version: string): string[] => {
186+
const IS_ALIAS = /^npm:/;
187+
188+
if (!version.match(IS_ALIAS)) {
189+
return [name, version];
190+
}
191+
192+
const parts = version.match(/^npm:(.+)@(.+)/);
193+
return [parts[1], parts[2]];
194+
};
195+
181196
async function getMeta(
182197
name: string,
183198
packageJSONPath: string | null,
184199
version: string,
185200
useFallback = false
186201
) {
187-
const nameWithoutAlias = name.replace(ALIAS_REGEX, '');
188-
const id = `${packageJSONPath || name}@${version}`;
202+
const [depName, depVersion] = resolveNPMAlias(name, version);
203+
const nameWithoutAlias = depName.replace(ALIAS_REGEX, '');
204+
const id = `${packageJSONPath || depName}@${depVersion}`;
189205
if (metas[id]) {
190206
return metas[id];
191207
}
192208

193-
const protocol = getFetchProtocol(version, useFallback);
209+
const protocol = getFetchProtocol(depVersion, useFallback);
194210

195211
metas[id] = protocol
196-
.meta(nameWithoutAlias, version)
212+
.meta(nameWithoutAlias, depVersion)
197213
.then(fetchWithRetries)
198214
.then(x => x.json())
199215
.catch(e => {
@@ -206,10 +222,11 @@ async function getMeta(
206222
}
207223

208224
export async function downloadDependency(
209-
depName: string,
210-
depVersion: string,
225+
name: string,
226+
version: string,
211227
path: string
212228
): Promise<Module> {
229+
const [depName, depVersion] = resolveNPMAlias(name, version);
213230
const id = depName + depVersion + path;
214231
if (packages[id]) {
215232
return packages[id];

0 commit comments

Comments
 (0)