I export const FontXLarge = 18; in ThemeFont.ts.
When I want to use it, I used to use import {FontXLarge} from '../theme/ThemeFont';
After I add
{
"name": "theme"
}
I can use import {FontXLarge} from 'theme/ThemeFont'; but I can't link to the folder.
So I think that if I declare module 'ThemeFont' I can link it. And the fact that I did it. When I ctrl + click (or alt + click in VSCode, depending on your setting), it can open the file ThemeFont.ts when I use import {FontXLarge} from 'ThemeFont';
declare module 'ThemeFont' {
export const FontXLarge = 18;
}
When hover, it will show like that
But it shows error when build: Unable to resolve module ThemeFont in node_modules
We can use this library to declare local component https://github.com/tleunen/babel-plugin-module-resolver
My config for it:
create/edit: tsconfig.json
{
"compilerOptions": {
"skipLibCheck": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["es6"],
"moduleResolution": "node",
"noEmit": true,
"strict": true,
"target": "esnext",
"baseUrl": "./app",
"paths": {
"#/*": ["./*"]
},
"outDir": "../dst/",
"typeRoots": ["node_modules/#types"]
},
"lib": ["es2016"],
"exclude": [
"node_modules",
"babel.config.js",
"metro.config.js",
"jest.config.js"
]
}
create/edit jsconfig.json
{
"compilerOptions": {
"paths": {
"#/*": [
"./app/*"
]
}
}
}
create/edit jsconfig.js
System.config({
paths: {
'#/*': './app/*',
},
})
edit babel.config.js
const presets = ['module:metro-react-native-babel-preset'];
const plugins = [
[
'react-native-reanimated/plugin',
// {
// globals: ['__scanQRCodes', '__decode'],
// },
],
[
'module-resolver',
{
root: ['./app'],
extensions: ['.js', '.json'],
alias: {
'#': './app',
},
},
],
];
const env = {
production: {
plugins: ['react-native-paper/babel'],
},
};
module.exports = {
presets,
plugins,
env,
};
Now you can use
import {FontXLarge} from '#/Theme/ThemeFont';
remember that after config it, resetting app cache:
yarn start --reset-cache
Related
Why base url and paths work on normal React but not on React Native??
This is my tsconfig file:
{
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"baseUrl": ".",
"paths": {
"components/*": ["./components/*"],
"assets/*": ["./assets/*"],
"screens/*": ["./screens/*"],
"navigation/*": ["./navigation/*"],
"api/*": ["./api/*"],
"utils/*": ["./utils/*"],
"#shared/*": [
"../shared/*"
]
}
},
"include": [
"./*"
],
"extends": "expo/tsconfig.base"
}
From App.tsx I tried to import AppNav component from "navigation/AppNav" but it gives error that "Unable to resolve module navigation/AppNav"
I even tried to install modeule-resolver plugin for babel but it didnt work:
module.exports = function(api) {
api.cache(false);
return {
presets: ['babel-preset-expo'],
plugins: [
"nativewind/babel",
[
"module-resolver",
{
"root": ["./"],
"alias": {
"components/*": "components/*",
"assets/*": "assets/*",
"screens/*": "screens/*",
"navigation/*": "navigation/*",
"api/*": "api/*",
"utils/*": "utils/*",
}
}
]
],
};
};
I tried with regex expression but still didnt work
module.exports = function(api) {
api.cache(false);
return {
presets: ['babel-preset-expo', "module:metro-react-native-babel-preset"],
plugins: [
"nativewind/babel",
[
"module-resolver",
{
"root": ["."],
"alias": {
"components/(.+)": "./components/\\1",
"assets/(.+)": "./assets/*",
"screens/(.+)": "./screens/*",
"navigation/(.+)": "./navigation/\\1",
"api/(.+)": "./api/*",
"utils/(.+)": "./utils/*",
}
}
]
],
};
};
So I have a custom private npm package which I want to dynamically imports it like so.
export default function SelectedIcon({path, ...props}: Props) {
const {data: icon} = require(`#mypackagelib/icons/${path}`)
return <Icon icon={icon} {...props} />
}
// I also tried to have it differently such as:
export default function SelectedIcon({path, ...props}: Props) {
const Component = React.lazy(() => import(`#mypackagelib/icons/${path}`))
return <Component {...props} />
}
// However, same error, so it seems like dynamically importing this is the problem
However, I got many errors, all the same message for different components in the library, from running Storybook
Module parse failed: Unexpected token (2:101)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| /// <reference types="react" />
| import { PropA, PropB } from '../../File';
> declare function NameOfAComponent(props: PropA): JSX.Element;
| declare namespace NameOfAComponent {
| var data: PropB;
The weird thing that I don't understand is that, if I import the package like the below code, then it works
import OneOfTheComponent from '#mypackagelib/icons/OneOfTheComponent'
However, I have a case that I need to use dynamic import
I have the following config tsconfig.json when I build the #mypackagelib package with tsc and ts-node
{
"compilerOptions": {
"outDir": "dist",
"target": "ES2020",
"module": "commonjs",
"jsx": "react",
"lib": ["es6", "dom", "ES2020"],
"moduleResolution": "node",
"declaration": true,
"strict": true,
"skipLibCheck": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmitHelpers": true,
"importHelpers": true,
"pretty": false,
},
"ts-node": {
"swc": true
},
"exclude": [
"node_moduls_local",
"node_modules",
"dist"
],
"include": [
"types.d.ts",
"src/icons/**/*"
]
}
My main.js under ./storybook folder looks like this:
module.exports = {
stories: [
"../src/**/*.stories.#(ts|tsx)"
],
addons: [
"#storybook/addon-links",
"#storybook/addon-essentials",
"#storybook/addon-interactions",
"#storybook/preset-create-react-app",
{
name: 'storybook-addon-swc', // I thought adding this will works, but it doesn't
options: {
enable: true,
enableSwcLoader: true,
enableSwcMinify: true,
},
},
],
framework: "#storybook/react",
core: {
builder: 'webpack5',
},
experiments: {
topLevelAwait: true,
}
}
I think this might be webpack problem in general. What can I do to make it work? What loader do I need to add? What do my main.js file needs to look like? Why it needs a loader when I use dynamic imports but works fine if it's a normal import?
I am setting up a new project with Remix (remix.run) and I am trying to configure Cypress/ESLint for component testing. I have a TestComponent.cy.ts with some boilerplate code:
describe('TestComponent.cy.ts', () => {
it('playground', () => {
// cy.mount()
})
})
However, the describe function throws this Error:
Parsing error: ESLint was configured to run on `<tsconfigRootDir>/component/TestComponent.cy.ts` using `parserOptions.project`: <tsconfigRootDir>/../../../../../../users/tduke/desktop/dev/blog/cypress/tsconfig.json
However, that TSConfig does not include this file. Either:
- Change ESLint's list of included files to not include this file
- Change that TSConfig to include this file
- Create a new TSConfig that includes this file and include it in your parserOptions.project
I have tried to reconfigure my .tsconfig.json and .eslintrc.js to no avail. Currently, this is what those files look like:
tsconfig.json:
{
"exclude": ["./cypress", "./cypress.config.ts"],
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx", "./cypress/component/*.cy.ts", "./cypress/**/*.cy.ts",
],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"types": ["vitest/globals"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"module": "CommonJS",
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "ES2019",
"strict": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
},
"skipLibCheck": true,
// Remix takes care of building everything in `remix build`.
"noEmit": true
}
}
.eslintrc.js:
/** #type {import('#types/eslint').Linter.BaseConfig} */
module.exports = {
extends: [
"#remix-run/eslint-config",
"#remix-run/eslint-config/node",
"#remix-run/eslint-config/jest-testing-library",
"prettier",
],
env: {
"cypress/globals": true,
},
parserOptions: {
project: './tsconfig.json'
},
plugins: ["cypress"],
// We're using vitest which has a very similar API to jest
// (so the linting plugins work nicely), but we have to
// set the jest version explicitly.
settings: {
jest: {
version: 28,
},
},
};
I also encountered the same problem, I solved the problem by removing the project property under parserOptions
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 12,
sourceType: 'module',
// project: 'tsconfig.json',
tsconfigRootDir: __dirname,
},
You should add ["./.eslintrc.js"] to your tsconfig.json file:
{
"include": ["./.eslintrc.js"],
//...
}
This is what I ended up putting in my .eslintrc.json:
module.exports = {
...
parserOptions: {
parser: '#typescript-eslint/parser',
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
};
It may have been that the project needed to be ./tsconfig.json instead of tsconfig.json. My tsconfig file is in the project root.
And this brought up some more errors linting things like babel config so I created an .eslintignore file and added those in there:
.eslintrc.js
ios/
android/
*.config.js
This solved all my issues.
I am currently working on a project with ReactJS, Typescript and TailwindCSS and everything seems to be working properly. The point is, all TailwindCSS classes work just fine except the ones for the background. Whenever I use a class like,bg-sky-500 or bg-slate-500, the color never displays and the background remains as it is. Also, I have tried to set a custom theme inside tailwindcss.config.json, but it didn't work either. Here are the configs used in the project:
tsconfig.json
{
"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-jsx",
"baseUrl": "src",
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src"]
}
tailwind.config.ts
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}", "./public/index.html"],
purge: [],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
postcss.config.ts
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
main.ts
const path = require("path");
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.#(js|jsx|ts|tsx)"],
addons: [
"#storybook/addon-links",
"#storybook/addon-essentials",
"#storybook/addon-interactions",
"#storybook/preset-create-react-app",
],
framework: "#storybook/react",
core: {
builder: "#storybook/builder-webpack5",
},
webpackFinal: async (config) => {
config.module.rules.push({
test: /\,css&/,
use: [
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins: [require("tailwindcss"), require("autoprefixer")],
},
},
],
include: path.resolve(__dirname, "../"),
});
return config;
},
};
preview.ts
import "index.css";
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
index.css
#tailwind base;
#tailwind components;
#tailwind utilities;
#tailwind forms;
Would anyone be able to help with this?
The "sky" and "slate" colors only became available in Tailwind CSS version 3+. I would recommend upgrading to the latest for these colors and the many additional improvements.
If you are stuck with v2, you should reference the list of colors in the old documentation: https://v2.tailwindcss.com/docs/background-color
Alternatively, you can add these colors in your tailwind.config.js file in the theme section, copying the values from Tailwind 3+:
module.exports = {
theme: {
extend: {
colors: {
slate: {
50: "rgb(248 250 252)",
100: "rgb(241 245 249)",
...
},
sky: {
50: "rgb(240 249 255)",
100: "rgb(224 242 254)",
...
},
},
},
},
}
See: https://v2.tailwindcss.com/docs/customizing-colors
i'm using next version 10.0.1, and react 17.0.2,
When i'm trying build my next app, i get an error:
ReferenceError: define is not defined
at Object.<anonymous> (/Users/***/Desktop/gm/toesim-web/node_modules/#glonassmobile/codebase-web/createAction.js:1:1)
at Module._compile (node:internal/modules/cjs/loader:1092:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
at Module.load (node:internal/modules/cjs/loader:972:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Module.require (node:internal/modules/cjs/loader:996:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.i1ag (/Users//Desktop/gm/toesim-web/.next/server/pages/deeplink/payment/[paymentId].js:7425:18)
at __webpack_require__ (/Users/Desktop/gm/toesim-web/.next/server/pages/deeplink/payment/[paymentId].js:23:31)
at Object.S/7G (/Users/Desktop/gm/toesim-web/.next/server/pages/deeplink/payment/[paymentId].js:5128:21) {
type: 'ReferenceError'
}
this is the code from node_modules where error is occurred
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
exports.createAction = void 0;
var createAction = function (type, payload) { return ({ type: type, payload: payload }); };
exports.createAction = createAction;
});
//# sourceMappingURL=createAction.js.map
i try to add to my next.config file
require('amd-loader') but it doesnt work too
i was looking for answer around a week, but it all was useless
could somebody give a help?
PS next.config.js
const path = require('path');
const withCSS = require('#zeit/next-css');
const withSass = require('#zeit/next-sass');
const webpack = require('webpack');
const ES6Promise = require("es6-promise");
ES6Promise.polyfill();
if (typeof define !== 'function') {
var define = require('amdefine')(module); // this doesn't work too
}
module.exports = withCSS(withSass({
webpack: function (config, options) {
config.plugins.push(new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
Promise: 'es6-promise-promise',
}))
config.module.rules.push(
{
test: /.(js|jsx|tsx|ts)$/,
exclude: /node_modules/,
include: path.resolve('./client/pages/'),
use: {
loader: "ts-loader",
options: {
configFile: './src/client/app/tsc.json',
},
},
},
{
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use:{
loader: 'url-loader',
options: {
name: '[name].[ext]',
limit: 10000000,
esModule : false,
}
}
},
)
return config
}
}))
and tsconfig
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"target": "ES6",
"moduleResolution": "Node",
"sourceMap": true,
"module": "commonjs",
"jsx": "preserve",
"allowJs": true,
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": false,
"esModuleInterop": true,
"resolveJsonModule": true,
"baseUrl": "./src/client/",
"isolatedModules": true
},
"files": [
"./src/pages/index.tsx"
],
"include": [
"src/**/*.ts",
"src/**/*.tsx",
],
"exclude": [
"src/**/*.proto",
"node_modules"
]
}```
Probably you are trying to import some library on server which require browser environment
looking from the error trace its /node_modules/#glonassmobile/codebase-web/createAction.js:1:1
If you decide to use this library, you need to check if you are server or client side like const isServer = typeof window === 'undefined', and import the library only in case you are client side