Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 51 additions & 43 deletions src/utils/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Options {
defaults: Models.Defaults;
constructor(optsObj: Object) {
this.vueFileRegex = /([\w/.\-@_\d]*\.vue)/igm;
this.requireRegex = /(require\(')([\w/.\-@_\d]*\.vue)('\))/igm;
this.requireRegex = /(require\(['"])([\w/.\-@_\d]*\.vue)(['"]\))/igm;
this.appendPaths = optsObj.appendPaths || [];
this.prependPaths = optsObj.prependPaths || [];
this.rootPath = optsObj.rootPath || '';
Expand Down Expand Up @@ -46,18 +46,30 @@ function getVueObject(componentPath: string, rootPath: string, vueComponentFileM
}

function replaceRelativePaths(code: string, rootPath: string): string {
const parentMatches = code.match(/(require\('\.\.\/)/gm);
const currentMatches = code.match(/(require\('\.\/)/gm);
if (parentMatches) {
for (const match of parentMatches) {
const parentMatchesSingle = code.match(/(require\('\.\.\/)/gm);
const currentMatchesSingle = code.match(/(require\('\.\/)/gm);
const parentMatchesDouble = code.match(/(require\("\.\.\/)/gm);
const currentMatchesDouble = code.match(/(require\("\.\/)/gm);
if (parentMatchesSingle) {
for (const match of parentMatchesSingle) {
code = code.replace(match, `require('${rootPath}/../`);
}
}
if (currentMatches) {
for (const match of currentMatches) {
if (parentMatchesDouble) {
for (const match of parentMatchesDouble) {
code = code.replace(match, `require("${rootPath}/../`);
}
}
if (currentMatchesSingle) {
for (const match of currentMatchesSingle) {
code = code.replace(match, `require('${rootPath}/./`);
}
}
if (currentMatchesDouble) {
for (const match of currentMatchesDouble) {
code = code.replace(match, `require("${rootPath}/./`);
}
}

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

if (typeof code !== 'string') {
throw new Error('code must be a string, not ' + typeof code);
Expand All @@ -75,44 +88,39 @@ function requireFromString(code: string, filename: string = '', optsObj: Object
var m = new Module(filename, options.rootPath);
m.filename = filename;
m.paths = [].concat(options.prependPaths).concat(paths).concat(options.appendPaths);
try {
m._compile(code, filename);
resolve(m.exports.default);
} catch (error) {
//Check if the error is because the file isn't javascript
if (error.message.includes('Unexpected token')) {
//find matches for the require paths
let vueComponentFileMatches = code.match(options.requireRegex);
if (vueComponentFileMatches && vueComponentFileMatches.length > 0) {
//iterate through the matches
for (var index = 0; index < vueComponentFileMatches.length; index++) {
var vueComponentFileMatch = vueComponentFileMatches[index];
//get the file out of the require string
//this is because its easier to do string replace later
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
if (vueComponentFile && vueComponentFile.length > 0) {
getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch)
.then(renderedItem => {
const rawString = renderedItem.rendered.scriptStringRaw;
code = code.replace(renderedItem.match, rawString);
//check if its the last element and then render
const last_element = code.match(options.requireRegex);
if (last_element === undefined || last_element === null) {
m._compile(code, filename);
resolve(m.exports.default);
}
})
.catch(error => {
reject(error);
});
}
}
} else {
reject(new Error('Couldnt require component from string: ' + error));

//find matches for the require paths
let vueComponentFileMatches = code.match(options.requireRegex);
if (vueComponentFileMatches && vueComponentFileMatches.length > 0) {
//iterate through the matches
for (var index = 0; index < vueComponentFileMatches.length; index++) {
var vueComponentFileMatch = vueComponentFileMatches[index];
//get the file out of the require string
//this is because its easier to do string replace later
const vueComponentFile = vueComponentFileMatch.match(options.vueFileRegex);
if (vueComponentFile && vueComponentFile.length > 0) {
promiseArray.push(getVueObject(vueComponentFile[0], options.rootPath, vueComponentFileMatch));
}
} else {
reject(new Error('Couldnt require from string: ' + error));
}
Promise.all(promiseArray)
.then(renderedItemArray => {
for (var renderedItem of renderedItemArray) {
const rawString = renderedItem.rendered.scriptStringRaw;
code = code.replace(renderedItem.match, rawString);
}
//check if its the last element and then render
const last_element = code.match(options.vueFileRegex);
if (last_element === undefined || last_element === null) {
m._compile(code, filename);
resolve(m.exports.default);
}
})
.catch(error => {
reject(error);
});
} else {
m._compile(code, filename);
resolve(m.exports.default);
}
});
}
Expand Down