Next.js: Issue: svg-components with absolute imports not working

Created on 23 Apr 2019  路  7Comments  路  Source: vercel/next.js

Examples bug report

Example name

custom-server-typescript
svg-components
with-absolute-imports

Describe the bug

with the svg-components and absolute import the svg's are not found

[ error ] ./components/cowsay.tsx
Error: Cannot find module 'svg/cat.svg' from '/Users/Projects/Playground/next/components' 

with tslint it does not see .jpg, .svg or any images as paths but instead as modules

Absolute Paths:
_Cannot find module 'svg/cat.svg.ts(2307)_
_Cannot find module 'images/pexels-photo.jpg'.ts(2307)_

Relative Paths:
_Cannot find module '../assets/svg/cat.svg'.ts(2307)_
_Cannot find module '../assets/images/pexels-photo.jpg'.ts(2307)_

_( as i side note i added paths to my tsconfig )_

    "paths": {
      "images/*": ["assets/images/*"],
      "svg/*": ["assets/svg/*"]
    },

To Reproduce

https://github.com/PaulPCIO/next.js-issues-svg-absolute-imports-tslint

uncomment lines in components/ImageExamples.tsx

Expected behavior

  1. Expect SVG's to import from absolute paths
  2. Expect tslint to recognise image importations

Screenshots

issue 1:

import Absolute_CatSVG from 'svg/cat.svg'

image

#issue 2: RESOLVED
image

System information

  • OS: macOS
  • Version of Next.js: 8.1.0
good first issue

Most helpful comment

Update:

as for the tslinting part i ended up just adding an index.d.ts under my @types folder and then declaring the modules

declare module '*.png'
declare module '*.jpg'
declare module '*.jpeg'
declare module '*.svg'
declare module '*.gif'

All 7 comments

Update:

as for the tslinting part i ended up just adding an index.d.ts under my @types folder and then declaring the modules

declare module '*.png'
declare module '*.jpg'
declare module '*.jpeg'
declare module '*.svg'
declare module '*.gif'

I would like to work on this. @Timer , any advice on where I can start looking?

I would like to work on this. @Timer , any advice on where I can start looking?

This should be resolved, as i explained in my update it was just a declaration problem with typescript,
I had to define the declaration for images:

Update:

as for the tslinting part i ended up just adding an index.d.ts under my @types folder and then declaring the modules

declare module '*.png'
declare module '*.jpg'
declare module '*.jpeg'
declare module '*.svg'
declare module '*.gif'

For anyone else landing here from Google at 1.30am in the morning whilst trying to deploy a prototype for a pitch, the following format in the index.d.ts resulted in errors for my build:

declare module '*.png'

I had to declare the properties as such:

declare module "*.png" {
  const value: any;
  export default value;
}

Not sure if it is related but I recently stumbled upon this while using Next.js absolute import with babel-plugin-inline-react-svg and expected import SvgThing from '@/assets/svg/thing.svg' magically works. It won't.

I guess because it is Babel related, not Webpack related so absolute import won't work out of the box. Also this

My solution is to create a index.ts in assets/svg and reexports the svgs (export { default as SvgThing } from './thing.svg') and import from that file instead of directly from the .svg files.

Not sure if it is related but I recently stumbled upon this while using Next.js absolute import with babel-plugin-inline-react-svg and expected import SvgThing from '@/assets/svg/thing.svg' magically works. It won't.

I guess because it is Babel related, not Webpack related so absolute import won't work out of the box. Also this

My solution is to create a index.ts in assets/svg and reexports the svgs (export { default as SvgThing } from './thing.svg') and import from that file instead of directly from the .svg files.

Thank you! So far, this is the best solution

Not sure if it is related but I recently stumbled upon this while using Next.js absolute import with babel-plugin-inline-react-svg and expected import SvgThing from '@/assets/svg/thing.svg' magically works. It won't.

I guess because it is Babel related, not Webpack related so absolute import won't work out of the box. Also this

My solution is to create a index.ts in assets/svg and reexports the svgs (export { default as SvgThing } from './thing.svg') and import from that file instead of directly from the .svg files.

If I use this I get the following error:
index.ts: pass.get(...) is not a function

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rauchg picture rauchg  路  3Comments

olifante picture olifante  路  3Comments

timneutkens picture timneutkens  路  3Comments

wagerfield picture wagerfield  路  3Comments

swrdfish picture swrdfish  路  3Comments