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)
Related
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
I'm trying to integrate the jest to my current react project. It's a typescript project. I followed the ts-jest instructions and configured the jest. If I run simple test cases, It works well. When I add the following line or any import line the terminal gives me an error.
import React from 'react';
When I run the "npm test" command, the terminal gives me the following error. The error source is the sample test file. (test.tsx)
SyntaxError: Cannot use import statement outside a module.
I tried lots of different solutions but none of them worked.
My tsconfig.json file is;
{
"compilerOptions": {
"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,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src",
"global.d.ts",
"test"
],
"exclude": [
"build",
"coverage",
"node_modules",
"public"
]
}
babel.config.js is;
module.exports = {
presets: ['#babel/preset-env']
}
jest.config.js is;
/** #type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
transform: {
'^.+\\.(ts|tsx)?$': 'ts-jest',
"^.+\\.(js|jsx)$": "babel-jest",
},
transformIgnorePatterns: ["/src/(?!serviceWorker)"],
globals: {
'ts-jest': {
diagnostics: false
}
},
moduleNameMapper: {
"\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|css|less)$": "<rootDir>/src/test/resourceFileMock.ts",
}
};
Thank you for any advice.
could you try with these values in your tsconfig.json:
"target": "es5",
"module": "commonjs",
"lib": ["es6"],
"jsx":"preserve",
"strict": true
Remove below values:
"module": "esnext",
I have a create-react-app (CRA) so I don't have access to the webpack.config, I'm using typescript to create path aliases to common core imports for the application. This is all working I can import everything through the path aliases but .svg files.
Question: How can I import .SVG files through my #assets/images/some.svg path alias?
.tsconfig
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "./"
},
"extends": "./paths.json",
"include": [
"src",
"./custom.d.ts"
]
}
path.json
{
"compilerOptions": {
"paths": {
"#pages": ["src/pages"],
"#pages/*": ["src/pages/*"],
"#hooks": ["src/core/hooks"],
"#hooks/*": ["src/core/hooks/*"],
"#components": ["src/core/components"],
"#components/*": ["src/core/components/*"],
"#containers": ["src/containers"],
"#containers/*": ["src/containers/*"],
"#services": ["src/core/services"],
"#services/*": ["src/core/services/*"],
"#configs": ["src/core/configs"],
"#configs/*": ["src/core/configs/*"],
"#assets": ["src/core/assets"],
"#assets/*": ["src/core/assets/*"],
"#models": ["src/core/models"],
"#models/*": ["src/core/models/*"],
"#store": ["src/core/store"],
"#store/*": ["src/core/store/*"],
"#utils": ["src/core/utils"],
"#utils/*": ["src/core/utils/*"],
"#styles": ["src/core/styles"],
"#styles/*": ["src/core/styles/*"]
}
}
}
custom.d.ts
declare module '*.svg' {
const content: string;
export default content;
}
In the file I'm importing the SVG's as follows:
import uploadStep1Logo from '#assets/images/UploadConfirmationDialog_Step1.svg';
import uploadStep2Logo from '#assets/images/UploadConfirmationDialog_Step2.svg';
import uploadStep3Logo from '#assets/images/UploadConfirmationDialog_Step3.svg';
For some reason, if I use the absolute path it works, however, if I use the #assets/... it cannot resolve and errors as below.
Module not found: Can't resolve '#assets/images/UploadConfirmationDialog_Step1.svg'
Check out this so answer: https://stackoverflow.com/a/60598134/11047070.
But in order for it to work your react-scripts version has to be below 4.0 at moment.
You must use craco and craco-alias packages with tsconfig
https://www.npmjs.com/package/#craco/craco
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.
I am trying to use custom paths in my typescript project, the problem is that webpack doesn't recognize my modules giving me the "webpackMissingModule" error.
I have tried different solutions but nothing works for me. Suggestions? Ideas?
Some packages
"react": "^16.8.4",
"react-dom": "^16.8.4",
"typescript": "3.3.1",
tsconfig.json
{
"compilerOptions": {
"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,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve"
},
"include": ["./src"],
"exclude": ["node_modules", "build"],
"extends": "./paths.json"
}
paths.json
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"#src/*": ["*"],
"#components/*": ["components/*"]
}
}
}
.env
NODE_PATH=./
Component
import React from "react";
import Hello from "#components/Hello";
const Home = () => (
<div>
<h1>Home</h1>
<Hello />
</div>
);
export default Home;
Well, I found something that worked for me. It's called
tsconfig-paths-webpack-plugin. All you need to do is to import it in your webpack config, connect with the tsconfig.json, and that's all.
The plugin is added in the resolve.plugins array of the exported config, not plugins in the root.
The only thing that worked for me was changing tsconfig.json as following:
{
"compilerOptions": {
// ...
"baseUrl": "./src",
"paths": {
"#/*": ["src/*"]
}
}
All else failed.