Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: svg-components with absolute imports not working #7117

Closed
paul-vd opened this issue Apr 23, 2019 · 9 comments
Closed

Issue: svg-components with absolute imports not working #7117

paul-vd opened this issue Apr 23, 2019 · 9 comments
Labels
good first issue Easy to fix issues, good for newcomers

Comments

@paul-vd
Copy link
Contributor

paul-vd commented Apr 23, 2019

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
@paul-vd
Copy link
Contributor Author

paul-vd commented Apr 23, 2019

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'

@Timer Timer added good first issue Easy to fix issues, good for newcomers help wanted labels Apr 29, 2019
@AdamT213
Copy link

AdamT213 commented May 1, 2019

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

@paul-vd
Copy link
Contributor Author

paul-vd commented May 7, 2019

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'

@paul-vd paul-vd closed this as completed May 7, 2019
@chasevida
Copy link

chasevida commented Mar 12, 2020

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;
}

@hoangvvo
Copy link
Contributor

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.

@samerset-rmn
Copy link

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

@EzraBerendsen
Copy link

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

@dbroadhurst
Copy link

fixed VSCode from complaining and works in runtime as expected

import InfoIcon from '.info-solid.svg'

return (<InfoIcon />)

next-env.d.ts

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

next-config.js

module.exports = {
  webpack(config) {
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    })

    return config
  },
}

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

No branches or pull requests

9 participants