Skip to content

Commit

Permalink
feat: add getFilenameFromUrl to API (#911)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed May 19, 2021
1 parent 08f32fe commit 1edc726
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 173 deletions.
104 changes: 89 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,32 +200,64 @@ interact with the middleware at runtime:

### `close(callback)`

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

### Parameters
#### Parameters

#### callback
##### `callback`

Type: `Function`
Required: `No`

A function executed once the middleware has stopped watching.

### `invalidate()`
```js
const express = require('express');
const webpack = require('webpack');
const compiler = webpack({
/* Webpack configuration */
});
const middleware = require('webpack-dev-middleware');
const instance = middleware(compiler);

const app = new express();

app.use(instance);

setTimeout(() => {
// Says `webpack` to stop watch changes
instance.close();
}, 1000);
```

Instructs a webpack-dev-middleware instance to recompile the bundle.
e.g. after a change to the configuration.
### `invalidate(callback)`

Instructs `webpack-dev-middleware` instance to recompile the bundle, e.g. after a change to the configuration.

#### Parameters

##### `callback`

Type: `Function`
Required: `No`

A function executed once the middleware has invalidated.

```js
const express = require('express');
const webpack = require('webpack');
const compiler = webpack({ ... });
const compiler = webpack({
/* Webpack configuration */
});
const middleware = require('webpack-dev-middleware');
const instance = middleware(compiler);

const app = new express();

app.use(instance);

setTimeout(() => {
// After a short delay the configuration is changed and a banner plugin is added
// to the config
// After a short delay the configuration is changed and a banner plugin is added to the config
new webpack.BannerPlugin('A new banner').apply(compiler);

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

### Parameters
#### Parameters

#### callback
##### `callback`

Type: `Function`
Required: `No`

A function executed when the bundle becomes valid. If the bundle is
valid at the time of calling, the callback is executed immediately.
A function executed when the bundle becomes valid.
If the bundle is valid at the time of calling, the callback is executed immediately.

```js
const express = require('express');
const webpack = require('webpack');
const compiler = webpack({ ... });
const compiler = webpack({
/* Webpack configuration */
});
const middleware = require('webpack-dev-middleware');
const instance = middleware(compiler);

const app = new express();

app.use(instance);

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

### `getFilenameFromUrl(url)`

Get filename from URL.

#### Parameters

##### `url`

Type: `String`
Required: `Yes`

URL for the requested file.

```js
const express = require('express');
const webpack = require('webpack');
const compiler = webpack({
/* Webpack configuration */
});
const middleware = require('webpack-dev-middleware');
const instance = middleware(compiler);

const app = new express();

app.use(instance);

instance.waitUntilValid(() => {
const filename = instance.getFilenameFromUrl('/bundle.js');

console.log(`Filename is ${filename}`);
});
```

## Known Issues

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

```js
const express = require('express');
const webpack = require('webpack');
const compiler = webpack({
// webpack options
/* Webpack configuration */
});
const isObject = require('is-object');
const middleware = require('webpack-dev-middleware');

const app = new express();

// This function makes server rendering of asset references consistent with different webpack chunk/entry configurations
function normalizeAssets(assets) {
if (isObject(assets)) {
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { validate } from 'schema-utils';
import mime from 'mime-types';

import middleware from './middleware';
import getFilenameFromUrl from './utils/getFilenameFromUrl';
import setupHooks from './utils/setupHooks';
import setupWriteToDisk from './utils/setupWriteToDisk';
import setupOutputFileSystem from './utils/setupOutputFileSystem';
Expand Down Expand Up @@ -76,17 +77,22 @@ export default function wdm(compiler, options = {}) {
const instance = middleware(context);

// API
instance.getFilenameFromUrl = (url) => getFilenameFromUrl(context, url);

instance.waitUntilValid = (callback = noop) => {
ready(context, callback);
};

instance.invalidate = (callback = noop) => {
ready(context, callback);

context.watching.invalidate();
};

instance.close = (callback = noop) => {
context.watching.close(callback);
};

instance.context = context;

return instance;
Expand Down
12 changes: 9 additions & 3 deletions src/utils/getFilenameFromUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ export default function getFilenameFromUrl(context, url) {
const { options } = context;
const paths = getPaths(context);

let filename;
let foundFilename;
let urlObject;

try {
// The `url` property of the `request` is contains only `pathname`, `search` and `hash`
urlObject = memoizedParse(url, false, true);
} catch (_ignoreError) {
return filename;
return;
}

for (const { publicPath, outputPath } of paths) {
let filename;
let publicPathObject;

try {
Expand Down Expand Up @@ -62,6 +63,8 @@ export default function getFilenameFromUrl(context, url) {
}

if (fsStats.isFile()) {
foundFilename = filename;

break;
} else if (
fsStats.isDirectory() &&
Expand All @@ -83,11 +86,14 @@ export default function getFilenameFromUrl(context, url) {
}

if (fsStats.isFile()) {
foundFilename = filename;

break;
}
}
}
}

return filename;
// eslint-disable-next-line consistent-return
return foundFilename;
}
Loading

0 comments on commit 1edc726

Please sign in to comment.