feats / babel-plugin-inline-import

inline the raw content of import statements
https://www.npmjs.com/package/babel-plugin-inline-import
MIT License
243 stars 29 forks source link

Schema is cached after first run #1

Closed hinterlist closed 8 years ago

hinterlist commented 8 years ago

Hi @zvictor,

Thank you for your work, that's a nice plugin and works great, but I think I've spotted a problem. A schema files are cached after first run and will not updated unless you remove ~/.babel.json file.

zvictor commented 8 years ago

Hi!

I think you are right. The plugin gets the content of the imported file and inline it in the importing one, but Babel is not aware of this dependency. Therefore, you need to change the importing file in order to see your changes in the imported file spread. I tried many ways to overcome this, unsuccessfully.

The only workaround I found was disabling babel cache (BABEL_DISABLE_CACHE=1) and making sure that my task runner was watching for changes in the inlined file as well. You can see it working here.

I thought I had added this to the documentation, but it seems that I forgot. I am sorry for that. If you have any idea of how we can improve this issue, please let us know.

jaydenseric commented 7 years ago

All these issues go away when using a webpack loader instead to achieve the same thing... I got importing .qraphql files as template strings for Apollo working with https://github.com/bradbenvenuti/template-string-loader. With webpack watching, changes to the imported .graphql files results in automatic recompiles just fine.

In your server webpack config, add:

{
  test: /\.(graphql|gql)$/,
  exclude: /node_modules/,
  loader: 'template-string-loader'
}

To import a .graphql file:

import graphql from './types.graphql'
const types = graphql()

You have to run the import as a function to get the template string, as the library caters for passing arguments into the template.

First impressions; it doesn't sit well that .graphql files are processed on import very differently in the server code vs client code (https://github.com/apollostack/graphql-tag loader)... Perhaps when I try to get stuff working isomorphically this will cause issues, and it is easy to forget this behavior when moving around the codebase.

HaveF commented 7 years ago

@zvictor Thanks for your plugin. And I also puzzled by this problem, I do suppose this should be a default option --- when *.graphql file changes, the program restart. At first, I thought it is some bug in my code, after failed many times, I read the doc of this plugin, and find the answer... nodemon's watch and babel's cache must be configured

@jaydenseric Thanks for your thoughts about the import. Could you tell me more the detail of graphql-tag loader user case. I do not understand the different between them, it seems grahpql-tag is import *.graphql as GraphQL ASTs. Is it only used in client? Confused... I also want to find some thing to work isomorphically

@stubailo what do you think about this issue, and how to use grahpql-tag loader in your project? Only use it as client part?

stubailo commented 7 years ago

Hey, I didn't contribute the Webpack loader to the package so I don't know if there is a good way to use it on the server. On the server there aren't any common build systems except Meteor that can be integrated with, so perhaps the best option is to use a require hook?

HaveF commented 7 years ago

@stubailo thanks for your reply :blush:

@heldr Hi, Helder Santana, I found you create grahpql-tag loader, thanks for your work. @jaydenseric and I meet the import problem when working isomorphically.

The current problem is

  1. import *.graphql file may confused people

a. If you want to import *.graphql file on the server, you have to use this babel plugin, and set nodemon's watch and babel's cache, or use template-string-loader as @jaydenseric showed above.

b. If you want to import *.graphql file on the client, use the loader you created, actually, the import result is Graphql ASTs

  1. Could it possible to use just one tool to handle import problem?

The possible solution

  1. Use some conventions, in jimkyndemeyer/js-graphql-intellij-plugin, *.graphqls is schema, and *.graphql is for query (on the client). what do you think?

  2. have no idea

Koleok commented 7 years ago

I ended up running this chained in with my build command, which basically comes from babel-register/lib/cache.js 😱

const homeOrTmp = require('home-or-tmp');
const path = require('path');

const cachePath =
  process.env.BABEL_CACHE_PATH || path.join(homeOrTmp, '.babel.json');

require('rimraf')(cachePath, err =>
  console.log(`graphql schema was${err ? ' not' : ''} cleared`)
);

Seems pretty crude but I have not had any success setting the BABEL_DISABLE_CACHE=1 and just need some way to have new schema changes actually deploy.