I want to create custom component and build with webpack
// packages/button/src/index.js
import React from 'react';
function Button({ href }) {
return <a href={href}>Test</a>;
}
export default Button;
// webpack.config.js
const path = require('path');
const entries = {
button: {
import: '/packages/button/src/index.js',
filename: './button/dist/button.js',
},
};
module.exports = {
entry: { ...entries },
output: {
filename: '[name].js',
path: __dirname + '/packages',
asyncChunks: true,
libraryTarget: 'umd',
globalObject: 'this',
},
mode: 'production',
module: {
rules: [
{
test: /\.less$/,
use: [
{
loader: 'css-loader',
},
{
loader: 'less-loader',
options: {
lessOptions: {
strictMath: true,
},
},
},
],
},
{
test: /\.m?js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react'],
},
},
resolve: {
fullySpecified: false,
},
},
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
],
},
optimization: {
splitChunks: {
chunks: 'async',
minSize: 20000,
minRemainingSize: 0,
minChunks: 1,
maxAsyncRequests: 30,
maxInitialRequests: 30,
enforceSizeThreshold: 50000,
cacheGroups: {
defaultVendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10,
reuseExistingChunk: true,
},
default: {
minChunks: 2,
priority: -20,
reuseExistingChunk: true,
},
},
},
},
resolve: {
alias: {
Styles: path.resolve(__dirname, '/shared/styles'),
},
},
};
it builds component but when I want to import it from node_modules I got error
// App.js
import React from 'react';
import Button from '@custom/button';
const App = () => {
return <Button />
}
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.