Invalidate nested .eslintrc - javascript

I've a node project which works with ES6 and serves an angular project in public folder:
├── public
│ ├── client.js
│ └── .eslintrc
├── server.js
└── .eslintrc
I would like to use airbnb extension in server and angular extension in client side so I've tried:
.eslintrc:
{
"parser": "babel-eslint",
"extends": "airbnb"
}
public/.eslintrc:
{
"extends": "angular"
}
The result is that in client.js rules for airbnb are being applied too. Is it possible to invalidate parent .eslintrc?

You can add root: true to the top of any config to stop ESLint from searching parent folders for config files. So you should update your public/.eslintrc and add top level property "root": true.

Related

What is the proper way to bundle assets with an NPM module?

I have a library, let's call it my-lib. The packaged code contains some JS and type declarations for that code in /dist, as well as a number of SVG files in /assets:
my-lib
├── assets
│   ├── a.svg
│   ├── b.svg
├── dist
│   ├── index.d.ts
│   ├── index.mjs
│   ├── index.umd.js
├── LICENSE
├── package.json
My intention is to have the JS modules exported on the root of the package, my-lib, with the assets importable from my-lib/assets (when using an appropriate plugin for the user's framework and bundler, E.G. vite-plugin-svgr for React+Vite). This seems to work by default in a create-react-app project with Webpack using import { ReactComponent as A } from "my-lib/assets/a.svg"; and the right declaration file to appease TypeScript, but in a Vite project ends up throwing errors:
[vite] Internal server error: Missing "./assets/a.svg" export in "my-lib" package
The important bits of my package.json:
// package.json
{
"main": "./dist/index.umd.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"./dist",
"./assets"
],
// ...rest
}
Are there specific fields to use for non-JS assets in package.json? Is this simply an issue with bundler config?
Minimum reproducible example

How to structure a simple yarn package for local sharing

I want to create a simple yarn package that can be installed by multiple local yarn projects using yarn add link:/path/to/package. Imagine the package of the package to be shared looks like this
├── package.json
├── tsconfig.json
└── src
├── generated
│   ├── abc.js
│   ├── abc.d.ts
│   └── def.js
│   └── def.d.ts
My goal is that abc and def should be accessible like this
import * from "myPackage/abc"
Currently if I install the above package with yarn add link:/path/to/package then I have to do
import * from "myPackage/src/generated/abc"
I'm a rookie on making typescript packages so forgive me. Can someone point me in the right direction here?
I've tried the suggestion here but it doesn't make a difference.
So it seems I need to set exports and typesVersions in package.json
"exports": {
"./": "./src/generated/"
},
"typesVersions": {
"*": {
"abc": [
"./src/generated/abc.d.ts"
],
"def": [
"./src/generated/def.d.ts"
],
}
},

Is it possible for vscode to auto-import paths in a non-relative way when using app-module-path library?

I'm using app-module-path, for resolving require statements without having to explicitly provide the relative path to the module. But vscode cant recognize the module since it's not relative path. I had tried using jsconfig by following this thread. I created the jsconfig in the root of my project. But the path resolution done by vscode conflicted with app-module-path, since vscode adds a server/ prefix before all paths. What changes should I make to jsconfig.json inorder to make it work with app-module-path?
Context
.
├── jsconfig.json
├── package.json
├── server
│   ├── app.js --------> here I set the app-module-path
│   ├── controllers
│   ├── helpers
│   │   ├── email
│   │   │   ├── fetch.js
│   │   └── urlparser.js
│   └── services
│      └── github
│      └── index.js-> i want import "helpers/email/fetch" from here, not "server/helpers/email/fetch"
└── yarn.lock
With app-module-path set in app.js in base of server, I am able to do the following from server/services/github/index.js:-
const {emailFetch} = require("helpers/email/fetch"); // Note: I cant use the "server/" prefix before "helpers" since app-module-path will lead to error
const {emailFetch} = require("server/helpers/email/fetch"); // This is how vscode auto-imports
This is how I have registered the app-module-path in app.js:-
require("app-module-path").addPath(__dirname);
With the following jsconfig.json, vscode gives intellisense, signature help, go to definition etc even if the path is helpers/email/fetch(without server/ prefix). But how can i make vscode auto-import or fix the require statements, in way such that it doesn't add server prefix before all import statements? Should I use some vscode-extension for doing this job?
{
"compilerOptions": {
"target": "ES6",
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"baseUrl": ".",
"paths": {
"*": [
"*",
"server/*"
]
}
},
"include": [
"server/**/*.js"
]
}
Just posting my answer too over here, so that someone else finds it useful. Currently I have made it to work by changing baseUrl into "server" and removing "server/*" from paths, in jsconfig.json. With this I get correct auto-imports + change in import paths while I move files around too. Do post your answers if you know to do this is in a better way. Cheers!

Using a library sibling folder container for shared packages

I have many applications in a mono git repository. I'd like to share some libraries between my applications.
Here is is my directory structure:
.
├── libs
│   └── messages
| ├── package.json
| ├── src
| ├── tsconfig.json
| └── ...all other good things for a library
├── main
│   ├── config
│   ├── dist
│   ├── node_modules
│   ├── package.json
│   ├── package-lock.json
│   ├── src
│   └── tsconfig.json
├── README.md
└── server
├── main.ts
├── node_modules
├── package.json
├── package-lock.json
├── setupEnvVars.js
├── src
└── tsconfig.json
I want to add the library "message" to my project "main" so I tried to add it this way:
{
"name": "me",
"version": "1.0.0",
"main": "index.js",
"private": true,
"dependencies": {
"#me/messages": "../libs/messages/"
}
}
I'm stuck because it doesn't work (but doesn't throw errors either). Can you help me? Thanks!
I'd need to see more about the content of each package.json files and the project overall, but here are some elements which may help you get a goo structure that would accomplish what you have set yourself to do.
If you really want to go the proper monorepo way, you might want to have a look at https://github.com/lerna/lerna which will work well enough for Node 8 and up (e.g. auto-symlink of file dependencies, which is useful during development).
Another way to solve this issue in TypeScript is using paths:
tsconfig.json
{
"compilerOptions": {
// path
"baseUrl": ".",
"paths": {
"#lib/*": ["./*"],
"#shared/*": ["../shared/*"]
}
}
However, depending on how you build and run your projects, paths are sometimes not processed properly. You may need to use tsconfig-paths if you run your project using ts-node, or tsconfig-paths-webpack-plugin if you are using TypeScript as part of a webpack project.

ESLint configuration woes

I am trying to use an eslint workflow. I have installed Node via nvm and the nessary plugins globally:
├── babel-eslint#6.0.2
├── eslint#2.7.0
├── eslint-config-angular#0.5.0
├── eslint-config-semistandard#6.0.1
├── eslint-config-standard#5.1.0
├── eslint-plugin-angular#1.0.0
├── eslint-plugin-promise#1.1.0
├── eslint-plugin-standard#1.3.2
├── npm#3.8.3
├── standard#6.0.8
And locally in my project:
"devDependencies": {
"eslint": "2.7.0",
"eslint-config-angular": "0.5.0",
"eslint-config-standard": "5.1.0",
"eslint-config-semistandard": "6.0.1",
"eslint-plugin-angular": "1.0.0",
"eslint-plugin-promise": "1.1.0",
"eslint-plugin-standard": "1.3.2"
},
And I have set up an .eslintrc.json file in my project root:
{
"env": {
"browser": 1
},
"extends": "semistandard",
"plugins": [
"standard","angular"
],
"globals": {
"angular": 1,
"$": 1,
"angularDragula": 1
}
}
The Atom ESlint and standard-formatter plugins find and respect my config file, but it not the command line or the Sublime Linter. It does not read the config, so I get errors flagged:
Which make it obvious it's not reading the settings. What am I doing wrong here? As I said, it works in Atom but not command line or Sublime (which uses the command-line options).
I'd recommend against using a global installation of ESLint. If you want to run it to lint the files in your project, you can use:
node_modules/.bin/eslint feedback-alerts.controller.js
Or, better yet, create an npm script for the task. In your package.json, you can add:
"scripts": {
"lint": "eslint feedback-alerts.controller.js"
}
Furthermore, it looks like you are using a config file that is not in your project root (~/.eslintrc.json is in your user root, not project root). ESLint will do its own config resolution, so you should not have to specify the path to the config file except in advanced cases.

Categories