graphql-mini-transforms
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -8,4 +8,8 @@ # Changelog | ||
## [1.0.2] - 2019-04-29 | ||
- Fixed issues with the Webpack loader when a document had one or more imports ([#80](https://github.com/Shopify/graphql-tools-web/pull/80)) | ||
## [1.0.0] - 2019-04-09 | ||
Initial release |
@@ -5,3 +5,3 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/m; | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/gm; | ||
const DEFAULT_NAME = 'Operation'; | ||
@@ -8,0 +8,0 @@ function cleanDocument(document) { |
@@ -0,2 +1,3 @@ | ||
/// <reference types="node" /> | ||
import { loader } from 'webpack'; | ||
export default function graphQLLoader(this: loader.LoaderContext, source: string): Promise<void>; | ||
export default function graphQLLoader(this: loader.LoaderContext, source: string | Buffer): Promise<void>; |
@@ -33,3 +33,4 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { imports, source } = document_1.extractImports(rawSource); | ||
const normalizedSource = typeof rawSource === 'string' ? rawSource : rawSource.toString(); | ||
const { imports, source } = document_1.extractImports(normalizedSource); | ||
const document = graphql_1.parse(source); | ||
@@ -36,0 +37,0 @@ if (imports.length === 0) { |
{ | ||
"name": "graphql-mini-transforms", | ||
"description": "Transformers for importing .graphql files in various build tools.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"types": "lib", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -5,3 +5,3 @@ "use strict"; | ||
const graphql_1 = require("graphql"); | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/m; | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/gm; | ||
const DEFAULT_NAME = 'Operation'; | ||
@@ -8,0 +8,0 @@ function cleanDocument(document) { |
@@ -13,3 +13,3 @@ import {createHash} from 'crypto'; | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/m; | ||
const IMPORT_REGEX = /^#import\s+['"]([^'"]*)['"];?[\s\n]*/gm; | ||
const DEFAULT_NAME = 'Operation'; | ||
@@ -16,0 +16,0 @@ |
@@ -0,2 +1,3 @@ | ||
/// <reference types="node" /> | ||
import { loader } from 'webpack'; | ||
export default function graphQLLoader(this: loader.LoaderContext, source: string): Promise<void>; | ||
export default function graphQLLoader(this: loader.LoaderContext, source: string | Buffer): Promise<void>; |
@@ -33,3 +33,4 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { imports, source } = document_1.extractImports(rawSource); | ||
const normalizedSource = typeof rawSource === 'string' ? rawSource : rawSource.toString(); | ||
const { imports, source } = document_1.extractImports(normalizedSource); | ||
const document = graphql_1.parse(source); | ||
@@ -36,0 +37,0 @@ if (imports.length === 0) { |
@@ -9,3 +9,3 @@ import {dirname} from 'path'; | ||
this: loader.LoaderContext, | ||
source: string, | ||
source: string | Buffer, | ||
) { | ||
@@ -31,7 +31,10 @@ this.cacheable(); | ||
async function loadDocument( | ||
rawSource: string, | ||
rawSource: string | Buffer, | ||
resolveContext: string, | ||
loader: loader.LoaderContext, | ||
): Promise<DocumentNode> { | ||
const {imports, source} = extractImports(rawSource); | ||
const normalizedSource = | ||
typeof rawSource === 'string' ? rawSource : rawSource.toString(); | ||
const {imports, source} = extractImports(normalizedSource); | ||
const document = parse(source); | ||
@@ -38,0 +41,0 @@ |
@@ -108,2 +108,28 @@ "use strict"; | ||
})); | ||
it('includes multiple imported sources', () => __awaiter(this, void 0, void 0, function* () { | ||
const context = '/app/'; | ||
const fragmentFiles = new Map([ | ||
['/app/FooFragment.graphql', 'fragment FooFragment on Shop { id }'], | ||
['/app/BarFragment.graphql', 'fragment BarFragment on Shop { name }'], | ||
]); | ||
const loader = createLoaderContext({ | ||
context, | ||
readFile: (file) => fragmentFiles.get(file), | ||
}); | ||
const { loc: { source: { body }, }, } = yield extractDocumentExport(common_tags_1.stripIndent ` | ||
#import './FooFragment.graphql'; | ||
#import './BarFragment.graphql'; | ||
query Shop { | ||
shop { | ||
...FooFragment | ||
...BarFragment | ||
} | ||
} | ||
`, loader); | ||
expect(body).toContain('...FooFragment'); | ||
expect(body).toContain('...BarFragment'); | ||
expect(body).toContain('fragment FooFragment on Shop'); | ||
expect(body).toContain('fragment BarFragment on Shop'); | ||
})); | ||
it('excludes imported sources if they are not used', () => __awaiter(this, void 0, void 0, function* () { | ||
@@ -137,3 +163,3 @@ const context = '/app/'; | ||
if (typeof read === 'string') { | ||
withFile(null, read); | ||
withFile(null, Buffer.from(read, 'utf8')); | ||
} | ||
@@ -140,0 +166,0 @@ else { |
@@ -139,2 +139,40 @@ import * as path from 'path'; | ||
it('includes multiple imported sources', async () => { | ||
const context = '/app/'; | ||
const fragmentFiles = new Map([ | ||
['/app/FooFragment.graphql', 'fragment FooFragment on Shop { id }'], | ||
['/app/BarFragment.graphql', 'fragment BarFragment on Shop { name }'], | ||
]); | ||
const loader = createLoaderContext({ | ||
context, | ||
readFile: (file) => fragmentFiles.get(file)!, | ||
}); | ||
const { | ||
loc: { | ||
source: {body}, | ||
}, | ||
} = await extractDocumentExport( | ||
stripIndent` | ||
#import './FooFragment.graphql'; | ||
#import './BarFragment.graphql'; | ||
query Shop { | ||
shop { | ||
...FooFragment | ||
...BarFragment | ||
} | ||
} | ||
`, | ||
loader, | ||
); | ||
expect(body).toContain('...FooFragment'); | ||
expect(body).toContain('...BarFragment'); | ||
expect(body).toContain('fragment FooFragment on Shop'); | ||
expect(body).toContain('fragment BarFragment on Shop'); | ||
}); | ||
it('excludes imported sources if they are not used', async () => { | ||
@@ -187,3 +225,3 @@ const context = '/app/'; | ||
file: string, | ||
withFile: (error: Error | null, result?: string) => void, | ||
withFile: (error: Error | null, result?: string | Buffer) => void, | ||
) { | ||
@@ -193,3 +231,3 @@ const read = readFile(file); | ||
if (typeof read === 'string') { | ||
withFile(null, read); | ||
withFile(null, Buffer.from(read, 'utf8')); | ||
} else { | ||
@@ -196,0 +234,0 @@ withFile(read); |
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
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
50950
1303