My library react-utils-jave with no side effects and with preserveModules bundle cant be used without importing in a react-dropdown-jave` project bundle.
I want to extract only one function from library and provide to bundle, but no all library.
src/index.ts → dist...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
react-utils-jave (imported by src/Dropdown.tsx)
library to use: https://github.com/javeoff/react-utils
lib where i use: https://github.com/javeoff/react-dropdown
TSConfig.json
{
"compilerOptions": {
"declaration": true,
"noEmit": false,
"outDir": "./dist/",
"rootDir": "src",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "ESNext",
"jsx": "react-jsx",
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noUncheckedIndexedAccess": true,
"incremental": true,
"noImplicitOverride": true,
"isolatedModules": false,
"composite": true,
"sourceMap": false
},
"include": ["src/**/*.ts", "src/**/*.tsx"]
}
rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import swc from 'rollup-plugin-swc';
import { terser } from 'rollup-plugin-terser';
const config = () => {
const plugins = [
typescript({
tsconfig: 'tsconfig.json',
}),
swc({
rollup: {
exclude: /node_modules/,
},
jsc: {
parser: {
syntax: 'typescript',
},
},
}),
];
if (process.env.NODE_ENV === 'production') {
plugins.push(terser());
}
return {
input: 'src/index.ts',
output: {
format: 'esm',
dir: 'dist',
preserveModulesRoot: './src',
preserveModules: true,
},
plugins,
external: ['react/jsx-runtime', 'react'],
};
};
export default config;
I tried replace swc to babel
Replace import * from to import { onClickOutside } from './...'
Add commonjs and node-resolve plugins to library to use
Set sideEffects: false in library to use
Set module param with path to dist/index.js in library to use
Related
I've stumbled upon an issue, of HMR infinitely updating a file when using hot reload mode in vite app. The file seems to be different every time. I've attached a screenshot of the issue below.
Vite-HMR-infinite-updates
Env:
OS - Windows 10 and Macos Monterey 12.5
Node version - v16.14.0
npm verion - 8.3.1
vite version - ^3.2.3
vite.config.ts
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import react from '#vitejs/plugin-react';
import path from 'path';
const getPath = (s: string) => path.resolve(__dirname, s);
const aliases = {
'#actions': getPath('./src/actions'),
'#api': getPath('./src/api'),
'#assets': getPath('./src/assets'),
'#components': getPath('./src/components'),
'#config': getPath('./src/config'),
'#libs': getPath('./src/libs'),
'#pages': getPath('./src/pages'),
'#providers': getPath('./src/providers'),
'#shared': getPath('./src/shared'),
'#slices': getPath('./src/slices'),
'#store': getPath('./src/store'),
'#themes': getPath('./src/themes'),
'#formComponents': getPath('./src/formComponents'),
'#hooks': getPath('./src/hooks'),
'#schemas': getPath('./src/schemas'),
'#translations': getPath('./src/translations'),
};
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths({ root: '.' })],
resolve: {
alias: aliases,
},
});
tsconfig.json
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "src",
"rootDir": ".",
"typeRoots": ["node_modules/yup"],
"paths": {
"#actions/*": ["actions/*"],
"#api/*": ["api/*"],
"#assets/*": ["assets/*"],
"#components/*": ["components/*"],
"#config/*": ["config/*"],
"#libs/*": ["libs/*"],
"#pages/*": ["pages/*"],
"#providers/*": ["providers/*"],
"#shared/*": ["shared/*"],
"#slices/*": ["slices/*"],
"#store/*": ["store/*"],
"#themes/*": ["themes/*"],
"#formComponents/*": ["formComponents/*"],
"#hooks/*": ["hooks/*"],
"#schemas/*": ["schemas/*"],
"#translations/*": ["translations/*"],
"#components/*": ["components/*"]
}
},
"include": ["./src"]
}
reinstalling node_modules/
Roll back to 3.1 version of vite (Version 4 also has this problem)
During a migration from webpack to vite, we were using process.env.NODE_ENV to add specific code.
Now using vite, the bundle includes every thing meant to be in development mode, include process.env.NODE_ENV.
Here is the vite.config.js:
import path from 'path'
import { defineConfig } from 'vite'
/**
* #type {import('vite').UserConfig}
*/
export default defineConfig(({mode}) => {
console.log('vite configuration for mode', mode);
return {
define: {
NODE_ENV: "production",
},
build: {
mode: "development",
lib: {
name: 'MyLib',
fileName: 'mylib.production',
entry: path.join(process.cwd(), "src/index.ts"),
},
rollupOptions: {
external: 'react',
output: [
{
format: 'umd',
globals: { react: 'react' },
entryFileNames: `mylib.production.js`,
},
]
}
}
}
})
and tsconfig.json:
{
"compilerOptions": {
"sourceMap": true,
"strictNullChecks": true,
"module": "esnext",
"jsx": "react-jsx",
"target": "es5",
"allowJs": true,
"moduleResolution": "Node",
"noImplicitAny": false,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"isolatedModules": true,
"declaration": true,
"declarationDir": "dist",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"strict": false,
"noEmit": false
},
"include": [
"src"
]
}
How to eliminate process.env.NODE_ENV and the unused code from the bundle ?
Thanks.
Fixed via the replace plugin:
const replace = require('#rollup/plugin-replace');
// or import replace form '#rollup/plugin-replace';
// in plugins section:
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify('production'),
}),
I'm trying to make React UI component libraries. In my tsconfig, if I set {..., "jsx": "react-jsx"}, I see in the bundle index.esm.js that React code is included.
But if I use the old jsx {"jsx": "react"}, React code is not included in the bundle.
I am using rollup to bundle. This is rollup.config.ts
export default {
input: "./index.ts",
output: [
{
file: pkg.main,
format: "cjs",
},
{
file: pkg.module,
format: "es",
},
],
external: ["react", "react-dom", "#emotion", "#mui/material", "#mui/style"],
plugins: [
resolve(),
external(),
commonjs(),
typescript({
useTsconfigDeclarationDir: true,
}),
terser(),
],
};
This is tsconfig
{
"compilerOptions": {
"declaration": true,
"declarationDir": "lib/types",
"esModuleInterop": true,
"moduleResolution": "Node",
"jsx": "react-jsx",
"resolveJsonModule": true,
"strict": true,
"target": "ESNext"
},
"include": ["./**/*"],
"exclude": ["./**/*.stories.tsx", "./lib", "./node_modules"]
}
How can I use the react-jsx and not include the React code in the bundle. I tried setting sideEffects:false in package.json but it did not work.
I am working on a react project which was built using create-react-app and later we ejected and modified the webpack config a lot. Right now I am having a hard time importing const enums from external libraries. I don't have control over that external package. It has the const enums defined in its d.ts files.
I have tried with babel-plugin-use-const-enum babel plugin and preset. It doesn't help me either.
My webpack config:
.....
.....
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: [paths.appSrc],
loader: require.resolve("babel-loader"),
options: {
customize: require.resolve("babel-preset-react-app/webpack-overrides"),
plugins: [
[require.resolve("babel-plugin-const-enum"), { transform: "constObject" }],
[require.resolve("#babel/plugin-transform-typescript"), { isTSX: true, optimizeConstEnums: true }],
[require.resolve("#babel/plugin-transform-react-jsx")],
[require.resolve("#babel/plugin-proposal-class-properties"), { loose: true }],
[require.resolve("#babel/plugin-proposal-nullish-coalescing-operator")],
[require.resolve("#babel/plugin-proposal-optional-chaining"), { isTSX: true }],
[require.resolve("#babel/plugin-transform-arrow-functions")],
[require.resolve("#babel/plugin-proposal-private-methods"), { loose: true }],
[
require.resolve("babel-plugin-named-asset-import"),
{
loaderMap: {
svg: {
ReactComponent: "#svgr/webpack?-svgo,+titleProp,+ref![path]",
},
},
},
],
],
presets: ["#babel/preset-env"],
},
},
.....
.....
My tsconfig:
{
"compilerOptions": {
"typeRoots": ["./typings", "node_modules/#types"],
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"noEmit": true,
"jsx": "react",
"downlevelIteration": true
},
"exclude": ["dist/**/*"],
}
The problem is that the build is successful but I am facing the runtime issue with all the const enums import like below
Uncaught TypeError: Cannot read properties of undefined (reading '<ConstEnumName>').
Package versions:
"#babel/core": "^7.11.6",
"babel-loader": "^8.1.0",
"typescript": "^3.9.7",
"webpack": "^4.44.2",
According to this thread, where the plugin's creator is involved, babel does not transpile .d.ts files, making it impossible to get enums from there. The only option seems to be migrating your config to use ts-loader and include the .d.ts files that have the enum declarations
tsconfig
{
...
"exclude": ["dist/**/*"],
"include": ["node_modules/[external_library]/**/*.d.ts"],
}
Very new to bundling. My team is used to code completion and being able to jump to the type declaration file of a corresponding component using command + click in their editor (VSCode).
However the bundle generated does not come with this behaviour... I don't know how to go form here, or what I am missing. These feature are very important to my team. Any help is appreciated 🙏
Versions
typescript: 3.7.5
rollup: 1.31.0
rollup-plugin-typescript2: 0.26.0
rollup.config.js
import babel from 'rollup-plugin-babel';
import commonjs from '#rollup/plugin-commonjs';
import resolve from '#rollup/plugin-node-resolve';
import typescript from 'rollup-plugin-typescript2';
import svgr from '#svgr/rollup';
import url from '#rollup/plugin-url';
import json from '#rollup/plugin-json';
import external from 'rollup-plugin-peer-deps-external';
import includePaths from 'rollup-plugin-includepaths';
const pkg = require('./package.json');
export default {
cache: false,
input: 'src/index.ts',
output: { file: pkg.module, format: 'esm' },
plugins: [
external({
preferBuiltins: false,
}),
babel({
exclude: /node_modules/,
plugins: ['external-helpers'],
externalHelpers: true,
}),
json(),
resolve(),
svgr({
ref: true,
svgo: false,
}),
typescript({
clean: true,
typescript: require('typescript'),
}),
url(),
includePaths({
paths: ['src'],
}),
commonjs({
namedExports: {
'prop-types': [
'oneOfType',
'func',
'shape',
'any',
'number',
'object',
'bool',
'string',
],
},
}),
],
};
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "src",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "es6",
"moduleResolution": "node",
"noEmit": true,
"noErrorTruncation": true,
"outDir": "./dist",
"skipLibCheck": true,
"strict": true,
"target": "es5",
"rootDir": "src"
},
"include": ["src"],
"exclude": ["storybook", "dist"]
}
package.json
Relevant bits only 👇
{
"main": "dist/index.js",
"module": "dist/index.js",
"typings": "dist/index.d.ts",
}
Found the issue - it was because I was using absolute paths. Changing import paths to relative imports or using a plugin to convert absolute to relative import paths when building fixes this issue.