Rollup including my react library into bundle even though I have externals written - reactjs

I am trying to build a component library for myself with a few overrides. But it's constantly giving me an insane challenge. I can't import it into my projects. Rollup for some reason is packing the whole react lib into my bundle.js file.
This rollup config
/* eslint-disable */
import { readdirSync } from 'fs';
import path from 'path';
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import external from 'rollup-plugin-peer-deps-external';
import replace from 'rollup-plugin-replace';
import resolve from 'rollup-plugin-node-resolve';
import postcss from 'rollup-plugin-postcss';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';
const EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.json'];
const CODES = [
'THIS_IS_UNDEFINED',
'MISSING_GLOBAL_NAME',
'CIRCULAR_DEPENDENCY'
];
const discardWarning = (warning) => {
if (CODES.includes(warning.code)) {
return;
}
console.error(warning);
};
const env = process.env.NODE_ENV;
const commonPlugins = () => [
postcss({
extract: true,
modules: true,
use: ['sass']
}),
external(),
babel({
babelrc: false,
presets: [['#babel/preset-env', { modules: false }], '#babel/preset-react'],
extensions: EXTENSIONS,
exclude: [
'node_modules/**',
'src/*/*.stories.js',
'src/*/*.spec.js',
'src/*/*.test.js'
]
}),
commonjs({
include: /node_modules/
}),
replace({ 'process.env.NODE_ENV': JSON.stringify(env) }),
resolve()
];
export default [
{
onwarn: discardWarning,
input: 'src/index.js',
output: {
esModule: false,
file: pkg.unpkg,
format: 'cjs',
name: 'ph-shared',
exports: 'named'
globals: {
antd: 'antd',
react: 'React',
'react-dom': 'ReactDOM'
}
},
external: ['react', '#ant-design', 'react-notification-system', 'antd'],
plugins: [...commonPlugins(), env === 'production' && terser()],
output: [{ dir: 'dist', format: 'cjs', exports: 'named', sourcemap: true }]
}
];
and this is package.json file
{
"name": "ph-shared",
"version": "0.0.1",
"author": "usmantahirr",
"description": "Component library for PH",
"main": "src/index.js",
"source": "src/index.js",
"module": "dist/bundle.js",
"engines": {
"node": ">=12"
},
"scripts": {
"build": "rollup -c",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint ./src/",
"lint:fix": "eslint ./src/ --fix",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,jsx,css}": [
"prettier --write",
"npm run lint:fix",
"git add"
]
},
"peerDependencies": {
"#ant-design/icons": "^4.2.2",
"react-notification-system": "^0.4.0",
"antd": "^4.6.4",
"react": "^16.13.1",
"node-sass": "^4.14.1"
},
"devDependencies": {
"#ant-design/icons": "^4.2.2",
"#babel/core": "^7.11.6",
"#babel/preset-env": "^7.11.5",
"#babel/preset-react": "^7.10.4",
"#storybook/addon-actions": "^6.0.21",
"#storybook/addon-essentials": "^6.0.21",
"#storybook/addon-knobs": "^6.0.21",
"#storybook/addon-links": "^6.0.21",
"#storybook/preset-create-react-app": "^3.1.4",
"#storybook/react": "^6.0.21",
"antd": "^4.6.4",
"babel": "^6.23.0",
"babel-loader": "^8.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.7.0",
"eslint-config-standard": "^14.1.0",
"eslint-config-standard-react": "^9.2.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-standard": "^4.0.1",
"husky": "^1.3.1",
"lint-staged": "^8.1.5",
"node-sass": "^4.14.1",
"prettier": "^2.0.4",
"react-is": "^16.13.1",
"react-notification-system": "^0.4.0",
"react-scripts": "^3.4.3",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-peer-deps-external": "^2.2.3",
"rollup-plugin-postcss": "^3.1.8",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"sass-resources-loader": "^2.1.0",
"styled-components": "^5.2.0"
},
"dependencies": {}
}
I also want to override some antD styles using SCSS. and it's converting all the classes with funny names even though they are not scss modules. It's extracting out a single file with all of my classes renamed. I was expecting only scss modules classes to be renamed.

Related

React does not have hot/live reload, how to set it up?

I recently joined a company that works with react (classes) and typescript, I was flabbergasted when I knew that the react they are using does not hot/live reload on changes (CSS, state, or any change), what happens on changes is webpack recompiles (takes around 25 seconds) and to see changes I should reload the page which is slowing down the development experience greatly.
Talking to the manager it was decided that we do a migration to a new create react app gradually as there is lots of code and redundancy and we want to use functional react hooks (which will take years maybe as the app is big) but until then I want to resolve the hot reload issue.
I believe the react app a long time ago was not started with creat-react-app or something.
Just to mention I have 1.5 yrs experience in the industry so config stuff is still new to me and am just doing this for my learning benefits and to improve the team experience, it's not really an assigned task to do.
Below are our package.json, webpack file, and index file. Please let me know if you need to see anything else.
Package.json:
{
"name": "web-front-end",
"version": "1.22.19",
"description": "HTML5 implementation of the <our company name>. Written in Typescript, compiled to JavaScript, bundled with Webpack, executing on NodeJS, using React, adopting FLUX patterns.",
"private": true,
"scripts": {
"babel": "babel built/client/js/client.js -o built/client/js/client.js",
"clean": "rm -rf node_modules && rm -rf built && rm -rf coverage && echo 'clean complete'",
"reset": "yarn clean && yarn cache clean && yarn install --frozen-lockfile",
"compile": "yarn compile-client && yarn compile-server && yarn babel",
"debug-client": "NODE_ENV=debug webpack --config webpack.dev.js && echo 'client compile complete'",
"compile-client": "NODE_ENV=production webpack --config webpack.prod.js && echo 'production client compile complete'",
"compile-client-dev": "NODE_ENV=development webpack --config webpack.dev.js && echo 'development client compile complete'",
"compile-client-dev-watch": "NODE_ENV=development webpack --config webpack.dev-watch.js",
"compile-server": "tsc -p src/server && echo 'server compile complete'",
"compile-client-watch": "NODE_ENV=development webpack --config webpack.dev-watch.js",
"compile-server-watch": "tsc -p src/server --watch",
"validate": "yarn --ignore-engines && tslint './src/**/*.{ts,tsx}' './test/**/*.{ts,tsx}' && echo 'validate complete'",
"start-server": "node ./built/server/ServerApp.js",
"test": "tslint --project test && tslint --project src/app && tsc -p test --jsx react && node ./node_modules/jest/bin/jest.js --config=jest.test.config.json --coverage --verbose",
"lint-fix": "tslint --fix --project src && tslint --fix --project test",
"prepare": "patch-package"
},
"dependencies": {
"#emotion/react": "^11.9.3",
"#emotion/styled": "^11.9.3",
"#mui/icons-material": "^5.8.4",
"#mui/material": "^5.8.4",
"#mui/styles": "^5.8.4",
"#stripe/stripe-js": "1.11.0",
"assert": "^2.0.0",
"axios": "^0.27.2",
"blob-stream": "0.1.3",
"buffer": "^6.0.3",
"compression": "1.7.3",
"compression-webpack-plugin": "^10.0.0",
"connected-react-router": "^6.9.2",
"cors": "2.8.4",
"device-uuid": "1.0.4",
"dotenv": "6.0.0",
"express": "4.16.3",
"file-saver": "^2.0.5",
"html-to-image": "^1.10.8",
"https-browserify": "^1.0.0",
"js-cookie": "2.2.0",
"node-zip": "1.1.1",
"path-to-regexp": "2.4.0",
"pug": "2.0.3",
"query-string": "6.1.0",
"react": "17.0.0",
"react-copy-to-clipboard": "5.0.1",
"react-dom": "^17.0.2",
"react-form-validator-core": "0.4.4",
"react-hook-form": "^7.36.0",
"react-inlinesvg": "2.3.0",
"react-jss": "8.5.1",
"react-lazy-load": "3.0.13",
"react-markdown": "4.0.4",
"react-material-ui-form-validator": "^3.0.1",
"react-player": "1.14.2",
"react-redux": "^7.2.6",
"react-router": "5.2.1",
"react-router-dom": "5.3.0",
"react-share": "2.4.0",
"react-speech-recognition": "^3.10.0",
"react-stripe-elements": "2.0.1",
"react-svgmt": "^1.2.0",
"react-virtualized": "9.22.3",
"react-youtube": "7.9.0",
"recompose": "0.27.1",
"rect": "1.2.1",
"redux": "4.0.0",
"redux-thunk": "2.3.0",
"regenerator-runtime": "^0.13.9",
"shortid": "2.2.15",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"svg-to-pdfkit": "0.1.7",
"url": "^0.11.0",
"utf8": "3.0.0"
},
"devDependencies": {
"#babel/cli": "^7.17.6",
"#babel/core": "^7.17.8",
"#babel/types": "^7.17.0",
"#types/blob-stream": "0.1.29",
"#types/code": "4.0.4",
"#types/detect-browser": "3.0.0",
"#types/jest": "25.2.3",
"#types/js-cookie": "2.2.1",
"#types/lab": "11.1.0",
"#types/react": "17.0.14",
"#types/react-dom": "17.0.14",
"#types/react-inlinesvg": "1.0.0",
"#types/react-redux": "^7.1.20",
"#types/react-router": "4.4.5",
"#types/react-virtualized": "9.21.18",
"#types/shortid": "0.0.29",
"#types/utf8": "2.1.6",
"awesome-typescript-loader": "3.4.1",
"copy-webpack-plugin": "4.5.1",
"detect-browser": "3.0.1",
"fork-ts-checker-webpack-plugin": "^7.2.13",
"jest": "^27.5.1",
"patch-package": "6.1.2",
"postinstall-postinstall": "2.0.0",
"process": "^0.11.10",
"source-map-loader": "0.2.3",
"source-map-support": "0.4.18",
"ts-jest": "27.1.2",
"ts-loader": "^9.3.1",
"ts-mockito": "2.3.0",
"ts-node": "^10.8.1",
"tslint": "^6.0.1",
"tslint-eslint-rules": "5.4.0",
"tslint-react": "^5.0.0",
"typescript": "4.1.5",
"webpack": "^5.73.0",
"webpack-bundle-analyzer": "^3.0.3",
"webpack-cli": "^4.10.0",
"webpack-merge": "^5.8.0"
},
"resolutions": {
"#types/prop-types": "15.7.5",
"#types/unist": "2.0.0",
"#types/react-transition-group": "2.0.8",
"#types/react": "17.0.14",
"#types/react-dom": "17.0.14"
},
"repository": {
"type": "git",
"url": "our repo link"
},
"author": "our company name",
"license": "MIT",
}
Webpack.common.js:
const path = require("path");
const webpack = require('webpack');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
module.exports = {
entry: "./src/app/Main.ts",
cache: false,
optimization: {
providedExports: true,
usedExports: true,
removeAvailableModules: true,
removeEmptyChunks: true,
mergeDuplicateChunks: true,
chunkIds: 'named',
},
plugins: [
new webpack.ProvidePlugin({process: "process/browser"}),
new ForkTsCheckerWebpackPlugin(
{
typescript: {
configFile: './src/app/tsconfig.json'
}
}),
],
module: {
rules: [
{
test: /\.tsx?$/i,
loader: "ts-loader",
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: "asset/resource",
generator: {
filename: 'assets/[hash][ext]',
},
},
],
},
resolve: {
alias: {
core: path.join(__dirname, "src", "core"),
client: path.join(__dirname, "src", "client"),
server: path.join(__dirname, "src", "server"),
'<our company name>-typescript-common': path.resolve(__dirname, 'node_modules/<our company name>-typescript-common/'),
},
extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".jsx"],
fallback: {
"util": require.resolve('util'),
"os": false,
"fs": false,
"path": false,
"zlib": false,
"buffer": require.resolve('buffer'),
"http": require.resolve('stream-http'),
"https": require.resolve('https-browserify'),
"url": require.resolve('url'),
"stream": require.resolve('stream-browserify'),
},
},
output: {
path: path.resolve(__dirname, "built"),
filename: "client/js/client.js",
clean: true,
},
};
index.js (called Main.ts on our codebase)
import { render } from 'react-dom';
import { AppWrapper } from './view/App/AppWrapper';
declare var window: any;
class CompanyName {
constructor() {
window.dataLayer = window.dataLayer || [];
window._hsq = window._hsq || [];
const rootHtmlElement = document.body.appendChild(document.createElement('div'));
render(new AppWrapper({
lang: window.appConfig.LANGUAGE,
appConfig: window.appConfig,
startupTimestamp: window.sts}).render(), rootHtmlElement);
}
}
// tslint:disable-next-line:no-unused-expression
new CompanyName();
webpack.dev-watch.js:
const { merge } = require("webpack-merge");
const common = require("./webpack.dev.js");
const path = require('path');
module.exports = merge(common, {
watch: true,
optimization: {
minimize: false,
mangleExports: false,
mangleWasmImports: false
},
watchOptions: {
ignored: [
path.posix.resolve(__dirname, './node_modules'),
path.posix.resolve(__dirname, './rfg'),
path.posix.resolve(__dirname, './patches'),
path.posix.resolve(__dirname, './built'),
],
poll: 1500,
aggregateTimeout: 1000,
},
});
I would be grateful if you provide any explanations, explanatory links or videos in your answers so I can learn more.
Thanks.

Module build failed (from ./node_modules/ts-loader/index.js): Error: TypeScript emitted no output for

**Error**
**ERROR in ./src/app-chat.tsx
Module build failed (from ./node_modules/ts-loader/index.js):
Error: TypeScript emitted no output for D:\React\kasper\new-kasper\kasper-ui-chat\chat\src\app-chat.tsx.
at makeSourceMapAndFinish (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:52:18)
at successLoader (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:39:5)
at Object.loader (D:\React\kasper\new-kasper\kasper-ui-chat\chat\node_modules\ts-loader\dist\index.js:22:5)
webpack 5.74.0 compiled with 1 error in 7299 ms**
Webpack
const { merge } = require('webpack-merge');
const Dotenv = require('dotenv-webpack');
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
const path = require('path');
const singleSpaDefaults = require('webpack-config-single-spa-react');
const override = require('./config-overrides.js');
module.exports = (webpackConfigEnv, argv) => {
const defaultConfig = singleSpaDefaults({
orgName: 'app',
projectName: 'chat',
webpackConfigEnv,
argv,
override,
});
return merge(defaultConfig, {
resolve: {
extensions: ['.ts', '.tsx', '.js'],
preferRelative: true,
alias: {
containers: path.resolve(__dirname, 'src/containers'),
components: path.resolve(__dirname, 'src/components'),
layouts: path.resolve(__dirname, 'src/layouts'),
stores: path.resolve(__dirname, 'src/stores'),
hooks: path.join(__dirname, 'src/hooks'),
context: path.join(__dirname, 'src/context'),
assets: path.join(__dirname, 'src/assets'),
helpers: path.join(__dirname, 'src/helpers'),
},
// plugins: [new TsconfigPathsPlugin()],
},
devServer: {
port: 8500,
},
plugins: [
new Dotenv({
path: './.env.development',
}),
new FilterWarningsPlugin({
exclude:
/There are multiple modules with names that only differ in casing/,
}),
],
output: {
path: path.resolve(__dirname, 'build'),
assetModuleFilename: 'assets/[name][ext]',
},
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules/,
use: 'ts-loader',
},
],
},
});
};
tsconfig.json
{
"extends": "ts-config-single-spa",
"compilerOptions": {
"jsx": "react-jsx",
"declarationDir": "build",
"baseUrl": "./src",
},
"files": ["src/app-chat.tsx"],
"include": ["src/**/*"],
"exclude": ["src/**/*.test*"],
}
.babel
{
"presets": [
"#babel/preset-env",
"#babel/preset-react"
],
"plugins": [["#babel/plugin-proposal-decorators", { "legacy": true }], "inline-react-svg" ]
}
package.json
{
"name": "#app/login",
"scripts": {
"start": "webpack serve",
"start:standalone": "webpack serve --env standalone",
"build": "concurrently yarn:build:*",
"build:webpack": "webpack --mode=production",
"analyze": "webpack --mode=production --env analyze",
"lint": "eslint src --ext js,ts,tsx",
"lint:fix": "eslint src --ext js,ts,tsx --fix",
"format": "prettier --write .",
"check-format": "prettier --check .",
"test": "cross-env BABEL_ENV=test jest",
"watch-tests": "cross-env BABEL_ENV=test jest --watch",
"prepare": "cd .. && husky install",
"coverage": "cross-env BABEL_ENV=test jest --coverage",
"build:types": "tsc"
},
"devDependencies": {
"#babel/core": "^7.15.0",
"#babel/eslint-parser": "^7.15.0",
"#babel/plugin-transform-runtime": "^7.15.0",
"#babel/preset-env": "^7.15.0",
"#babel/preset-react": "^7.14.5",
"#babel/preset-typescript": "^7.15.0",
"#babel/runtime": "^7.15.3",
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^12.0.0",
"#types/js-cookie": "^3.0.2",
"#types/testing-library__jest-dom": "^5.14.1",
"babel-jest": "^27.0.6",
"babel-loader": "^8.2.5",
"concurrently": "^6.2.1",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-ts-react-important-stuff": "^3.0.0",
"eslint-plugin-prettier": "^3.4.1",
"husky": "^7.0.2",
"identity-obj-proxy": "^3.0.0",
"jest": "^27.0.6",
"jest-cli": "^27.0.6",
"json-loader": "^0.5.7",
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1",
"ts-config-single-spa": "^3.0.0",
"ts-loader": "^9.4.1",
"tsconfig-paths-webpack-plugin": "^4.0.0",
"typescript": "^4.3.5",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0",
"webpack-config-single-spa-react": "^4.0.0",
"webpack-config-single-spa-react-ts": "^4.0.0",
"webpack-config-single-spa-ts": "^4.0.0",
"webpack-dev-server": "^4.0.0",
"webpack-merge": "^5.8.0"
},
"dependencies": {
"#date-io/moment": "^2.13.1",
"#emotion/react": "^11.10.4",
"#emotion/styled": "^11.10.4",
"#material-ui/core": "^4.12.4",
"#material-ui/icons": "^4.11.3",
"#material-ui/pickers": "^3.3.10",
"#mui/material": "^5.10.8",
"#sentry/react": "^6.19.7",
"#sentry/tracing": "^6.19.7",
"#types/jest": "^27.0.1",
"#types/react": "^17.0.19",
"#types/react-dom": "^17.0.9",
"#types/systemjs": "^6.1.1",
"#types/webpack-env": "^1.16.2",
"awesome-phonenumber": "^2.72.0",
"aws-amplify": "^3.3.2",
"aws-amplify-react": "^5.1.9",
"bootstrap": "^5.1.3",
"dotenv-webpack": "^8.0.1",
"eslint-plugin-jest": "^27.1.0",
"eslint-plugin-jest-dom": "^4.0.2",
"eslint-plugin-react": "^7.31.8",
"eslint-plugin-storybook": "^0.6.4",
"eslint-plugin-testing-library": "^5.7.2",
"firebase": "^8.2.2",
"formik": "^2.2.9",
"formik-material-ui": "^3.0.1",
"formik-material-ui-lab": "0.0.8",
"formik-material-ui-pickers": "0.0.12",
"js-cookie": "^3.0.1",
"logrocket": "^3.0.0",
"logrocket-react": "^5.0.1",
"mobx": "^5.15.6",
"mobx-devtools-mst": "^0.9.22",
"mobx-react": "^6.3.1",
"mobx-react-devtools": "^6.1.1",
"mobx-react-router": "^4.1.0",
"mobx-state-tree": "^3.17.2",
"mobx-utils": "^5.6.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-helmet": "^6.1.0",
"react-password-strength-bar": "^0.4.0",
"react-query": "^3.34.16",
"react-router-dom": "^5.2.0",
"single-spa": "^5.9.3",
"single-spa-react": "^4.3.1",
"yup": "^0.32.11",
"yup-phone": "1.3.1"
},
"types": "dist/app-login.d.ts",
"resolutions": {
"#types/react": "^17.0.2"
}
}
In webpack add :
module: {
rules: [
{
test: /\.json$/,
use: 'json-loader',
},
{
test: /\.(js)x?$/,
exclude: /node_modules/,
use: 'babel-loader',
},
{
test: /\.(ts)x?$/,
exclude: /node_modules|\.d\.ts$/, // this line as well
use: {
loader: 'ts-loader',
options: {
compilerOptions: {
noEmit: false, // this option will solve the issue
},
},
},
},
],
},
Usually webpack hangs when declaration is true without creating d.ts files and that causes errors. By default is true so all you need it to set it to false.

Invalid hook call on npm link a library to application

Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the render er (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app.
I am developing own library of components and using same library in my application through npm link but getting above error while testing.
here is my webpack.config
const path = require('path');
module.exports = {
entry: './src/index.ts',
devtool: 'eval-source-map',
module: {
rules: [
{
// "oneOf" will traverse all following loaders until one will
// match the requirements. When no loader matches it will fall
// back to the "file" loader at the end of the loader list.
oneOf: [
{
test: /\.(tsx|ts|mjs)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
plugins: [['#babel/plugin-proposal-class-properties'], ['#babel/plugin-transform-runtime', { "regenerator": true }]],
presets: ['#babel/preset-env', '#babel/preset-react', '#babel/preset-typescript'],
},
},
},
{
test: /\.mjs$/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
},
},
},
{
test: /\.(css|scss)$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }, { loader: 'sass-loader' }],
},
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
exclude: [/\.js$/, /\.ts$/, /\.html$/, /\.json$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
],
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.tsx', '.json'],
},
output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '',
filename: 'bundle.js',
libraryTarget: 'commonjs'
}
};
here is my package.json
{
"name": "abc",
"version": "1.0.42",
"description": "A React component library",
"main": "dist/bundle.js",
"types": "dist/types/index.d.ts",
"scripts": {
"test": "jest",
"build:development": "npm-run-all --parallel webpack:watch generate-typings",
"build:production": "npm-run-all webpack:prod generate-typings",
"build:clean": "rmdir /Q /S dist",
"generate-typings": "tsc --declaration --emitDeclarationOnly",
"styleguide": "npx styleguidist server",
"styleguide:build": "npx styleguidist build",
"webpack:watch": "webpack --watch --mode=development",
"webpack:prod": "webpack --mode=production",
"localVersion": "node -p \"require('./package.json').version\"> localVersion.txt",
"serverVersion": "npm view #company/abc version > serverVersion.txt"
},
"repository": {
"type": "git",
"url": "url"
},
"keywords": [
"react",
"components",
"typescript"
],
"author": "Spencer Cousino",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.11.1",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/preset-env": "^7.11.0",
"#babel/preset-react": "^7.10.4",
"#babel/preset-typescript": "^7.10.4",
"#fortawesome/fontawesome-svg-core": "^1.2.28",
"#fortawesome/free-solid-svg-icons": "^5.13.0",
"#types/enzyme": "^3.10.5",
"#types/fetch-mock": "^7.3.2",
"#types/jest": "^24.9.1",
"#types/nock": "^10.0.3",
"#types/node": "^11.15.9",
"#types/react": "^16.9.0",
"#types/react-dom": "^16.9.0",
"#types/react-table": "^6.8.7",
"async-wait-until": "^1.2.4",
"babel-loader": "^8.1.0",
"css-loader": "^1.0.1",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"fetch-mock": "^7.7.3",
"file-loader": "^3.0.1",
"identity-obj-proxy": "^3.0.0",
"jest": "^25.3.0",
"nock": "^10.0.6",
"node-fetch": "^2.6.1",
"node-sass": "^4.14.1",
"npm-run-all": "^4.1.5",
"oidc-client": "^1.10.1",
"powerbi-report-component": "^1.6.0",
"prettier": "1.17.0",
"react": "^16.14.0",
"react-docgen-typescript": "^1.20.1",
"react-dom": "^16.14.0",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-table": "6.10.3",
"react-slick": "^0.23.2",
"react-spinners": "^0.9.0",
"react-styleguidist": "^11.1.5",
"reactstrap": "^8.4.1",
"sass-loader": "^7.3.1",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3",
"url-loader": "^1.1.2",
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
},
"dependencies": {
"#babel/plugin-transform-runtime": "^7.12.1",
"#babel/runtime": "^7.12.5",
"#company/abc": "0.0.6",
"#company/abc1": "^1.2.0",
"#company/abc2": "^1.0.23",
"#company/abc3": "^1.9.1",
"#company/abc4": "^1.0.1",
"core-js": "^3.8.0",
"date-fns": "^1.30.1",
"react-bingmaps": "^3.6.1",
"react-bootstrap": "^0.32.3",
"react-grid-layout": "^0.16.6",
"react-scripts": "^4.0.3",
"react-tabs": "^3.1.1",
"slick-carousel": "^1.8.1"
},
"peerDependencies": {
"react": "~16.8.4",
"react-dom": "~16.8.4",
"#types/node": "^11.11.6",
"node-fetch": "^2.3.0",
"oidc-client": "^1.7.0",
"powerbi-report-component": "^1.1.3",
"#fortawesome/fontawesome-svg-core": "^1.2.17",
"#fortawesome/free-solid-svg-icons": "^5.8.1",
"react-slick": "^0.23.2",
"react-spinners": "^0.5.3",
"reactstrap": "^8.0.0"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"setupFilesAfterEnv": [
"./testSetup.ts"
],
"moduleNameMapper": {
"^.+\\.(css|scss)$": "identity-obj-proxy",
"^.+\\.(svg)$": "<rootDir>/src/mocks/fileMock.js"
},
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx"
],
"collectCoverageFrom": [
"**/context-components/**/*.{js,jsx,ts,tsx}"
]
}
}
I have tried multiple ways but none of them worked for me.
Any help will be appreciated.

NPM package component gives an error if reexported in another library

I have a component library and one of our components uses react-toggle library. When I try to use our component in another app I get the error Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. I am using rollup as a bundler, here's the configuration.
import path from 'path';
import babel from '#rollup/plugin-babel';
import resolve from '#rollup/plugin-node-resolve';
import commonjs from '#rollup/plugin-commonjs';
import image from '#rollup/plugin-image';
import filesize from 'rollup-plugin-filesize';
import localResolve from 'rollup-plugin-local-resolve';
import { terser } from 'rollup-plugin-terser';
import copy from 'rollup-plugin-copy';
import pkg from './package.json';
const SOURCE_DIR = path.resolve(__dirname, 'src');
const INPUT_FILE_PATH = `${SOURCE_DIR}/index.ts`;
const OUTPUT_NAME = 'InPlayerUI';
const extensions = ['.js', '.jsx', '.ts', '.tsx'];
const GLOBALS = {
react: 'React',
'react-dom': 'ReactDOM',
'styled-components': 'styled-components',
};
const PLUGINS = [
image(),
resolve({
mainFields: ['module', 'main', 'jsnext:main', 'browser'],
extensions,
}),
commonjs({ include: ['node_modules/**'] }),
babel({
include: ['src/**/*'],
exclude: 'node_modules/**',
babelHelpers: 'bundled',
extensions,
}),
localResolve(),
terser(),
filesize(),
copy({
targets: [{ src: './index.d.ts', dest: 'dist' }],
}),
];
const EXTERNAL = ['react', 'react-dom', 'styled-components'];
const OUTPUT_DATA = [
{
file: pkg.browser,
format: 'umd',
},
{
file: pkg.main,
format: 'cjs',
},
{
file: pkg.module,
format: 'es',
},
];
const config = OUTPUT_DATA.map(({ file, format }) => ({
input: INPUT_FILE_PATH,
output: {
file,
format,
name: OUTPUT_NAME,
globals: GLOBALS,
},
external: EXTERNAL,
plugins: PLUGINS,
}));
export default config;
and the package.json
{
"name": "#inplayer-org/inplayer-ui",
"sideEffects": false,
"version": "2.0.4",
"author": "InPlayer",
"description": "InPlayer React UI Components",
"main": "dist/inplayer-ui.cjs.js",
"module": "dist/inplayer-ui.esm.js",
"browser": "dist/inplayer-ui.umd.js",
"jsnext:main": "dist/inplayer-ui.esm.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/inplayer-org/inplayer-ui"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/inplayer-org/inplayer-ui/issues"
},
"homepage": "https://github.com/inplayer-org/inplayer-ui#readme",
"scripts": {
"start": "yarn run storybook",
"storybook": "start-storybook -p 3001",
"build:storybook": "build-storybook -c .storybook -o docs",
"clean:dist": "rimraf ./dist",
"build": "yarn run clean:dist && npx rollup -c",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix",
"lint:watch": "esw -w src/**",
"prepublishOnly": "yarn build",
"lint:style": "stylelint -- \"./src/**/*.js\""
},
"dependencies": {
"react-toggle": "^4.1.1"
},
"peerDependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"styled-components": "^5.1.1"
},
"devDependencies": {
"#babel/cli": "^7.10.5",
"#babel/core": "^7.10.5",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-proposal-private-methods": "^7.10.4",
"#babel/plugin-transform-classes": "^7.10.4",
"#babel/preset-env": "^7.10.4",
"#babel/preset-react": "^7.10.4",
"#babel/preset-typescript": "^7.10.4",
"#rollup/plugin-babel": "^5.1.0",
"#rollup/plugin-commonjs": "^14.0.0",
"#rollup/plugin-image": "^2.0.5",
"#rollup/plugin-node-resolve": "^8.4.0",
"#storybook/addon-docs": "^5.3.19",
"#storybook/addon-info": "^5.3.19",
"#storybook/addon-knobs": "^5.3.19",
"#storybook/react": "^5.3.19",
"#types/dompurify": "^2.0.2",
"#types/lodash": "^4.14.157",
"#types/react": "^16.9.43",
"#types/react-dates": "^17.1.13",
"#types/react-dom": "^16.9.8",
"#types/react-toggle": "^4.0.2",
"#types/styled-components": "^5.1.1",
"#typescript-eslint/eslint-plugin": "3.7.0",
"#typescript-eslint/parser": "3.7.0",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.1.0",
"dompurify": "^2.0.12",
"eslint": "^7.5.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-react-app": "^5.2.1",
"eslint-loader": "^4.0.2",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.8",
"eslint-watch": "^7.0.0",
"html-react-parser": "^0.13.0",
"lodash-es": "^4.17.15",
"moment": "^2.27.0",
"polished": "^3.6.5",
"prettier": "^2.0.5",
"rc-progress": "^3.0.0",
"react": "^16.13.1",
"react-addons-shallow-compare": "^15.6.2",
"react-dates": "^21.8.0",
"react-dom": "^16.13.1",
"react-icons": "^3.10.0",
"react-paginate": "^6.3.2",
"rimraf": "^3.0.2",
"rollup-plugin-copy": "^3.3.0",
"rollup-plugin-filesize": "^9.0.2",
"rollup-plugin-local-resolve": "^1.0.7",
"rollup-plugin-terser": "^6.1.0",
"styled-components": "^5.1.1",
"styled-components-modifiers": "^1.2.5",
"styled-tools": "^1.7.2",
"stylelint": "^13.6.1",
"stylelint-config-standard": "^20.0.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
"typescript": "^3.9.7"
}
}
This is the component
import React, { ChangeEvent } from 'react';
import styled from 'styled-components';
import Toggle from 'react-toggle';
import SwitchWrapper from './SwitchWrapper';
import Label from '../Label';
type SwitchProps = {
/**
* Whether the switch is checked or not
*/
checked: boolean;
/**
* Disable switch
*/
disabled?: boolean;
/**
* Id
*/
id: string;
/**
* Switch label
*/
label: string;
/**
* Function executed when the switch value changes
*/
onChange: (checked: boolean) => void;
};
const MarginLeftLabel = styled(Label)`
margin-right: 0.5rem;
`;
const Switch = ({ id, checked, label, disabled = false, onChange }: SwitchProps) => {
const onToggleChange = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target) {
onChange(e.target.checked);
}
};
return (
<SwitchWrapper>
<MarginLeftLabel disabled={disabled} htmlFor={id}>
{label}
</MarginLeftLabel>
<Toggle
id={id}
disabled={disabled}
checked={checked}
onChange={onToggleChange}
icons={false}
/>
</SwitchWrapper>
);
};
export default Switch;
I have seen some suggestions that the import could be wrong but I've made sure it isn't and if I use react-toggle directly in another app it works.
Note: I've tried using different npm packages and reexport components from them, it works fine, so I suppose it has something to do with react-toggle itself.

Cannot resolve stream dependency of styled components

I'm stuck with adding styled-components library into my reactjs project. I'm getting an error when trying to run my app in browser, not at the building stage. It's quite common:
Here is my webpack config:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
context: path.join(__dirname, 'src'),
entry: {
server: '../server/index.js',
app: './app.jsx',
},
resolve: {
extensions: ['.jsx', '.js'],
},
module: {
rules: [
{
test: /\.(jsx|js)?$/,
use: {
loader: 'babel-loader',
},
},
],
},
target: 'node',
plugins: [
new HtmlWebpackPlugin({
template: './index.html',
filename: './index.html',
excludeChunks: ['server'],
}),
],
};
Here is my babelrc:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [
"#babel/plugin-transform-runtime",
"babel-plugin-styled-components"
]
}
Here is my styled component:
import React from 'react';
import styled from 'styled-components';
export function SearchPage() {
const Container = styled.div`
text-align: center;
`;
return <Container>test</Container>;
}
Here is my package.json:
{
"devDependencies": {
"#babel/core": "^7.3.4",
"#babel/plugin-transform-runtime": "^7.4.0",
"#babel/preset-env": "^7.3.4",
"#babel/preset-react": "^7.0.0",
"babel-eslint": "^10.0.1",
"babel-loader": "^8.0.5",
"babel-plugin-styled-components": "^1.10.0",
"enzyme": "^3.9.0",
"enzyme-adapter-react-16": "^1.11.2",
"eslint": "^5.15.3",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"eslint-plugin-react-hooks": "^1.6.0",
"husky": "^1.3.1",
"jest": "^24.5.0",
"live-server": "^1.2.1",
"nodemon": "^1.18.10",
"npm-run-all": "^4.1.5",
"prettier": "^1.16.4",
"react-redux": "^6.0.1",
"react-router": "^4.4.0",
"redux": "^4.0.1",
"rimraf": "^2.6.3",
"stream": "^0.0.2",
"uglifyjs-webpack-plugin": "^2.1.2",
"webpack": "^4.29.6",
"webpack-cli": "^3.2.3"
},
"version": "1.0.0",
"main": "dist/server.js",
"scripts": {
"clean": "rimraf dist build",
"dev": "npm-run-all -p dev:watch server:run dev:server",
"dev:build": "webpack --config webpack.dev.js",
"dev:watch": "yarn run dev:build --watch",
"dev:server": "live-server dist",
"server:run": "nodemon",
"prod:build": "npm-run-all clean webpack --config webpack.prod.js",
"test": "jest"
},
"dependencies": {
"express": "^4.16.4",
"html-webpack-plugin": "^3.2.0",
"webpack-merge": "^4.2.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"styled-components": "^4.2.0"
}
}
I thought that Webpack should bundle all the dependencies. And I don't understand why stream import is not covered by webpack bundling.
I guess that the answer is pretty simple, but I cannot find it. So any advice will be helpful.
The thing is in target: 'node' into my package.json. It's supposed to use only for server-side build not for client.

Resources