Skip to content

Commit 925f3e6

Browse files
authored
Merge pull request #2997 from BuckleScript/fix_2986
fix #2986
2 parents 422b386 + f73f9a9 commit 925f3e6

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

jscomp/bsb/templates/react-lite/loader.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,29 @@ function getPackageName(id) {
254254
}
255255
return id.substring(0, index)
256256
}
257+
258+
/**
259+
*
260+
* @param {string} s
261+
* @param {string} text
262+
* @returns {undefined | string }
263+
*/
264+
function isJustAPackageAndHasMainField(s,text){
265+
if(s.indexOf('/') < 0){
266+
return
267+
} else {
268+
var mainField;
269+
try {
270+
mainField = JSON.parse(text).main
271+
}catch(_){
272+
}
273+
if(mainField === undefined){
274+
return
275+
} else {
276+
return mainField
277+
}
278+
}
279+
}
257280
function getPackageJsPromise(id, parent) {
258281
var idNodeModulesPrefix = './node_modules/' + id
259282
var link = getPathWithJsSuffix(idNodeModulesPrefix, parent)
@@ -269,6 +292,19 @@ function getPackageJsPromise(id, parent) {
269292
.then(
270293
function (text) {
271294
if (text !== false) {
295+
var mainField;
296+
if( (mainField = isJustAPackageAndHasMainField(id, text)) !== undefined){
297+
var packageLink = BsGetPath(addSuffixJsIfNot(`./node_modules/${id}/${mainField}`), parent)
298+
return cachedFetch(packageLink)
299+
.then(function(text){
300+
if(text !== false){
301+
return {text, link : packageLink}
302+
} else {
303+
return getParentModulePromise(id,parent)
304+
}
305+
})
306+
307+
} else {
272308
// package indeed exist
273309
return cachedFetch(link).then(function (text) {
274310
if (text !== false) {
@@ -289,6 +325,7 @@ function getPackageJsPromise(id, parent) {
289325
}
290326
})
291327
}
328+
}
292329
else {
293330
return getParentModulePromise(id, parent)
294331
}
@@ -317,7 +354,19 @@ function getModulePromise(id, parent) {
317354
function (text) {
318355
if (text !== false) {
319356
return { text, link }
320-
} else {
357+
} else if (!id.endsWith('.js')){
358+
// could be "./dir"
359+
var newLink = getPathWithJsSuffix( id +"/index.js",parent)
360+
return cachedFetch(newLink)
361+
.then(function(text){
362+
if(text !== false){
363+
return{text, link : newLink }
364+
} else {
365+
throw new Error(` ${id} : ${parent} could not be resolved`)
366+
}
367+
})
368+
}
369+
else {
321370
throw new Error(` ${id} : ${parent} could not be resolved`)
322371
}
323372
}

0 commit comments

Comments
 (0)