@graphql-toolkit/graphql-tag-pluck
Advanced tools
Comparing version 0.7.5-alpha-8dfe565.0 to 0.7.5-alpha-90cb687.15
118
index.cjs.js
@@ -7,2 +7,21 @@ 'use strict'; | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) { return e; } else { | ||
var n = {}; | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { | ||
return e[k]; | ||
} | ||
}); | ||
}); | ||
} | ||
n['default'] = e; | ||
return n; | ||
} | ||
} | ||
const parser = require('@babel/parser'); | ||
@@ -68,2 +87,5 @@ const t = require('@babel/types'); | ||
break; | ||
case '.vue': | ||
plugins.push('vue'); | ||
break; | ||
default: | ||
@@ -82,2 +104,11 @@ plugins.push('jsx', ...dynamicFlowPlugins); | ||
const getExtNameFromFilePath = (filePath) => { | ||
const partials = filePath.split('.'); | ||
let ext = '.' + partials.pop(); | ||
if (partials.length > 1 && partials[partials.length - 1] == 'flow') { | ||
ext = '.' + partials.pop() + ext; | ||
} | ||
return ext; | ||
}; | ||
// Will use the shortest indention as an axis | ||
@@ -110,52 +141,2 @@ const freeText = text => { | ||
const parseCode = (code, config) => { | ||
// The 'typescript' plug-in has few bugs... It's just better to use the native one | ||
// even though it affects performance | ||
if (config.plugins.includes('typescript')) { | ||
let ts; | ||
try { | ||
ts = require('typescript'); | ||
} | ||
catch (e) { | ||
throw Error(freeText(` | ||
GraphQL template literals cannot be plucked from a TypeScript code without having the "typescript" package installed. | ||
Please install it and try again. | ||
Via NPM: | ||
$ npm install typescript | ||
Via Yarn: | ||
$ yarn add typescript | ||
`)); | ||
} | ||
code = ts.transpileModule(code, { | ||
compilerOptions: { | ||
target: ts.ScriptTarget.ES2018, | ||
// "preserve" mode would be more correct, but it will keep not transpile generic | ||
// React.Components which are provided with null or undefined e.g. <Foo<undefined />> | ||
jsx: config.plugins.includes('jsx') && 'react', | ||
}, | ||
}).outputText; | ||
const plugins = config.plugins.slice(); | ||
const tsIndex = plugins.indexOf('typescript'); | ||
plugins.splice(tsIndex, 1); | ||
config = { ...config, plugins }; | ||
} | ||
const ast = parser.parse(code, config); | ||
// Necessary to get the original code in case it was transformed by TypeScript | ||
ast['code'] = code; | ||
return ast; | ||
}; | ||
const getExtNameFromFilePath = (filePath) => { | ||
const partials = filePath.split('.'); | ||
let ext = '.' + partials.pop(); | ||
if (partials.length > 1 && partials[partials.length - 1] == 'flow') { | ||
ext = '.' + partials.pop() + ext; | ||
} | ||
return ext; | ||
}; | ||
const defaults = { | ||
@@ -373,3 +354,3 @@ modules: [ | ||
const gqlExtensions = ['.graphqls', '.graphql', '.gqls', '.gql']; | ||
const jsExtensions = ['.js', '.jsx', '.ts', '.tsx', '.flow', '.flow.js', '.flow.jsx']; | ||
const jsExtensions = ['.js', '.jsx', '.ts', '.tsx', '.flow', '.flow.js', '.flow.jsx', '.vue']; | ||
const supportedExtensions = [...gqlExtensions, ...jsExtensions]; | ||
@@ -379,3 +360,25 @@ supportedExtensions.toString = function toString() { | ||
}; | ||
const gqlPluckFromFile = (filePath, options = {}) => { | ||
async function pluckVueFileScript(fileData) { | ||
let vueTemplateCompiler; | ||
try { | ||
vueTemplateCompiler = await new Promise(function (resolve) { resolve(_interopNamespace(require('vue-template-compiler'))); }); | ||
} | ||
catch (e) { | ||
throw Error(freeText(` | ||
GraphQL template literals cannot be plucked from a Vue template code without having the "vue-template-compiler" package installed. | ||
Please install it and try again. | ||
Via NPM: | ||
$ npm install vue-template-compiler | ||
Via Yarn: | ||
$ yarn add vue-template-compiler | ||
`)); | ||
} | ||
const parsed = vueTemplateCompiler.parseComponent(fileData); | ||
return parsed.script ? parsed.script.content : ''; | ||
} | ||
const gqlPluckFromFile = async (filePath, options = {}) => { | ||
if (typeof filePath != 'string') { | ||
@@ -396,6 +399,9 @@ throw TypeError('Provided file path must be a string'); | ||
options = { ...options, fileExt }; | ||
const code = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
let code = fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
if (fileExt === '.vue') { | ||
code = await pluckVueFileScript(code); | ||
} | ||
return gqlPluckFromCodeString(code, options); | ||
}; | ||
const gqlPluckFromCodeString = (code, options = {}) => { | ||
const gqlPluckFromCodeString = async (code, options = {}) => { | ||
if (typeof code != 'string') { | ||
@@ -416,4 +422,4 @@ throw TypeError('Provided code must be a string'); | ||
const out = { returnValue: null }; | ||
const ast = parseCode(code, generateConfig(code, options)); | ||
const visitor = createVisitor(ast['code'], out, options); | ||
const ast = parser.parse(code, generateConfig(code, options)); | ||
const visitor = createVisitor(code, out, options); | ||
traverse(ast, visitor); | ||
@@ -420,0 +426,0 @@ return out.returnValue; |
@@ -10,8 +10,8 @@ export interface GraphQLTagPluckOptions { | ||
} | ||
export declare const gqlPluckFromFile: (filePath: string, options?: GraphQLTagPluckOptions) => string; | ||
export declare const gqlPluckFromCodeString: (code: string, options?: GraphQLTagPluckOptions) => string; | ||
export declare const gqlPluckFromFile: (filePath: string, options?: GraphQLTagPluckOptions) => Promise<string>; | ||
export declare const gqlPluckFromCodeString: (code: string, options?: GraphQLTagPluckOptions) => Promise<string>; | ||
declare const _default: { | ||
fromFile: (filePath: string, options?: GraphQLTagPluckOptions) => string; | ||
fromCodeString: (code: string, options?: GraphQLTagPluckOptions) => string; | ||
fromFile: (filePath: string, options?: GraphQLTagPluckOptions) => Promise<string>; | ||
fromCodeString: (code: string, options?: GraphQLTagPluckOptions) => Promise<string>; | ||
}; | ||
export default _default; |
@@ -61,2 +61,5 @@ import { parse } from '@babel/parser'; | ||
break; | ||
case '.vue': | ||
plugins.push('vue'); | ||
break; | ||
default: | ||
@@ -75,2 +78,11 @@ plugins.push('jsx', ...dynamicFlowPlugins); | ||
const getExtNameFromFilePath = (filePath) => { | ||
const partials = filePath.split('.'); | ||
let ext = '.' + partials.pop(); | ||
if (partials.length > 1 && partials[partials.length - 1] == 'flow') { | ||
ext = '.' + partials.pop() + ext; | ||
} | ||
return ext; | ||
}; | ||
// Will use the shortest indention as an axis | ||
@@ -103,52 +115,2 @@ const freeText = text => { | ||
const parseCode = (code, config) => { | ||
// The 'typescript' plug-in has few bugs... It's just better to use the native one | ||
// even though it affects performance | ||
if (config.plugins.includes('typescript')) { | ||
let ts; | ||
try { | ||
ts = require('typescript'); | ||
} | ||
catch (e) { | ||
throw Error(freeText(` | ||
GraphQL template literals cannot be plucked from a TypeScript code without having the "typescript" package installed. | ||
Please install it and try again. | ||
Via NPM: | ||
$ npm install typescript | ||
Via Yarn: | ||
$ yarn add typescript | ||
`)); | ||
} | ||
code = ts.transpileModule(code, { | ||
compilerOptions: { | ||
target: ts.ScriptTarget.ES2018, | ||
// "preserve" mode would be more correct, but it will keep not transpile generic | ||
// React.Components which are provided with null or undefined e.g. <Foo<undefined />> | ||
jsx: config.plugins.includes('jsx') && 'react', | ||
}, | ||
}).outputText; | ||
const plugins = config.plugins.slice(); | ||
const tsIndex = plugins.indexOf('typescript'); | ||
plugins.splice(tsIndex, 1); | ||
config = { ...config, plugins }; | ||
} | ||
const ast = parse(code, config); | ||
// Necessary to get the original code in case it was transformed by TypeScript | ||
ast['code'] = code; | ||
return ast; | ||
}; | ||
const getExtNameFromFilePath = (filePath) => { | ||
const partials = filePath.split('.'); | ||
let ext = '.' + partials.pop(); | ||
if (partials.length > 1 && partials[partials.length - 1] == 'flow') { | ||
ext = '.' + partials.pop() + ext; | ||
} | ||
return ext; | ||
}; | ||
const defaults = { | ||
@@ -366,3 +328,3 @@ modules: [ | ||
const gqlExtensions = ['.graphqls', '.graphql', '.gqls', '.gql']; | ||
const jsExtensions = ['.js', '.jsx', '.ts', '.tsx', '.flow', '.flow.js', '.flow.jsx']; | ||
const jsExtensions = ['.js', '.jsx', '.ts', '.tsx', '.flow', '.flow.js', '.flow.jsx', '.vue']; | ||
const supportedExtensions = [...gqlExtensions, ...jsExtensions]; | ||
@@ -372,3 +334,25 @@ supportedExtensions.toString = function toString() { | ||
}; | ||
const gqlPluckFromFile = (filePath, options = {}) => { | ||
async function pluckVueFileScript(fileData) { | ||
let vueTemplateCompiler; | ||
try { | ||
vueTemplateCompiler = await import('vue-template-compiler'); | ||
} | ||
catch (e) { | ||
throw Error(freeText(` | ||
GraphQL template literals cannot be plucked from a Vue template code without having the "vue-template-compiler" package installed. | ||
Please install it and try again. | ||
Via NPM: | ||
$ npm install vue-template-compiler | ||
Via Yarn: | ||
$ yarn add vue-template-compiler | ||
`)); | ||
} | ||
const parsed = vueTemplateCompiler.parseComponent(fileData); | ||
return parsed.script ? parsed.script.content : ''; | ||
} | ||
const gqlPluckFromFile = async (filePath, options = {}) => { | ||
if (typeof filePath != 'string') { | ||
@@ -389,6 +373,9 @@ throw TypeError('Provided file path must be a string'); | ||
options = { ...options, fileExt }; | ||
const code = readFileSync(filePath, { encoding: 'utf8' }); | ||
let code = readFileSync(filePath, { encoding: 'utf8' }); | ||
if (fileExt === '.vue') { | ||
code = await pluckVueFileScript(code); | ||
} | ||
return gqlPluckFromCodeString(code, options); | ||
}; | ||
const gqlPluckFromCodeString = (code, options = {}) => { | ||
const gqlPluckFromCodeString = async (code, options = {}) => { | ||
if (typeof code != 'string') { | ||
@@ -409,4 +396,4 @@ throw TypeError('Provided code must be a string'); | ||
const out = { returnValue: null }; | ||
const ast = parseCode(code, generateConfig(code, options)); | ||
const visitor = createVisitor(ast['code'], out, options); | ||
const ast = parse(code, generateConfig(code, options)); | ||
const visitor = createVisitor(code, out, options); | ||
traverse(ast, visitor); | ||
@@ -413,0 +400,0 @@ return out.returnValue; |
{ | ||
"name": "@graphql-toolkit/graphql-tag-pluck", | ||
"version": "0.7.5-alpha-8dfe565.0+8dfe565", | ||
"version": "0.7.5-alpha-90cb687.15+90cb687", | ||
"description": "Pluck graphql-tag template literals", | ||
@@ -14,7 +14,10 @@ "peerDependencies": { | ||
"dependencies": { | ||
"@babel/parser": "7.7.4", | ||
"@babel/parser": "7.7.5", | ||
"@babel/traverse": "7.7.4", | ||
"@babel/types": "7.7.4" | ||
}, | ||
"optionalDependencies": { | ||
"vue-template-compiler": "^2.6.10" | ||
}, | ||
"sideEffects": false | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
33789
5
9
848
+ Added@babel/parser@7.7.5(transitive)
+ Addedde-indent@1.0.2(transitive)
+ Addedhe@1.2.0(transitive)
+ Addedvue-template-compiler@2.7.16(transitive)
- Removed@babel/parser@7.7.4(transitive)
Updated@babel/parser@7.7.5