Skip to content

fix #2986 #2997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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