I'm having an issue importing a component from one React project into another. The problems seems very basic but I'm having a really hard time figuring out where the problem is and what is the exact craco configuration to achieve my goal.
Exporting project
I'm exporting App
, which is a functional React component.
src/index.js
import App from "./App";
export default App;
I'm using craco mainly because of Tailwindcss and antd, this is the configuration file:
craco.config.js
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const WebpackBar = require("webpackbar");
const CracoAntDesignPlugin = require("craco-antd");
const path = require("path");
module.exports = {
style: {
postcss: {
plugins: [require("tailwindcss"), require("autoprefixer")],
},
},
webpack: {
plugins: [
new WebpackBar({ profile: true }),
...(process.env.NODE_ENV === "development"
? [new BundleAnalyzerPlugin({ openAnalyzer: false })]
: []),
],
configure: (webpackConfig, { env, paths }) => {
paths.appBuild = webpackConfig.output.path = path.resolve("dist");
webpackConfig.output = {
...webpackConfig.output,
filename: "index.bundle.js",
path: path.resolve(__dirname, "dist"),
library: "library",
libraryTarget: "umd",
};
webpackConfig.entry = path.join(__dirname, "./src/index.js");
return webpackConfig;
},
},
plugins: [
{
plugin: CracoAntDesignPlugin,
options: {
customizeThemeLessPath: path.join(
__dirname,
"./src/styles/variables.less"
),
lessLoaderOptions: {
lessOptions: {
javascriptEnabled: true,
},
},
},
},
],
};
I'm using npm to publish my package and import it in the other project. This is the package.json start configuration (infos removed for privacy):
package.json
{
"name": [company-repo-name],
"version": "1.1.1-alpha16",
"homepage": ".",
"repository": [company-repo],
"main": "dist/index.bundle.js",
"publishConfig": {
"registry": [company-registry]
},
...
npm run build
works as intended and generate a index.bundle.js
inside the dist folder. I'm not sure if the problem lies here, but the file is full of minified functions and doesn't seem to export anything.
Consumer project
Installing the exporting project via npm works fine, and trying to import the App
component gives me no result:
import App from [company-repo-name];
gives me{}
(empty object)import { App } from [company-repo-name];
gives meundefined
I currently don't know where the problem lies and I'm looking forward for suggestions to try out.
import * as Something from [company-repo-name];
give you? – RomanticizelibraryTarget
ascommonjs2
iswebpackConfig.output
would fix the issue. Just trynpm link
to test out the pkg locally without having to deploy. – Outplay