Skip to content

Commit 6323430

Browse files
Merge pull request #66 from express-vue/feature/fix_regexes
Feature/fix regexes
2 parents 6f76b7d + 5753943 commit 6323430

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/require.js

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Options {
1414
defaults: Models.Defaults;
1515
constructor(optsObj: Object) {
1616
this.vueFileRegex = /([\w/.\-@_\d]*\.vue)/igm;
17-
this.requireRegex = /(require\(')([\w/.\-@_\d]*\.vue)('\))/igm;
17+
this.requireRegex = /(require\(['"])([\w/.\-@_\d]*\.vue)(['"]\))/igm;
1818
this.appendPaths = optsObj.appendPaths || [];
1919
this.prependPaths = optsObj.prependPaths || [];
2020
this.rootPath = optsObj.rootPath || '';
@@ -46,18 +46,30 @@ function getVueObject(componentPath: string, rootPath: string, vueComponentFileM
4646
}
4747

4848
function replaceRelativePaths(code: string, rootPath: string): string {
49-
const parentMatches = code.match(/(require\('\.\.\/)/gm);
50-
const currentMatches = code.match(/(require\('\.\/)/gm);
51-
if (parentMatches) {
52-
for (const match of parentMatches) {
49+
const parentMatchesSingle = code.match(/(require\('\.\.\/)/gm);
50+
const currentMatchesSingle = code.match(/(require\('\.\/)/gm);
51+
const parentMatchesDouble = code.match(/(require\("\.\.\/)/gm);
52+
const currentMatchesDouble = code.match(/(require\("\.\/)/gm);
53+
if (parentMatchesSingle) {
54+
for (const match of parentMatchesSingle) {
5355
code = code.replace(match, `require('${rootPath}/../`);
5456
}
5557
}
56-
if (currentMatches) {
57-
for (const match of currentMatches) {
58+
if (parentMatchesDouble) {
59+
for (const match of parentMatchesDouble) {
60+
code = code.replace(match, `require("${rootPath}/../`);
61+
}
62+
}
63+
if (currentMatchesSingle) {
64+
for (const match of currentMatchesSingle) {
5865
code = code.replace(match, `require('${rootPath}/./`);
5966
}
6067
}
68+
if (currentMatchesDouble) {
69+
for (const match of currentMatchesDouble) {
70+
code = code.replace(match, `require("${rootPath}/./`);
71+
}
72+
}
6173

6274
return code;
6375
}
@@ -66,6 +78,7 @@ function replaceRelativePaths(code: string, rootPath: string): string {
6678
function requireFromString(code: string, filename: string = '', optsObj: Object = {}): Promise < Object > {
6779
return new Promise((resolve, reject) => {
6880
const options = new Options(optsObj);
81+
let promiseArray = [];
6982

7083
if (typeof code !== 'string') {
7184
throw new Error('code must be a string, not ' + typeof code);
@@ -75,44 +88,39 @@ function requireFromString(code: string, filename: string = '', optsObj: Object
7588
var m = new Module(filename, options.rootPath);
7689
m.filename = filename;
7790
m.paths = [].concat(options.prependPaths).concat(paths).concat(options.appendPaths);
78-
try {
79-
m._compile(code, filename);
80-
resolve(m.exports.default);
81-
} catch (error) {
82-
//Check if the error is because the file isn't javascript
83-
if (error.message.includes('Unexpected token')) {
84-
//find matches for the require paths
85-
let vueComponentFileMatches = code.match(options.requireRegex);
86-
if (vueComponentFileMatches && vueComponentFileMatches.length > 0) {
87-
//iterate through the matches
88-
for (var index = 0; index < vueComponentFileMatches.length; index++) {
89-
var vueComponentFileMatch = vueComponentFileMatches[index];
90-
//get the file out of the require string
91-
//this is because its easier to do string replace later
92-
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
93-
if (vueComponentFile && vueComponentFile.length > 0) {
94-
getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch)
95-
.then(renderedItem => {
96-
const rawString = renderedItem.rendered.scriptStringRaw;
97-
code = code.replace(renderedItem.match, rawString);
98-
//check if its the last element and then render
99-
const last_element = code.match(options.requireRegex);
100-
if (last_element === undefined || last_element === null) {
101-
m._compile(code, filename);
102-
resolve(m.exports.default);
103-
}
104-
})
105-
.catch(error => {
106-
reject(error);
107-
});
108-
}
109-
}
110-
} else {
111-
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));
112103
}
113-
} else {
114-
reject(new Error('Couldnt require from string: ' + error));
115104
}
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);
116124
}
117125
});
118126
}

0 commit comments

Comments
 (0)