From abf8691ecbf3702361ef7004712e08d1597823ac Mon Sep 17 00:00:00 2001 From: Daniel Mohns Date: Fri, 5 Apr 2019 01:06:42 +0200 Subject: [PATCH] Pass mimeTypes option to webpack-dev-middleware (#1714) --- lib/options.json | 4 ++ test/CreateConfig.test.js | 26 +++++++++ test/MimeTypes.test.js | 58 ++++++++++++++++++++ test/__snapshots__/CreateConfig.test.js.snap | 41 ++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 test/MimeTypes.test.js diff --git a/lib/options.json b/lib/options.json index d3a3bdda62..f4941cc655 100644 --- a/lib/options.json +++ b/lib/options.json @@ -278,6 +278,9 @@ "noInfo": { "type": "boolean" }, + "mimeTypes": { + "type": "object" + }, "quiet": { "type": "boolean" }, @@ -342,6 +345,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)", diff --git a/test/CreateConfig.test.js b/test/CreateConfig.test.js index c1227fc3f9..5cce86344f 100644 --- a/test/CreateConfig.test.js +++ b/test/CreateConfig.test.js @@ -516,6 +516,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, diff --git a/test/MimeTypes.test.js b/test/MimeTypes.test.js new file mode 100644 index 0000000000..31a57a9585 --- /dev/null +++ b/test/MimeTypes.test.js @@ -0,0 +1,58 @@ +'use strict'; + +const request = require('supertest'); +const helper = require('./helper'); +const config = require('./fixtures/simple-config/webpack.config'); + +describe('MimeTypes configuration', () => { + afterEach(helper.close); + + it('remapping mime type without force should throw an error', () => { + expect(() => { + helper.start(config, { + mimeTypes: { 'application/octet-stream': ['js'] }, + }); + }).toThrow(/Attempt to change mapping for/); + }); + + it('remapping mime type with force should not throw an error', (done) => { + helper.start( + config, + { + mimeTypes: { + typeMap: { 'application/octet-stream': ['js'] }, + force: true, + }, + }, + done + ); + }); +}); + +describe('custom mimeTypes', () => { + let server; + let req; + + beforeAll((done) => { + server = helper.start( + config, + { + mimeTypes: { + typeMap: { 'application/octet-stream': ['js'] }, + force: true, + }, + }, + done + ); + req = request(server.app); + }); + + afterAll(helper.close); + + it('request to bundle file with modified mime type', (done) => { + req + .get('/main.js') + .expect('Content-Type', /application\/octet-stream/) + .expect(200, done); + }); +}); diff --git a/test/__snapshots__/CreateConfig.test.js.snap b/test/__snapshots__/CreateConfig.test.js.snap index 2f146b276b..bd0044f1a4 100644 --- a/test/__snapshots__/CreateConfig.test.js.snap +++ b/test/__snapshots__/CreateConfig.test.js.snap @@ -671,6 +671,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,