Skip to content

Rollup import of images compiling as a const in build file #190

Description

@AaronMcCloskey
  • Rollup Plugin Name: @rollup/plugin-image
  • Rollup Plugin Version: ^2.0.1
  • Rollup Version: ^1.31.0
  • Operating System (or Browser): iPad Mini 9.3.5 (Safari)
  • Node Version: v10.16.2

How Do We Reproduce?

Import image in react component

import twoMinOrangeSvg from '../../img/supporting/2-min-orange.svg';

Using the following config

import babel from 'rollup-plugin-babel';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import postcss from 'rollup-plugin-postcss';
import filesize from 'rollup-plugin-filesize';
import autoprefixer from 'autoprefixer';
import localResolve from 'rollup-plugin-local-resolve';
import json from 'rollup-plugin-json';
import pkg from './package.json';
import externals from 'rollup-plugin-node-externals';
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
import image from '@rollup/plugin-image';
import { terser } from 'rollup-plugin-terser';

const ENVIRONMENT = process.env.ENVIRONMENT;
const PRODUCTION = ENVIRONMENT === 'production';

const config = {
  input: 'src/index.js',
  watch: {
    chokidar: {
        usePolling: true,
        paths: 'src/**'
    }
  },
  output:
   [
    {
      file: pkg.browser,
      format: 'umd',
      name: 'Example',
    },
    {
      file: pkg.main,
      format: 'cjs',
      name: 'Example',
    },
    {
      file: pkg.module,
      format: 'es',
    },
  ],
  external: [
    'react',
    'react-dom',
    'styled-components',
    'formik',
    'scheduler',
    'date-fns'
  ],
  globals: {
      // Use external version of React
      "react": "React"
  },
  plugins: [
    PRODUCTION &&  globals(),
    PRODUCTION &&  builtins(),
    PRODUCTION &&  externals(),
    babel({ exclude: 'node_modules/**', presets: ['@babel/env', '@babel/preset-react'] }),
    PRODUCTION &&  commonjs({
      namedExports: {
        // left-hand side can be an absolute path, a path
        // relative to the current directory, or the name
        // of a module in node_modules
        'node_modules/formik/node_modules/scheduler/index.js' : ['unstable_runWithPriority'],
      }
    }),
    PRODUCTION && peerDepsExternal(),
    PRODUCTION && postcss({ extract: true, plugins: [autoprefixer] }),
    PRODUCTION && json({  include: 'node_modules/**' }),
    PRODUCTION && localResolve(),
    PRODUCTION && resolve({dedupe: [ 'react', 'react-dom' ]}),
    PRODUCTION && filesize(),
    image(),
    PRODUCTION && terser()
  ]
};

export default config;

Expected Behavior

Image import to compile as a var in build file

Actual Behavior

In the console I get the following error

Unexpected keyword 'const'. Const declarations are not supported in strict mode.

in my build index.js file this compiles to the svg as a const, instead of a var

const img = 'data:image/svg+xml;utf-8,<?xml version="1.0" encoding="utf-8"?><!-- Generator: Adobe Illustrator 24.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  --><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"  viewBox="0 0 40 40" style="enable-background:new 0 0 40 40;" xml:space="preserve">....path code here.....</svg>';

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions