fix: js 语法错误导致 dev 退出 (#149)

This commit is contained in:
qlin 2022-09-27 09:56:12 +08:00 committed by GitHub
parent c1ea990535
commit 04189feb2f
3 changed files with 65 additions and 52 deletions

View File

@ -112,10 +112,15 @@ export const genModels = (imports, absSrcPath) => {
const checkDuplicates = list => new Set(list).size !== list.length; const checkDuplicates = list => new Set(list).size !== list.length;
const raw = contents.map((ele, index) => { const raw = contents.map((ele, index) => {
const ast = parser.parse(ele.content, { let ast;
try {
ast = parser.parse(ele.content, {
sourceType: 'module', sourceType: 'module',
plugins: ['jsx', 'typescript'] plugins: ['jsx', 'typescript']
}); });
} catch (err) {
return null;
}
const use = []; const use = [];
@ -136,7 +141,7 @@ export const genModels = (imports, absSrcPath) => {
}); });
return { namespace: ele.namespace, use, importName: `model${index}` }; return { namespace: ele.namespace, use, importName: `model${index}` };
}); }).filter(Boolean);
const models = sort(raw); const models = sort(raw);
@ -149,6 +154,8 @@ export const genModels = (imports, absSrcPath) => {
}; };
export const isValidHook = (filePath) => { export const isValidHook = (filePath) => {
let valid = false;
try {
const content = readFileSync(filePath, { encoding: 'utf-8' }).toString(); const content = readFileSync(filePath, { encoding: 'utf-8' }).toString();
const ast = parser.parse(content, { const ast = parser.parse(content, {
@ -165,7 +172,7 @@ export const isValidHook = (filePath) => {
'decorators-legacy' 'decorators-legacy'
].filter(Boolean) ].filter(Boolean)
}); });
let valid = false;
let identifierName = ''; let identifierName = '';
traverse(ast, { traverse(ast, {
enter(p) { enter(p) {
@ -187,7 +194,6 @@ export const isValidHook = (filePath) => {
} }
}); });
try {
if (identifierName) { if (identifierName) {
ast.program.body.forEach((ele) => { ast.program.body.forEach((ele) => {
if (ele.type === 'FunctionDeclaration') { if (ele.type === 'FunctionDeclaration') {

View File

@ -105,11 +105,14 @@ function parseModel(paths = [], root) {
importModules.push(`import ${moduleName} from '${path}'`); importModules.push(`import ${moduleName} from '${path}'`);
modules.push(moduleName); modules.push(moduleName);
const content = readFileSync(path).toString('utf-8'); const content = readFileSync(path).toString('utf-8');
let ast = parser.parse(content, { let ast;
try {
ast = parser.parse(content, {
sourceType: 'module', sourceType: 'module',
plugins: ['jsx', 'typescript'] plugins: ['jsx', 'typescript']
}); });
ast = ast.program.body.filter(body => body.type === 'ExportDefaultDeclaration')[0]; ast = ast.program.body.filter(body => body.type === 'ExportDefaultDeclaration')[0];
} catch (err) {}
if (ast) { if (ast) {
const { mutations, actions, getters } = getModelTypes(ast.declaration, moduleName); const { mutations, actions, getters } = getModelTypes(ast.declaration, moduleName);
MUTATION_TYPES = { MUTATION_TYPES = {

View File

@ -69,6 +69,7 @@ const getRoutePath = function (parentRoutePath, fileName, isFile = true) {
}; };
function getRouteMeta(content) { function getRouteMeta(content) {
try {
const ast = parser.parse(content, { const ast = parser.parse(content, {
sourceType: 'module', sourceType: 'module',
plugins: ['jsx', 'typescript'] plugins: ['jsx', 'typescript']
@ -79,6 +80,9 @@ function getRouteMeta(content) {
return JSON.parse(argument.code.replace(/'/g, '"').replace(/(\S+):/g, (global, m1) => `"${m1}":`)); return JSON.parse(argument.code.replace(/'/g, '"').replace(/(\S+):/g, (global, m1) => `"${m1}":`));
} }
return null; return null;
} catch (error) {
return null;
}
} }
let cacheGenRoutes = {}; let cacheGenRoutes = {};