Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
a9cf735f
Unverified
Commit
a9cf735f
authored
Dec 02, 2018
by
Tim Neutkens
Committed by
GitHub
Dec 02, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Convert babel plugins to typescript (#5789)
Slowly moving files over
💯
parent
e43d21fd
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
175 additions
and
124 deletions
+175
-124
next-to-next-server.ts
packages/next/build/babel/plugins/next-to-next-server.ts
+5
-2
react-loadable-plugin.ts
packages/next/build/babel/plugins/react-loadable-plugin.ts
+54
-44
preset.js
packages/next/build/babel/preset.js
+0
-63
preset.ts
packages/next/build/babel/preset.ts
+84
-0
generate-build-id.ts
packages/next/build/generate-build-id.ts
+13
-0
index.js
packages/next/build/index.js
+1
-14
package.json
packages/next/package.json
+6
-0
tsconfig.json
packages/next/tsconfig.json
+12
-1
No files found.
packages/next/build/babel/plugins/next-to-next-server.
j
s
→
packages/next/build/babel/plugins/next-to-next-server.
t
s
View file @
a9cf735f
import
{
PluginObj
}
from
'@babel/core'
import
{
NodePath
}
from
'@babel/traverse'
import
{
ImportDeclaration
}
from
'@babel/types'
// Rewrite imports using next/<something> to next-server/<something>
// Rewrite imports using next/<something> to next-server/<something>
export
default
function
({
types
:
t
,
template
})
{
export
default
function
NextToNextServer
():
PluginObj
{
return
{
return
{
visitor
:
{
visitor
:
{
ImportDeclaration
(
path
)
{
ImportDeclaration
(
path
:
NodePath
<
ImportDeclaration
>
)
{
const
source
=
path
.
node
.
source
.
value
const
source
=
path
.
node
.
source
.
value
if
(
source
===
'next/asset'
)
{
if
(
source
===
'next/asset'
)
{
path
.
node
.
source
.
value
=
'next-server/asset'
path
.
node
.
source
.
value
=
'next-server/asset'
...
...
packages/next/build/babel/plugins/react-loadable-plugin.
j
s
→
packages/next/build/babel/plugins/react-loadable-plugin.
t
s
View file @
a9cf735f
...
@@ -23,10 +23,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
...
@@ -23,10 +23,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
// Modified to put `webpack` and `modules` under `loadableGenerated` to be backwards compatible with next/dynamic which has a `modules` key
// Modified to put `webpack` and `modules` under `loadableGenerated` to be backwards compatible with next/dynamic which has a `modules` key
// Modified to support `dynamic(import('something'))` and `dynamic(import('something'), options)
// Modified to support `dynamic(import('something'))` and `dynamic(import('something'), options)
export
default
function
({
types
:
t
,
template
})
{
import
{
PluginObj
}
from
'@babel/core'
import
{
NodePath
}
from
'@babel/traverse'
import
*
as
BabelTypes
from
'@babel/types'
export
default
function
({
types
:
t
}:
{
types
:
typeof
BabelTypes
}):
PluginObj
{
return
{
return
{
visitor
:
{
visitor
:
{
ImportDeclaration
(
path
)
{
ImportDeclaration
(
path
:
NodePath
<
BabelTypes
.
ImportDeclaration
>
)
{
let
source
=
path
.
node
.
source
.
value
let
source
=
path
.
node
.
source
.
value
if
(
source
!==
'next/dynamic'
)
return
if
(
source
!==
'next/dynamic'
)
return
...
@@ -36,8 +40,12 @@ export default function ({ types: t, template }) {
...
@@ -36,8 +40,12 @@ export default function ({ types: t, template }) {
if
(
!
defaultSpecifier
)
return
if
(
!
defaultSpecifier
)
return
let
bindingName
=
defaultSpecifier
.
node
.
local
.
name
const
bindingName
=
defaultSpecifier
.
node
.
local
.
name
let
binding
=
path
.
scope
.
getBinding
(
bindingName
)
const
binding
=
path
.
scope
.
getBinding
(
bindingName
)
if
(
!
binding
)
{
return
}
binding
.
referencePaths
.
forEach
(
refPath
=>
{
binding
.
referencePaths
.
forEach
(
refPath
=>
{
let
callExpression
=
refPath
.
parentPath
let
callExpression
=
refPath
.
parentPath
...
@@ -53,20 +61,22 @@ export default function ({ types: t, template }) {
...
@@ -53,20 +61,22 @@ export default function ({ types: t, template }) {
if
(
!
callExpression
.
isCallExpression
())
return
if
(
!
callExpression
.
isCallExpression
())
return
let
args
=
callExpression
.
get
(
'arguments'
)
let
args
=
callExpression
.
get
(
'arguments'
)
if
(
args
.
length
>
2
)
throw
callExpression
.
error
if
(
args
.
length
>
2
)
{
throw
callExpression
.
buildCodeFrameError
(
'next/dynamic only accepts 2 arguments'
)
let
loader
}
let
options
if
(
!
args
[
0
])
{
if
(
!
args
[
0
])
{
return
return
}
}
let
loader
let
options
if
(
args
[
0
].
isObjectExpression
())
{
if
(
args
[
0
].
isObjectExpression
())
{
options
=
args
[
0
]
options
=
args
[
0
]
}
else
{
}
else
{
if
(
!
args
[
1
])
{
if
(
!
args
[
1
])
{
callExpression
.
pushContainer
(
'arguments'
,
t
.
objectExpression
([]))
callExpression
.
node
.
arguments
.
push
(
t
.
objectExpression
([]))
}
}
// This is needed as the code is modified above
// This is needed as the code is modified above
args
=
callExpression
.
get
(
'arguments'
)
args
=
callExpression
.
get
(
'arguments'
)
...
@@ -77,10 +87,10 @@ export default function ({ types: t, template }) {
...
@@ -77,10 +87,10 @@ export default function ({ types: t, template }) {
if
(
!
options
.
isObjectExpression
())
return
if
(
!
options
.
isObjectExpression
())
return
let
properties
=
options
.
get
(
'properties'
)
let
properties
=
options
.
get
(
'properties'
)
let
propertiesMap
=
{}
let
propertiesMap
:
{[
key
:
string
]:
NodePath
<
BabelTypes
.
ObjectProperty
|
BabelTypes
.
ObjectMethod
|
BabelTypes
.
SpreadProperty
>
}
=
{}
properties
.
forEach
(
property
=>
{
properties
.
forEach
(
property
=>
{
let
ke
y
=
property
.
get
(
'key'
)
const
key
:
an
y
=
property
.
get
(
'key'
)
propertiesMap
[
key
.
node
.
name
]
=
property
propertiesMap
[
key
.
node
.
name
]
=
property
})
})
...
@@ -96,20 +106,23 @@ export default function ({ types: t, template }) {
...
@@ -96,20 +106,23 @@ export default function ({ types: t, template }) {
loader
=
propertiesMap
.
modules
.
get
(
'value'
)
loader
=
propertiesMap
.
modules
.
get
(
'value'
)
}
}
let
loaderMethod
=
loader
if
(
!
loader
||
Array
.
isArray
(
loader
))
{
let
dynamicImports
=
[]
return
}
const
dynamicImports
:
BabelTypes
.
StringLiteral
[]
=
[]
loader
Method
.
traverse
({
loader
.
traverse
({
Import
(
path
)
{
Import
(
path
)
{
dynamicImports
.
push
(
path
.
parentPath
)
const
args
=
path
.
parentPath
.
get
(
'arguments'
)
if
(
!
Array
.
isArray
(
args
))
return
const
node
:
any
=
args
[
0
].
node
dynamicImports
.
push
(
node
)
}
}
})
})
if
(
!
dynamicImports
.
length
)
return
if
(
!
dynamicImports
.
length
)
return
options
.
pushContainer
(
options
.
node
.
properties
.
push
(
t
.
objectProperty
(
'properties'
,
t
.
objectProperty
(
t
.
identifier
(
'loadableGenerated'
),
t
.
identifier
(
'loadableGenerated'
),
t
.
objectExpression
([
t
.
objectExpression
([
t
.
objectProperty
(
t
.
objectProperty
(
...
@@ -123,7 +136,7 @@ export default function ({ types: t, template }) {
...
@@ -123,7 +136,7 @@ export default function ({ types: t, template }) {
t
.
identifier
(
'require'
),
t
.
identifier
(
'require'
),
t
.
identifier
(
'resolveWeak'
)
t
.
identifier
(
'resolveWeak'
)
),
),
[
dynamicImport
.
get
(
'arguments'
)[
0
].
node
]
[
dynamicImport
]
)
)
})
})
)
)
...
@@ -132,14 +145,11 @@ export default function ({ types: t, template }) {
...
@@ -132,14 +145,11 @@ export default function ({ types: t, template }) {
t
.
objectProperty
(
t
.
objectProperty
(
t
.
identifier
(
'modules'
),
t
.
identifier
(
'modules'
),
t
.
arrayExpression
(
t
.
arrayExpression
(
dynamicImports
.
map
(
dynamicImport
=>
{
dynamicImports
return
dynamicImport
.
get
(
'arguments'
)[
0
].
node
})
)
)
)
)
])
])
)
))
)
// Turns `dynamic(import('something'))` into `dynamic(() => import('something'))` for backwards compat.
// Turns `dynamic(import('something'))` into `dynamic(() => import('something'))` for backwards compat.
// This is the replicate the behavior in versions below Next.js 7 where we magically handled not executing the `import()` too.
// This is the replicate the behavior in versions below Next.js 7 where we magically handled not executing the `import()` too.
...
...
packages/next/build/babel/preset.js
deleted
100644 → 0
View file @
e43d21fd
const
env
=
process
.
env
.
NODE_ENV
const
isProduction
=
env
===
'production'
const
isDevelopment
=
env
===
'development'
const
isTest
=
env
===
'test'
// Resolve styled-jsx plugins
function
styledJsxOptions
(
opts
)
{
if
(
!
opts
)
{
return
{}
}
if
(
!
Array
.
isArray
(
opts
.
plugins
))
{
return
opts
}
opts
.
plugins
=
opts
.
plugins
.
map
(
plugin
=>
{
if
(
Array
.
isArray
(
plugin
))
{
const
[
name
,
options
]
=
plugin
return
[
require
.
resolve
(
name
),
options
]
}
return
require
.
resolve
(
plugin
)
})
return
opts
}
module
.
exports
=
(
context
,
opts
=
{})
=>
({
presets
:
[
[
require
(
'@babel/preset-env'
).
default
,
{
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules
:
isDevelopment
||
isProduction
?
false
:
'auto'
,
...
opts
[
'preset-env'
]
}],
[
require
(
'@babel/preset-react'
),
{
// This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development
development
:
isDevelopment
||
isTest
,
...
opts
[
'preset-react'
]
}]
],
plugins
:
[
require
(
'babel-plugin-react-require'
),
require
(
'@babel/plugin-syntax-dynamic-import'
),
require
(
'./plugins/react-loadable-plugin'
),
require
(
'./plugins/next-to-next-server'
),
[
require
(
'@babel/plugin-proposal-class-properties'
),
opts
[
'class-properties'
]
||
{}],
require
(
'@babel/plugin-proposal-object-rest-spread'
),
[
require
(
'@babel/plugin-transform-runtime'
),
{
corejs
:
2
,
helpers
:
true
,
regenerator
:
true
,
useESModules
:
!
isTest
,
...
opts
[
'transform-runtime'
]
}],
[
require
(
'styled-jsx/babel'
),
styledJsxOptions
(
opts
[
'styled-jsx'
])],
process
.
env
.
NODE_ENV
===
'production'
&&
require
(
'babel-plugin-transform-react-remove-prop-types'
)
].
filter
(
Boolean
)
})
packages/next/build/babel/preset.ts
0 → 100644
View file @
a9cf735f
import
{
PluginItem
}
from
'@babel/core'
const
env
=
process
.
env
.
NODE_ENV
const
isProduction
=
env
===
'production'
const
isDevelopment
=
env
===
'development'
const
isTest
=
env
===
'test'
type
StyledJsxPlugin
=
[
string
,
any
]
|
string
type
StyledJsxBabelOptions
=
{
plugins
?:
StyledJsxPlugin
[]
}
|
undefined
// Resolve styled-jsx plugins
function
styledJsxOptions
(
options
:
StyledJsxBabelOptions
)
{
if
(
!
options
)
{
return
{}
}
if
(
!
Array
.
isArray
(
options
.
plugins
))
{
return
options
}
options
.
plugins
=
options
.
plugins
.
map
((
plugin
:
StyledJsxPlugin
):
StyledJsxPlugin
=>
{
if
(
Array
.
isArray
(
plugin
))
{
const
[
name
,
options
]
=
plugin
return
[
require
.
resolve
(
name
),
options
]
}
return
require
.
resolve
(
plugin
)
})
return
options
}
type
NextBabelPresetOptions
=
{
'preset-env'
?:
any
,
'preset-react'
?:
any
,
'class-properties'
?:
any
,
'transform-runtime'
?:
any
,
'styled-jsx'
?:
StyledJsxBabelOptions
}
type
BabelPreset
=
{
presets
?:
PluginItem
[]
|
null
,
plugins
?:
PluginItem
[]
|
null
}
module
.
exports
=
(
context
:
any
,
options
:
NextBabelPresetOptions
=
{}):
BabelPreset
=>
{
return
{
presets
:
[
[
require
(
'@babel/preset-env'
).
default
,
{
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules
:
isDevelopment
||
isProduction
?
false
:
'auto'
,
...
options
[
'preset-env'
]
}],
[
require
(
'@babel/preset-react'
),
{
// This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development
development
:
isDevelopment
||
isTest
,
...
options
[
'preset-react'
]
}]
],
plugins
:
[
require
(
'babel-plugin-react-require'
),
require
(
'@babel/plugin-syntax-dynamic-import'
),
require
(
'./plugins/react-loadable-plugin'
),
require
(
'./plugins/next-to-next-server'
),
[
require
(
'@babel/plugin-proposal-class-properties'
),
options
[
'class-properties'
]
||
{}],
require
(
'@babel/plugin-proposal-object-rest-spread'
),
[
require
(
'@babel/plugin-transform-runtime'
),
{
corejs
:
2
,
helpers
:
true
,
regenerator
:
true
,
useESModules
:
!
isTest
,
...
options
[
'transform-runtime'
]
}],
[
require
(
'styled-jsx/babel'
),
styledJsxOptions
(
options
[
'styled-jsx'
])],
process
.
env
.
NODE_ENV
===
'production'
&&
require
(
'babel-plugin-transform-react-remove-prop-types'
)
].
filter
(
Boolean
)
}
}
packages/next/build/generate-build-id.ts
0 → 100644
View file @
a9cf735f
export
async
function
generateBuildId
(
generate
:
()
=>
string
|
null
,
fallback
:
()
=>
string
):
Promise
<
string
>
{
let
buildId
=
await
generate
()
// If there's no buildId defined we'll fall back
if
(
buildId
===
null
)
{
buildId
=
fallback
()
}
if
(
typeof
buildId
!==
'string'
)
{
throw
new
Error
(
'generateBuildId did not return a string. https://err.sh/zeit/next.js/generatebuildid-not-a-string'
)
}
return
buildId
.
trim
()
}
packages/next/build/index.js
View file @
a9cf735f
...
@@ -6,24 +6,11 @@ import nanoid from 'nanoid'
...
@@ -6,24 +6,11 @@ import nanoid from 'nanoid'
import
loadConfig
from
'next-server/next-config'
import
loadConfig
from
'next-server/next-config'
import
{
PHASE_PRODUCTION_BUILD
,
BUILD_ID_FILE
}
from
'next-server/constants'
import
{
PHASE_PRODUCTION_BUILD
,
BUILD_ID_FILE
}
from
'next-server/constants'
import
getBaseWebpackConfig
from
'./webpack'
import
getBaseWebpackConfig
from
'./webpack'
import
{
generateBuildId
}
from
'./generate-build-id'
const
access
=
promisify
(
fs
.
access
)
const
access
=
promisify
(
fs
.
access
)
const
writeFile
=
promisify
(
fs
.
writeFile
)
const
writeFile
=
promisify
(
fs
.
writeFile
)
async
function
generateBuildId
(
generate
,
fallback
)
{
let
buildId
=
await
generate
()
// If there's no buildId defined we'll fall back
if
(
buildId
===
null
)
{
buildId
=
fallback
()
}
if
(
typeof
buildId
!==
'string'
)
{
throw
new
Error
(
'generateBuildId did not return a string. https://err.sh/zeit/next.js/generatebuildid-not-a-string'
)
}
return
buildId
.
trim
()
}
async
function
ensureProjectDirectoryIsWriteAble
(
dir
)
{
async
function
ensureProjectDirectoryIsWriteAble
(
dir
)
{
try
{
try
{
await
access
(
dir
,
(
fs
.
constants
||
fs
).
W_OK
)
await
access
(
dir
,
(
fs
.
constants
||
fs
).
W_OK
)
...
...
packages/next/package.json
View file @
a9cf735f
...
@@ -94,6 +94,12 @@
...
@@ -94,6 +94,12 @@
"@taskr/clear"
:
"1.1.0"
,
"@taskr/clear"
:
"1.1.0"
,
"@taskr/esnext"
:
"1.1.0"
,
"@taskr/esnext"
:
"1.1.0"
,
"@taskr/watch"
:
"1.1.0"
,
"@taskr/watch"
:
"1.1.0"
,
"@types/babel__core"
:
"7.0.4"
,
"@types/babel__traverse"
:
"7.0.3"
,
"@types/nanoid"
:
"1.2.0"
,
"@types/babel-types"
:
"7.0.4"
,
"@types/babel__generator"
:
"7.0.1"
,
"@types/babel__template"
:
"7.0.1"
,
"taskr"
:
"1.1.0"
,
"taskr"
:
"1.1.0"
,
"typescript"
:
"3.1.6"
"typescript"
:
"3.1.6"
},
},
...
...
packages/next/tsconfig.json
View file @
a9cf735f
...
@@ -5,6 +5,17 @@
...
@@ -5,6 +5,17 @@
"module"
:
"esnext"
,
"module"
:
"esnext"
,
"target"
:
"ES2017"
,
"target"
:
"ES2017"
,
"esModuleInterop"
:
true
,
"esModuleInterop"
:
true
,
"jsx"
:
"react"
"jsx"
:
"react"
,
"strictNullChecks"
:
true
,
"noImplicitAny"
:
true
,
"baseUrl"
:
"../.."
,
"paths"
:
{
"@babel/types"
:
[
"babel-types/index.d.ts"
,
"@types/babel-types/index.d.ts"
,
"node_modules/@types/babel-types/index.d.ts"
,
"packages/next/node_modules/@types/babel-types/index.d.ts"
]
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment