diff --git a/package-lock.json b/package-lock.json index 2f3f6b8..b450f95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1450,7 +1450,7 @@ "requires": { "ansi-align": "2.0.0", "camelcase": "4.1.0", - "chalk": "2.0.1", + "chalk": "2.1.0", "cli-boxes": "1.0.0", "string-width": "2.1.1", "term-size": "1.2.0", @@ -1473,9 +1473,9 @@ "dev": true }, "chalk": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.0.1.tgz", - "integrity": "sha512-Mp+FXEI+FrwY/XYV45b2YD3E8i3HwnEAoFcM0qlZzq/RZ9RwWitt2Y/c7cqRAz70U7hfekqx6qNYthuKFO6K0g==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.1.0.tgz", + "integrity": "sha512-LUHGS/dge4ujbXMJrnihYMcL4AoOweGnw9Tp3kQuqy1Kx5c1qKjqvMJZ6nVJPMWJtKCTN72ZogH3oeSO9g9rXQ==", "dev": true, "requires": { "ansi-styles": "3.2.0", diff --git a/src/utils/require.js b/src/utils/require.js index 427f894..e526c00 100644 --- a/src/utils/require.js +++ b/src/utils/require.js @@ -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 || ''; @@ -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; } @@ -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); @@ -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); } }); }