Skip to content

Commit e93c20f

Browse files
committed
feat(server): add server command
1 parent f016a5f commit e93c20f

File tree

9 files changed

+143
-1
lines changed

9 files changed

+143
-1
lines changed

examples/server/dist/bundle.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/server/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<html>
2+
3+
<head>
4+
<meta charset="utf-8">
5+
<title>YKit Pack-CommonJs Test</title>
6+
</head>
7+
8+
<body>
9+
<h1 id="ykit">YKit Pack-CommonJs Test</h1>
10+
<script src="//localhost:8080/dist/bundle.js"></script>
11+
</body>
12+
13+
</html>

examples/server/src/entry.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var m1Log = require('./module1')
2+
var m2Log = require('./module2')
3+
4+
document.write("<h2>require following modules:</h2>")
5+
m1Log()
6+
m2Log()

examples/server/src/module1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function () {
2+
document.write("<h3>module 1</h3>")
3+
}

examples/server/src/module2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function () {
2+
document.write("<h3>module 2</h3>")
3+
}

examples/server/ykit.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"left-pad": "^1.1.0",
1717
"optimist": "^0.6.1",
1818
"right-pad": "^1.0.0",
19-
"webpack": "^1.13.1"
19+
"webpack": "^1.13.1",
20+
"webpack-dev-server": "^1.14.1"
2021
},
2122
"repository": {
2223
"type": "git",
@@ -28,6 +29,7 @@
2829
"babel-preset-es2015": "^6.9.0",
2930
"chai": "^3.5.0",
3031
"mocha": "^2.5.3",
32+
"pm2": "^1.1.3",
3133
"shelljs": "^0.7.0"
3234
}
3335
}

src/commands/server.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
var webpack = require('webpack')
2+
var WebpackDevServer = require("webpack-dev-server");
3+
4+
exports.usage = "开启本地服务";
5+
6+
exports.setOptions = (optimist) => {
7+
optimist.alias('h', 'hot');
8+
optimist.describe('h', '热加载');
9+
10+
optimist.alias('s', 'https');
11+
optimist.describe('s', '使用https协议');
12+
13+
optimist.alias('p', 'port');
14+
optimist.describe('p', '端口');
15+
};
16+
17+
exports.run = function(options) {
18+
var cmdExecutedPath = process.cwd()
19+
20+
var compiler = webpack({
21+
context: cmdExecutedPath,
22+
entry: "./src/entry.js",
23+
output: {
24+
path: "./dist",
25+
filename: "bundle.js"
26+
},
27+
module: {
28+
loaders: [{
29+
test: /\.js$/,
30+
exclude: /(node_modules|bower_components)/,
31+
loader: 'babel',
32+
query: {
33+
presets: ['es2015']
34+
}
35+
}]
36+
}
37+
});
38+
39+
var defaultOptions = {
40+
h: false,
41+
s: false,
42+
p: 8080,
43+
}
44+
options = Object.assign(defaultOptions, options);
45+
46+
var server = new WebpackDevServer(compiler, {
47+
// webpack-dev-server options
48+
49+
contentBase: cmdExecutedPath,
50+
// or: contentBase: "http://localhost/",
51+
52+
hot: options.h,
53+
// Enable special support for Hot Module Replacement
54+
// Page is no longer updated, but a "webpackHotUpdate" message is send to the content
55+
// Use "webpack/hot/dev-server" as additional module in your entry point
56+
// Note: this does _not_ add the `HotModuleReplacementPlugin` like the CLI option does.
57+
58+
// Set this as true if you want to access dev server from arbitrary url.
59+
// This is handy if you are using a html5 router.
60+
historyApiFallback: false,
61+
62+
// Set this if you want to enable gzip compression for assets
63+
compress: false,
64+
65+
// Set this if you want webpack-dev-server to delegate a single path to an arbitrary server.
66+
// Use "*" to proxy all paths to the specified server.
67+
// This is useful if you want to get rid of 'http://localhost:8080/' in script[src],
68+
// and has many other use cases (see https://github.com/webpack/webpack-dev-server/pull/127 ).
69+
// proxy: {
70+
// "*": "http://localhost:9090"
71+
// },
72+
73+
// pass [static options](http://expressjs.com/en/4x/api.html#express.static) to inner express server
74+
staticOptions: {},
75+
76+
// webpack-dev-middleware options
77+
https: options.s,
78+
quiet: false,
79+
noInfo: false,
80+
lazy: true,
81+
filename: "bundle.js",
82+
watchOptions: {
83+
aggregateTimeout: 300,
84+
poll: 1000
85+
},
86+
publicPath: "/",
87+
headers: {
88+
"X-Custom-Header": "yes"
89+
},
90+
stats: {
91+
colors: true
92+
}
93+
});
94+
95+
console.log('YKit server running on ' + options.p + '...');
96+
server.listen(options.p, "localhost", function() {});
97+
98+
// server.close();
99+
};

test/server.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require('shelljs/global')
2+
var pm2 = require('pm2');
3+
4+
var assert = require('chai').assert;
5+
6+
describe('server command', function() {
7+
this.timeout(8000);
8+
9+
afterEach(function() {
10+
cd('../../')
11+
})
12+
13+
// TODO use pm2 test server
14+
});

0 commit comments

Comments
 (0)