Skip to content

Commit 1edc726

Browse files
authoredMay 19, 2021
feat: add getFilenameFromUrl to API (#911)
1 parent 08f32fe commit 1edc726

File tree

6 files changed

+395
-173
lines changed

6 files changed

+395
-173
lines changed
 

‎README.md

+89-15
Original file line numberDiff line numberDiff line change
@@ -200,32 +200,64 @@ interact with the middleware at runtime:
200200

201201
### `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

205-
### Parameters
205+
#### Parameters
206206

207-
#### callback
207+
##### `callback`
208208

209209
Type: `Function`
210+
Required: `No`
210211

211212
A function executed once the middleware has stopped watching.
212213

213-
### `invalidate()`
214+
```js
215+
const express = require('express');
216+
const webpack = require('webpack');
217+
const compiler = webpack({
218+
/* Webpack configuration */
219+
});
220+
const middleware = require('webpack-dev-middleware');
221+
const instance = middleware(compiler);
222+
223+
const app = new express();
224+
225+
app.use(instance);
226+
227+
setTimeout(() => {
228+
// Says `webpack` to stop watch changes
229+
instance.close();
230+
}, 1000);
231+
```
214232

215-
Instructs a webpack-dev-middleware instance to recompile the bundle.
216-
e.g. after a change to the configuration.
233+
### `invalidate(callback)`
234+
235+
Instructs `webpack-dev-middleware` instance to recompile the bundle, e.g. after a change to the configuration.
236+
237+
#### Parameters
238+
239+
##### `callback`
240+
241+
Type: `Function`
242+
Required: `No`
243+
244+
A function executed once the middleware has invalidated.
217245

218246
```js
247+
const express = require('express');
219248
const webpack = require('webpack');
220-
const compiler = webpack({ ... });
249+
const compiler = webpack({
250+
/* Webpack configuration */
251+
});
221252
const middleware = require('webpack-dev-middleware');
222253
const instance = middleware(compiler);
223254

255+
const app = new express();
256+
224257
app.use(instance);
225258

226259
setTimeout(() => {
227-
// After a short delay the configuration is changed and a banner plugin is added
228-
// to the config
260+
// After a short delay the configuration is changed and a banner plugin is added to the config
229261
new webpack.BannerPlugin('A new banner').apply(compiler);
230262

231263
// Recompile the bundle with the banner plugin:
@@ -238,28 +270,67 @@ setTimeout(() => {
238270
Executes a callback function when the compiler bundle is valid, typically after
239271
compilation.
240272

241-
### Parameters
273+
#### Parameters
242274

243-
#### callback
275+
##### `callback`
244276

245277
Type: `Function`
278+
Required: `No`
246279

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.
280+
A function executed when the bundle becomes valid.
281+
If the bundle is valid at the time of calling, the callback is executed immediately.
249282

250283
```js
284+
const express = require('express');
251285
const webpack = require('webpack');
252-
const compiler = webpack({ ... });
286+
const compiler = webpack({
287+
/* Webpack configuration */
288+
});
253289
const middleware = require('webpack-dev-middleware');
254290
const instance = middleware(compiler);
255291

292+
const app = new express();
293+
256294
app.use(instance);
257295

258296
instance.waitUntilValid(() => {
259297
console.log('Package is in a valid state');
260298
});
261299
```
262300

301+
### `getFilenameFromUrl(url)`
302+
303+
Get filename from URL.
304+
305+
#### Parameters
306+
307+
##### `url`
308+
309+
Type: `String`
310+
Required: `Yes`
311+
312+
URL for the requested file.
313+
314+
```js
315+
const express = require('express');
316+
const webpack = require('webpack');
317+
const compiler = webpack({
318+
/* Webpack configuration */
319+
});
320+
const middleware = require('webpack-dev-middleware');
321+
const instance = middleware(compiler);
322+
323+
const app = new express();
324+
325+
app.use(instance);
326+
327+
instance.waitUntilValid(() => {
328+
const filename = instance.getFilenameFromUrl('/bundle.js');
329+
330+
console.log(`Filename is ${filename}`);
331+
});
332+
```
333+
263334
## Known Issues
264335

265336
### Multiple Successive Builds
@@ -289,13 +360,16 @@ process is finished with server-side rendering enabled._
289360
Example Implementation:
290361

291362
```js
363+
const express = require('express');
292364
const webpack = require('webpack');
293365
const compiler = webpack({
294-
// webpack options
366+
/* Webpack configuration */
295367
});
296368
const isObject = require('is-object');
297369
const middleware = require('webpack-dev-middleware');
298370

371+
const app = new express();
372+
299373
// This function makes server rendering of asset references consistent with different webpack chunk/entry configurations
300374
function normalizeAssets(assets) {
301375
if (isObject(assets)) {

‎src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { validate } from 'schema-utils';
22
import mime from 'mime-types';
33

44
import middleware from './middleware';
5+
import getFilenameFromUrl from './utils/getFilenameFromUrl';
56
import setupHooks from './utils/setupHooks';
67
import setupWriteToDisk from './utils/setupWriteToDisk';
78
import setupOutputFileSystem from './utils/setupOutputFileSystem';
@@ -76,17 +77,22 @@ export default function wdm(compiler, options = {}) {
7677
const instance = middleware(context);
7778

7879
// API
80+
instance.getFilenameFromUrl = (url) => getFilenameFromUrl(context, url);
81+
7982
instance.waitUntilValid = (callback = noop) => {
8083
ready(context, callback);
8184
};
85+
8286
instance.invalidate = (callback = noop) => {
8387
ready(context, callback);
8488

8589
context.watching.invalidate();
8690
};
91+
8792
instance.close = (callback = noop) => {
8893
context.watching.close(callback);
8994
};
95+
9096
instance.context = context;
9197

9298
return instance;

‎src/utils/getFilenameFromUrl.js

+9-3
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,14 @@ 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+
// eslint-disable-next-line consistent-return
98+
return foundFilename;
9399
}

0 commit comments

Comments
 (0)
Please sign in to comment.