Skip to content

Commit

Permalink
feat: dll and lib support
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli committed Feb 28, 2018
1 parent b50ebd9 commit bf0019c
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ app/web/store
plugins/
app/web/asset/
app/view/
test.js
test.js
script/webpack4/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# egg-vue-webpack-boilerplate

基于 Egg + Vue + Webpack3/Webpack2 多页面和单页面服务端客户端渲染同构工程骨架项目.
基于 Egg + Vue + Webpack4 多页面和单页面服务端客户端渲染同构工程骨架项目.

## 纯净版

Expand All @@ -14,7 +14,7 @@

- Egg 版本: ^2.x.x
- Node 版本: Node ^8.x.x+, Node 6.x.x 版本请见 [Egg 1.0 + Node6分支](https://github.com/hubcarl/egg-vue-webpack-boilerplate/tree/node6)
- Webpack 版本: ^3.8.1, 对应 `easywebpack-vue` 版本为 ^3.5.0
- Webpack 版本: ^4.x.x, 对应 `easywebpack-vue` 版本为 ^4.x.x
- Vue 版本: ^2.5.0

## 说明
Expand Down
9 changes: 1 addition & 8 deletions app/web/page/about/about.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,12 @@
</style>
<script type="text/babel">
import { Toast } from 'mint-ui';
//import 'mint-ui/lib/style.css';
export default {
components: {},
computed: {},
methods: {},
mounted() {
Toast({
message: 'Upload Complete',
position: 'bottom',
duration: 5000
});
}
mounted() {}
}
</script>

2 changes: 1 addition & 1 deletion app/web/page/index/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li class="tauthor">
<a href="#" target="_blank" class="get">Sky</a>
</li>
<li><a>+收藏</a></li>
<li><a>+收藏1111</a></li>
<li>
<span class="timeago">{{item.summary}}</span>
</li>
Expand Down
4 changes: 4 additions & 0 deletions config/config.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = app => {
injectCss: false
};

exports.webpack = {
webpackConfigList: require('easywebpack-vue').getWebpackConfig()
};

const localIP = ip.address();
const domainWhiteList = [];
[7001, 9000, 9001].forEach(port => {
Expand Down
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "3.6.2",
"description": "基于 easywebpack-vue 和 egg-view-vue-ssr插件的 egg + vue 服务端渲染同构工程骨架项目",
"scripts": {
"postinstall": "node ./script/postinstall.js",
"debug": "egg-bin debug",
"build": "cross-env easywebpack build prod",
"build:dev": "cross-env easywebpack build dev",
Expand All @@ -23,23 +24,21 @@
"dependencies": {
"axios": "^0.17.1",
"cross-env": "^5.0.0",
"easywebpack": "^4.0.0-beta.7",
"egg": "^2.1.0",
"egg-bin": "^4.3.7",
"egg-cors": "^2.0.0",
"egg-logger": "^1.5.0",
"egg-validate": "^1.0.0",
"egg-view-vue-ssr": "^3.0.2",
"element-ui": "^2.0.8",
"extend": "~3.0.0",
"lodash": "^4.17.4",
"mint-ui": "^2.2.9",
"mockjs": "^1.0.1-beta3",
"moment": "^2.17.1",
"server-side-render-resource": "^1.0.0",
"vue": "^2.5.0",
"vue-hot-reload-api": "^2.1.0",
"vue-router": "^3.0.1",
"vue-server-renderer": "^2.5.3",
"vue-template-compiler": "^2.5.13",
"vuex": "^3.0.1",
"vuex-router-sync": "^5.0.0"
},
Expand All @@ -54,12 +53,11 @@
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-env": "^1.6.0",
"easywebpack-vue": "next",
"egg-bin": "^4.3.7",
"egg-logview": "^1.0.0",
"egg-webpack": "next",
"egg-webpack-vue": "^2.0.0",
"eslint": "^4.3.0",
"eslint-config-egg": "^5.0.0",
"eslint-loader": "^1.9.0",
"eslint-plugin-vue": "^2.0.1",
"ip": "^1.1.5",
"less": "^2.7.2",
Expand Down
6 changes: 6 additions & 0 deletions script/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
const path = require('path');
const fs = require('fs');
const source = path.join(process.cwd(), 'script/webpack4/webpack-hot-middleware/middleware.js');
const target = path.join(process.cwd(), 'node_modules/webpack-hot-middleware/middleware.js');
fs.createReadStream(source).pipe(fs.createWriteStream(target));
128 changes: 128 additions & 0 deletions script/webpack4/webpack-hot-middleware/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
module.exports = webpackHotMiddleware;

var helpers = require('./helpers');
var pathMatch = helpers.pathMatch;

function webpackHotMiddleware(compiler, opts) {
opts = opts || {};
opts.log = typeof opts.log == 'undefined' ? console.log.bind(console) : opts.log;
opts.path = opts.path || '/__webpack_hmr';
opts.heartbeat = opts.heartbeat || 10 * 1000;

var eventStream = createEventStream(opts.heartbeat);
var latestStats = null;

if(compiler.hooks) {
compiler.hooks.run.tap("webpackHotMiddleware", function() {
latestStats = null;
if (opts.log) opts.log("webpack building...");
eventStream.publish({action: "building"});
});
compiler.hooks.done.tap("webpackHotMiddleware", function(statsResult) {
// Keep hold of latest stats so they can be propagated to new clients
latestStats = statsResult;
publishStats("built", latestStats, eventStream, opts.log);
});
} else {
compiler.plugin("compile", function() {
latestStats = null;
if (opts.log) opts.log("webpack building...");
eventStream.publish({action: "building"});
});
compiler.plugin("done", function(statsResult) {
// Keep hold of latest stats so they can be propagated to new clients
latestStats = statsResult;
publishStats("built", latestStats, eventStream, opts.log);
});
}
var middleware = function(req, res, next) {
if (!pathMatch(req.url, opts.path)) return next();
eventStream.handler(req, res);
if (latestStats) {
// Explicitly not passing in `log` fn as we don't want to log again on
// the server
publishStats("sync", latestStats, eventStream);
}
};
middleware.publish = eventStream.publish;
return middleware;
}

function createEventStream(heartbeat) {
var clientId = 0;
var clients = {};
function everyClient(fn) {
Object.keys(clients).forEach(function(id) {
fn(clients[id]);
});
}
setInterval(function heartbeatTick() {
everyClient(function(client) {
client.write("data: \uD83D\uDC93\n\n");
});
}, heartbeat).unref();
return {
handler: function(req, res) {
req.socket.setKeepAlive(true);
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream;charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
// While behind nginx, event stream should not be buffered:
// http://nginx.org/docs/http/ngx_http_proxy_module.html#proxy_buffering
'X-Accel-Buffering': 'no'
});
res.write('\n');
var id = clientId++;
clients[id] = res;
req.on("close", function(){
delete clients[id];
});
},
publish: function(payload) {
everyClient(function(client) {
client.write("data: " + JSON.stringify(payload) + "\n\n");
});
}
};
}

function publishStats(action, statsResult, eventStream, log) {
// For multi-compiler, stats will be an object with a 'children' array of stats
var bundles = extractBundles(statsResult.toJson({ errorDetails: false }));
bundles.forEach(function(stats) {
if (log) {
log("webpack built " + (stats.name ? stats.name + " " : "") +
stats.hash + " in " + stats.time + "ms");
}
eventStream.publish({
name: stats.name,
action: action,
time: stats.time,
hash: stats.hash,
warnings: stats.warnings || [],
errors: stats.errors || [],
modules: buildModuleMap(stats.modules)
});
});
}

function extractBundles(stats) {
// Stats has modules, single bundle
if (stats.modules) return [stats];

// Stats has children, multiple bundles
if (stats.children && stats.children.length) return stats.children;

// Not sure, assume single
return [stats];
}

function buildModuleMap(modules) {
var map = {};
modules.forEach(function(module) {
map[module.id] = module.name;
});
return map;
}
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = {
store: 'app/web/store',
vue: 'vue/dist/vue.js'
},
resolveLoader: {
modules: [ path.join(__dirname, 'node_modules/easywebpack-vue/node_modules') ]
},
dll: ['vue', 'axios', 'vue-router', 'vuex', 'vuex-router-sync'],
loaders: {},
plugins: {},
Expand Down

0 comments on commit bf0019c

Please sign in to comment.