Skip to content

Commit

Permalink
error won't halt script execution anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
shawwwn committed Apr 13, 2019
1 parent a01af8f commit d89c714
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions vueify.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ function importModule(url) {
* @param {string} url
* @param {function(string)} callback
*/
function getContent(url, callback) {
function getContent(url, callback, error) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status>=200 && xhr.status<300) {
callback(xhr.responseText);
} else {
throw new VueifyJSError(`Failed to load ${url}!`);
error(new VueifyJSError(`Failed to load ${url}!`));
}
};
xhr.open('GET', url, true);
Expand Down Expand Up @@ -235,7 +235,9 @@ function findSFC(callback) {
}
}

let ret = callback(dom, url, name);
let ret = callback(dom, url, name).catch((e)=>{
console.error(e);
});
if (ret.__proto__ === Promise.prototype) {
promises.push(ret);
}
Expand All @@ -251,11 +253,12 @@ function findSFC(callback) {
* @return sfc_src
*/
function downloadSFC(sfc_url) {
// console.log(`download: ${sfc_url}`); // DEBUG
return new Promise(resolve => {
getContent(sfc_url, (sfc_src) => {
resolve(sfc_src);
});
return new Promise((resolve, reject) => {
getContent(
sfc_url,
(sfc_src) => resolve(sfc_src),
(error) => reject(error)
);
});
}

Expand Down Expand Up @@ -305,7 +308,6 @@ async function preprocessCSS(sfc_obj) {
// generate scopeId
if (!_scopeId) {
_scopeId = genScopeId();
// console.log("scopeId:", _scopeId); // DEBUG
}

// create temporary iframe to hold & parse css text
Expand All @@ -320,7 +322,11 @@ async function preprocessCSS(sfc_obj) {
external_txt = await new Promise((resolve, reject) => {
if (el.dom.hasAttribute('src')) {
let path = el.dom.getAttribute('src');
getContent(resolveUrl(path), (content) => resolve(content));
getContent(
resolveUrl(path),
(content) => resolve(content),
(error) => reject(error)
);
} else {
resolve('');
}
Expand Down Expand Up @@ -378,15 +384,18 @@ var cachedSFCs = {}; // { url : {blob_url, sfc_obj} }
* @return input JS object
*/
async function preprocessJS(sfc_obj, deps=[]) {
// console.log('dependency stack', deps); // DEBUG
var js = sfc_obj.js;

// get content only from last script tag
var el = js[js.length-1];
var _jsText = await new Promise((resolve, reject) => {
if (el.dom.hasAttribute('src')) {
let path = el.dom.getAttribute('src');
getContent(resolveUrl(path), (content) => resolve(content));
getContent(
resolveUrl(path),
(content) => resolve(content),
(error) => reject(error)
);
} else {
resolve('');
}
Expand All @@ -400,7 +409,6 @@ async function preprocessJS(sfc_obj, deps=[]) {
_jsText = await asyncStringReplace(_jsText, re, async(match, path, offset, txt) => {

let child_sfc_url = resolveUrl(path);
// console.log(`found child: ${child_sfc_url}`); // DEBUG

// check cyclic dependency
if (deps.indexOf(child_sfc_url) != -1) {
Expand All @@ -416,7 +424,6 @@ async function preprocessJS(sfc_obj, deps=[]) {
// replace import statement from relative path to blob url
let cache = await Promise.resolve(cachedSFCs[child_sfc_url]);
new_str = match.replace(path, cache.blob_url);
// console.log(`${match} ====> ${new_str}`); // DEBUG
return new_str;
});

Expand Down Expand Up @@ -461,7 +468,6 @@ async function uploadSFC(sfc_code) {
* @return {string, object}
*/
async function transpileSFC(sfc_src, deps=[]) {
// console.log('transpile:', deps[deps.length-1]); // DEBUG
let sfc_obj = await parseSFC(sfc_src);
await preprocessCSS(sfc_obj);
await preprocessHTML(sfc_obj);
Expand Down Expand Up @@ -578,14 +584,12 @@ async function registerRootSFCs(VueClass) {

return async function() {
sfc_url = resolveUrl(sfc_url);
// console.log(`found: ${sfc_url}`); // DEBUG

await importSFC(sfc_url, [sfc_url]);
let sfc_obj = await Promise.resolve(cachedSFCs[sfc_url]);

// register as global components
if (!rootSFCs.includes(sfc_obj)) {
// console.log(`register: ${sfc_url}`); // DEBUG
rootSFCs.push(sfc_obj);
new_sfcs.push(sfc_obj);
sfc_obj.name = sfc_name;
Expand Down

0 comments on commit d89c714

Please sign in to comment.