Skip to content

Commit

Permalink
feat: allow to redefine mimeTypes (possible to use force option) (w…
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch authored and evilebottnawi committed Dec 13, 2018
1 parent a59b9fd commit e56a181
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,15 @@ Type: `Object`
Default: `null`

This property allows a user to register custom mime types or extension mappings.
eg. `{ 'text/html': [ 'phtml' ] }`. Please see the documentation for
[`node-mime`](https://github.com/broofa/node-mime#mimedefine) for more information.
eg. `mimeTypes: { 'text/html': [ 'phtml' ] }`.

By default node-mime will throw an error if you try to map a type to an extension
that is already assigned to another type. Passing `force: true` will suppress this behavior
(overriding any previous mapping).
eg. `mimeTypes: { typeMap: { 'text/html': [ 'phtml' ] } }, force: true }`.

Please see the documentation for
[`node-mime`](https://github.com/broofa/node-mime#mimedefinetypemap-force--false) for more information.

### publicPath

Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ module.exports = function wdm(compiler, opts) {

// defining custom MIME type
if (options.mimeTypes) {
mime.define(options.mimeTypes);
const typeMap = options.mimeTypes.typeMap || options.mimeTypes;
const force = !!options.mimeTypes.force;
mime.define(typeMap, force);
}

const context = createContext(compiler, options);
Expand Down
27 changes: 27 additions & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@ describe('Server', () => {
});
});

describe('force option for custom mimeTypes', () => {
before((done) => {
app = express();
const compiler = webpack(webpackClientServerConfig);
instance = middleware(compiler, {
stats: 'errors-only',
logLevel,
index: 'Index.phtml',
mimeTypes: {
typeMap: { 'text/html': ['phtml'] },
force: true
}
});
app.use(instance);
listen = listenShorthand(done);
instance.fileSystem.writeFileSync('/Index.phtml', 'welcome');
});
after(close);

it('request to Index.phtml', (done) => {
request(app).get('/')
.expect('welcome')
.expect('Content-Type', /text\/html/)
.expect(200, done);
});
});

describe('WebAssembly', () => {
before((done) => {
app = express();
Expand Down

0 comments on commit e56a181

Please sign in to comment.