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

Upgrade for webpack 4 #38

Open
wants to merge 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [2.0.0] - 2018-03-15
### Changed
- Upgraded to work with webpack 4

## [1.3.0] - 2017-05-15
### Added
- Allow use of module with `.js` extension @bryanbecker #28
Expand Down
38 changes: 24 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const debug = require('debug')('screeps-webpack-plugin')
const path = require('path')
const fs = require('fs')
const { SyncHook, SyncWaterfallHook, AsyncSeriesWaterfallHook } = require('tapable')

const ScreepsModules = require('screeps-modules')

Expand All @@ -10,6 +11,8 @@ const CONFIG_CLIENT = 'screeps-webpack-plugin-configure-client'
const BEFORE_COMMIT = 'screeps-webpack-plugin-before-commit'
const AFTER_COMMIT = 'screeps-webpack-plugin-after-commit'

const pluginName = 'ScreepsWebpackPlugin'

class ScreepsWebpackPluginError extends Error {
constructor (msg) {
super(msg)
Expand All @@ -23,17 +26,19 @@ class ScreepsWebpackPlugin {
}

apply (compiler) {
compiler.plugin('compilation', (compilation) => {
if (compiler.options.target !== 'node') {
const err = new ScreepsWebpackPluginError(`Can only support Node.js {target: 'node'}`)
compiler.hooks.compilation.tap(pluginName, (compilation) => {
// compiler.options not supported in webpack 4

return compilation.errors.push(err)
}
// if (compiler.options.target !== 'node') {
// const err = new ScreepsWebpackPluginError(`Can only support Node.js {target: 'node'}`)

// return compilation.errors.push(err)
// }

this.registerHandlers(compilation)
})

compiler.plugin('after-emit', (compilation, cb) => {
compiler.hooks.afterEmit.tapAsync(pluginName, (compilation, cb) => {
Promise.resolve()
.then(() => {
return new Promise((resolve, reject) => {
Expand All @@ -43,26 +48,26 @@ class ScreepsWebpackPlugin {
compilation
}

compilation.applyPluginsAsyncWaterfall(COLLECT_MODULES, initial, (err, {modules}) => {
compilation.hooks[COLLECT_MODULES].callAsync(initial, (err, obj) => {
if (err) {
debug('Error while collecting modules', err.stack)

return reject(err)
} else {
resolve(modules)
resolve(obj.modules)
}
})
})
})
.then((modules) => {
const client = compilation.applyPluginsWaterfall(CONFIG_CLIENT, null, this)
const client = compilation.hooks[CONFIG_CLIENT].call(null, this)
const {branch} = this.options

compilation.applyPlugins(BEFORE_COMMIT, branch, modules)
compilation.hooks[BEFORE_COMMIT].call(branch, modules)

return client.commit(branch, modules)
.then((body) => {
compilation.applyPlugins(AFTER_COMMIT, body)
compilation.hooks[AFTER_COMMIT].call(body)
})
.catch((body) => {
throw new Error(body)
Expand All @@ -78,8 +83,13 @@ class ScreepsWebpackPlugin {
}

registerHandlers (compilation) {
compilation.plugin(COLLECT_MODULES, this.collectModules)
compilation.plugin(CONFIG_CLIENT, this.configureClient)
compilation.hooks[COLLECT_MODULES] = new AsyncSeriesWaterfallHook(['opts'])
compilation.hooks[CONFIG_CLIENT] = new SyncWaterfallHook(['initial', 'plugin'])
compilation.hooks[BEFORE_COMMIT] = new SyncHook(['branch', 'modules'])
compilation.hooks[AFTER_COMMIT] = new SyncHook(['body'])

compilation.hooks[COLLECT_MODULES].tapAsync(pluginName, this.collectModules)
compilation.hooks[CONFIG_CLIENT].tap(pluginName, this.configureClient)
}

collectModules ({modules: initial, plugin, compilation}, cb) {
Expand Down Expand Up @@ -124,7 +134,7 @@ class ScreepsWebpackPlugin {

cb(null, {modules, plugin, compilation})
})
.catch(cb)
.catch((err) => cb(err, {modules: null}))
}

configureClient (initial, plugin) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "screeps-webpack-plugin",
"version": "1.3.0",
"version": "2.0.0",
"description": "Compile your AI against a Screeps server",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -52,6 +52,6 @@
"nyc": "^10.2.0",
"snazzy": "^7.0.0",
"standard": "^10.0.1",
"webpack": "^2.1.0-beta.26"
"webpack": "^4.1.0"
}
}
67 changes: 37 additions & 30 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function compile (options) {
path: '/',
filename: '[name]'
},
mode: 'development',
plugins: []
}, options))
compiler.outputFileSystem = new MemoryFs()
Expand All @@ -33,38 +34,34 @@ function compile (options) {
if (err) return reject(err)

if (stats.hasErrors() || stats.hasWarnings()) {
return reject(stats.compilation.errors)
return reject(new Error({ errors: stats.compilation.errors, warnings: stats.compilation.warnings }))
}

resolve({compiler, stats})
})
})
}

const plugin = (name, ...args) => (
const plugin = (name, sync, ...args) => (
new (class {
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
compilation.plugin(name, ...args)
compiler.hooks.compilation.tap('name', (compilation) => {
if (sync) {
compilation.hooks[name].tap('name', ...args)
} else {
compilation.hooks[name].tapAsync('name', ...args)
}
})
}
})()
)

const checkError = (t, err, ...checks) => {
t.is(err.name, 'ScreepsWebpackPluginError')

for (const check of checks) {
t.truthy(err.toString().match(check))
}
}

test('Test Webpack compiler setup', async t => {
t.plan(1)

class TestPlugin {
apply (compiler) {
compiler.plugin('done', () => {
compiler.hooks.done.tap('TestPlugin', () => {
t.pass()
})
}
Expand All @@ -73,25 +70,35 @@ test('Test Webpack compiler setup', async t => {
await compile({plugins: [new TestPlugin()]})
})

test(`Test requires target 'node'`, async t => {
try {
await compile({
target: 'web',
plugins: [
new ScreepsWebpackPlugin()
]
})
// Functionality not supported in webpack 4

t.fail()
} catch ([e]) {
checkError(t, e, 'target', 'node')
}
})
// test(`Test requires target 'node'`, async t => {
// try {
// await compile({
// target: 'web',
// plugins: [
// new ScreepsWebpackPlugin()
// ]
// })

// t.fail()
// } catch ([e]) {
// checkError(t, e, 'target', 'node')
// }
// })

// const checkError = (t, err, ...checks) => {
// t.is(err.name, 'ScreepsWebpackPluginError')

// for (const check of checks) {
// t.truthy(err.toString().match(check))
// }
// }

test('Test commit', async t => {
t.plan(10)

const collectModules = plugin('screeps-webpack-plugin-collect-modules',
const collectModules = plugin('screeps-webpack-plugin-collect-modules', false,
({modules, plugin, compilation}, cb) => {
t.deepEqual(Object.keys(modules), ['etc', 'main'])
t.truthy(modules.main.match(/foobar/))
Expand All @@ -106,7 +113,7 @@ test('Test commit', async t => {
}
)

const configureClient = plugin('screeps-webpack-plugin-configure-client',
const configureClient = plugin('screeps-webpack-plugin-configure-client', true,
(client, plugin) => {
t.true(client instanceof ScreepsModules)
t.true(plugin instanceof ScreepsWebpackPlugin)
Expand All @@ -119,14 +126,14 @@ test('Test commit', async t => {
}
)

const beforeCommit = plugin('screeps-webpack-plugin-before-commit',
const beforeCommit = plugin('screeps-webpack-plugin-before-commit', true,
(branch, modules) => {
t.is(branch, 'test')
t.is(modules.quux, 'norf')
}
)

const afterCommit = plugin('screeps-webpack-plugin-after-commit',
const afterCommit = plugin('screeps-webpack-plugin-after-commit', true,
(body) => {
t.is(body, 'foobar')
}
Expand Down
Loading