Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CoCo_Code_Op2
next.js
提交
07b95ae0
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,发现更多精彩内容 >>
提交
07b95ae0
编写于
10月 24, 2016
作者:
N
nkzawa
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dynamically add/remove pages
上级
ec774a39
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
204 addition
and
8 deletion
+204
-8
server/build/plugins/dynamic-entry-plugin.js
server/build/plugins/dynamic-entry-plugin.js
+62
-0
server/build/plugins/unlink-file-plugin.js
server/build/plugins/unlink-file-plugin.js
+28
-0
server/build/plugins/watch-pages-plugin.js
server/build/plugins/watch-pages-plugin.js
+48
-0
server/build/plugins/watch-remove-event-plugin.js
server/build/plugins/watch-remove-event-plugin.js
+40
-0
server/build/webpack.js
server/build/webpack.js
+19
-8
server/hot-reloader.js
server/hot-reloader.js
+7
-0
未找到文件。
server/build/plugins/dynamic-entry-plugin.js
0 → 100644
浏览文件 @
07b95ae0
import
SingleEntryPlugin
from
'
webpack/lib/SingleEntryPlugin
'
import
MultiEntryPlugin
from
'
webpack/lib/MultiEntryPlugin
'
export
default
class
DynamicEntryPlugin
{
apply
(
compiler
)
{
compiler
.
entryNames
=
getInitialEntryNames
(
compiler
)
compiler
.
addEntry
=
addEntry
compiler
.
removeEntry
=
removeEntry
compiler
.
hasEntry
=
hasEntry
compiler
.
createCompilation
=
createCompilation
(
compiler
.
createCompilation
)
}
}
function
getInitialEntryNames
(
compiler
)
{
const
entryNames
=
new
Set
()
const
{
entry
}
=
compiler
.
options
if
(
typeof
entry
===
'
string
'
||
Array
.
isArray
(
entry
))
{
entryNames
.
add
(
'
main
'
)
}
else
if
(
typeof
entry
===
'
object
'
)
{
Object
.
keys
(
entry
).
forEach
((
name
)
=>
{
entryNames
.
add
(
name
)
})
}
return
entryNames
}
function
addEntry
(
entry
,
name
=
'
main
'
)
{
const
{
context
}
=
this
.
options
const
Plugin
=
Array
.
isArray
(
entry
)
?
MultiEntryPlugin
:
SingleEntryPlugin
this
.
apply
(
new
Plugin
(
context
,
entry
,
name
))
this
.
entryNames
.
add
(
name
)
}
function
removeEntry
(
name
=
'
main
'
)
{
this
.
entryNames
.
delete
(
name
)
}
function
hasEntry
(
name
=
'
main
'
)
{
this
.
entryNames
.
has
(
name
)
}
function
createCompilation
(
original
)
{
return
function
(...
args
)
{
const
compilation
=
original
.
apply
(
this
,
args
)
compilation
.
addEntry
=
compilationAddEntry
(
compilation
.
addEntry
)
return
compilation
}
}
function
compilationAddEntry
(
original
)
{
return
function
(
context
,
entry
,
name
,
callback
)
{
if
(
!
this
.
compiler
.
entryNames
.
has
(
name
))
{
// skip removed entry
callback
()
return
}
return
original
.
call
(
this
,
context
,
entry
,
name
,
callback
)
}
}
server/build/plugins/unlink-file-plugin.js
0 → 100644
浏览文件 @
07b95ae0
import
{
join
}
from
'
path
'
import
{
unlink
}
from
'
mz/fs
'
export
default
class
UnlinkFilePlugin
{
constructor
()
{
this
.
prevAssets
=
{}
}
apply
(
compiler
)
{
compiler
.
plugin
(
'
after-emit
'
,
(
compilation
,
callback
)
=>
{
const
removed
=
Object
.
keys
(
this
.
prevAssets
)
.
filter
((
a
)
=>
!
compilation
.
assets
[
a
])
this
.
prevAssets
=
compilation
.
assets
Promise
.
all
(
removed
.
map
(
async
(
f
)
=>
{
const
path
=
join
(
compiler
.
outputPath
,
f
)
try
{
await
unlink
(
path
)
}
catch
(
err
)
{
if
(
err
.
code
===
'
ENOENT
'
)
return
throw
err
}
}))
.
then
(()
=>
callback
(),
callback
)
})
}
}
server/build/plugins/watch-pages-plugin.js
0 → 100644
浏览文件 @
07b95ae0
import
{
resolve
,
relative
,
join
,
extname
}
from
'
path
'
export
default
class
WatchPagesPlugin
{
constructor
(
dir
)
{
this
.
dir
=
resolve
(
dir
,
'
pages
'
)
}
apply
(
compiler
)
{
compiler
.
plugin
(
'
emit
'
,
(
compilation
,
callback
)
=>
{
// watch the pages directory
compilation
.
contextDependencies
=
compilation
.
contextDependencies
.
concat
([
this
.
dir
])
callback
()
})
const
isPageFile
=
this
.
isPageFile
.
bind
(
this
)
const
getEntryName
=
(
f
)
=>
{
return
join
(
'
bundles
'
,
relative
(
compiler
.
options
.
context
,
f
))
}
compiler
.
plugin
(
'
watch-run
'
,
(
watching
,
callback
)
=>
{
Object
.
keys
(
compiler
.
fileTimestamps
)
.
filter
(
isPageFile
)
.
forEach
((
f
)
=>
{
const
name
=
getEntryName
(
f
)
if
(
compiler
.
hasEntry
(
name
))
return
const
entries
=
[
'
webpack/hot/only-dev-server
'
,
f
]
compiler
.
addEntry
(
entries
,
name
)
})
compiler
.
removedFiles
.
filter
(
isPageFile
)
.
forEach
((
f
)
=>
{
const
name
=
getEntryName
(
f
)
compiler
.
removeEntry
(
name
)
})
callback
()
})
}
isPageFile
(
f
)
{
return
f
.
indexOf
(
this
.
dir
)
===
0
&&
extname
(
f
)
===
'
.js
'
}
}
server/build/plugins/watch-remove-event-plugin.js
0 → 100644
浏览文件 @
07b95ae0
// watch and trigger file remove event
// see: https://github.com/webpack/webpack/issues/1533
export
default
class
WatchRemoveEventPlugin
{
constructor
()
{
this
.
removedFiles
=
[]
}
apply
(
compiler
)
{
compiler
.
removedFiles
=
[]
compiler
.
plugin
(
'
environment
'
,
()
=>
{
if
(
!
compiler
.
watchFileSystem
)
return
const
{
watchFileSystem
}
=
compiler
const
{
watch
}
=
watchFileSystem
watchFileSystem
.
watch
=
(
files
,
dirs
,
missing
,
startTime
,
options
,
callback
,
callbackUndelayed
)
=>
{
const
result
=
watch
.
call
(
watchFileSystem
,
files
,
dirs
,
missing
,
startTime
,
options
,
(...
args
)
=>
{
compiler
.
removedFiles
=
this
.
removedFiles
this
.
removedFiles
=
[]
callback
(...
args
)
},
callbackUndelayed
)
const
watchpack
=
watchFileSystem
.
watcher
watchpack
.
fileWatchers
.
forEach
((
w
)
=>
{
w
.
on
(
'
remove
'
,
this
.
onRemove
.
bind
(
this
,
watchpack
,
w
.
path
))
})
return
result
}
})
}
onRemove
(
watchpack
,
file
)
{
this
.
removedFiles
.
push
(
file
)
watchpack
.
emit
(
'
remove
'
,
file
)
watchpack
.
_onChange
(
file
)
}
}
server/build/webpack.js
浏览文件 @
07b95ae0
...
...
@@ -2,6 +2,10 @@ import { resolve, join } from 'path'
import
webpack
from
'
webpack
'
import
glob
from
'
glob-promise
'
import
WriteFilePlugin
from
'
write-file-webpack-plugin
'
import
UnlinkFilePlugin
from
'
./plugins/unlink-file-plugin
'
import
WatchPagesPlugin
from
'
./plugins/watch-pages-plugin
'
import
WatchRemoveEventPlugin
from
'
./plugins/watch-remove-event-plugin
'
import
DynamicEntryPlugin
from
'
./plugins/dynamic-entry-plugin
'
export
default
async
function
createCompiler
(
dir
,
{
hotReload
=
false
}
=
{})
{
dir
=
resolve
(
dir
)
...
...
@@ -27,17 +31,24 @@ export default async function createCompiler (dir, { hotReload = false } = {}) {
const
nodeModulesDir
=
join
(
__dirname
,
'
..
'
,
'
..
'
,
'
..
'
,
'
node_modules
'
)
const
plugins
=
[
hotReload
?
new
webpack
.
HotModuleReplacementPlugin
()
:
new
webpack
.
optimize
.
UglifyJsPlugin
({
compress
:
{
warnings
:
false
},
sourceMap
:
false
}),
new
WriteFilePlugin
({
exitOnErrors
:
false
,
log
:
false
log
:
false
,
// required not to cache removed files
useHashIndex
:
false
})
].
concat
(
hotReload
?
[
new
webpack
.
HotModuleReplacementPlugin
(),
new
DynamicEntryPlugin
(),
new
UnlinkFilePlugin
(),
new
WatchRemoveEventPlugin
(),
new
WatchPagesPlugin
(
dir
)
]
:
[
new
webpack
.
optimize
.
UglifyJsPlugin
({
compress
:
{
warnings
:
false
},
sourceMap
:
false
})
]
]
)
const
babelRuntimePath
=
require
.
resolve
(
'
babel-runtime/package
'
)
.
replace
(
/
[\\\/]
package
\.
json$/
,
''
)
...
...
server/hot-reloader.js
浏览文件 @
07b95ae0
...
...
@@ -9,6 +9,7 @@ export default class HotReloader {
this
.
server
=
null
this
.
stats
=
null
this
.
compilationErrors
=
null
this
.
prevAssets
=
{}
}
async
start
()
{
...
...
@@ -25,6 +26,12 @@ export default class HotReloader {
for
(
const
f
of
Object
.
keys
(
assets
))
{
deleteCache
(
assets
[
f
].
existsAt
)
}
for
(
const
f
of
Object
.
keys
(
this
.
prevAssets
))
{
if
(
!
assets
[
f
])
{
deleteCache
(
this
.
prevAssets
[
f
].
existsAt
)
}
}
this
.
prevAssets
=
assets
callback
()
})
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录