Skip to content

Commit f056207

Browse files
committed
Fix dynamic fetching for predefined sub node_modules
Fixes codesandbox#922
1 parent b5e0ec8 commit f056207

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ async function compile({
450450
managerModuleToTranspile = modules[main];
451451

452452
dispatch({ type: 'status', status: 'transpiling' });
453+
manager.setStage('transpilation');
453454

454455
await manager.preset.setup(manager);
455456

@@ -459,6 +460,7 @@ async function compile({
459460
debug(`Transpilation time ${Date.now() - t}ms`);
460461

461462
dispatch({ type: 'status', status: 'evaluating' });
463+
manager.setStage('evaluation');
462464

463465
if (!skipEval) {
464466
resetScreen();

packages/app/src/sandbox/eval/manager.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ const SHIMMED_MODULE: Module = {
7474
const debug = _debug('cs:compiler:manager');
7575

7676
type HMRStatus = 'idle' | 'check' | 'apply' | 'fail' | 'dispose';
77+
type Stage = 'transpilation' | 'evaluation';
7778

7879
export default class Manager {
7980
id: string;
@@ -108,6 +109,8 @@ export default class Manager {
108109

109110
configurations: Configurations;
110111

112+
stage: Stage;
113+
111114
constructor(id: string, preset: Preset, modules: { [path: string]: Module }) {
112115
this.id = id;
113116
this.preset = preset;
@@ -121,6 +124,7 @@ export default class Manager {
121124
this.isFirstLoad = true;
122125
this.transpiledModulesByHash = {};
123126
this.configurations = {};
127+
this.stage = 'transpilation';
124128

125129
this.modules = modules;
126130
Object.keys(modules).forEach(k => this.addModule(modules[k]));
@@ -166,8 +170,14 @@ export default class Manager {
166170
}
167171

168172
// Hoist these 2 functions to the top, since they get executed A LOT
169-
isFile = (p: string) =>
170-
!!this.transpiledModules[p] || !!getCombinedMetas()[p];
173+
isFile = (p: string) => {
174+
if (this.stage === 'transpilation') {
175+
// In transpilation phase we can afford to download the file if not found,
176+
// because we're async. That's why we also include the meta here.
177+
return !!this.transpiledModules[p] || !!getCombinedMetas()[p];
178+
}
179+
return !!this.transpiledModules[p];
180+
};
171181

172182
readFileSync = (p: string) => {
173183
if (this.transpiledModules[p]) {
@@ -181,6 +191,10 @@ export default class Manager {
181191
throw err;
182192
};
183193

194+
setStage = (stage: Stage) => {
195+
this.stage = stage;
196+
};
197+
184198
setManifest(manifest: ?Manifest) {
185199
this.manifest = manifest || {
186200
contents: {},

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ function getUnpkgUrl(name: string, version: string) {
7979
: `https://unpkg.com/${nameWithoutAlias}@${version}`;
8080
}
8181

82-
function getMeta(name: string, version: string) {
82+
function getMeta(name: string, packageJSONPath: string, version: string) {
8383
const nameWithoutAlias = name.replace(/\/\d*\.\d*\.\d*$/, '');
84-
const id = `${nameWithoutAlias}@${version}`;
84+
const id = `${packageJSONPath}@${version}`;
8585
if (metas[id]) {
8686
return metas[id];
8787
}
@@ -269,7 +269,12 @@ export default async function fetchModule(
269269
manager: Manager,
270270
defaultExtensions: Array<string> = ['js', 'jsx', 'json']
271271
): Promise<Module> {
272-
const dependencyName = getDependencyName(path);
272+
// Get the last part of the path as dependency name for paths like
273+
// instantsearch.js/node_modules/lodash/sum.js
274+
// In this case we want to get the lodash dependency info
275+
const dependencyName = getDependencyName(
276+
path.replace(/.*\/node_modules\//, '')
277+
);
273278

274279
const versionInfo = await findDependencyVersion(
275280
currentPath,
@@ -284,7 +289,7 @@ export default async function fetchModule(
284289

285290
const { packageJSONPath, version } = versionInfo;
286291

287-
const meta = await getMeta(dependencyName, version);
292+
const meta = await getMeta(dependencyName, packageJSONPath, version);
288293

289294
const normalizeFunction = TEMP_USE_JSDELIVR ? normalizeJSDelivr : normalize;
290295
const normalizedMeta = normalizeFunction(

0 commit comments

Comments
 (0)