Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: switch over to webpack-dev-server #105

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ const cli = meow(`
},
port: {
type: 'string',
alias: 'p'
alias: 'p',
default: '8080'
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed to supply these because i think webpack-serve was previously providing this default

},
host: {
type: 'string',
alias: 'h',
default: '0.0.0.0'
},
analyze: {},
// build
Expand Down Expand Up @@ -145,8 +151,7 @@ switch (cmd) {
const dev = require('./lib/dev')
dev(opts)
.then(({ server }) => {
const { port } = server.options
const url = `http://localhost:${port}`
const url = `http://${server.host}:${server.port}`
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now we report the listening host interface

log.stop(
'dev server listening on',
chalk.green(url),
Expand Down
78 changes: 47 additions & 31 deletions lib/dev.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const serve = require('webpack-serve')
const WebpackDevServer = require("webpack-dev-server")
const history = require('connect-history-api-fallback')
const convert = require('koa-connect')
const MiniHTMLWebpackPlugin = require('mini-html-webpack-plugin')
const merge = require('webpack-merge')

const baseConfig = require('./config')
const createTemplate = require('./createTemplate')

const dev = {
hot: true,
logLevel: 'error',
clientLogLevel: 'none',
stats: 'errors-only'
}

module.exports = async (opts) => {
if (opts.basename) delete opts.basename
const config = merge(baseConfig, opts.webpack)
const config = merge(baseConfig,
{devServer: {
hot: true,
port: opts.port,
host: opts.host
}},opts.webpack)
const template = createTemplate(opts)

config.mode = 'development'
config.context = opts.dirname
config.entry = opts.entry || path.join(__dirname, '../src/entry')
config.entry = opts.entry || path.join(__dirname, '../src/entry');
config.devtool = 'source-map';

config.output = {
path: path.join(process.cwd(), 'dev'),
filename: 'dev.js',
Expand Down Expand Up @@ -53,12 +52,15 @@ module.exports = async (opts) => {
})
)

config.plugins.push(
config.plugins = config.plugins.concat([
new MiniHTMLWebpackPlugin({
context: opts,
template
})
)
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
])

if (opts.analyze) {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
Expand All @@ -77,25 +79,39 @@ module.exports = async (opts) => {
// todo: enable other logging
}

const serveOpts = {
config,
dev,
logLevel: 'error',
content: opts.dirname,
port: opts.port,
hot: { logLevel: 'error' },
add: (app, middleware, options) => {
app.use(convert(history({})))
const serveOpts = Object.assign({}, config.devServer, {
hot: true,

stats: {
colors: true
},

logLevel: opts.debug && 'info' || 'error',
clientLogLevel: opts.debug && 'info' || 'error',
contentBase: opts.dirname,

before (app) {
app.use(history({}));
}
}
});

return new Promise((resolve, reject) => {
serve(serveOpts)
.then(server => {
server.compiler.hooks.done.tap({ name: 'x0' }, (stats) => {
resolve({ server, stats })
})
})
.catch(reject)

WebpackDevServer.addDevServerEntrypoints(config, serveOpts);
const compiler = webpack(config);
const {port, host} = serveOpts;

const server = new WebpackDevServer(compiler, serveOpts)
server.listen(port, host, (err) => {
if (err) {
reject(err);
return;
}
compiler.hooks.done
.tap({ name: 'x0' }, (stats) => {
const server = {port, host};
resolve({ server, stats })
})
});
})
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"x0": "cli.js"
},
"scripts": {
"start": "./cli.js docs",
"build": "./cli.js build docs",
"start": "node ./cli.js docs --debug",
"debug": "node --nolazy --inspect-brk=5858 ./cli.js docs",
"build": "node ./cli.js build docs",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on windows 10, (powershell/cmd.exe) hashbangs have no effect.

"test": "nyc ava --timeout=60s",
"test:components": "nyc ava test/components.js",
"cover": "nyc report --reporter=html --reporter=lcov"
Expand Down Expand Up @@ -53,7 +54,6 @@
"glamor": "^2.20.40",
"gray-matter": "^4.0.1",
"html-minifier": "^3.5.16",
"koa-connect": "^2.0.1",
"lodash.sortby": "^4.7.0",
"meow": "^5.0.0",
"mini-html-webpack-plugin": "^0.2.3",
Expand All @@ -63,7 +63,7 @@
"react": "^16.4.1",
"react-dev-utils": "^5.0.1",
"react-dom": "^16.4.1",
"react-live": "^1.10.1",
"react-live": "1.8.0-0",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-scope-provider": "^1.0.0-1",
Expand All @@ -81,7 +81,7 @@
"webpack": "^4.10.2",
"webpack-bundle-analyzer": "^2.13.1",
"webpack-merge": "^4.1.3",
"webpack-serve": "^1.0.4"
"webpack-dev-server": "3.1.9"
},
"devDependencies": {
"@compositor/logo": "^1.4.0",
Expand Down
2 changes: 1 addition & 1 deletion test/snapshots/build.js.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `test/build.js`
# Snapshot report for `test\build.js`

The actual snapshot is saved in `build.js.snap`.

Expand Down
Binary file modified test/snapshots/build.js.snap
Binary file not shown.
3 changes: 2 additions & 1 deletion test/snapshots/components.js.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `test/components.js`
# Snapshot report for `test\components.js`

The actual snapshot is saved in `components.js.snap`.

Expand Down Expand Up @@ -94,6 +94,7 @@ Generated by [AVA](https://ava.li).
>
<div
className="react-live"
scope={{}}
>
<div
className="react-live-preview"
Expand Down
Binary file modified test/snapshots/components.js.snap
Binary file not shown.
2 changes: 1 addition & 1 deletion test/snapshots/template.js.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Snapshot report for `test/template.js`
# Snapshot report for `test\template.js`

The actual snapshot is saved in `template.js.snap`.

Expand Down
Binary file modified test/snapshots/template.js.snap
Binary file not shown.