From 2d2c43f77ce498660307d6a110b9bf76d6f2c882 Mon Sep 17 00:00:00 2001 From: Daniel Cherubini Date: Wed, 16 Aug 2017 13:07:39 +0200 Subject: [PATCH 1/3] potential solution to double/single quotes --- package-lock.json | 40 ++++++++++++++++++++-------------------- package.json | 4 ++-- src/utils/require.js | 26 +++++++++++++++++++------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index e0f42e2..8be14ca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -338,9 +338,9 @@ "dev": true }, "ava": { - "version": "0.21.0", - "resolved": "https://registry.npmjs.org/ava/-/ava-0.21.0.tgz", - "integrity": "sha512-+ZjahyjqyzkPLlFZe2OoLmiE3aaQ2jK5h74wrkuX5I+J6LpNAPoQ8X/EhqEtKEjuWwmniLAjnVjZ7OY8rWdJwA==", + "version": "0.22.0", + "resolved": "https://registry.npmjs.org/ava/-/ava-0.22.0.tgz", + "integrity": "sha512-dYxvVDL9CeIcgaQ/FojaBVaL/rnIsXdgPVliDOMe1O5nSsIZEsPYDIzmZ1KnO/cuxeQx1PQbtW6qziiEwQZusg==", "dev": true, "requires": { "@ava/babel-preset-stage-4": "1.1.0", @@ -358,7 +358,7 @@ "babel-core": "6.25.0", "bluebird": "3.5.0", "caching-transform": "1.0.1", - "chalk": "2.0.1", + "chalk": "2.1.0", "chokidar": "1.7.0", "clean-stack": "1.3.0", "clean-yaml-object": "0.1.0", @@ -441,9 +441,9 @@ } }, "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", @@ -1275,7 +1275,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", @@ -1298,9 +1298,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", @@ -2060,9 +2060,9 @@ "dev": true }, "deepmerge": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-1.5.0.tgz", - "integrity": "sha512-Hm4+NyDQGgH3oYhKqR0gd99veBBZpnEUNoEfFl+3PRkQL+LKGJEBgqimeofAWzUn6aBzcaYPJrRigto/WfDzTg==" + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-1.5.1.tgz", + "integrity": "sha512-Ndl8eeOHB9dQkmT1HWCgY3t0odl4bmWKFzjQZBYAxVTNs2B3nn5b6orimRYHKZ4FI8psvZkA1INRCW6l7vc9lQ==" }, "default-require-extensions": { "version": "1.0.0", @@ -2343,9 +2343,9 @@ } }, "eslint": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.4.0.tgz", - "integrity": "sha1-o+FT5wS2T3gpDvA1kklOq6Io07w=", + "version": "4.4.1", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.4.1.tgz", + "integrity": "sha1-mc1+r8/8ov+Zpcj18qR01jZLS9M=", "dev": true, "requires": { "ajv": "5.2.2", @@ -2771,9 +2771,9 @@ } }, "flow-bin": { - "version": "0.52.0", - "resolved": "https://registry.npmjs.org/flow-bin/-/flow-bin-0.52.0.tgz", - "integrity": "sha1-ttmr6LzR7lxi3zhkUaTiVTytw6M=", + "version": "0.53.0", + "resolved": "https://registry.npmjs.org/flow-bin/-/flow-bin-0.53.0.tgz", + "integrity": "sha1-94MOYJygKxLbQScRQhPMzHwHcbk=", "dev": true }, "flow-remove-types": { diff --git a/package.json b/package.json index 498c0bc..ff215cb 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "babel-preset-es2015": "^6.24.1", "camel-case": "^3.0.0", "clean-css": "^4.1.7", - "deepmerge": "^1.5.0", + "deepmerge": "^1.5.1", "html-minifier": "^3.5.3", "lru-cache": "^4.1.1", "pug": "^2.0.0-rc.3", @@ -88,7 +88,7 @@ "babel-preset-flow": "^6.23.0", "codecov": "^2.3.0", "coveralls": "^2.13.1", - "eslint": "^4.4.0", + "eslint": "^4.4.1", "eslint-config-xo-space": "^0.16.0", "eslint-plugin-flowtype": "^2.35.0", "express": "^4.15.4", diff --git a/src/utils/require.js b/src/utils/require.js index 427f894..078dcc3 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; } From 5aa54b7112a09c5d79c9ce4ade84fed0ea34fc47 Mon Sep 17 00:00:00 2001 From: Daniel Cherubini Date: Mon, 21 Aug 2017 14:58:31 +0200 Subject: [PATCH 2/3] refactored require --- src/utils/require.js | 68 +++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/src/utils/require.js b/src/utils/require.js index 078dcc3..e526c00 100644 --- a/src/utils/require.js +++ b/src/utils/require.js @@ -78,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); @@ -87,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); } }); } From 57539436edb8342668937b8ca16e3c5b96320e75 Mon Sep 17 00:00:00 2001 From: Daniel Cherubini Date: Mon, 21 Aug 2017 17:23:23 +0200 Subject: [PATCH 3/3] Update package-lock.json --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index ac7bf30..b450f95 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2517,7 +2517,7 @@ } } }, - "eslint": + "eslint": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/eslint/-/eslint-4.5.0.tgz", "integrity": "sha1-u3XTuL3pf7XhPvzVOXRGd/6wGcM=",