Skip to content

Commit 3833a85

Browse files
xiaoyannshellscape
authored andcommitted
Add a option, mimeTypes, to support defining custom mime types (#150)
* Add a option, mimeTypes, to support defining custom mime types * added new option mimeTypes in example usage (#150) * Add a option, mimeTypes, to support defining custom mime types
1 parent ebb1fe5 commit 3833a85

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ app.use(webpackMiddleware(webpack({
7676
headers: { "X-Custom-Header": "yes" },
7777
// custom headers
7878

79+
mimeTypes: { "text/html": [ "phtml" ] },
80+
// Add custom mime/extension mappings
81+
// https://github.com/broofa/node-mime#mimedefine
82+
// https://github.com/webpack/webpack-dev-middleware/pull/150
83+
7984
stats: {
8085
colors: true
8186
},

lib/Shared.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var mime = require("mime");
12
var parseRange = require("range-parser");
23
var pathIsAbsolute = require("path-is-absolute");
34
var MemoryFileSystem = require("memory-fs");
@@ -28,6 +29,9 @@ module.exports = function Shared(context) {
2829
options.filename = new RegExp("^[\/]{0,1}" + str + "$");
2930
}
3031
}
32+
// defining custom MIME type
33+
if(options.mimeTypes) mime.define(options.mimeTypes);
34+
3135
context.options = options;
3236
},
3337
defaultReporter: function(reporterOptions) {

middleware.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module.exports = function(compiler, options) {
2121
};
2222
var shared = Shared(context);
2323

24-
2524
// The middleware function
2625
function webpackDevMiddleware(req, res, next) {
2726
function goNext() {

test/Server.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ describe("Server", function() {
143143
});
144144
});
145145

146+
describe("custom mimeTypes", function() {
147+
before(function(done) {
148+
app = express();
149+
var compiler = webpack(webpackConfig);
150+
var instance = middleware(compiler, {
151+
stats: "errors-only",
152+
quiet: true,
153+
index: "Index.phtml",
154+
mimeTypes: {
155+
"text/html": ["phtml"]
156+
}
157+
});
158+
app.use(instance);
159+
listen = listenShorthand(done);
160+
instance.fileSystem.writeFileSync("/Index.phtml", "welcome");
161+
});
162+
after(close);
163+
164+
it("request to Index.phtml", function(done) {
165+
request(app).get("/")
166+
.expect("welcome")
167+
.expect("Content-Type", /text\/html/)
168+
.expect(200, done);
169+
});
170+
});
171+
146172
describe("MultiCompiler", function() {
147173
before(function(done) {
148174
app = express();

0 commit comments

Comments
 (0)