From c6783f6fed2868f76b4f3b1189d5cc6c0030d955 Mon Sep 17 00:00:00 2001 From: pshu Date: Wed, 7 Sep 2022 17:25:50 +0800 Subject: [PATCH 1/6] =?UTF-8?q?refactor(plugi-qiankun):=20=F0=9F=8E=A8=20w?= =?UTF-8?q?alkaround=20type=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-qiankun/src/slave/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/plugin-qiankun/src/slave/index.ts b/packages/plugin-qiankun/src/slave/index.ts index 4be7e2d3..ec175a59 100644 --- a/packages/plugin-qiankun/src/slave/index.ts +++ b/packages/plugin-qiankun/src/slave/index.ts @@ -86,8 +86,9 @@ export default function (api: IApi) { api.modifyPublicPathStr((publicPathStr) => { const { runtimePublicPath } = api.config; - const { shouldNotModifyRuntimePublicPath } = (api.config.qiankun || {}) - .slave!; + const { shouldNotModifyRuntimePublicPath } = ( + api.config.qiankun || {} + ).slave!; if (runtimePublicPath === true && !shouldNotModifyRuntimePublicPath) { return `window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || "${ @@ -246,6 +247,7 @@ export default function (api: IApi) { followRedirects: false, changeOrigin: true, selfHandleResponse: true, + // @ts-ignore onProxyRes: responseInterceptor( async (responseBuffer, proxyRes, req, res) => { if (proxyRes.statusCode === 302) { From e2deaef6d9f9a7194838faa263b539553faf7921 Mon Sep 17 00:00:00 2001 From: pshu Date: Wed, 7 Sep 2022 17:26:39 +0800 Subject: [PATCH 2/6] =?UTF-8?q?refactor(plugin-inital-state):=20?= =?UTF-8?q?=F0=9F=8E=A8=20better=20error=20of=20app.ts=20parse=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-initial-state/package.json | 3 +++ packages/plugin-initial-state/src/index.ts | 18 +++++++++++++++++- yarn.lock | 22 +++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/packages/plugin-initial-state/package.json b/packages/plugin-initial-state/package.json index 7a4d3e57..81590588 100755 --- a/packages/plugin-initial-state/package.json +++ b/packages/plugin-initial-state/package.json @@ -27,5 +27,8 @@ }, "publishConfig": { "access": "public" + }, + "dependencies": { + "@umijs/deps": "^3.5.34" } } diff --git a/packages/plugin-initial-state/src/index.ts b/packages/plugin-initial-state/src/index.ts index f5614203..bdaa68b7 100755 --- a/packages/plugin-initial-state/src/index.ts +++ b/packages/plugin-initial-state/src/index.ts @@ -12,6 +12,7 @@ import { RELATIVE_EXPORT_PATH, } from './constants'; import { readFileSync } from 'fs'; +import codeFrame from '@umijs/deps/compiled/babel/code-frame'; const { winPath, getFile } = utils; @@ -65,7 +66,22 @@ export default (api: IApi) => { }); const relEntryFile = relative(api.paths.cwd!, entryFile || ''); - const enable = shouldPluginEnable(entryFile); + let enable = false; + + try { + enable = shouldPluginEnable(entryFile); + } catch (e) { + const error: any = e; + api.logger.error(`parse ${entryFile} Failed`); + if (error.loc && entryFile) { + const code = readFileSync(entryFile, 'utf-8'); + const frame = codeFrame(code, error.loc.line, error.loc.column + 1, { + highlightCode: true, + }); + console.log(frame); + } + throw e; + } api.writeTmpFile({ path: RELATIVE_MODEL_PATH, diff --git a/yarn.lock b/yarn.lock index f7104d93..42c355a5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3845,6 +3845,19 @@ jest-worker "24.9.0" prettier "2.2.1" +"@umijs/deps@^3.5.34": + version "3.5.34" + resolved "https://registry.npmmirror.com/@umijs/deps/-/deps-3.5.34.tgz#b0c71a04316fca5519f6265a5e5c888cc37285e7" + integrity sha512-F67mdR5fIXk95kOFJHjAtEmy4O4lvrxLYiOPuVkTeCkPVjXJWCSo8rZ/8hiMSvKPBlQBcg/WGeXbdR98Z0OOCA== + dependencies: + "@bloomberg/record-tuple-polyfill" "0.0.3" + chokidar "3.5.1" + clipboardy "2.3.0" + esbuild "0.12.15" + jest-worker "24.9.0" + prettier "2.2.1" + regenerate-unicode-properties "10.0.1" + "@umijs/fabric@^2.2.2", "@umijs/fabric@^2.5.6": version "2.6.2" resolved "https://registry.yarnpkg.com/@umijs/fabric/-/fabric-2.6.2.tgz#5c862cd7a8cf624a2a5beb6150bad3e8f03f484d" @@ -15637,6 +15650,13 @@ reftools@^1.1.9: resolved "https://registry.yarnpkg.com/reftools/-/reftools-1.1.9.tgz#e16e19f662ccd4648605312c06d34e5da3a2b77e" integrity sha512-OVede/NQE13xBQ+ob5CKd5KyeJYU2YInb1bmV4nRoOfquZPkAkxuOXicSe1PvqIuZZ4kD13sPKBbR7UFDmli6w== +regenerate-unicode-properties@10.0.1: + version "10.0.1" + resolved "https://registry.npmmirror.com/regenerate-unicode-properties/-/regenerate-unicode-properties-10.0.1.tgz#7f442732aa7934a3740c779bb9b3340dccc1fb56" + integrity sha512-vn5DU6yg6h8hP/2OkQo3K7uVILvY4iu0oI4t3HFa81UPkhGJwkRwM10JEc3upjdhHjs/k8GJY1sRBhk5sr69Bw== + dependencies: + regenerate "^1.4.2" + regenerate-unicode-properties@^8.2.0: version "8.2.0" resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-8.2.0.tgz#e5de7111d655e7ba60c057dbe9ff37c87e65cdec" @@ -15644,7 +15664,7 @@ regenerate-unicode-properties@^8.2.0: dependencies: regenerate "^1.4.0" -regenerate@^1.4.0: +regenerate@^1.4.0, regenerate@^1.4.2: version "1.4.2" resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.2.tgz#b9346d8827e8f5a32f7ba29637d398b69014848a" integrity sha512-zrceR/XhGYU/d/opr2EKO7aRHUeiBI8qjtfHqADTwZd6Szfy16la6kqD0MIUs5z5hx6AaKa+PixpPrR289+I0A== From 0689d6e347d7c49c6bbc949e9d60358d7f1836e1 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 8 Sep 2022 15:29:16 +0800 Subject: [PATCH 3/6] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20try=20catch=20pa?= =?UTF-8?q?rse=20app.ts=20for=20qiankun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-qiankun/package.json | 1 + packages/plugin-qiankun/src/master/index.ts | 24 +++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/plugin-qiankun/package.json b/packages/plugin-qiankun/package.json index 76a3533e..a327797a 100644 --- a/packages/plugin-qiankun/package.json +++ b/packages/plugin-qiankun/package.json @@ -43,6 +43,7 @@ }, "dependencies": { "@babel/runtime": "^7.4.5", + "@umijs/deps": "^3.5.34", "address": "^1.1.2", "http-proxy-middleware": "^2.0.3", "lodash": "^4.17.15", diff --git a/packages/plugin-qiankun/src/master/index.ts b/packages/plugin-qiankun/src/master/index.ts index 609b5342..9c75c54a 100644 --- a/packages/plugin-qiankun/src/master/index.ts +++ b/packages/plugin-qiankun/src/master/index.ts @@ -10,6 +10,7 @@ import { } from '../constants'; import modifyRoutes from './modifyRoutes'; import { hasExportWithName } from './utils'; +import codeFrame from '@umijs/deps/compiled/babel/code-frame'; const { getFile, winPath } = utils; @@ -51,10 +52,25 @@ export default function (api: IApi) { }); if (appFile) { const exportName = 'useQiankunStateForSlave'; - const hasExport = hasExportWithName({ - name: exportName, - filePath: appFile.path, - }); + + let hasExport = false; + try { + hasExport = hasExportWithName({ + name: exportName, + filePath: appFile.path, + }); + } catch (e) { + const error: any = e; + api.logger.error(`parse ${appFile.path} Failed`); + if (error.loc && appFile.path) { + const code = readFileSync(appFile.path, 'utf-8'); + const frame = codeFrame(code, error.loc.line, error.loc.column + 1, { + highlightCode: true, + }); + console.log(frame); + } + throw e; + } if (hasExport) { api.addRuntimePluginKey(() => exportName); From 20bc37d59839fa2e323ae38044f4fd5d28a0a339 Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 8 Sep 2022 16:13:10 +0800 Subject: [PATCH 4/6] =?UTF-8?q?fix:=20=F0=9F=90=9B=20compile=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-qiankun/src/master/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/plugin-qiankun/src/master/index.ts b/packages/plugin-qiankun/src/master/index.ts index 9c75c54a..d176b8f1 100644 --- a/packages/plugin-qiankun/src/master/index.ts +++ b/packages/plugin-qiankun/src/master/index.ts @@ -10,6 +10,7 @@ import { } from '../constants'; import modifyRoutes from './modifyRoutes'; import { hasExportWithName } from './utils'; +// @ts-ignore import codeFrame from '@umijs/deps/compiled/babel/code-frame'; const { getFile, winPath } = utils; @@ -164,7 +165,7 @@ export default function (api: IApi) { // 开启了 antd 插件的时候,使用 antd 的 loader 组件,否则提示用户必须设置一个自定义的 loader 组件 content: api.hasPlugins(['@umijs/plugin-antd']) ? readFileSync(join(__dirname, 'AntdLoader.tsx.tpl'), 'utf-8') - : `export default function Loader() { console.warn(\`[@umijs/plugin-qiankun]: Seems like you'r not using @umijs/plugin-antd, you need to provide a custom loader or set autoSetLoading false to shut down this warning!\`); return null; }`, + : `export default function Loader() { console.warn(\`[@umijs/plugin-qiankun]: Seems like you're not using @umijs/plugin-antd, you need to provide a custom loader or set autoSetLoading false to shut down this warning!\`); return null; }`, }); api.writeTmpFile({ path: 'plugin-qiankun/ErrorBoundary.tsx', From b3adc1f01cb74ed97937e86fa61b901f43ac495d Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 8 Sep 2022 16:18:45 +0800 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20=20plugin=20mode?= =?UTF-8?q?l=20try=20catch=20parse=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-model/package.json | 1 + packages/plugin-model/src/utils/index.ts | 77 ++++++++++++++++-------- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/packages/plugin-model/package.json b/packages/plugin-model/package.json index c987547a..85c9a445 100644 --- a/packages/plugin-model/package.json +++ b/packages/plugin-model/package.json @@ -29,6 +29,7 @@ "access": "public" }, "dependencies": { + "@umijs/deps": "^3.5.34", "fast-deep-equal": "3.1.1" } } diff --git a/packages/plugin-model/src/utils/index.ts b/packages/plugin-model/src/utils/index.ts index 0431c604..c02663f6 100644 --- a/packages/plugin-model/src/utils/index.ts +++ b/packages/plugin-model/src/utils/index.ts @@ -2,6 +2,7 @@ import path from 'path'; import { EOL } from 'os'; import { readFileSync } from 'fs'; import { utils } from 'umi'; +import codeFrame from '@umijs/deps/compiled/babel/code-frame'; const { t, parser, traverse, winPath } = utils; export type ModelItem = @@ -122,10 +123,24 @@ export const genModels = (imports: string[], absSrcPath: string) => { new Set(list).size !== list.length; const raw = contents.map((ele, index) => { - const ast = parser.parse(ele.content, { - sourceType: 'module', - plugins: ['jsx', 'typescript'], - }); + let ast: ReturnType | null = null; + + try { + ast = parser.parse(ele.content, { + sourceType: 'module', + plugins: ['jsx', 'typescript'], + }); + } catch (e) { + if (e.loc) { + const frame = codeFrame(ele.content, e.loc.line, e.loc.column + 1, { + highlightCode: true, + }); + console.log(`parse ${absSrcPath[index]} failed`); + console.log(frame); + } + + throw e; + } const use: string[] = []; @@ -163,26 +178,40 @@ export const isValidHook = (filePath: string) => { const isTSX = path.extname(filePath) === '.tsx'; const content = readFileSync(filePath, { encoding: 'utf-8' }).toString(); - const ast = parser.parse(content, { - sourceType: 'module', - plugins: [ - // .ts 不能加 jsx,因为里面可能有 `{}` 这种写法 - // .tsx, .js, .jsx 可以加 - isTS ? false : 'jsx', - // 非 ts 不解析 typescript - isTS || isTSX ? 'typescript' : false, - // 支持更多语法 - 'classProperties', - 'dynamicImport', - 'exportDefaultFrom', - 'exportNamespaceFrom', - 'functionBind', - 'nullishCoalescingOperator', - 'objectRestSpread', - 'optionalChaining', - 'decorators-legacy', - ].filter(Boolean) as utils.parser.ParserPlugin[], - }); + let ast: ReturnType | null = null; + + try { + ast = parser.parse(content, { + sourceType: 'module', + plugins: [ + // .ts 不能加 jsx,因为里面可能有 `{}` 这种写法 + // .tsx, .js, .jsx 可以加 + isTS ? false : 'jsx', + // 非 ts 不解析 typescript + isTS || isTSX ? 'typescript' : false, + // 支持更多语法 + 'classProperties', + 'dynamicImport', + 'exportDefaultFrom', + 'exportNamespaceFrom', + 'functionBind', + 'nullishCoalescingOperator', + 'objectRestSpread', + 'optionalChaining', + 'decorators-legacy', + ].filter(Boolean) as utils.parser.ParserPlugin[], + }); + } catch (e) { + if (e.loc) { + const frame = codeFrame(content, e.loc.line, e.loc.column + 1, { + highlightCode: true, + }); + console.log(`parse ${filePath} failed`); + console.log(frame); + } + + throw e; + } let valid = false; let identifierName = ''; traverse.default(ast, { From 3d3028e2410f31649e177318962c642f78b9441c Mon Sep 17 00:00:00 2001 From: pshu Date: Thu, 8 Sep 2022 16:25:39 +0800 Subject: [PATCH 6/6] =?UTF-8?q?refactor:=20=F0=9F=8E=A8=20ajdust=20log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/plugin-initial-state/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-initial-state/src/index.ts b/packages/plugin-initial-state/src/index.ts index bdaa68b7..da7d7b03 100755 --- a/packages/plugin-initial-state/src/index.ts +++ b/packages/plugin-initial-state/src/index.ts @@ -72,8 +72,8 @@ export default (api: IApi) => { enable = shouldPluginEnable(entryFile); } catch (e) { const error: any = e; - api.logger.error(`parse ${entryFile} Failed`); if (error.loc && entryFile) { + api.logger.error(`parse ${entryFile} Failed`); const code = readFileSync(entryFile, 'utf-8'); const frame = codeFrame(code, error.loc.line, error.loc.column + 1, { highlightCode: true,