个人博客
aiden-art / blog Goto Github PK
View Code? Open in Web Editor NEW个人博客
个人博客
Node verison 13.2.0
起开始正式支持 ES Modules
特性。
在 Node
中使用ESM
有两种方式:
package.json
中,增加 "type": "module"
配置,文件名还是以.js
结尾.mjs
文件可以直接使用 import
和 export
;node xx.js
报错ERR_MODULE_NOT_FOUND
在1
的基础上,使用Node
运行文件还是报错了,可能是import
时没有指定js
后缀
比如:import bootstrap from "../lib/index"
->import bootstrap from "../lib/index.js"
目前tsc
编译后的文件都不带后缀,所以想要正确执行,可以node --experimental-modules --es-module-specifier-resolution=node xx.js
关于tsc
编译时能否自动带上`.js``后缀?
解决办法如下:
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
console.log(__dirname)
import json
文件会采取commomjs
的方式读取内容可以使用fs
读取文件内容
const pkg = JSON.parse(fs.readFileSync(path.resolve(__dirname, "../../package.json"), "utf8"))
在开发monorepo
项目时,发现packages
下面的项目.eslintignore
不生效,解决方法如下:
vscode
默认只会识别根目录下这个文件,一个配置项可以指定工作目录:eslint.workingDirectories
,值有四种类型:
[{ "mode": "location" }]
默认模式,取当前根目录。
[{ "mode": "auto" }]
根据package.json
.eslintignore
.eslintrc*
三个文件去确定一个工作目录,但是不太推荐使用,因为会导致一些意料不到的问题。
string[]
手动指定目录列表,可以用,但是要经常维护
[{ "pattern": "./packages/*/" }]
最好的解决方案,通过正则匹配,特别适合monorepo
项目结构。
在项目中,为了代码检查和风格检查,会安装ESlint
,Prettier
等工具,但是有一个疑问在于:为什么编辑器(vscode
)已经安装了插件,还需要额外在项目中安装对应的npm
包?
vscode
中的ESlint
插件仅仅只是ESlint
插件,其本质还是会调用项目本地或全局安装的ESlint
(npm
包),然后将ESlint
的报错反馈给vscode
,因此我们可以直接在编辑器中看到错误,所以说ESlint
必须依赖本地安装/全局安装的ESlint
才可以正常工作。
npm
包安装的ESlint
包,可以在命令行执行ESlint
命令时,去触发检测代码,并把不符合lint
规则输出到命令行终端,需要与ESlint
配置文件配合使用,该配置文件的生效顺序是就近原则。
Prettier
的作用规则同Eslint
一致。
ESLint
主要负责: 质量检查(例如使用了某个变量却忘记了定义)、风格检查
Prettier
主要负责: 风格检查, 没有质量检查
需要注意一点就是:由于会出现引入多个风格检查的插件和配置lint
,这就导致存在部分会与Prettier
的lint
出现冲突。因此需要lint
预设包eslint-config-prettier
,这个包的作用是先关闭这些与Prettier
冲突的lint
规则,然后之后再启用Prettier
的规则,使得冲突的lint
规则以Prettier
为准。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.