npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-transform-graphql-tag

v0.2.1

Published

Compiles GraphQL tagged template strings using graphql-tag in TypeScript files.

Downloads

20,454

Readme

ts-transform-graphql-tag

npm Travis FOSSA Status license

Compiles GraphQL tagged template strings using graphql-tag in TypeScript files.

The plugin was mostly inspired by great Babel's plugin babel-plugin-graphql-tag.

Motivation

Compiling GraphQL queries at the build time:

  • reduces the script initialization time; and
  • removes the graphql-tag dependency

Removing the graphql-tag dependecy from the bundle saves approx. 50 KB.

Implementation

  • Searches for imports of graphql-tag and removes them.
  • Searches for tagged template literals with gql identifier and compiles them using graphql-tag.

Installation

The following command adds the packages to the project as a development-time dependency:

npm i --save-dev ts-transform-graphql-tag

This also depends on graphql and graphql-tag so you'll need those in your project as well (if you don't already have them):

# usually, this is a production dependency
npm i graphql

# add this as a development-time dependency
npm i --save-dev graphql-tag

Usage

Integration with Webpack

If you using Webpack, there are two popular TypeScript loaders that support specifying custom transformers:

Both loaders use the same setting getCustomTransformers which is an optional function that returns { before?: Transformer[], after?: Transformer[] }. In order to inject the transformer into compilation, add it to before transformers array, like: { before: [getTransformer()] }.

awesome-typescript-loader

In the webpack.config.js file in the section where awesome-typescript-loader is configured as a loader:

// 1. import `getTransformer` from the module
var getTransformer = require('ts-transform-graphql-tag').getTransformer

// 2. create a transformer and add getCustomTransformer method to the loader config
var config = {
  /// ...
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'awesome-typescript-loader',
        options: {
          // ... other loader's options
          getCustomTransformers: () => ({ before: [getTransformer()] })
        }
      }
    ]
  }
  /// ...
}

ts-loader

In the webpack.config.js file in the section where ts-loader is configured as a loader:

// 1. import `getTransformer` from the module
var getTransformer = require('ts-transform-graphql-tag').getTransformer

// 2. create a transformer and add getCustomTransformer method to the loader config
var config = {
  // ...
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        options: {
          // ... other loader's options
          getCustomTransformers: () => ({ before: [getTransformer()] })
        }
      }
    ]
  }
  // ...
};

Integration with FuseBox

FuseBox is a blazing fast (TypeScipt first) bundler/module loader.

In the fuse.ts file, you can configured like this:

// 1. import `getTransformer` from the module
import { getTransformer } from "ts-transform-graphql-tag"

// 2. create a transformer and add it to the `transformers.before` config
const fuse = FuseBox.init({
  // ... other init options
  transformers: {
    before: [
      getTransformer()
    ]
  }
})

More information about using TypeScript custom transformer in FuseBox.

Example

before

// with transformer
import gql from "graphql-tag"
export default gql`query Hello {hello}`

after

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "query", "name": { "kind": "Name", "value": "Hello" }, "variableDefinitions": [], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "alias": undefined, "name": { "kind": "Name", "value": "hello" }, "arguments": [], "directives": [], "selectionSet": undefined }] } }], "loc": { "start": 0, "end": 19, "source": { "body": "query Hello {hello}", "name": "GraphQL request", "locationOffset": { "line": 1, "column": 1 } } } };

Need more example? run npm test and checkout test/fixture/actual/*.js.

Related

Thanks