Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
f1423518
N
next.js
项目概览
CoCo_Code_Op2
/
next.js
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
next.js
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f1423518
编写于
5月 13, 2020
作者:
J
Joe Haddad
提交者:
GitHub
5月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Better Babel syntax errors (#12821)
上级
615f658d
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
153 addition
and
9 deletion
+153
-9
packages/next/build/webpack-config.ts
packages/next/build/webpack-config.ts
+2
-0
packages/next/build/webpack/plugins/wellknown-errors-plugin/index.ts
...xt/build/webpack/plugins/wellknown-errors-plugin/index.ts
+17
-0
packages/next/build/webpack/plugins/wellknown-errors-plugin/parseBabel.ts
...ild/webpack/plugins/wellknown-errors-plugin/parseBabel.ts
+40
-0
packages/next/build/webpack/plugins/wellknown-errors-plugin/simpleWebpackError.ts
...ack/plugins/wellknown-errors-plugin/simpleWebpackError.ts
+11
-0
packages/next/build/webpack/plugins/wellknown-errors-plugin/webpackModuleError.ts
...ack/plugins/wellknown-errors-plugin/webpackModuleError.ts
+43
-0
test/acceptance/ReactRefreshLogBox.test.js
test/acceptance/ReactRefreshLogBox.test.js
+38
-7
test/integration/custom-error/test/index.test.js
test/integration/custom-error/test/index.test.js
+1
-1
test/integration/typescript-numeric-sep-exclusive/test/index.test.js
...ation/typescript-numeric-sep-exclusive/test/index.test.js
+1
-1
未找到文件。
packages/next/build/webpack-config.ts
浏览文件 @
f1423518
...
...
@@ -50,6 +50,7 @@ import WebpackConformancePlugin, {
MinificationConformanceCheck
,
ReactSyncScriptsConformanceCheck
,
}
from
'
./webpack/plugins/webpack-conformance-plugin
'
import
{
WellKnownErrorsPlugin
}
from
'
./webpack/plugins/wellknown-errors-plugin
'
type
ExcludesFalse
=
<
T
>
(
x
:
T
|
false
)
=>
x
is
T
...
...
@@ -1004,6 +1005,7 @@ export default async function getBaseWebpackConfig(
}),
].
filter
(
Boolean
),
}),
new
WellKnownErrorsPlugin
(),
].
filter
((
Boolean
as
any
)
as
ExcludesFalse
),
}
...
...
packages/next/build/webpack/plugins/wellknown-errors-plugin/index.ts
0 → 100644
浏览文件 @
f1423518
import
{
Compiler
}
from
'
webpack
'
import
{
getModuleBuildError
}
from
'
./webpackModuleError
'
export
class
WellKnownErrorsPlugin
{
apply
(
compiler
:
Compiler
)
{
compiler
.
hooks
.
compilation
.
tap
(
'
WellKnownErrorsPlugin
'
,
compilation
=>
{
compilation
.
hooks
.
seal
.
tap
(
'
WellKnownErrorsPlugin
'
,
()
=>
{
if
(
compilation
.
errors
?.
length
)
{
compilation
.
errors
=
compilation
.
errors
.
map
(
err
=>
{
const
moduleError
=
getModuleBuildError
(
compilation
,
err
)
return
moduleError
===
false
?
err
:
moduleError
})
}
})
})
}
}
packages/next/build/webpack/plugins/wellknown-errors-plugin/parseBabel.ts
0 → 100644
浏览文件 @
f1423518
import
Chalk
from
'
next/dist/compiled/chalk
'
import
{
SimpleWebpackError
}
from
'
./simpleWebpackError
'
const
chalk
=
new
Chalk
.
constructor
({
enabled
:
true
})
export
function
getBabelError
(
fileName
:
string
,
err
:
Error
&
{
code
?:
'
BABEL_PARSE_ERROR
'
loc
?:
{
line
:
number
;
column
:
number
}
}
):
SimpleWebpackError
|
false
{
if
(
err
.
code
!==
'
BABEL_PARSE_ERROR
'
)
{
return
false
}
// https://github.com/babel/babel/blob/34693d6024da3f026534dd8d569f97ac0109602e/packages/babel-core/src/parser/index.js
if
(
err
.
loc
)
{
const
lineNumber
=
Math
.
max
(
1
,
err
.
loc
.
line
)
const
column
=
Math
.
max
(
1
,
err
.
loc
.
column
)
let
message
=
err
.
message
// Remove file information, which instead is provided by webpack.
.
replace
(
/^.+
?
: /
,
''
)
// Remove column information from message
.
replace
(
new
RegExp
(
`[^\\S\\r\\n]*\\(
${
lineNumber
}
:
${
column
}
\\)[^\\S\\r\\n]*`
),
''
)
return
new
SimpleWebpackError
(
`
${
chalk
.
cyan
(
fileName
)}
:
${
chalk
.
yellow
(
lineNumber
.
toString
()
)}
:
${
chalk
.
yellow
(
column
.
toString
())}
`
,
chalk
.
red
.
bold
(
'
Syntax error
'
).
concat
(
`:
${
message
}
`
)
)
}
return
false
}
packages/next/build/webpack/plugins/wellknown-errors-plugin/simpleWebpackError.ts
0 → 100644
浏览文件 @
f1423518
// This class creates a simplified webpack error that formats nicely based on
// webpack's build in serializer.
// https://github.com/webpack/webpack/blob/c9d4ff7b054fc581c96ce0e53432d44f9dd8ca72/lib/Stats.js#L294-L356
export
class
SimpleWebpackError
extends
Error
{
file
:
string
constructor
(
file
:
string
,
message
:
string
)
{
super
(
message
)
this
.
file
=
file
}
}
packages/next/build/webpack/plugins/wellknown-errors-plugin/webpackModuleError.ts
0 → 100644
浏览文件 @
f1423518
import
*
as
path
from
'
path
'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import
{
compilation
}
from
'
webpack
'
import
{
getBabelError
}
from
'
./parseBabel
'
import
{
SimpleWebpackError
}
from
'
./simpleWebpackError
'
function
getFilename
(
compilation
:
compilation
.
Compilation
,
m
:
any
):
string
{
const
requestShortener
=
compilation
.
requestShortener
if
(
typeof
m
?.
readableIdentifier
===
'
function
'
)
{
return
m
.
readableIdentifier
(
requestShortener
)
}
if
(
typeof
m
.
resource
===
'
string
'
)
{
const
res
=
path
.
relative
(
compilation
.
context
,
m
.
resource
)
return
res
.
startsWith
(
'
.
'
)
?
res
:
`.
${
path
.
sep
}${
res
}
`
}
return
m
.
request
??
'
<unknown>
'
}
export
function
getModuleBuildError
(
compilation
:
compilation
.
Compilation
,
input
:
any
):
SimpleWebpackError
|
false
{
if
(
!
(
typeof
input
===
'
object
'
&&
input
?.
name
===
'
ModuleBuildError
'
&&
Boolean
(
input
.
module
)
&&
input
.
error
instanceof
Error
)
)
{
return
false
}
const
err
:
Error
=
input
.
error
const
sourceFilename
=
getFilename
(
compilation
,
input
.
module
)
const
babel
=
getBabelError
(
sourceFilename
,
err
)
if
(
babel
!==
false
)
{
return
babel
}
return
false
}
test/acceptance/ReactRefreshLogBox.test.js
浏览文件 @
f1423518
...
...
@@ -33,7 +33,13 @@ test('logbox: can recover from a syntax error without losing state', async () =>
await
session
.
patch
(
'
index.js
'
,
`export default () => <div/`
)
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
expect
(
await
session
.
getRedboxSource
()).
toMatch
(
'
SyntaxError
'
)
expect
(
await
session
.
getRedboxSource
()).
toMatchInlineSnapshot
(
`
"./index.js:1:26
Syntax error: Unexpected token, expected \\"jsxTagEnd\\"
> 1 | export default () => <div/
| ^"
`
)
await
session
.
patch
(
'
index.js
'
,
...
...
@@ -482,18 +488,33 @@ test('syntax > runtime error', async () => {
i++
throw Error('no ' + i)
}, 1000)
export default function FunctionNamed() {
`
export default function FunctionNamed() {`
)
await
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
1000
))
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
expect
(
await
session
.
getRedboxSource
()).
toMatch
(
'
SyntaxError
'
)
expect
(
await
session
.
getRedboxSource
()).
toMatchInlineSnapshot
(
`
"./index.js:8:47
Syntax error: Unexpected token
6 | throw Error('no ' + i)
7 | }, 1000)
> 8 | export default function FunctionNamed() {
| ^"
`
)
// Test that runtime error does not take over:
await
new
Promise
(
resolve
=>
setTimeout
(
resolve
,
2000
))
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
expect
(
await
session
.
getRedboxSource
()).
toMatch
(
'
SyntaxError
'
)
expect
(
await
session
.
getRedboxSource
()).
toMatchInlineSnapshot
(
`
"./index.js:8:47
Syntax error: Unexpected token
6 | throw Error('no ' + i)
7 | }, 1000)
> 8 | export default function FunctionNamed() {
| ^"
`
)
await
cleanup
()
})
...
...
@@ -600,8 +621,18 @@ test('unterminated JSX', async () => {
expect
(
await
session
.
hasRedbox
(
true
)).
toBe
(
true
)
const
source
=
await
session
.
getRedboxSource
()
expect
(
source
).
not
.
toMatch
(
'
Unexpected token
'
)
expect
(
source
).
toMatch
(
'
Unterminated JSX contents
'
)
expect
(
source
).
toMatchInlineSnapshot
(
`
"./index.js:5:22
Syntax error: Unterminated JSX contents
3 | return (
4 | <div>
> 5 | <p>lol</p>
| ^
6 | div
7 | )
8 | }"
`
)
await
cleanup
()
})
...
...
test/integration/custom-error/test/index.test.js
浏览文件 @
f1423518
...
...
@@ -46,7 +46,7 @@ describe('Custom _error', () => {
await
fs
.
writeFile
(
page404
,
'
export default <h1>
'
)
const
html
=
await
renderViaHTTP
(
appPort
,
'
/404
'
)
await
fs
.
remove
(
page404
)
expect
(
html
).
toContain
(
'
Module build failed
'
)
expect
(
html
).
toContain
(
'
Syntax error
'
)
expect
(
stderr
).
not
.
toMatch
(
customErrNo404Match
)
})
})
...
...
test/integration/typescript-numeric-sep-exclusive/test/index.test.js
浏览文件 @
f1423518
...
...
@@ -16,7 +16,7 @@ describe('TypeScript Exclusivity of Numeric Separator', () => {
expect
(
code
).
toBe
(
1
)
expect
(
stderr
).
toContain
(
'
Failed to compile.
'
)
expect
(
stderr
).
toContain
(
'
Syntax
Error:
'
)
expect
(
stderr
).
toContain
(
'
Syntax
error
'
)
expect
(
stderr
).
toContain
(
'
config to enable transformation
'
)
})
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录