高级前端必知知识点

2022-12-05  本文已影响0人  99ZY

1、 file 协议打开 报CORS错误

如果导出的 HTML 文件是通过 file 协议打开的,那么其中的 script 将不会运行,且报告下列错误。

Access to script at 'file:///foo/bar.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///foo/bar.js. (Reason: CORS request not http).

请查看 释因:CORS 请求不是 HTTP 请求 - HTTP | MDN 了解为什么会发生这种情况的更多信息。

你需要通过 http 协议访问该文件。

2、tsc打包构建typescript库项目为esm

打包命令(以下为typescript+scss+react项目实际使用命令)

rm -rf esm && tsc -p tsconfig.esm.json && tsc-alias && node-sass src -o esm && tsccss -o esm && copyfiles src/**/*.{png,gif,svg,css} esm -u 1

依赖包

tsc-alias "tsc-alias": "^1.8.2", 将 @转相对路径;
node-sass "node-sass": "^8.0.0", sass转css
tsccss "tsccss": "^1.0.0" import './a.module.scss' --> import './a.module.css'
copyfiles "copyfiles": "^2.4.1" 其他文件搬运

tsc配置文件

{
  "compilerOptions": {
    "baseUrl": "./",
    "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,
    "jsx": "react-jsx",
    "outDir": "esm",
    "declaration": true,
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  },
  "include": [
    "src"
  ]
}

3、peerDependencies以及npm install或者 yarn的寻包安装逻辑;

例子:在项目P中使用了依赖包A,A中又依赖了b,c;那么项目P安装依赖包A的时候,进行了哪些寻包逻辑;

上一篇 下一篇

猜你喜欢

热点阅读