gpt4 book ai didi

javascript - 无法公开组件库以响应 webpack 和 babel

转载 作者:行者123 更新时间:2023-12-05 07:48:49 26 4
gpt4 key购买 nike

我正在为 React 创建一个小型组件库。可能需要的东西,例如 var Components = require('components')。这将有单独的组件,就像 react-bootstrap 一样。我正在尝试将 webpack 与 babel 结合使用,将其编译成 index.js 文件。编译很顺利。我将其发布到我的本地 npm 注册表并将其安装在我的其他项目之一中。当我需要它时 - require('components') - require 返回一个空对象。下面是我的文件夹结构

root
|- components
| |- ImageEditor.js
|
|- lib
| |- index.compiled.js (file compiled by webpack)
|
|- index.js (requires ./components/ImageEditor.js, entry point for webpack)
|- webpack.config.js

ImageEditor.js

import React from 'react';
import ReactDOM from 'react-dom';
import Canvas from './utils/canvas';
import '../stylesheets/imageeditor.scss';

class ImageManipulation extends React.Component {
static propTypes = {};
state = {
height: 200,
width: 200,
scale: 1.25
};

static defaultProps = {
color: [213, 214, 217, 0.6],
image: ""
};

...

render() {
return (
<div className="_react-image-manipulation">
<div className="_cover-box">
{ this.loadComponent() }
</div>
</div>
);
}
}

export default ImageManipulation;

index.js

import ImageEditor from './components/ImageEditor';

export default {
ImageEditor
};

webpack.config.js

var path = require('path');
var NODE_ENV = process.env.NODE_ENV || 'development';
var WebpackNotifierPlugin = require('webpack-notifier');
var UglifyJSPlugin = require("webpack/lib/optimize/UglifyJsPlugin");
var CleanWebpackPlugin = require("clean-webpack-plugin");

var commonPlugins = [
new WebpackNotifierPlugin({
title: 'Contour Components'
})
];

function getPlugins() {
if (NODE_ENV == 'prod') {
commonPlugins.push(new CleanWebpackPlugin(['lib/*']));
commonPlugins.push(new UglifyJSPlugin({
compress: {
warnings: false
}
}));
}
return commonPlugins;
}

module.exports = {
devtool: 'sourcemap',
entry: {
index: './index.js'
},
output: {
path: path.join(__dirname, 'public'),
filename: '[name].compiled.js'
},
plugins: getPlugins(),
module: {
loaders: [{
test: /\.js$/,
loader: 'babel',
query: {
cacheDirectory: true,
presets: ['es2015', 'stage-0', 'react'],
plugins: ['add-module-exports', "transform-class-properties"]
},
exclude: /node_modules/
}, {
test: /\.json$/,
loader: 'json-loader'
}, {
test: /\.png$/,
loader: "url-loader?limit=100000&mimetype=image/png"
}, {
test: /(\.scss|\.css)$/,
include: /components/,
loader: 'style!css!sass'
}]
},
resolve: {
extensions: ['', '.scss', '.js', '.json', '.png'],
modulesDirectories: [
'node_modules',
'components'
]
}
};

知道我在这里做错了什么吗?

最佳答案

您正在默认导出中导出对象中的组件。Babel 6 为您生成以下 CommonJS 模块。 See REPL :

exports.default = {
ImageEditor: ImageEditor
};

然后你可以像这样使用这个组件:

var ImageEditor = require('my-lib').default.ImageEditor

您的组件隐藏在默认键下。如果您不想要它,请改用命名导出。

export {ImageEditor};

为此,Babel 生成以下代码

exports.ImageEditor = ImageEditor;

看,没有额外的 default 键,一切都按预期工作

关于javascript - 无法公开组件库以响应 webpack 和 babel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38121955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com