Skip to content

[WIP] add webpack-dev-middleware #250

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 12 additions & 11 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const union = require('lodash.union')
const merge = require('lodash.merge')
const {accessSync} = require('fs')
const hygienist = require('hygienist-middleware')
const BrowserSyncPlugin = require('browser-sync-webpack-plugin')
const SpikePlugin = require('./plugin')
const SpikeUtils = require('spike-util')

Expand Down Expand Up @@ -63,7 +62,6 @@ module.exports = class Config {
module: Joi.object().default().keys({
rules: Joi.array().default([])
}),
devServer: Joi.func(),
server: Joi.object().default().keys({
watchOptions: Joi.object().default().keys({
ignored: Joi.array().default('node_modules')
Expand Down Expand Up @@ -141,9 +139,11 @@ module.exports = class Config {
res.server.files = [{
match: allWatchedFiles,
fn: (event, file) => {
console.log(event, file)
const util = new SpikeUtils(this)
const f = path.join(this.context, file.replace(p, ''))
const opts = util.getSpikeOptions()
console.log(opts.files.all.indexOf(f))
if (opts.files.all.indexOf(f) < 0 && !util.isFileIgnored(f) && event !== 'addDir') {
opts.project.watcher.watch([], [], [f])
}
Expand Down Expand Up @@ -185,9 +185,7 @@ module.exports = class Config {
}

// this is sometimes necessary for webpackjsonp loads in old browsers
if (opts.outputPublicPath) {
this.output.publicPath = opts.outputPublicPath
}
this.output.publicPath = opts.outputPublicPath || this.output.path

this.resolveLoader = {
modules: [
Expand Down Expand Up @@ -239,16 +237,19 @@ module.exports = class Config {

const util = new SpikeUtils(this)
const spikePlugin = new SpikePlugin(util, spike)
const server2 = (_, bs) => {
if (bs.utils.devIp.length) {
spike.project.emit('info', `External IP: http://${bs.utils.devIp[0]}:${spike.server.port}`)
}
}

// set custom dev server options
this.customDevServer = [opts.server, server2]

this.plugins = [
...opts.plugins,
spikePlugin,
...opts.afterSpikePlugins,
new BrowserSyncPlugin(opts.server, { callback: (_, bs) => {
if (bs.utils.devIp.length) {
spike.project.emit('info', `External IP: http://${bs.utils.devIp[0]}:${spike.server.port}`)
}
} })
...opts.afterSpikePlugins
]

return this
Expand Down
41 changes: 33 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const webpack = require('webpack')
const rimraf = require('rimraf')
const Sprout = require('sprout')
const Joi = require('joi')
const browserSync = require('browser-sync')
const webpackDevMiddleware = require('webpack-dev-middleware')
const {EventEmitter} = require('events')
const Config = require('./config')
const {Error, Warning} = require('./errors')
Expand All @@ -26,7 +28,10 @@ const {Error, Warning} = require('./errors')
class Spike extends EventEmitter {
constructor (opts = {}) {
super()
this.config = new Config(opts, this)
const config = new Config(opts, this)
this.devServer = config.customDevServer
delete config.customDevServer
this.config = config
}

/**
Expand All @@ -51,12 +56,35 @@ class Spike extends EventEmitter {
* @fires Spike#compile
* @return {Object} watch id, webpack watcher
*/

watch (opts = {}) {
const id = uuid()
this.compiler = webpack(this.config)
const watcher = this.compiler.watch(opts, compileCallback.bind(this, id))
this.watcher = watcher
return {id, watcher}

// add webpack dev middleware
const devMiddleware = webpackDevMiddleware(this.compiler, {
publicPath: this.config.output.publicPath,
stats: false,
overlay: true,
watchOptions: opts,
quiet: true,
compilerCallback: compileCallback.bind(this, id)
})
this.devServer[0].middleware.unshift(devMiddleware)

// TODO: A couple issues here. First, for some reason the watcher
// is not picking up new files, although it's detecting changes to
// existing files. Second, it's not writing the public folder.
// These changes are confusing as we're using the same thing exactly
// under the hood, but we'll figure it out!

// run browsersync server
if (!this.browserSync) this.browserSync = browserSync.create()
this.browserSync.init(this.devServer[0], this.devServer[1])

// return the middleware watcher and id
this.watcher = devMiddleware.watcher
return {id, watcher: this.watcher}
}

/**
Expand Down Expand Up @@ -165,9 +193,7 @@ function npmInstall (opts) {
* @fires Spike#compile
*/
function compileCallback (id, err, stats) {
if (err) {
return this.emit('error', new Error({ id, err }))
}
if (err) { return this.emit('error', new Error({ id, err })) }
// Webpack "soft errors" are classified as warnings in spike. An error is
// an error. If it doesn't break the build, it's a warning.
const cstats = stats.compilation
Expand All @@ -178,7 +204,6 @@ function compileCallback (id, err, stats) {
if (cstats.warnings.length) {
this.emit('warning', new Warning({ id, err: cstats.warnings[0] }))
}

this.emit('compile', {id, stats})
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
"spike-util": "^1.3.0",
"sprout": "^1.2.1",
"webpack": "^3.2.0",
"webpack-dev-middleware": "^1.11.0",
"when": "^3.7.8"
},
"devDependencies": {
"ava": "^0.20.0",
"ava": "^0.21.0",
"chalk": "^2.0.1",
"coveralls": "^2.13.1",
"husky": "^0.14.3",
Expand Down
Empty file added test/fixtures/watch/index.js
Empty file.
6 changes: 4 additions & 2 deletions test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {fixturesPath} = require('./_helpers')
test.cb('watches the project, reloads on modification', (t) => {
const project = new Spike({
root: path.join(fixturesPath, 'watch'),
server: { open: false }
server: { open: false },
entry: { main: './index.js' }
})
let i = 0

Expand All @@ -32,7 +33,8 @@ test.cb('watches the project, reloads on modification', (t) => {
test.cb('incorporates new file when added while watching', (t) => {
const project = new Spike({
root: path.join(fixturesPath, 'watch'),
server: { open: false }
server: { open: false },
entry: { main: './index.js' }
})
let i = 0
const testFile = path.join(fixturesPath, 'watch/test.html')
Expand Down
Loading