-
-
Notifications
You must be signed in to change notification settings - Fork 47
feat: add waitForBuild option #102
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
Merged
shellscape
merged 2 commits into
shellscape:master
from
smashercosmo:wait-for-bundle-ready
Feb 4, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,7 @@ class WebpackPluginServe extends EventEmitter { | |
| this.log = getLogger(options.log || {}); | ||
| this.options = options; | ||
| this.compilers = []; | ||
| this.state = {}; | ||
| } | ||
|
|
||
| apply(compiler) { | ||
|
|
@@ -154,6 +155,20 @@ class WebpackPluginServe extends EventEmitter { | |
| invalid.tap(key, (filePath) => this.emit('invalid', filePath, compiler)); | ||
| watchClose.tap(key, () => this.emit('close', compiler)); | ||
|
|
||
| if (this.options.waitForBuild) { | ||
| // track the first build of the bundle | ||
| this.state.compiling = new Promise((resolve) => { | ||
| this.once('done', () => resolve()); | ||
| }); | ||
|
|
||
| // track subsequent builds from watching | ||
| this.on('invalid', () => { | ||
| this.state.compiling = new Promise((resolve) => { | ||
| this.once('done', () => resolve()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| }); | ||
| }); | ||
| } | ||
|
|
||
| compiler.hooks.compilation.tap(key, (compilation) => { | ||
| compilation.hooks.afterHash.tap(key, () => { | ||
| // webpack still has a 4 year old bug whereby in watch mode, file timestamps aren't properly | ||
|
|
@@ -168,14 +183,14 @@ class WebpackPluginServe extends EventEmitter { | |
| }); | ||
|
|
||
| watchRun.tapPromise(key, async () => { | ||
| if (!this.startPromise) { | ||
| if (!this.state.starting) { | ||
| // ensure we're only trying to start the server once | ||
| this.startPromise = start.bind(this)(); | ||
| this.startPromise.then(() => newline()); | ||
| this.state.starting = start.bind(this)(); | ||
| this.state.starting.then(() => newline()); | ||
| } | ||
|
|
||
| // wait for the server to startup so we can get our client connection info from it | ||
| await this.startPromise; | ||
| await this.state.starting; | ||
|
matheus1lva marked this conversation as resolved.
|
||
|
|
||
| const compilerData = { | ||
| // only set the compiler name if we're dealing with more than one compiler. otherwise, the | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = 'hello'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* eslint-disable no-param-reassign */ | ||
| const { resolve } = require('path'); | ||
|
|
||
| const { WebpackPluginServe: Serve } = require('../../../lib/'); | ||
|
|
||
| const make = (port) => { | ||
| const outputPath = resolve(__dirname, './output/output.js'); | ||
| const serve = new Serve({ | ||
| host: 'localhost', | ||
| port, | ||
| waitForBuild: true, | ||
| middleware: (app) => { | ||
| app.use(async (ctx, next) => { | ||
| if (ctx.url === '/test') { | ||
| try { | ||
| // eslint-disable-next-line import/no-dynamic-require, global-require | ||
| require(outputPath); | ||
| ctx.body = 'success'; | ||
| } catch (e) { | ||
| ctx.body = 'error'; | ||
| } | ||
| } | ||
| await next(); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| const config = { | ||
| context: __dirname, | ||
| entry: ['./app.js'], | ||
| mode: 'development', | ||
| output: { | ||
| filename: './output.js', | ||
| path: resolve(__dirname, './output'), | ||
| publicPath: 'output/', | ||
| libraryTarget: 'commonjs2' | ||
| }, | ||
| plugins: [serve], | ||
| target: 'node', | ||
| watch: true | ||
| }; | ||
|
|
||
| return { serve, config }; | ||
| }; | ||
|
|
||
| module.exports = { make }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| const getPort = require('get-port'); | ||
| const del = require('del'); | ||
| const webpack = require('webpack'); | ||
| const test = require('ava'); | ||
| const fetch = require('node-fetch'); | ||
| const defer = require('p-defer'); | ||
|
|
||
| const { make } = require('./fixtures/wait-for-build/make-config'); | ||
|
|
||
| let watcher; | ||
| let port; | ||
|
|
||
| test.before('Starting server', async () => { | ||
| const deferred = defer(); | ||
| port = await getPort(); | ||
| const { serve, config } = make(port); | ||
| const compiler = webpack(config); | ||
| watcher = compiler.watch({}, () => {}); | ||
| serve.on('listening', deferred.resolve); | ||
| await deferred.promise; | ||
| }); | ||
|
|
||
| test.after.always('Closing server', async () => { | ||
| watcher.close(); | ||
| await del('./test/fixtures/waitForBuild/output'); | ||
| }); | ||
|
|
||
| test('should wait until bundle is compiled', async (t) => { | ||
| const response = await fetch(`http://localhost:${port}/test`); | ||
| const text = await response.text(); | ||
| t.is(text, 'success'); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be shortened to
this.once('done', resolve);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could :) in certain situations I like to be a bit more verbose.