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

feat: mimeTypeDefault #1527

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ See [below](#other-servers) for an example of use with fastify.

## Options

| Name | Type | Default | Description |
| :-----------------------------------------: | :-----------------------: | :-------------------------------------------: | :------------------------------------------------------------------------------------------------------------------- |
| **[`methods`](#methods)** | `Array` | `[ 'GET', 'HEAD' ]` | Allows to pass the list of HTTP request methods accepted by the middleware |
| **[`headers`](#headers)** | `Array\|Object\|Function` | `undefined` | Allows to pass custom HTTP headers on each request. |
| **[`index`](#index)** | `Boolean\|String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
| **[`mimeTypes`](#mimetypes)** | `Object` | `undefined` | Allows to register custom mime types or extension mappings. |
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
| **[`stats`](#stats)** | `Boolean\|String\|Object` | `stats` (from a configuration) | Stats options object or preset name. |
| **[`serverSideRender`](#serversiderender)** | `Boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |
| **[`writeToDisk`](#writetodisk)** | `Boolean\|Function` | `false` | Instructs the module to write files to the configured location on disk as specified in your `webpack` configuration. |
| **[`outputFileSystem`](#outputfilesystem)** | `Object` | [`memfs`](https://github.com/streamich/memfs) | Set the default file system which will be used by webpack as primary destination of generated files. |
| Name | Type | Default | Description |
| :-----------------------------------------: | :--------: | :-------------------------------------------: | :--------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| **[`methods`](#methods)** | `Array` | `[ 'GET', 'HEAD' ]` | Allows to pass the list of HTTP request methods accepted by the middleware |
| **[`headers`](#headers)** | `Array\ | Object\| Function` | `undefined` | Allows to pass custom HTTP headers on each request. |
| **[`index`](#index)** | `Boolean\ | String` | `index.html` | If `false` (but not `undefined`), the server will not respond to requests to the root URL. |
| **[`mimeTypes`](#mimetypes)** | `Object` | `undefined` | Allows to register custom mime types or extension mappings. |
| **[`mimeTypeDefault`](#mimetypedefault)** | `String` | `undefined` | Allows to register a default mime type when we can't determine the content type. |
| **[`publicPath`](#publicpath)** | `String` | `output.publicPath` (from a configuration) | The public path that the middleware is bound to. |
| **[`stats`](#stats)** | `Boolean\ | String\| Object` | `stats` (from a configuration) | Stats options object or preset name. |
| **[`serverSideRender`](#serversiderender)** | `Boolean` | `undefined` | Instructs the module to enable or disable the server-side rendering mode. |
| **[`writeToDisk`](#writetodisk)** | `Boolean\ | Function` | `false` | Instructs the module to write files to the configured location on disk as specified in your `webpack` configuration. |
| **[`outputFileSystem`](#outputfilesystem)** | `Object` | [`memfs`](https://github.com/streamich/memfs) | Set the default file system which will be used by webpack as primary destination of generated files. |

The middleware accepts an `options` Object. The following is a property reference for the Object.

Expand Down Expand Up @@ -164,6 +165,13 @@ eg. `mimeTypes: { phtml: 'text/html' }`.

Please see the documentation for [`mime-types`](https://github.com/jshttp/mime-types) for more information.

### mimeTypeDefault

Type: `String`
Default: `undefined`

This property allows a user to register a default mime type when we can't determine the content type.

### publicPath

Type: `String`
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const noop = () => {};
* @template {ServerResponse} ResponseInternal
* @typedef {Object} Options
* @property {{[key: string]: string}} [mimeTypes]
* @property {string | undefined} [mimeTypeDefault]
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
* @property {string} [methods]
* @property {Headers<RequestInternal, ResponseInternal>} [headers]
Expand Down
6 changes: 6 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ function wrapper(context) {
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
if (contentType) {
setHeaderForResponse(res, "Content-Type", contentType);
} else if (context.options.mimeTypeDefault) {
setHeaderForResponse(
res,
"Content-Type",
context.options.mimeTypeDefault
);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"link": "https://github.com/webpack/webpack-dev-middleware#mimetypes",
"type": "object"
},
"mimeTypeDefault": {
"description": "Allows a user to register a default mime type when we can't determine the content type.",
"link": "https://github.com/webpack/webpack-dev-middleware#mimetypedefault",
"type": "string"
},
"writeToDisk": {
"description": "Allows to write generated files on disk.",
"link": "https://github.com/webpack/webpack-dev-middleware#writetodisk",
Expand Down
46 changes: 46 additions & 0 deletions test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,52 @@ describe.each([
});
});

describe("mimeTypeDefault option", () => {
describe('should set the correct value for "Content-Type" header to unknown MIME type', () => {
beforeAll((done) => {
const outputPath = path.resolve(__dirname, "./outputs/basic");
const compiler = getCompiler({
...webpackConfig,
output: {
filename: "bundle.js",
path: outputPath,
},
});

instance = middleware(compiler, {
mimeTypeDefault: "text/plain",
});

app = framework();
app.use(instance);

listen = listenShorthand(done);

req = request(app);

instance.context.outputFileSystem.mkdirSync(outputPath, {
recursive: true,
});
instance.context.outputFileSystem.writeFileSync(
path.resolve(outputPath, "file.unknown"),
"welcome"
);
});

afterAll(close);

it('should return the "200" code for the "GET" request to "file.html"', async () => {
const response = await req.get("/file.unknown");

expect(response.statusCode).toEqual(200);
expect(
response.headers["content-type"].startsWith("text/plain")
).toBe(true);
expect(response.text).toEqual("welcome");
});
});
});

describe("watchOptions option", () => {
describe("should work without value", () => {
let compiler;
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export = wdm;
* @template {ServerResponse} ResponseInternal
* @typedef {Object} Options
* @property {{[key: string]: string}} [mimeTypes]
* @property {string | undefined} [mimeTypeDefault]
* @property {boolean | ((targetPath: string) => boolean)} [writeToDisk]
* @property {string} [methods]
* @property {Headers<RequestInternal, ResponseInternal>} [headers]
Expand Down Expand Up @@ -164,6 +165,7 @@ type Options<
[key: string]: string;
}
| undefined;
mimeTypeDefault?: string | undefined;
writeToDisk?: boolean | ((targetPath: string) => boolean) | undefined;
methods?: string | undefined;
headers?: Headers<RequestInternal, ResponseInternal>;
Expand Down