Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] Fix graphql compiler on typescript #949

Merged
merged 16 commits into from
Jun 15, 2017

Conversation

fabien0102
Copy link
Contributor

@fabien0102 fabien0102 commented May 10, 2017

Bug description

Actually graphql queries are compiled with a complexe ast parser and a babel plugin. Due to typescript files don't use any babel loader and not finished by .js they are totally ignored in this process.

What I did

Just add a babel into typescript loader (and hijack query from js loader), and deal with .ts|.tsx|.jsx files for queries parsing

Integration test on my starter

@fabien0102 fabien0102 force-pushed the 1.0-fix-graphql-compiler branch from adc55d0 to 9f60674 Compare May 10, 2017 16:55
@gatsbybot
Copy link
Collaborator

Deploy preview failed.

Built with commit adc55d074820650df7ec1dbd46976860beeb5c4b

https://app.netlify.com/sites/using-drupal/deploys/591345b4cf321c336be384d3

@KyleAMathews
Copy link
Contributor

Deploy preview failed.

Built with commit adc55d074820650df7ec1dbd46976860beeb5c4b

https://app.netlify.com/sites/gatsbyjs/deploys/591345b4cf321c336be384cd

@KyleAMathews
Copy link
Contributor

Deploy preview failed.

Built with commit adc55d074820650df7ec1dbd46976860beeb5c4b

https://app.netlify.com/sites/gatsbygram/deploys/591345b4cf321c336be384d0

@gatsbybot
Copy link
Collaborator

gatsbybot commented May 10, 2017

Deploy preview ready!

Built with commit 7cc009e

https://deploy-preview-949--using-drupal.netlify.com

@KyleAMathews
Copy link
Contributor

KyleAMathews commented May 10, 2017

Deploy preview ready!

Built with commit 7cc009e

https://deploy-preview-949--gatsbyjs.netlify.com

@KyleAMathews
Copy link
Contributor

KyleAMathews commented May 10, 2017

Deploy preview ready!

Built with commit 7cc009e

https://deploy-preview-949--gatsbygram.netlify.com

@@ -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)}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may do more than you want here. You really only want to run the babel query extractor plugin. This will run the file through all the babel transforms (admittedly most won't matter after tsc runs).

Honestly you probably better to just leave this off entirely, the plugin isn't important to running anything, it's really more of a minification step, as the queries aren't used a runtime it's better to just remove the code.

another option is to move the babel plugin to a proper package that can be consumed by plugins directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit overkill, I just want to avoid any futur missed babel transformer (we never think to typescript module ^^).

I'm not a big fan with another package for babel, we have already manyyyyy packages (and it introduce some tricky part for anyone want use gatsby without starter)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for information:

// During a `yarn build` on my starter
config._loaders.js.config.query = {
          "plugins": [
            "D:\\Projets\\gatsby-starter\\node_modules\\gatsby\\dist\\utils\\babel-plugin-extract-graphql.js",
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-plugin-add-module-exports\\lib\\index.js",
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-plugin-add-module-exports\\lib\\index.js",
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-plugin-transform-object-assign\\lib\\index.js"
          ],
          "presets": [
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-preset-es2015\\lib\\index.js",
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-preset-stage-0\\lib\\index.js",
            "D:\\Projets\\gatsby-starter\\node_modules\\babel-preset-react\\lib\\index.js"
          ],
          "cacheDirectory": true
        }

I will try to only take graphql-extract plugin ;) (but tomorow, it's late ^^)

@@ -50,7 +50,8 @@ class Runner {
}

async parseEverything() {
let files = await globp(`${this.baseDir}/**/*.js`)
let files = await globp(`${this.baseDir}/**/*.+(t|j)s?(x)`)
files = files.filter(d => !d.match(/(\.stories\.|\.test\.|\.d\.)/))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (I think) is too prescriptive. you'll undoubtably catch actual source files. perhaps it makes sense to make a a config option for "ignore paths" or something that is specifiable in the gatsby-config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's based on classic patterns of jest/storybook/typescript declaration files 😉 ignorePath can be a cool options but I don't want to provide useless option as it's commun patterns.

@KyleAMathews What do you thing on this issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why you chose it :) my point is only that conventions only meet some peoples needs. for instance in my own projects this wouldn't catch any of my test files or stories. More importantly tho it will likely silently filter real source files which is a different version of the problem of this problem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is only to avoid a warning on .d.ts file in my current project, I've just add stories and tests to optimize a little bit more ;) (so it's not really important for now if any test file are not filtered)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @jquense that by default we shouldn't ignore anything.

Shouldn't this just be globbing the src directory though?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this files are in src (one folder for one module, with tests beside component)
My only issue is with d.ts (needed for js module and global var as graphql) I can remove test and stories if you want ;) It was just to avoid useless processing

@fabien0102
Copy link
Contributor Author

BTW I don't try to have a project with coffeescript, but I think it will have the same issues

@KyleAMathews
Copy link
Contributor

@fabien0102 what's the status on Gatsby + Typescript?

@fabien0102
Copy link
Contributor Author

Sorry, I was very busy these last weeks, I will update this PR during next week ;)

@gatsbybot
Copy link
Collaborator

gatsbybot commented Jun 1, 2017 via email

@fabien0102 fabien0102 changed the title [1.0] Fix graphql compiler on typescript [1.0] [WIP] Fix graphql compiler on typescript Jun 9, 2017
@@ -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, `___`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think removing this is the best option, you can get namespace conflcits on fields without the @ that aren't resolvable.

I did fix this in two ways tho in #1136

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will cherry-pick your commit 😄 For instant I just try to have a functionnal gatsby env, I will clean this PR before remove [WIP] tag 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jquense I've cherry-pick your two commits in my branch and…
image

It's not working for me…

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you didn't change the queries to adjust to the commits? or maybe i didn't update the examples. it replaces characters with a single _ instead of 3 so frontmatter_updateRole etc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed! My bad

@fabien0102 fabien0102 mentioned this pull request Jun 12, 2017
@fabien0102 fabien0102 changed the title [1.0] [WIP] Fix graphql compiler on typescript [1.0] Fix graphql compiler on typescript Jun 14, 2017
@fabien0102
Copy link
Contributor Author

After many pains, this PR is fully working on my starter integration (fabien0102/gatsby-starter#24)

Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Sorry for all the API changes but glad you were able to get it working again.

I'm not in love with adding stuff to globs, etc. to support additional languages but this is the cheapest way to get this working right now for sure. This is what we did in 0.x and it worked out alright. When we add support for more compile-to-js languages we can debate a better API then and update this plugin accordingly.

@@ -15,14 +14,6 @@ import type { DocumentNode, DefinitionNode } from "graphql"

const readFileAsync = Bluebird.promisify(fs.readFile)

function getAssignedIdenifier(path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this not being used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My VSCode tell me that is not used, but you can recheck ;) (I try to remove unused code when I see it)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jquense you know what this was used for?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well if no code is calling it whatevers. We can recover it later if it's needed for something.

@KyleAMathews KyleAMathews merged commit 1639377 into gatsbyjs:1.0 Jun 15, 2017
@KyleAMathews
Copy link
Contributor

Thanks again @fabien0102 for holding the line on Typescript :-D

@fabien0102
Copy link
Contributor Author

@KyleAMathews When do you plan a new release? I need a "real" version for my starter 😉

@KyleAMathews
Copy link
Contributor

As soon as I finish #1174 going to do a beta release!

@fabien0102
Copy link
Contributor Author

Perfect!

@fabien0102 fabien0102 deleted the 1.0-fix-graphql-compiler branch October 4, 2017 08:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants