最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle svg imports (ES6) in rollup - Stack Overflow

programmeradmin3浏览0评论

If I'm importing an svg file into an ES6 module, how do I handle that in rollup? I currently have something like this (simple example of what I'm doing):

import next from '../assets/next.svg';
export default () => console.log('Here is some Svg: ! ', next);

And I have a rollup config that looks like this:

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  entry: 'src/app.js',
  dest: 'build/app.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    babel({
      exclude: 'node_modules/**',
    }),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
  ],
};

And then I get the following error:

Could not resolve '../assets/next.svg' from /home/magnferm/projects/slask/rollup-test/src/chill/index.js

There is nothing wrong with the path, but rollup doesn't seem to know how to handle svg-files. Is there a plugin I can use for that or do I have to treat it differently somehow?

If I'm importing an svg file into an ES6 module, how do I handle that in rollup? I currently have something like this (simple example of what I'm doing):

import next from '../assets/next.svg';
export default () => console.log('Here is some Svg: ! ', next);

And I have a rollup config that looks like this:

import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
  entry: 'src/app.js',
  dest: 'build/app.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    babel({
      exclude: 'node_modules/**',
    }),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
  ],
};

And then I get the following error:

Could not resolve '../assets/next.svg' from /home/magnferm/projects/slask/rollup-test/src/chill/index.js

There is nothing wrong with the path, but rollup doesn't seem to know how to handle svg-files. Is there a plugin I can use for that or do I have to treat it differently somehow?

Share Improve this question asked Feb 10, 2017 at 17:05 MaffeluMaffelu 2,0685 gold badges28 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 16

There is a plugin for it. It's called @rollup/plugin-image and it handles, among other formats, .svg imports:

import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import image from '@rollup/plugin-image';

export default {
  entry: 'src/app.js',
  dest: 'build/appn.min.js',
  format: 'iife',
  sourceMap: 'inline',
  plugins: [
    babel({
      exclude: 'node_modules/**',
    }),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    commonjs(),
    image()
  ],
};
发布评论

评论列表(0)

  1. 暂无评论