Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

feat(codegen): disallow src in fileName #87

Closed
wants to merge 12 commits into from
Closed

Conversation

d4rekanguok
Copy link
Owner

@d4rekanguok d4rekanguok commented Apr 15, 2020

Edit: Removed useModule, this PR now only add new improvements for the existing fileName option.

Update:

fileName

Outputing into src directory is now disallowed.

i.e

// gatsby-config.js

module.exports = {
  plugins: [{
    resolve: 'gatsby-plugin-graphql-codegen',
    options: { fileName: 'src/types.ts' }
  }]
}

// throw error

Dev stuff:

gatsby-plugin-graphql-codegen now use webpack to bundle into a single gatsby-node.js.


Related:

Close #85
Close #86

@d4rekanguok
Copy link
Owner Author

Todos:

  • make it work with additionalSchema
  • gatsby-starter-ts is broken for whatever reasons, gatsby doesn't recognize src/pages :(

@d4rekanguok
Copy link
Owner Author

@ricokahler let me know what you think about these new options!

@d4rekanguok
Copy link
Owner Author

Need to reconsider this in light of @cometkim comment in #87

It looks like yarn 2 can understand good ol' node_modules in loose mode, but I don't wanna risk the typescript slow update. Perhaps we could use just set a path alias in tsconfig.json?

@d4rekanguok d4rekanguok changed the title feat(codegen): generate into node_modules, disallow src feat(codegen): disallow src in fileName May 7, 2020
@d4rekanguok
Copy link
Owner Author

d4rekanguok commented May 7, 2020

Updated to remove the useModule stuff. Will close #85 when merged

@rico please take a look!

Copy link
Collaborator

@ricokahler ricokahler left a comment

Choose a reason for hiding this comment

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

Nice improvement! I think this will make the DX of the lib a lot better.

I think i caught few smaller things and I'm not sure I understand the benefit of bundling via webpack. Let me know what you think!

@@ -6,7 +6,7 @@ import Layout from '../components/layout'
import SEO from '../components/seo'
import { rhythm } from '../utils/typography'

import { BlogIndexQuery } from '../../gen/graphql-types'
import { BlogIndexQuery } from 'gatsby-ts'
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this still valid? I didn't see anything that would enable importing like this.

Copy link
Owner Author

Choose a reason for hiding this comment

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

shoot, thanks for catching this 🤦‍♀️

Comment on lines +20 to +29
!data ||
!data.site ||
!data.site.siteMetadata ||
!data.site.siteMetadata.title ||
!data.allMarkdownRemark
) {
throw new Error('no data')
}
const siteTitle = data.site.siteMetadata.title
const posts = data.allMarkdownRemark.edges
Copy link
Collaborator

Choose a reason for hiding this comment

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

old code but optional chaining would be nice here.

@@ -9,7 +9,7 @@ import * as React from 'react'
import Helmet from 'react-helmet'
import { useStaticQuery, graphql } from 'gatsby'
import ensureKeys from '../helpers/ensure-keys'
import { SeoQuery } from '../../graphql-types'
import { SeoQuery } from 'gatsby-ts'
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment

@@ -8,7 +8,7 @@
import * as React from 'react'
import { useStaticQuery, graphql } from 'gatsby'
import ensureKeys from '../helpers/ensure-keys'
import { SiteTitleQuery } from '../../graphql-types'
import { SiteTitleQuery } from 'gatsby-ts'
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment

@@ -2,7 +2,7 @@ import * as React from 'react'
import { useStaticQuery, graphql } from 'gatsby'
import Img from 'gatsby-image'
import ensureKeys from '../helpers/ensure-keys'
import { ImageQuery } from '../../graphql-types'
import { ImageQuery } from 'gatsby-ts'
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment

"typescript": "^3.7.5"
"ts-loader": "^7.0.0",
"typescript": "^3.7.5",
"webpack": "^4.42.1",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious: why did you decide to bundle this package webpack? Since these packages are meant for node, I don't see much value in bundling. And if you do decide to bundle, any reason why webpack over rollup?

Copy link
Owner Author

Choose a reason for hiding this comment

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

gatsby-node has to be placed in the root, otherwise Gatsby can't find it. Because of this, all files inside src is compiled to the root dir. I didn't want to ship unnecessary files, so I've been manually add/remove files to .npmignore. This is quite cumbersome as the amount of files increase.

Rollup was no.1 choice, but for whatever reasons I couldn't make it work with various typescript plugins. I had little time, so I decided to go with webpack, which worked first try.

Now come to think about it, I've realized I don't really need to bundle, I can just npmignore the src dir 🤦‍♀️ Or just ship the whole thing.

},
externals: [
{
'fs-extra': 'commonjs2 fs-extra',
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does putting commonjs before the package here do? I've never seen this before.

Copy link
Owner Author

Choose a reason for hiding this comment

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

It's additional context so webpack understand that the module (fs-extra) is a commonjs module, docs here. It's only useful for bundling node stuff. Without commonjs2, webpack would look for fs-extra in global space

type IsInSrc = (srcDir: string, fileDir: string) => boolean
export const isInSrc: IsInSrc = (srcDir, fileDir) => {
const relativePath = path.relative(srcDir, fileDir)
return !relativePath.startsWith('..')
Copy link
Collaborator

Choose a reason for hiding this comment

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

ah this is smart!

graphql-codegen.config.d.ts
utils
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is this gitignored?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Now this is why having a second pair of eyes is so valuable, this is probably meant for .npmignore but my 2am brain confused it with .gitignore, which is wrong anyway 🤦‍♀️

expect(fs.writeFile).toMatchInlineSnapshot(`
[MockFunction] {
"calls": Array [
Array [
"example-directory/example-types.ts",
undefined,
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm very curious on why this changed to undefined. this seems unexpected. can you confirm this is correct?

Copy link
Owner Author

Choose a reason for hiding this comment

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

You're right this isn't right, I'll run the test again

@d4rekanguok
Copy link
Owner Author

Thanks for reviewing @ricokahler, I shouldn't had tried to do more than one thing in one PR! I'll remove the bundle step & just use a normal .npmignore pattern.

@vimtor
Copy link

vimtor commented Oct 3, 2020

Maybe it is easier to add a way to ignore certain files so avoid an infinite loop?

@d4rekanguok d4rekanguok deleted the feat/module-name branch May 30, 2021 16:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[codegen] improve importing types from generated file Endless definition updates even when nothing goes on
3 participants