Skip to content

Commit

Permalink
Pass mimeTypes option to webpack-dev-middleware
Browse files Browse the repository at this point in the history
`webpack-dev-middleware` exposes an option to set custom mime types via 
passing a mimeType object, see 
https://github.com/webpack/webpack-dev-middleware#mimetypes

This commit allows the user to set `devServer.mimeType` in 
`webpack.config.js` to pass custom mimes types to 
`webpack-dev-middleware`
  • Loading branch information
dmohns committed Mar 10, 2019
1 parent 2f7f052 commit a4203c9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@
"noInfo": {
"type": "boolean"
},
"mimeTypes": {
"type": "object"
},
"quiet": {
"type": "boolean"
},
Expand Down Expand Up @@ -338,6 +341,7 @@
"reporter": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserver-reporter)",
"logTime": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-logtime)",
"noInfo": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-noinfo-)",
"mimeTypes": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devservermimetypes-)",
"quiet": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-quiet-)",
"serverSideRender": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-serversiderender)",
"index": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserver-index)",
Expand Down
26 changes: 26 additions & 0 deletions test/CreateConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,32 @@ describe('createConfig', () => {
expect(config).toMatchSnapshot();
});

it('mimeTypes option', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: { mimeTypes: { 'text/html': ['phtml'] } },
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('mimeTypes option - with force', () => {
const config = createConfig(
Object.assign({}, webpackConfig, {
devServer: {
mimeTypes: { typeMap: { 'text/html': ['phtml'] }, force: true },
},
}),
argv,
{ port: 8080 }
);

expect(config).toMatchSnapshot();
});

it('quiet option', () => {
const config = createConfig(
webpackConfig,
Expand Down
29 changes: 29 additions & 0 deletions test/MimeTypes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

const helper = require('./helper');
const config = require('./fixtures/simple-config/webpack.config');

describe('MimeTypes', () => {
afterEach(helper.close);

it('remapping mime type without force should throw an error', () => {
expect(() => {
helper.start(config, {
mimeTypes: { 'text/html': ['js'] },
});
}).toThrow(/Attempt to change mapping for/);
});

it('remapping mime type with force should not throw an error', (done) => {
helper.start(
config,
{
mimeTypes: {
typeMap: { 'text/html': ['js'] },
force: true,
},
},
done
);
});
});
41 changes: 41 additions & 0 deletions test/__snapshots__/CreateConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,47 @@ Object {
}
`;

exports[`createConfig mimeTypes option - with force 1`] = `
Object {
"hot": true,
"hotOnly": false,
"mimeTypes": Object {
"force": true,
"typeMap": Object {
"text/html": Array [
"phtml",
],
},
},
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig mimeTypes option 1`] = `
Object {
"hot": true,
"hotOnly": false,
"mimeTypes": Object {
"text/html": Array [
"phtml",
],
},
"noInfo": true,
"port": 8080,
"publicPath": "/",
"stats": Object {
"cached": false,
"cachedAssets": false,
},
}
`;

exports[`createConfig open option (boolean) (in devServer config) 1`] = `
Object {
"hot": true,
Expand Down

0 comments on commit a4203c9

Please sign in to comment.