Skip to content

Commit e56a181

Browse files
gpoitchevilebottnawi
authored andcommitted
feat: allow to redefine mimeTypes (possible to use force option) (#349)
1 parent a59b9fd commit e56a181

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ Type: `Object`
150150
Default: `null`
151151

152152
This property allows a user to register custom mime types or extension mappings.
153-
eg. `{ 'text/html': [ 'phtml' ] }`. Please see the documentation for
154-
[`node-mime`](https://github.com/broofa/node-mime#mimedefine) for more information.
153+
eg. `mimeTypes: { 'text/html': [ 'phtml' ] }`.
154+
155+
By default node-mime will throw an error if you try to map a type to an extension
156+
that is already assigned to another type. Passing `force: true` will suppress this behavior
157+
(overriding any previous mapping).
158+
eg. `mimeTypes: { typeMap: { 'text/html': [ 'phtml' ] } }, force: true }`.
159+
160+
Please see the documentation for
161+
[`node-mime`](https://github.com/broofa/node-mime#mimedefinetypemap-force--false) for more information.
155162

156163
### publicPath
157164

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ module.exports = function wdm(compiler, opts) {
3838

3939
// defining custom MIME type
4040
if (options.mimeTypes) {
41-
mime.define(options.mimeTypes);
41+
const typeMap = options.mimeTypes.typeMap || options.mimeTypes;
42+
const force = !!options.mimeTypes.force;
43+
mime.define(typeMap, force);
4244
}
4345

4446
const context = createContext(compiler, options);

test/tests/server.js

+27
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,33 @@ describe('Server', () => {
245245
});
246246
});
247247

248+
describe('force option for custom mimeTypes', () => {
249+
before((done) => {
250+
app = express();
251+
const compiler = webpack(webpackClientServerConfig);
252+
instance = middleware(compiler, {
253+
stats: 'errors-only',
254+
logLevel,
255+
index: 'Index.phtml',
256+
mimeTypes: {
257+
typeMap: { 'text/html': ['phtml'] },
258+
force: true
259+
}
260+
});
261+
app.use(instance);
262+
listen = listenShorthand(done);
263+
instance.fileSystem.writeFileSync('/Index.phtml', 'welcome');
264+
});
265+
after(close);
266+
267+
it('request to Index.phtml', (done) => {
268+
request(app).get('/')
269+
.expect('welcome')
270+
.expect('Content-Type', /text\/html/)
271+
.expect(200, done);
272+
});
273+
});
274+
248275
describe('WebAssembly', () => {
249276
before((done) => {
250277
app = express();

0 commit comments

Comments
 (0)