Skip to content

Commit

Permalink
Merge pull request #2997 from BuckleScript/fix_2986
Browse files Browse the repository at this point in the history
fix #2986
  • Loading branch information
bobzhang authored Aug 10, 2018
2 parents 422b386 + f73f9a9 commit 925f3e6
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion jscomp/bsb/templates/react-lite/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,29 @@ function getPackageName(id) {
}
return id.substring(0, index)
}

/**
*
* @param {string} s
* @param {string} text
* @returns {undefined | string }
*/
function isJustAPackageAndHasMainField(s,text){
if(s.indexOf('/') < 0){
return
} else {
var mainField;
try {
mainField = JSON.parse(text).main
}catch(_){
}
if(mainField === undefined){
return
} else {
return mainField
}
}
}
function getPackageJsPromise(id, parent) {
var idNodeModulesPrefix = './node_modules/' + id
var link = getPathWithJsSuffix(idNodeModulesPrefix, parent)
Expand All @@ -269,6 +292,19 @@ function getPackageJsPromise(id, parent) {
.then(
function (text) {
if (text !== false) {
var mainField;
if( (mainField = isJustAPackageAndHasMainField(id, text)) !== undefined){
var packageLink = BsGetPath(addSuffixJsIfNot(`./node_modules/${id}/${mainField}`), parent)
return cachedFetch(packageLink)
.then(function(text){
if(text !== false){
return {text, link : packageLink}
} else {
return getParentModulePromise(id,parent)
}
})

} else {
// package indeed exist
return cachedFetch(link).then(function (text) {
if (text !== false) {
Expand All @@ -289,6 +325,7 @@ function getPackageJsPromise(id, parent) {
}
})
}
}
else {
return getParentModulePromise(id, parent)
}
Expand Down Expand Up @@ -317,7 +354,19 @@ function getModulePromise(id, parent) {
function (text) {
if (text !== false) {
return { text, link }
} else {
} else if (!id.endsWith('.js')){
// could be "./dir"
var newLink = getPathWithJsSuffix( id +"/index.js",parent)
return cachedFetch(newLink)
.then(function(text){
if(text !== false){
return{text, link : newLink }
} else {
throw new Error(` ${id} : ${parent} could not be resolved`)
}
})
}
else {
throw new Error(` ${id} : ${parent} could not be resolved`)
}
}
Expand Down

0 comments on commit 925f3e6

Please sign in to comment.