Skip to content

Commit 5aa54b7

Browse files
refactored require
1 parent 2d2c43f commit 5aa54b7

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

src/utils/require.js

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function replaceRelativePaths(code: string, rootPath: string): string {
7878
function requireFromString(code: string, filename: string = '', optsObj: Object = {}): Promise < Object > {
7979
return new Promise((resolve, reject) => {
8080
const options = new Options(optsObj);
81+
let promiseArray = [];
8182

8283
if (typeof code !== 'string') {
8384
throw new Error('code must be a string, not ' + typeof code);
@@ -87,44 +88,39 @@ function requireFromString(code: string, filename: string = '', optsObj: Object
8788
var m = new Module(filename, options.rootPath);
8889
m.filename = filename;
8990
m.paths = [].concat(options.prependPaths).concat(paths).concat(options.appendPaths);
90-
try {
91-
m._compile(code, filename);
92-
resolve(m.exports.default);
93-
} catch (error) {
94-
//Check if the error is because the file isn't javascript
95-
if (error.message.includes('Unexpected token')) {
96-
//find matches for the require paths
97-
let vueComponentFileMatches = code.match(options.requireRegex);
98-
if (vueComponentFileMatches && vueComponentFileMatches.length > 0) {
99-
//iterate through the matches
100-
for (var index = 0; index < vueComponentFileMatches.length; index++) {
101-
var vueComponentFileMatch = vueComponentFileMatches[index];
102-
//get the file out of the require string
103-
//this is because its easier to do string replace later
104-
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
105-
if (vueComponentFile && vueComponentFile.length > 0) {
106-
getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch)
107-
.then(renderedItem => {
108-
const rawString = renderedItem.rendered.scriptStringRaw;
109-
code = code.replace(renderedItem.match, rawString);
110-
//check if its the last element and then render
111-
const last_element = code.match(options.requireRegex);
112-
if (last_element === undefined || last_element === null) {
113-
m._compile(code, filename);
114-
resolve(m.exports.default);
115-
}
116-
})
117-
.catch(error => {
118-
reject(error);
119-
});
120-
}
121-
}
122-
} else {
123-
reject(new Error('Couldnt require component from string: ' + error));
91+
92+
//find matches for the require paths
93+
let vueComponentFileMatches = code.match(options.requireRegex);
94+
if (vueComponentFileMatches && vueComponentFileMatches.length > 0) {
95+
//iterate through the matches
96+
for (var index = 0; index < vueComponentFileMatches.length; index++) {
97+
var vueComponentFileMatch = vueComponentFileMatches[index];
98+
//get the file out of the require string
99+
//this is because its easier to do string replace later
100+
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
101+
if (vueComponentFile && vueComponentFile.length > 0) {
102+
promiseArray.push(getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch));
124103
}
125-
} else {
126-
reject(new Error('Couldnt require from string: ' + error));
127104
}
105+
Promise.all(promiseArray)
106+
.then(renderedItemArray => {
107+
for (var renderedItem of renderedItemArray) {
108+
const rawString = renderedItem.rendered.scriptStringRaw;
109+
code = code.replace(renderedItem.match, rawString);
110+
}
111+
//check if its the last element and then render
112+
const last_element = code.match(options.vueFileRegex);
113+
if (last_element === undefined || last_element === null) {
114+
m._compile(code, filename);
115+
resolve(m.exports.default);
116+
}
117+
})
118+
.catch(error => {
119+
reject(error);
120+
});
121+
} else {
122+
m._compile(code, filename);
123+
resolve(m.exports.default);
128124
}
129125
});
130126
}

0 commit comments

Comments
 (0)