我的情况如下:
我为React创建了一个组件库。所以我有一个包(与Rollup捆绑在一起),包括一些图片(现在只有一个组件中使用的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
当我更新我的配置以增加限制时,一切正常