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

SVG / Image Import #343

Closed
seanemmer opened this issue Nov 21, 2019 · 7 comments
Closed

SVG / Image Import #343

seanemmer opened this issue Nov 21, 2019 · 7 comments
Labels
scope: integration Related to an integration, not necessarily to core (but could influence core) solution: duplicate This issue or pull request already exists

Comments

@seanemmer
Copy link

seanemmer commented Nov 21, 2019

Current Behavior

SVG imports into TS/TSX files are not found

Desired Behavior

The ability to import SVGs into TS/TSX files

Suggested Solution

Implement rollup-plugin-svg

Who does this impact? Who is this for?

All users

Describe alternatives you've considered

I am currently manually including the SVG in my TSX file which is not a sustainable solution

Additional context

Using v. 0.11.0

@arthurdenner
Copy link
Contributor

@seanemmer, can you try to extend the config with tsdx.config.js like in this example?

@swyxio swyxio added the kind: question This is a usage or similar question label Dec 4, 2019
@seanemmer
Copy link
Author

seanemmer commented Jan 31, 2020

I attempted this but no luck. tsdx.config.js below:

const svg = require('rollup-plugin-svg')

module.exports = {
  rollup(config) {
    config.plugins.push(svg())
    return config
  }
}

@hrgui
Copy link

hrgui commented Mar 1, 2020

I have the same problem, @rollup/plugin-image supports svgs, but it is not even reading it:

const postcss = require('rollup-plugin-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const image = require('@rollup/plugin-image');

module.exports = {
  rollup(config, options) {
    config.plugins.push(
      image(),
      postcss({
        plugins: [
          autoprefixer(),
          cssnano({
            preset: 'default',
          }),
        ]
      })
    );
    return config;
  },
};

@chrbala
Copy link

chrbala commented Mar 1, 2020

I was able to get it working, but I had to specifically include the files in the build output and inline them.

const svg = require('rollup-plugin-svg');

module.exports = {
  rollup(config) {
    const external = config.external;
    config.external = id => (id.match(/.svg$/) ? false : external(id));
    config.plugins.push(
      svg({
        base64: false,
      })
    );
    return config;
  },
};

@hrgui
Copy link

hrgui commented Mar 1, 2020

For me, it had to do with plugin order. Putting @rollup/plugin-image last causes it to be not called at all. Once I made image the first plugin, then it works:

const postcss = require('rollup-plugin-postcss');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const image = require('@rollup/plugin-image');

module.exports = {
  rollup(config, options) {
    config.plugins.push(
      postcss({
        plugins: [
          autoprefixer(),
          cssnano({
            preset: 'default',
          }),
        ],
      })
    );

    config.plugins.unshift(image());
    return config;
  },
};

@agilgur5
Copy link
Collaborator

agilgur5 commented Mar 9, 2020

As @hrgui mentioned, I believe the config order is the key detail here. See also #379 (comment) / #386 (comment) / #278

I was able to get it working, but I had to specifically include the files in the build output and inline them.

@chrbala that's a separate issue related to externals or aliases: #91 . See also #379 (comment)

@agilgur5 agilgur5 closed this as completed Mar 9, 2020
@agilgur5 agilgur5 added solution: duplicate This issue or pull request already exists and removed kind: question This is a usage or similar question labels Mar 9, 2020
@agilgur5 agilgur5 changed the title SVG Import SVG / Image Import Mar 9, 2020
@agilgur5 agilgur5 added the scope: integration Related to an integration, not necessarily to core (but could influence core) label Mar 9, 2020
@tobyscott25
Copy link

@chrbala's solution worked for me! I just had to create a svg.d.ts file in my project so TypeScript would recognise SVGs as modules :)

declare module "*.svg" {
    const content: any;
    export default content;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: integration Related to an integration, not necessarily to core (but could influence core) solution: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

7 participants