From 61720244ae888031bf83c46fc576d5d2e927e312 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 17:17:28 +0200 Subject: [PATCH 01/14] Remove unused var --- packages/gatsby/src/query-runner/file-parser.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/packages/gatsby/src/query-runner/file-parser.js b/packages/gatsby/src/query-runner/file-parser.js index aff0f7b192d5d..c4235221c8736 100644 --- a/packages/gatsby/src/query-runner/file-parser.js +++ b/packages/gatsby/src/query-runner/file-parser.js @@ -4,7 +4,6 @@ const crypto = require(`crypto`) // Traverse is a es6 module... import traverse from "babel-traverse" -import * as types from "babel-types" const babylon = require(`babylon`) const Bluebird = require(`bluebird`) @@ -15,14 +14,6 @@ import type { DocumentNode, DefinitionNode } from "graphql" const readFileAsync = Bluebird.promisify(fs.readFile) -function getAssignedIdenifier(path) { - let property = path.parentPath - while (property) { - if (types.isVariableDeclarator(property)) return property.node.id.name - property = property.parentPath - } -} - async function parseToAst(filePath, fileStr) { let ast @@ -121,7 +112,7 @@ const cache = {} export default class FileParser { async parseFile(file: string): Promise { const text = await readFileAsync(file, `utf8`) - + if (text.indexOf(`graphql`) === -1) return const hash = crypto .createHash(`md5`) From 764b733da95c511d349cfbf9971749cab47c4f84 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 17:17:46 +0200 Subject: [PATCH 02/14] Fix indent --- packages/gatsby/src/utils/babel-plugin-extract-graphql.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/utils/babel-plugin-extract-graphql.js b/packages/gatsby/src/utils/babel-plugin-extract-graphql.js index 3f8e73e457369..659a5bb595325 100644 --- a/packages/gatsby/src/utils/babel-plugin-extract-graphql.js +++ b/packages/gatsby/src/utils/babel-plugin-extract-graphql.js @@ -10,8 +10,8 @@ function getGraphQLTag(path) { if (quasis.length !== 1) { throw new Error( `BabelPluginGraphQL: Substitutions are not allowed in graphql. ` + - `fragments. Included fragments should be referenced ` + - `as \`...MyModule_foo\`.` + `fragments. Included fragments should be referenced ` + + `as \`...MyModule_foo\`.` ) } From 5401cda81fe1f17bf206a60f147f9725def28c6c Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 17:18:34 +0200 Subject: [PATCH 03/14] Add babel to typescript plugin It's usefull to parse any graphql queries --- packages/gatsby-plugin-typescript/src/gatsby-node.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index e99658cf45cdb..77ce9dd9f9a64 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -19,7 +19,10 @@ module.exports.modifyWebpackConfig = ({ config }, { compilerOptions }) => { const opts = { compilerOptions: copts, transpileOnly: true } config.loader(`typescript`, { test, - loader: `ts-loader?` + JSON.stringify(opts), + loaders: [ + `babel?${JSON.stringify(config._loaders.js.config.query)}`, + `ts-loader?${JSON.stringify(opts)}`, + ], }) } From 9580915193d6b17d63f0b2ef8b339e49f2863eed Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 17:19:07 +0200 Subject: [PATCH 04/14] Let `parseEverything` really parse everything --- packages/gatsby/src/query-runner/query-compiler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/gatsby/src/query-runner/query-compiler.js b/packages/gatsby/src/query-runner/query-compiler.js index 072e457f85240..d13f6c05d9998 100644 --- a/packages/gatsby/src/query-runner/query-compiler.js +++ b/packages/gatsby/src/query-runner/query-compiler.js @@ -50,9 +50,9 @@ class Runner { } async parseEverything() { - let files = await globp(`${this.baseDir}/**/*.js`) + let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`) let parser = new FileParser() - + return await parser.parseFiles(files) } From 347abf40e0fa1d920a351135c57bc1b529a8f026 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 18:03:05 +0200 Subject: [PATCH 05/14] Exclude tests, declarations and stories --- packages/gatsby/src/query-runner/query-compiler.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/gatsby/src/query-runner/query-compiler.js b/packages/gatsby/src/query-runner/query-compiler.js index d13f6c05d9998..b684bdbd5f33f 100644 --- a/packages/gatsby/src/query-runner/query-compiler.js +++ b/packages/gatsby/src/query-runner/query-compiler.js @@ -51,6 +51,7 @@ class Runner { async parseEverything() { let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`) + files = files.filter(d => !d.match(/(\.stories\.|\.test\.|\.d\.)/)) let parser = new FileParser() return await parser.parseFiles(files) From 9f60674c831db191a6ffe5652133c91978e0b2d6 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 10 May 2017 18:53:48 +0200 Subject: [PATCH 06/14] Run yarn format --- packages/gatsby-plugin-typescript/src/gatsby-node.js | 2 +- packages/gatsby/src/query-runner/file-parser.js | 2 +- packages/gatsby/src/query-runner/query-compiler.js | 2 +- packages/gatsby/src/utils/babel-plugin-extract-graphql.js | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index 77ce9dd9f9a64..71a6912c2a7ef 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -22,7 +22,7 @@ module.exports.modifyWebpackConfig = ({ config }, { compilerOptions }) => { loaders: [ `babel?${JSON.stringify(config._loaders.js.config.query)}`, `ts-loader?${JSON.stringify(opts)}`, - ], + ], }) } diff --git a/packages/gatsby/src/query-runner/file-parser.js b/packages/gatsby/src/query-runner/file-parser.js index c4235221c8736..d9a3ca9f076a0 100644 --- a/packages/gatsby/src/query-runner/file-parser.js +++ b/packages/gatsby/src/query-runner/file-parser.js @@ -112,7 +112,7 @@ const cache = {} export default class FileParser { async parseFile(file: string): Promise { const text = await readFileAsync(file, `utf8`) - + if (text.indexOf(`graphql`) === -1) return const hash = crypto .createHash(`md5`) diff --git a/packages/gatsby/src/query-runner/query-compiler.js b/packages/gatsby/src/query-runner/query-compiler.js index b684bdbd5f33f..57d14fdf01ce1 100644 --- a/packages/gatsby/src/query-runner/query-compiler.js +++ b/packages/gatsby/src/query-runner/query-compiler.js @@ -53,7 +53,7 @@ class Runner { let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`) files = files.filter(d => !d.match(/(\.stories\.|\.test\.|\.d\.)/)) let parser = new FileParser() - + return await parser.parseFiles(files) } diff --git a/packages/gatsby/src/utils/babel-plugin-extract-graphql.js b/packages/gatsby/src/utils/babel-plugin-extract-graphql.js index 659a5bb595325..3f8e73e457369 100644 --- a/packages/gatsby/src/utils/babel-plugin-extract-graphql.js +++ b/packages/gatsby/src/utils/babel-plugin-extract-graphql.js @@ -10,8 +10,8 @@ function getGraphQLTag(path) { if (quasis.length !== 1) { throw new Error( `BabelPluginGraphQL: Substitutions are not allowed in graphql. ` + - `fragments. Included fragments should be referenced ` + - `as \`...MyModule_foo\`.` + `fragments. Included fragments should be referenced ` + + `as \`...MyModule_foo\`.` ) } From 5cd6f142ca2a0bacde8f59d06ef4e256c8f2f86c Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Thu, 11 May 2017 00:43:19 +0200 Subject: [PATCH 07/14] Don't filter tests and stories --- packages/gatsby/src/query-runner/query-compiler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/query-runner/query-compiler.js b/packages/gatsby/src/query-runner/query-compiler.js index 57d14fdf01ce1..733c8f3884dc9 100644 --- a/packages/gatsby/src/query-runner/query-compiler.js +++ b/packages/gatsby/src/query-runner/query-compiler.js @@ -51,7 +51,7 @@ class Runner { async parseEverything() { let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`) - files = files.filter(d => !d.match(/(\.stories\.|\.test\.|\.d\.)/)) + files = files.filter(d => !d.match(/\.d\.ts$/)) let parser = new FileParser() return await parser.parseFiles(files) From 893a9d9754bd4618cd2f5b1fbae572f53adf4e20 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Fri, 9 Jun 2017 11:42:52 +0200 Subject: [PATCH 08/14] Fix building schema console.time --- packages/gatsby/src/bootstrap/index.js | 2 ++ packages/gatsby/src/schema/index.js | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/bootstrap/index.js b/packages/gatsby/src/bootstrap/index.js index ed9257ac85f02..e1e4128c7bdeb 100644 --- a/packages/gatsby/src/bootstrap/index.js +++ b/packages/gatsby/src/bootstrap/index.js @@ -225,7 +225,9 @@ data console.timeEnd(`initial sourcing and transforming nodes`) // Create Schema. + console.time(`building schema`) await require(`../schema`)() + console.timeEnd(`building schema`) // Collect resolvable extensions and attach to program. const extensions = [`.js`, `.jsx`] diff --git a/packages/gatsby/src/schema/index.js b/packages/gatsby/src/schema/index.js index 15615b986a5de..c36c78f626bf6 100644 --- a/packages/gatsby/src/schema/index.js +++ b/packages/gatsby/src/schema/index.js @@ -23,7 +23,6 @@ module.exports = async () => { }), }) - console.timeEnd(`building schema`) store.dispatch({ type: `SET_SCHEMA`, payload: schema, From 4bca4a035d9dff86a78439c566f8f58d3c2baa63 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Fri, 9 Jun 2017 17:26:40 +0200 Subject: [PATCH 09/14] Fix create-path on windows --- .../src/internal-plugins/component-page-creator/create-path.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js b/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js index 03ee04f38c47b..456272075ba2c 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js @@ -5,7 +5,6 @@ import path from "path" module.exports = (basePath: string, filePath: string): string => { const relativePath = path.posix.relative(basePath, filePath) const parsedPath = parsePath(relativePath) - const { dirname } = parsedPath let { name } = parsedPath if (name === `index`) { name = `` @@ -13,7 +12,7 @@ module.exports = (basePath: string, filePath: string): string => { // Use path.posix to force forward slash paths. Otherwise on windows // node.js will add backward slashes. - const pagePath = path.posix.join(`/`, dirname, name, `/`) + const pagePath = path.posix.join(`/`, name, `/`) return pagePath } From 353b44b6f20318f12e149e0b70a727513e90a13d Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Fri, 9 Jun 2017 17:27:33 +0200 Subject: [PATCH 10/14] Deal with `@` into keys This is to avoid conflit with `@types/{packageName}` pattern into packages.json --- packages/gatsby/src/schema/create-key.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/schema/create-key.js b/packages/gatsby/src/schema/create-key.js index 44ffda091ade3..eaef0a75a703b 100644 --- a/packages/gatsby/src/schema/create-key.js +++ b/packages/gatsby/src/schema/create-key.js @@ -1,3 +1,3 @@ const regex = new RegExp(`[^a-zA-Z0-9_]`, `g`) -module.exports = key => key.replace(regex, `___`) +module.exports = key => key.replace(`@`, ``).replace(regex, `___`) From 32e7508b54bbe264b90e1a0714efb56f62e3b85e Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Sun, 11 Jun 2017 11:21:48 +0200 Subject: [PATCH 11/14] Add tests and refactor create-path --- .../__snapshots__/gatsby-node.js.snap | 10 ++++++++- .../__tests__/gatsby-node.js | 21 +++++++++++++++---- .../component-page-creator/create-path.js | 16 +++++--------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap index 774e2782dd508..c71de51c4c29a 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap +++ b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/__snapshots__/gatsby-node.js.snap @@ -1,9 +1,17 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`JavaScript page creator Creates paths 1`] = ` +exports[`JavaScript page creator create-path should create unix paths 1`] = ` Array [ "/b/c/de/", "/bee/", "/b/d/c/", ] `; + +exports[`JavaScript page creator create-path should deal with windows paths 1`] = ` +Array [ + "/404/", + "/", + "/blog/", +] +`; diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js index a34f3b8182226..2063207819eb5 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/__tests__/gatsby-node.js @@ -34,10 +34,23 @@ describe(`JavaScript page creator`, () => { expect(files.filter(file => validatePath(file.path)).length).toEqual(1) }) - it(`Creates paths`, () => { - const basePath = `/a/` - const paths = [`/a/b/c/de`, `/a/bee`, `/a/b/d/c/`] + describe(`create-path`, () => { + it(`should create unix paths`, () => { + const basePath = `/a/` + const paths = [`/a/b/c/de`, `/a/bee`, `/a/b/d/c/`] - expect(paths.map(p => createPath(basePath, p))).toMatchSnapshot() + expect(paths.map(p => createPath(basePath, p))).toMatchSnapshot() + }) + + it(`should deal with windows paths`, () => { + const basePath = `D:/Projets/gatsby-starter/src/pages` + const paths = [ + `D:\\Projets\\gatsby-starter\\src\\pages\\404.tsx`, + `D:\\Projets\\gatsby-starter\\src\\pages\\index.tsx`, + `D:\\Projets\\gatsby-starter\\src\\pages\\blog.tsx`, + ] + + expect(paths.map(p => createPath(basePath, p))).toMatchSnapshot() + }) }) }) diff --git a/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js b/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js index 456272075ba2c..eb30b1c4b7342 100644 --- a/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js +++ b/packages/gatsby/src/internal-plugins/component-page-creator/create-path.js @@ -1,18 +1,12 @@ // @flow import parsePath from "parse-filepath" import path from "path" +import slash from "slash" module.exports = (basePath: string, filePath: string): string => { - const relativePath = path.posix.relative(basePath, filePath) - const parsedPath = parsePath(relativePath) - let { name } = parsedPath - if (name === `index`) { - name = `` - } + const relativePath = path.posix.relative(slash(basePath), slash(filePath)) + const { dirname, name } = parsePath(relativePath) + const parsedName = name === `index` ? `` : name - // Use path.posix to force forward slash paths. Otherwise on windows - // node.js will add backward slashes. - const pagePath = path.posix.join(`/`, name, `/`) - - return pagePath + return path.posix.join(`/`, dirname, parsedName, `/`) } From 698acc817129af5fb9bec53adeacaa6db52865fc Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Sun, 11 Jun 2017 13:24:12 +0200 Subject: [PATCH 12/14] Fix types of gatsby-link --- packages/gatsby-link/index.d.ts | 11 ++++++----- packages/gatsby-link/package.json | 1 - packages/gatsby-link/yarn.lock | 15 --------------- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/packages/gatsby-link/index.d.ts b/packages/gatsby-link/index.d.ts index ae25bb66da1de..863856250ed36 100644 --- a/packages/gatsby-link/index.d.ts +++ b/packages/gatsby-link/index.d.ts @@ -1,7 +1,8 @@ -import { ClassicComponentClass } from "react"; -import { LinkProps } from "react-router"; +import * as React from "react"; -type GatsbyLink = ClassicComponentClass; -declare const GatsbyLink: GatsbyLink; +export interface GatsbyLinkProps { + to: string; + onClick?: (event: any) => void +} -export = GatsbyLink; \ No newline at end of file +export default class GatbyLink extends React.Component; \ No newline at end of file diff --git a/packages/gatsby-link/package.json b/packages/gatsby-link/package.json index 3b49f305b2ebe..d6942d5ab0032 100644 --- a/packages/gatsby-link/package.json +++ b/packages/gatsby-link/package.json @@ -18,7 +18,6 @@ "babel-cli": "^6.24.1" }, "dependencies": { - "@types/react-router": "^2.0.49", "prop-types": "^15.5.8", "ric": "^1.3.0" } diff --git a/packages/gatsby-link/yarn.lock b/packages/gatsby-link/yarn.lock index c7ab7eb6e1ab2..a15511ad38163 100644 --- a/packages/gatsby-link/yarn.lock +++ b/packages/gatsby-link/yarn.lock @@ -2,21 +2,6 @@ # yarn lockfile v1 -"@types/history@^2": - version "2.0.48" - resolved "https://registry.yarnpkg.com/@types/history/-/history-2.0.48.tgz#7e2868c3ad73d83c482f1d68f148c4fdc79c8a79" - -"@types/react-router@^2.0.49": - version "2.0.49" - resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-2.0.49.tgz#96f8ad51f07a5890ab35fd55f05170efd132552a" - dependencies: - "@types/history" "^2" - "@types/react" "*" - -"@types/react@*": - version "15.0.23" - resolved "https://registry.yarnpkg.com/@types/react/-/react-15.0.23.tgz#f3facbef5290610f54242f00308759d3a3c27346" - abbrev@1: version "1.1.0" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f" From 7f6229a0a2884c095cb1f3780d1a20fc67809283 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Sun, 11 Jun 2017 15:38:05 +0200 Subject: [PATCH 13/14] Add ts and tsx into query-watcher --- .../gatsby/src/internal-plugins/query-runner/query-watcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js index 1520b1c9c6c26..78c027c814b3f 100644 --- a/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js +++ b/packages/gatsby/src/internal-plugins/query-runner/query-watcher.js @@ -69,7 +69,7 @@ exports.watch = rootDir => { }, 100) watcher = chokidar - .watch(`${rootDir}/src/**/*.{js,jsx}`) + .watch(`${rootDir}/src/**/*.{js,jsx,ts,tsx}`) .on(`change`, path => { debounceCompile() }) From 7cc009e4759a6da3553bf23e06d65c6ade091078 Mon Sep 17 00:00:00 2001 From: fabien0102 Date: Wed, 14 Jun 2017 23:14:40 +0200 Subject: [PATCH 14/14] Only take babel-plugin-extract-graphql plugin --- packages/gatsby-plugin-typescript/src/gatsby-node.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-plugin-typescript/src/gatsby-node.js b/packages/gatsby-plugin-typescript/src/gatsby-node.js index 71a6912c2a7ef..dd53c5ad9b9b6 100644 --- a/packages/gatsby-plugin-typescript/src/gatsby-node.js +++ b/packages/gatsby-plugin-typescript/src/gatsby-node.js @@ -1,4 +1,5 @@ const { transpileModule } = require(`typescript`) +const path = require(`path`) const test = /\.tsx?$/ const compilerDefaults = { @@ -14,13 +15,18 @@ module.exports.modifyWebpackConfig = ({ config }, { compilerOptions }) => { const copts = Object.assign({}, compilerDefaults, compilerOptions, { module: `commonjs`, }) + // React-land is rather undertyped; nontrivial TS projects will most likely // error (i.e., not build) at something or other. const opts = { compilerOptions: copts, transpileOnly: true } + + // Load gatsby babel plugin to extract graphql query + const extractQueryPlugin = path.resolve(__dirname, `../gatsby/dist/utils/babel-plugin-extract-graphql.js`) + config.loader(`typescript`, { test, loaders: [ - `babel?${JSON.stringify(config._loaders.js.config.query)}`, + `babel?${JSON.stringify({ plugins:[extractQueryPlugin] })}`, `ts-loader?${JSON.stringify(opts)}`, ], })