|
| 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 | +}; |
0 commit comments