Coder Social home page Coder Social logo

frontendr / babel-plugin-transform-compress-graphql Goto Github PK

View Code? Open in Web Editor NEW
6.0 6.0 1.0 50 KB

A Babel plugin which compresses / minifies tagged template literals containing a GraphQL query.

Home Page: https://www.npmjs.com/package/babel-plugin-transform-compress-graphql

License: MIT License

JavaScript 100.00%
babel babel-plugin graphql minification

babel-plugin-transform-compress-graphql's People

Contributors

bedrockjohan avatar frontendr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

1v4nx

babel-plugin-transform-compress-graphql's Issues

Broken string interpolation

When interpolating with alpha characters in between (e.g, fragments), we end up with the same issue as in #4.

Input

const fragName = 'name';
const fragType = 'type';

gql`
fragment ${fragName} on ${fragType} {
  id
  name
}
`

Output
`fragment${fragName} on${fragType}{id name}`;

Expected output
`fragment ${fragName} on ${fragType}{id name}`;

Versions:

  • Plugin version: v1.3.2

Default to removing the tag

It would be more useful if the tagFunction option by default was undefined, which would result in removing the tagName tag in the output, making the template string untagged.

The current default String.raw not only adds extra characters to a bundle, but it also has gotchas regarding escapes.

Broken number interpolation

This input:

const ONE = 1
const query = /* GraphQL */ `
  {
    a(b: ${ONE} c: true)
  }
`

Incorrectly results in this output:

const ONE = 1;
const query = `{a(b:${ONE}c:true)}`;

Which results in a query with invalid syntax:

{a(b:1c:true)}

This went unnoticed in my project for some time. GraphQL.js v15 no longer tolerates numbers being followed immediately with letters, and now the compressed queries are being rejected as having syntax errors:

I mistakenly thought at first this was a graphql-query-compress issue (rse/graphql-query-compress#14); but it turns out to definitely be an issue with this babel plugin.

1.3.3 breaks queries with template literals

Describe the bug

Seems like spaces are being added around template literal variables when they shouldn't be.

Input

graphql(
    > 82 |         gql`
         |             ^
      83 |             query getTable${isBug ? 'Bug' : ''}(

Output

GraphQLError: Syntax Error: Expected {, found Name "Bug"

Expected output
No bug.

Versions (please complete the following information):

  • Babel version: latest
  • GraphQL version: 14
  • Plugin version: 1.3.3

Additional context

Worked in 1.3.2 as long as there were no spaces. If there were spaces it broke. Can we support both cases?

Comparison with the graphql-query-compress compression approach

This library appears to have a simple compression function based on string replace() and regex:

https://github.com/frontendr/babel-plugin-transform-compress-graphql/blob/1.1.0/src/index.js#L3

graphql-query-compress has a more complicated approach using lexical analysis:

https://github.com/rse/graphql-query-compress/blob/1.2.1/src/graphql-query-compress.js#L29

It is also heavier tho, with a 143kb install size.

Which approach is "safer"? If graphql-query-compress is "safer", does it make sense to use it in babel-plugin-transform-compress-graphql?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.