javascript - JS包中包含图片

标签 javascript reactjs npm bundle rollup

我的情况如下:

我为 React 创建了一个组件库。所以我有一个包(与 Rollup bundle 在一起),其中包含一些图片(目前只有在组件中使用的 GIF 图片)。

使用我的图片的组件是这样的:

import React from 'react';
import PropTypes from 'prop-types';
import ui_spinner from '../../../assets/ui_progress.gif';

/**
 * CircularSpinner
 * Add a spinner when the user needs to wait
 */
class CircularSpinner extends React.PureComponent {
  static propTypes = {
    /** Width of the component */
    width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    /** Height of the component */
    height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
    /** Style of the component (overload existing properties) */
    style: PropTypes.object,
  }

  static defaultProps = {
    width: 128,
    height: 128,
    style: {},
  }

  render() {
    const { style, width, height } = this.props;

    return (
      <img src={ui_spinner} width={width} height={height} style={style} alt="ui_progress" aria-busy="true" />
    );
  }
}

export default CircularSpinner;

当我构建它时,一切正常。

现在我使用 create-react-app 创建一个 React 应用程序,我想测试我的组件库。为此,我使用 npm link (以避免推送部署我的 npm 包)。我的组件在我的测试应用程序中正常,但图片(我的 CircularSpinner 组件中的 GIF)未显示。

所以我的问题如下:如何使用 Rollup 将一些资源包含在 JS 包中?我的工作方法正确吗?

我的汇总配置如下:

import { uglify } from 'rollup-plugin-uglify'
import babel from 'rollup-plugin-babel'
import url from 'rollup-plugin-url'

const config = {
  input: 'src/index.js',
  external: ['react'],
  output: {
      format: 'umd',
      name: 'react-components',
      globals: {
          react: "React"
      }
  },
  plugins: [
    babel({
      exclude: "node_modules/**"
    }),
    uglify(),
    url(),
  ]
}
export default config

我用 rollup -c -o dist/index.js 构建.

dist 文件夹包含以下内容:

dist/
  assets
    92be5c546b4adf43.gif
  index.js

我的测试应用程序中使用我的图片的组件如下所示:

<img src="92be5c546b4adf43.gif" width="128" height="128" alt="ui_progress" aria-busy="true">

感谢您的帮助!

达米安

最佳答案

我找到了这个问题的解决方案。此回复可能对某人有帮助:

我更新了我的汇总配置以使用rollup-plugin-img。我已经使用过它,但我的配置不正确:

正确的配置如下:

import { uglify } from 'rollup-plugin-uglify'
import babel from 'rollup-plugin-babel'
import image from 'rollup-plugin-img'

const config = {
  input: 'src/index.js',
  external: ['react'],
  output: {
      format: 'umd',
      name: 'react-components',
      globals: {
          react: "React"
      }
  },
  plugins: [
    babel({
      exclude: "node_modules/**"
    }),
    image({
      limit: 100000,
    }),
    uglify(),
  ]
}
export default config

我的错误是我的 GIF 有点大,默认限制大小为 8192 字节。 在这种情况下,我遇到以下错误:

Error: Could not load <path of image> (imported by <path of component that use image>): The "path" argument must be of type string. Received type undefined

当我更新配置以增加限制时,一切正常

关于javascript - JS包中包含图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55186860/

相关文章:

javascript - 使用 javascript 剥离文本

javascript - 我可以检测我的 PWA 是作为应用程序启动还是作为网站访问?

javascript - webpack 无法在部署时解析 Reactjs 应用程序中的文件夹

node.js - 如何使用 node_modules 中本地安装的包中的可执行文件?

node.js - npm 会在本地 package.json 中安装全局查找吗?

javascript - Typescript import index.ts 不是一个模块

javascript - 从脚本触发所有页面生命周期事件

javascript - 功能组件是否在底层变成了类组件?

django - 使用 create-react-app 自定义静态文件路径?

asp.net-mvc - Azure 应用程序见解 - AI(内部): 24 message:"Missing required field specification