Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-initial-state)/prettier app.ts parse error #902

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/plugin-initial-state/package.json
Original file line number Diff line number Diff line change
@@ -27,5 +27,8 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@umijs/deps": "^3.5.34"
}
}
18 changes: 17 additions & 1 deletion packages/plugin-initial-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

封装成一个公共方法?比如 tryCatchFriendlyParseError,捕获 & codeFrame 逻辑写里面,外部传个函数进去执行

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在各个包都是独立的,提取还是重复代码就没提取了。

enable = shouldPluginEnable(entryFile);
} catch (e) {
const error: any = e;
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,
});
console.log(frame);
}
throw e;
}

api.writeTmpFile({
path: RELATIVE_MODEL_PATH,
1 change: 1 addition & 0 deletions packages/plugin-model/package.json
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
"access": "public"
},
"dependencies": {
"@umijs/deps": "^3.5.34",
"fast-deep-equal": "3.1.1"
}
}
77 changes: 53 additions & 24 deletions packages/plugin-model/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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<typeof parser.parse> | 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,因为里面可能有 `<Type>{}` 这种写法
// .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<typeof parser.parse> | null = null;

try {
ast = parser.parse(content, {
sourceType: 'module',
plugins: [
// .ts 不能加 jsx,因为里面可能有 `<Type>{}` 这种写法
// .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, {
1 change: 1 addition & 0 deletions packages/plugin-qiankun/package.json
Original file line number Diff line number Diff line change
@@ -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",
27 changes: 22 additions & 5 deletions packages/plugin-qiankun/src/master/index.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,8 @@ 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;

@@ -51,10 +53,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);
@@ -148,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',
6 changes: 4 additions & 2 deletions packages/plugin-qiankun/src/slave/index.ts
Original file line number Diff line number Diff line change
@@ -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) {
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -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,14 +15650,21 @@ 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"
integrity sha512-F9DjY1vKLo/tPePDycuH3dn9H1OTPIkVD9Kz4LODu+F2C75mgjAJ7x/gwy6ZcSNRAAkhNlJSOHRe8k3p+K9WhA==
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==