Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
293 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,7 @@ | ||
'use strict'; | ||
const server = require('./lib/service/server'); | ||
const WebpackServer = require('./lib/server'); | ||
module.exports = agent => { | ||
const config = agent.config.webpack; | ||
const port = config.port; | ||
if (config.clientConfig) { | ||
server(agent, { | ||
port, | ||
hot: true, | ||
type: 'client', | ||
webpackConfig: config.clientConfig, | ||
}); | ||
} | ||
if (config.serverConfig) { | ||
server(agent, { | ||
port: port + 1, | ||
type: 'server', | ||
hot: false, | ||
webpackConfig: config.serverConfig, | ||
}); | ||
} | ||
|
||
let buildServer = config.buildServer; | ||
if (buildServer) { | ||
buildServer = Array.isArray(buildServer) ? buildServer : [buildServer]; | ||
buildServer.forEach((item, index) => { | ||
server(agent, Object.assign({ port: port + 2 + index }, item)); | ||
}); | ||
} | ||
new WebpackServer(agent, config).start(); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,19 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
module.exports = app => { | ||
module.exports = () => { | ||
const config = {}; | ||
|
||
/** | ||
* webpack build config | ||
* @property {Number} port - webpack dev server port | ||
* @property {Object} clientConfig - webpack client(browser run) building config | ||
* @property {Object} serverConfig - webpack server(node run) building config | ||
* @property {Object | Array} [buildServer] - start custom webpack server | ||
* @property {Boolean} single - webpack dev server number | ||
* @property {Array} [webpackConfigList] - webpack building config | ||
*/ | ||
config.webpack = { | ||
port: 8090, | ||
webpackConfigList: [], | ||
}; | ||
|
||
const clientConfigPath = path.join(app.baseDir, 'build/client'); | ||
|
||
if (fs.existsSync(`${clientConfigPath}.js`) || fs.existsSync(`${clientConfigPath}/index.js`)) { | ||
config.webpack.clientConfig = require(path.join(app.baseDir, 'build/client')); | ||
} | ||
|
||
const serverConfigPath = path.join(app.baseDir, 'build/server'); | ||
|
||
if (fs.existsSync(`${serverConfigPath}.js`) || fs.existsSync(`${serverConfigPath}/index.js`)) { | ||
config.webpack.serverConfig = require(path.join(app.baseDir, 'build/server')); | ||
} | ||
|
||
return config; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
module.exports = { | ||
EVENT_WEBPACK_BUILD_STATE: 'webpack_build_state', | ||
EVENT_WEBPACK_READ_FILE_MEMORY: 'webpack_read_file_memory', | ||
EVENT_WEBPACK_READ_FILE_MEMORY_CONTENT: 'webpack_read_file_memory_content', | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
const Constant = require('./constant'); | ||
|
||
class FileSystem { | ||
|
||
constructor(app) { | ||
this.app = app; | ||
} | ||
|
||
readWebpackMemoryFile(filePath, fileName) { | ||
return new Promise(resolve => { | ||
this.app.messenger.sendToAgent(Constant.EVENT_WEBPACK_READ_FILE_MEMORY, { | ||
filePath, | ||
fileName, | ||
}); | ||
this.app.messenger.on(Constant.EVENT_WEBPACK_READ_FILE_MEMORY_CONTENT, data => { | ||
resolve(data.fileContent); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = FileSystem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
'use strict'; | ||
exports.Constant = require('./service/constant'); | ||
exports.FileSystem = require('./service/filesystem'); | ||
exports.Constant = require('./constant'); | ||
exports.FileSystem = require('./filesystem'); | ||
exports['koa-webpack-hot-middleware'] = require('koa-webpack-dev-middleware'); | ||
exports['koa-webpack-dev-middleware'] = require('koa-webpack-dev-middleware'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
'use strict'; | ||
|
||
const webpack = require('webpack'); | ||
const koa = require('koa'); | ||
const cors = require('kcors'); | ||
const Utils = require('./utils'); | ||
const app = koa(); | ||
app.use(cors()); | ||
|
||
const msg_build_state = 'webpack_build_state'; | ||
const msg_build_success = 'webpack_build_success'; | ||
const msg_read_file_memory = 'webpack_read_file_memory'; | ||
const msg_read_file_memory_content = 'webpack_read_file_memory_content'; | ||
|
||
class WebpackServer { | ||
constructor(agent, config) { | ||
this.agent = agent; | ||
this.config = config; | ||
this.buildCount = 0; | ||
this.compilerCount = this.config.webpackConfigList.length; | ||
} | ||
|
||
start() { | ||
const compilers = []; | ||
this.config.webpackConfigList.forEach((webpackConfig, index) => { | ||
const compiler = this.build(webpackConfig); | ||
this.server(compiler, { | ||
hot: webpackConfig.target !== 'node', | ||
port: this.config.port + index, | ||
publicPath: webpackConfig.output.publicPath, | ||
}); | ||
compilers.push(compiler); | ||
}); | ||
this.listen(compilers); | ||
} | ||
|
||
state() { | ||
return this.buildCount % this.compilerCount === 0; | ||
} | ||
|
||
|
||
build(webpackConfig) { | ||
const compiler = webpack([webpackConfig]); | ||
compiler.plugin('done', compilation => { | ||
this.buildCount++; | ||
compilation.stats.forEach(stat => { | ||
stat.compilation.children = stat.compilation.children.filter(child => { | ||
return child.name !== 'extract-text-webpack-plugin'; | ||
}); | ||
}); | ||
this.agent.messenger.sendToApp(msg_build_state, { state: this.state() }); | ||
this.agent[msg_build_success] = true; | ||
}); | ||
return compiler; | ||
} | ||
|
||
listen(compilers) { | ||
|
||
this.agent.messenger.on(msg_build_state, () => { | ||
this.agent.messenger.sendToApp(msg_build_state, { state: this.state() }); | ||
}); | ||
|
||
this.agent.messenger.on(msg_read_file_memory, data => { | ||
const fileContent = Utils.readWebpackMemoryFile(compilers, data.filePath); | ||
this.agent.messenger.sendToApp(msg_read_file_memory_content, { | ||
fileContent, | ||
}); | ||
}); | ||
} | ||
|
||
server(compiler, config) { | ||
const port = config.port; | ||
const hot = config.hot; | ||
const publicPath = config.publicPath; | ||
const devMiddleware = require('koa-webpack-dev-middleware')(compiler, { | ||
publicPath, | ||
stats: { | ||
colors: true, | ||
children: true, | ||
modules: false, | ||
chunks: false, | ||
chunkModules: false, | ||
}, | ||
watchOptions: { | ||
ignored: /node_modules/, | ||
}, | ||
}); | ||
|
||
app.use(devMiddleware); | ||
|
||
if (hot === undefined || hot) { | ||
const hotMiddleware = require('koa-webpack-hot-middleware')(compiler, { | ||
log: false, | ||
reload: true, | ||
}); | ||
app.use(hotMiddleware); | ||
} | ||
|
||
app.use(function *(next) { | ||
if (this.url === '/') { | ||
this.body = 'webpack build service'; | ||
} else { | ||
yield next; | ||
} | ||
}); | ||
|
||
app.listen(port, err => { | ||
if (!err) { | ||
this.agent.logger.info(`start webpack build server: http://127.0.0.1:${port}`); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = WebpackServer; |
Oops, something went wrong.