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
21e1db7b
Unverified
Commit
21e1db7b
authored
Dec 03, 2019
by
Joe Haddad
Committed by
GitHub
Dec 03, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Aggressively Replace Constants (#9610)
* Aggressively Replace Constants * Test both windows * add other switch
parent
fcea1cd8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
206 additions
and
1 deletion
+206
-1
webpack-config.ts
packages/next/build/webpack-config.ts
+1
-0
next-babel-loader.js
packages/next/build/webpack/loaders/next-babel-loader.js
+9
-1
next-babel-loader.test.js
test/unit/next-babel-loader.test.js
+196
-0
No files found.
packages/next/build/webpack-config.ts
View file @
21e1db7b
...
@@ -242,6 +242,7 @@ export default async function getBaseWebpackConfig(
...
@@ -242,6 +242,7 @@ export default async function getBaseWebpackConfig(
cache
:
true
,
cache
:
true
,
babelPresetPlugins
,
babelPresetPlugins
,
hasModern
:
!!
config
.
experimental
.
modern
,
hasModern
:
!!
config
.
experimental
.
modern
,
development
:
dev
,
},
},
},
},
// Backwards compat
// Backwards compat
...
...
packages/next/build/webpack/loaders/next-babel-loader.js
View file @
21e1db7b
...
@@ -58,6 +58,7 @@ module.exports = babelLoader.custom(babel => {
...
@@ -58,6 +58,7 @@ module.exports = babelLoader.custom(babel => {
pagesDir
:
opts
.
pagesDir
,
pagesDir
:
opts
.
pagesDir
,
hasModern
:
opts
.
hasModern
,
hasModern
:
opts
.
hasModern
,
babelPresetPlugins
:
opts
.
babelPresetPlugins
,
babelPresetPlugins
:
opts
.
babelPresetPlugins
,
development
:
opts
.
development
,
}
}
const
filename
=
join
(
opts
.
cwd
,
'noop.js'
)
const
filename
=
join
(
opts
.
cwd
,
'noop.js'
)
const
loader
=
Object
.
assign
(
const
loader
=
Object
.
assign
(
...
@@ -70,6 +71,7 @@ module.exports = babelLoader.custom(babel => {
...
@@ -70,6 +71,7 @@ module.exports = babelLoader.custom(babel => {
(
opts
.
isServer
?
'-server'
:
''
)
+
(
opts
.
isServer
?
'-server'
:
''
)
+
(
opts
.
isModern
?
'-modern'
:
''
)
+
(
opts
.
isModern
?
'-modern'
:
''
)
+
(
opts
.
hasModern
?
'-has-modern'
:
''
)
+
(
opts
.
hasModern
?
'-has-modern'
:
''
)
+
(
opts
.
development
?
'-development'
:
'-production'
)
+
JSON
.
stringify
(
JSON
.
stringify
(
babel
.
loadPartialConfig
({
babel
.
loadPartialConfig
({
filename
,
filename
,
...
@@ -91,6 +93,7 @@ module.exports = babelLoader.custom(babel => {
...
@@ -91,6 +93,7 @@ module.exports = babelLoader.custom(babel => {
delete
loader
.
hasModern
delete
loader
.
hasModern
delete
loader
.
pagesDir
delete
loader
.
pagesDir
delete
loader
.
babelPresetPlugins
delete
loader
.
babelPresetPlugins
delete
loader
.
development
return
{
loader
,
custom
}
return
{
loader
,
custom
}
},
},
config
(
config
(
...
@@ -103,6 +106,7 @@ module.exports = babelLoader.custom(babel => {
...
@@ -103,6 +106,7 @@ module.exports = babelLoader.custom(babel => {
hasModern
,
hasModern
,
pagesDir
,
pagesDir
,
babelPresetPlugins
,
babelPresetPlugins
,
development
,
},
},
}
}
)
{
)
{
...
@@ -175,7 +179,11 @@ module.exports = babelLoader.custom(babel => {
...
@@ -175,7 +179,11 @@ module.exports = babelLoader.custom(babel => {
options
.
plugins
.
push
([
options
.
plugins
.
push
([
require
.
resolve
(
'babel-plugin-transform-define'
),
require
.
resolve
(
'babel-plugin-transform-define'
),
{
'typeof window'
:
isServer
?
'undefined'
:
'object'
},
{
'process.env.NODE_ENV'
:
development
?
'development'
:
'production'
,
'typeof window'
:
isServer
?
'undefined'
:
'object'
,
'process.browser'
:
isServer
?
false
:
true
,
},
'next-js-transform-define-instance'
,
'next-js-transform-define-instance'
,
])
])
...
...
test/unit/next-babel-loader.test.js
0 → 100644
View file @
21e1db7b
/* eslint-env jest */
const
loader
=
require
(
'next/dist/build/webpack/loaders/next-babel-loader'
)
const
os
=
require
(
'os'
)
const
babel
=
async
(
code
,
{
isServer
=
false
,
resourcePath
=
'index.js'
,
hasModern
=
false
,
development
=
false
,
}
=
{}
)
=>
{
let
isAsync
=
false
return
new
Promise
((
resolve
,
reject
)
=>
{
function
callback
(
err
,
content
)
{
if
(
err
)
{
reject
(
err
)
}
else
{
resolve
(
content
)
}
}
const
res
=
loader
.
bind
({
resourcePath
,
async
()
{
isAsync
=
true
return
callback
},
callback
,
query
:
{
// babel opts
babelrc
:
false
,
configFile
:
false
,
sourceType
:
'module'
,
compact
:
true
,
caller
:
{
name
:
'tests'
,
supportsStaticESM
:
true
,
},
// loader opts
cwd
:
os
.
tmpdir
(),
isServer
,
distDir
:
os
.
tmpdir
(),
pagesDir
:
os
.
tmpdir
(),
cache
:
false
,
babelPresetPlugins
:
[],
hasModern
,
development
,
},
})(
code
,
null
)
if
(
!
isAsync
)
{
resolve
(
res
)
}
})
}
describe
(
'next-babel-loader'
,
()
=>
{
describe
(
'replace constants'
,
()
=>
{
it
(
'should replace typeof window expression nested'
,
async
()
=>
{
const
code
=
await
babel
(
'function a(){console.log(typeof window)}'
)
expect
(
code
).
toMatchInlineSnapshot
(
`"function a(){console.log(\\"object\\");}"`
)
})
it
(
'should replace typeof window expression top level (client)'
,
async
()
=>
{
const
code
=
await
babel
(
'typeof window;'
)
expect
(
code
).
toMatchInlineSnapshot
(
`"\\"object\\";"`
)
})
it
(
'should replace typeof window expression top level (server)'
,
async
()
=>
{
const
code
=
await
babel
(
'typeof window;'
,
{
isServer
:
true
})
expect
(
code
).
toMatchInlineSnapshot
(
`"\\"undefined\\";"`
)
})
it
(
'should replace typeof window expression nested'
,
async
()
=>
{
const
code
=
await
babel
(
`function a(){console.log(typeof window === 'undefined')}`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"function a(){console.log(false);}"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window === 'undefined';`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"false;"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window === 'object';`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"true;"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window !== 'undefined';`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"true;"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window !== 'object';`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"false;"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window !== 'undefined';`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"false;"`
)
})
it
(
'should replace typeof window expression top level'
,
async
()
=>
{
const
code
=
await
babel
(
`typeof window !== 'object';`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"true;"`
)
})
it
(
'should replace process.browser (1)'
,
async
()
=>
{
const
code
=
await
babel
(
`process.browser`
,
{
isServer
:
false
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"true;"`
)
})
it
(
'should replace process.browser (2)'
,
async
()
=>
{
const
code
=
await
babel
(
`process.browser`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"false;"`
)
})
it
(
'should replace process.browser (3)'
,
async
()
=>
{
const
code
=
await
babel
(
`process.browser == false`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"true;"`
)
})
it
(
'should replace process.browser (4)'
,
async
()
=>
{
const
code
=
await
babel
(
`if (process.browser === false) {}`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"if(true){}"`
)
})
it
(
'should replace process.browser (5)'
,
async
()
=>
{
const
code
=
await
babel
(
`if (process.browser) {}`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"if(false){}"`
)
})
it
(
'should replace NODE_ENV on client (dev)'
,
async
()
=>
{
const
code
=
await
babel
(
`process.env.NODE_ENV`
,
{
isServer
:
false
,
development
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"\\"development\\";"`
)
})
it
(
'should replace NODE_ENV on client (prod)'
,
async
()
=>
{
const
code
=
await
babel
(
`process.env.NODE_ENV`
,
{
isServer
:
false
,
development
:
false
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"\\"production\\";"`
)
})
it
(
'should replace NODE_ENV on server'
,
async
()
=>
{
const
code
=
await
babel
(
`process.env.NODE_ENV`
,
{
isServer
:
true
,
})
expect
(
code
).
toMatchInlineSnapshot
(
`"\\"production\\";"`
)
})
it
(
'should replace NODE_ENV in statement (dev)'
,
async
()
=>
{
const
code
=
await
babel
(
`if (process.env.NODE_ENV === 'development') {}`
,
{
development
:
true
}
)
expect
(
code
).
toMatchInlineSnapshot
(
`"if(true){}"`
)
})
it
(
'should replace NODE_ENV in statement (prod)'
,
async
()
=>
{
const
code
=
await
babel
(
`if (process.env.NODE_ENV === 'production') {}`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"if(true){}"`
)
})
it
(
'should replace NODE_ENV in statement (prod)'
,
async
()
=>
{
const
code
=
await
babel
(
`if (process.env.NODE_ENV !== 'production') {}`
)
expect
(
code
).
toMatchInlineSnapshot
(
`"if(false){}"`
)
})
})
})
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