graphql-tag icon indicating copy to clipboard operation
graphql-tag copied to clipboard

Webpack Import Alias Not Working

Open aboe026 opened this issue 6 years ago • 4 comments

I'm attempting to use aliases in my imports across my projects. For some reason, they do not work for my .gql files (I get Cannot find module). Here is my webpack config:

var ExtraWatchWebpackPlugin = require('extra-watch-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var LiveReloadPlugin = require('webpack-livereload-plugin')
var path = require('path')

var config = {
  entry: 'src/client/index.js',
  output: {
    path: path.resolve(__dirname, 'build/client'),
    publicPath: '/',
    filename: 'index_bundle.js'
  },
  mode: process.env.NODE_ENV === 'development' ? 'development' : 'production',
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        use: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.gql$/,
        use: 'graphql-tag/loader',
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    alias: {
      'src': path.resolve(__dirname, 'src')
    },
    extensions: ['.webpack.js', '.mjs', '.js', '.json', '.gql']
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'ABCSTravels',
      template: 'src/client/index.html'
    }),
    new ExtraWatchWebpackPlugin({
      files: [
        'src/client/**/*.gql'
      ]
    })
  ]
}

if (process.env.NODE_ENV === 'development') {
  // don't parse minified bundles (vendor libs) for faster builds
  config.module.noParse = [
    /\.min\.js$/
  ]
  config.plugins.push(new LiveReloadPlugin({
    appendScriptTag: true
  }))
}

module.exports = config

This import statement works:

import VISITS_QUERY from './VisitsQuery.gql'

but not this one:

import VISITS_QUERY from 'src/client/visits/VisitsQuery.gql'

I don't have this issue with any other file types in my project (.js, .json, .css)

graphql-tag version: 2.9.2 node version: 9.3.0 OS: Windows 10 Build 17134

aboe026 avatar Jul 26 '18 01:07 aboe026

having the same issue

sebas5384 avatar Aug 06 '18 05:08 sebas5384

I am unable to even import MyQuery from './queries.gql, I just get [ts] Cannot find module errors

ethanshry avatar Aug 10 '18 20:08 ethanshry

@ethanshry https://webpack.js.org/guides/typescript/#importing-other-assets might help you. With TS we can also use the paths CompilerOption with TS-loader or AWT, that works.

PaulKujawa avatar Dec 04 '18 11:12 PaulKujawa

@ethanshry See https://github.com/apollographql/graphql-tag/issues/59 - I had the same issue until I added a definition type to my project with this:

declare module '*.graphql' {
  import {DocumentNode} from 'graphql';

  const value: DocumentNode;
  export = value;
}

MattMorrisDev avatar Feb 13 '19 22:02 MattMorrisDev