正在显示
7 个修改的文件
包含
131 行增加
和
0 行删除
.gitignore
0 → 100644
1 | +node_modules |
app.js
0 → 100644
app/middleware/webpack-dev-middleware.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +const webpack = require('webpack') | ||
4 | +const webpackMiddleware = require('../../lib/webpack-middleware') | ||
5 | + | ||
6 | +module.exports = function (options, app) { | ||
7 | + // 获取配置文件路径 | ||
8 | + const configPath = options.config | ||
9 | + // 读取配置文件 | ||
10 | + const webpackConfig = require(configPath) | ||
11 | + | ||
12 | + // 构造中间件generator | ||
13 | + return webpackMiddleware(webpack(webpackConfig), { | ||
14 | + noInfo: options.noInfo, | ||
15 | + quiet: options.quiet, | ||
16 | + lazy: options.lazy, | ||
17 | + watchOptions: options.watchOptions, | ||
18 | + publicPath: options.publicPath || webpackConfig.output.publicPath, | ||
19 | + headers: options.headers, | ||
20 | + stats: options.stats, | ||
21 | + hotConfig: options.hotConfig | ||
22 | + }, { | ||
23 | + waitUntilValid: function () { | ||
24 | + app.logger.info('[plugin:webpackDevMiddleware] webpack dev server ready') | ||
25 | + } | ||
26 | + }) | ||
27 | +} |
config/config.default.js
0 → 100644
1 | +/* eslint valid-jsdoc: "off" */ | ||
2 | +'use strict'; | ||
3 | +module.exports = webpackDevMiddleware => { | ||
4 | + | ||
5 | + /** | ||
6 | + * 默认配置 | ||
7 | + */ | ||
8 | + const config = exports = { | ||
9 | + // config: 'path/to/webpack/config/file', | ||
10 | + publicPath: '/',// 绑定中间件的公共路径 | ||
11 | + noInfo: false,// 显示无信息到控制台(仅警告和错误) | ||
12 | + quiet: false,// 向控制台显示任何内容 | ||
13 | + lazy: false,// 切换到延迟模式 这意味着没有观看,而是重新编译每个请求 | ||
14 | + watchOptions: { | ||
15 | + aggregateTimeout: 300, | ||
16 | + poll: true | ||
17 | + },// watch options (only lazy: false) | ||
18 | + headers: { 'X-Custom-Header': 'yes' },// 自定义标题 | ||
19 | + stats: {// 用于形成统计信息的选项 | ||
20 | + colors: true, | ||
21 | + chunks: false | ||
22 | + }, | ||
23 | + hotConfig: { | ||
24 | + publicPath: '/',// 绑定中间件的公共路径 | ||
25 | + // path: '__webpack_hmr', | ||
26 | + log: false, | ||
27 | + heartbeat: 2000 | ||
28 | + } | ||
29 | + }; | ||
30 | + return { | ||
31 | + ...config | ||
32 | + }; | ||
33 | +}; | ||
34 | + |
lib/webpack-middleware.js
0 → 100644
1 | +'use strict'; | ||
2 | + | ||
3 | +const expressMiddleware = require('webpack-dev-middleware') | ||
4 | +const webpackHotMiddleware = require('webpack-hot-middleware') | ||
5 | + | ||
6 | +function middleware(doIt, req, res) { | ||
7 | + const originalEnd = res.end | ||
8 | + | ||
9 | + return function (done) { | ||
10 | + res.end = function () { | ||
11 | + originalEnd.apply(this, arguments); | ||
12 | + done(null, 0) | ||
13 | + } | ||
14 | + doIt(req, res, function () { | ||
15 | + done(null, 1) | ||
16 | + }) | ||
17 | + } | ||
18 | +} | ||
19 | + | ||
20 | +module.exports = function (compiler, option, callbacks) { | ||
21 | + const doIt = expressMiddleware(compiler, option); | ||
22 | + var action = webpackHotMiddleware(compiler, option.hotConfig); | ||
23 | + for (const key in callbacks) { | ||
24 | + const exist = doIt[key] && typeof doIt[key] === 'function' | ||
25 | + exist && doIt[key].call(doIt, callbacks[key]) | ||
26 | + } | ||
27 | + | ||
28 | + return function* (next) { | ||
29 | + const ctx = this | ||
30 | + const req = this.req | ||
31 | + const res = this.res | ||
32 | + const runNext = yield middleware(doIt, req, { | ||
33 | + end: function (content) { | ||
34 | + ctx.body = content | ||
35 | + }, | ||
36 | + setHeader: function () { | ||
37 | + ctx.set.apply(ctx, arguments) | ||
38 | + }, | ||
39 | + }); | ||
40 | + const nextStep = yield middleware(action, req, res); | ||
41 | + if (runNext && nextStep && next) { | ||
42 | + yield* next | ||
43 | + } | ||
44 | + } | ||
45 | +} |
package-lock.json
0 → 100644
此 diff 太大无法显示。
package.json
0 → 100644
1 | +{ | ||
2 | + "name": "alice-webpack-dev-middleware", | ||
3 | + "version": "1.0.0", | ||
4 | + "description": "", | ||
5 | + "private": true, | ||
6 | + "eggPlugin": { | ||
7 | + "name": "webpackDevMiddleware" | ||
8 | + }, | ||
9 | + "engines": { | ||
10 | + "node": ">=10.0.0" | ||
11 | + }, | ||
12 | + "author": "", | ||
13 | + "license": "MIT", | ||
14 | + "dependencies": { | ||
15 | + "webpack": "^4.42.0", | ||
16 | + "webpack-dev-middleware": "^3.7.2", | ||
17 | + "webpack-hot-middleware": "^2.25.0" | ||
18 | + } | ||
19 | +} |
请
注册
或
登录
后发表评论