Skip to content

Commit 5702b1b

Browse files
refactor: code
1 parent 3c7c404 commit 5702b1b

File tree

4 files changed

+85
-19
lines changed

4 files changed

+85
-19
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ middleware(compiler, { outputFileSystem: myOutputFileSystem });
198198
`webpack-dev-middleware` also provides convenience methods that can be use to
199199
interact with the middleware at runtime:
200200

201-
### `close(callback)`
201+
### `close(callback?)`
202202

203-
Instructs a webpack-dev-middleware instance to stop watching for file changes.
203+
Instructs `webpack-dev-middleware` instance to stop watching for file changes.
204204

205205
### Parameters
206206

@@ -210,14 +210,14 @@ Type: `Function`
210210

211211
A function executed once the middleware has stopped watching.
212212

213-
### `invalidate()`
213+
### `invalidate(callback?)`
214214

215215
Instructs a webpack-dev-middleware instance to recompile the bundle.
216216
e.g. after a change to the configuration.
217217

218218
```js
219219
const webpack = require('webpack');
220-
const compiler = webpack({ ... });
220+
const compiler = webpack();
221221
const middleware = require('webpack-dev-middleware');
222222
const instance = middleware(compiler);
223223

@@ -233,6 +233,14 @@ setTimeout(() => {
233233
}, 1000);
234234
```
235235

236+
### Parameters
237+
238+
#### callback
239+
240+
Type: `Function`
241+
242+
A function executed once the middleware has invalidated.
243+
236244
### `waitUntilValid(callback)`
237245

238246
Executes a callback function when the compiler bundle is valid, typically after
@@ -244,12 +252,12 @@ compilation.
244252

245253
Type: `Function`
246254

247-
A function executed when the bundle becomes valid. If the bundle is
248-
valid at the time of calling, the callback is executed immediately.
255+
A function executed when the bundle becomes valid.
256+
If the bundle is valid at the time of calling, the callback is executed immediately.
249257

250258
```js
251259
const webpack = require('webpack');
252-
const compiler = webpack({ ... });
260+
const compiler = webpack();
253261
const middleware = require('webpack-dev-middleware');
254262
const instance = middleware(compiler);
255263

@@ -260,22 +268,23 @@ instance.waitUntilValid(() => {
260268
});
261269
```
262270

263-
### `getFilenameFromUrl(context, url)`
271+
### `getFilenameFromUrl(url)`
264272

265-
Get filename from url.
273+
Get filename from URL.
266274

267275
```js
268276
const webpack = require('webpack');
269-
const compiler = webpack({ ... });
277+
const compiler = webpack();
270278
const middleware = require('webpack-dev-middleware');
271279
const instance = middleware(compiler);
272280

273281
app.use(instance);
274282

275-
const processRequest = (req, res) => {
276-
const filename = instance.getFilenameFromUrl(instance.contex, req.url);
277-
console.log(`filename is ${filename}`)
278-
}
283+
instance.waitUntilValid(() => {
284+
const filename = instance.getFilenameFromUrl('/bundle.js');
285+
286+
console.log(`Filename is ${filename}`);
287+
});
279288
```
280289

281290
## Known Issues

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ export default function wdm(compiler, options = {}) {
7777
const instance = middleware(context);
7878

7979
// API
80+
instance.getFilenameFromUrl = (url) => getFilenameFromUrl(context, url);
81+
8082
instance.waitUntilValid = (callback = noop) => {
8183
ready(context, callback);
8284
};
@@ -93,7 +95,5 @@ export default function wdm(compiler, options = {}) {
9395

9496
instance.context = context;
9597

96-
instance.getFilenameFromUrl = getFilenameFromUrl;
97-
9898
return instance;
9999
}

src/utils/getFilenameFromUrl.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ export default function getFilenameFromUrl(context, url) {
1212
const { options } = context;
1313
const paths = getPaths(context);
1414

15-
let filename;
15+
let foundFilename;
1616
let urlObject;
1717

1818
try {
1919
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
2020
urlObject = memoizedParse(url, false, true);
2121
} catch (_ignoreError) {
22-
return filename;
22+
return;
2323
}
2424

2525
for (const { publicPath, outputPath } of paths) {
26+
let filename;
2627
let publicPathObject;
2728

2829
try {
@@ -62,6 +63,8 @@ export default function getFilenameFromUrl(context, url) {
6263
}
6364

6465
if (fsStats.isFile()) {
66+
foundFilename = filename;
67+
6568
break;
6669
} else if (
6770
fsStats.isDirectory() &&
@@ -83,11 +86,13 @@ export default function getFilenameFromUrl(context, url) {
8386
}
8487

8588
if (fsStats.isFile()) {
89+
foundFilename = filename;
90+
8691
break;
8792
}
8893
}
8994
}
9095
}
9196

92-
return filename;
97+
return foundFilename;
9398
}

test/api.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
import express from 'express';
24
import connect from 'connect';
35
import webpack, { Stats } from 'webpack';
@@ -372,6 +374,56 @@ describe.each([
372374
});
373375
});
374376

377+
describe('getFilenameFromUrl method', () => {
378+
beforeEach((done) => {
379+
compiler = getCompiler(webpackConfig);
380+
381+
instance = middleware(compiler);
382+
383+
app = framework();
384+
app.use(instance);
385+
386+
listen = app.listen((error) => {
387+
if (error) {
388+
return done(error);
389+
}
390+
391+
return done();
392+
});
393+
});
394+
395+
afterEach((done) => {
396+
if (instance.context.watching.closed) {
397+
if (listen) {
398+
listen.close(done);
399+
} else {
400+
done();
401+
}
402+
403+
return;
404+
}
405+
406+
instance.close(() => {
407+
if (listen) {
408+
listen.close(done);
409+
} else {
410+
done();
411+
}
412+
});
413+
});
414+
415+
it('should work', (done) => {
416+
instance.waitUntilValid(() => {
417+
expect(instance.getFilenameFromUrl('/unknown.unknown')).toBeUndefined();
418+
expect(instance.getFilenameFromUrl('/bundle.js')).toBe(
419+
path.join(webpackConfig.output.path, '/bundle.js')
420+
);
421+
422+
done();
423+
});
424+
});
425+
});
426+
375427
describe('close method', () => {
376428
beforeEach((done) => {
377429
compiler = getCompiler(webpackConfig);

0 commit comments

Comments
 (0)