Skip to content

Commit 3fc2d76

Browse files
authored
Merge pull request #101 from Frank3K/feat/mime-4-support
Fix broken package / mime@4 compatibility
2 parents 5db1d51 + 1ba9864 commit 3fc2d76

File tree

10 files changed

+1044
-454
lines changed

10 files changed

+1044
-454
lines changed

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
"dist",
3838
"index.d.ts"
3939
],
40+
"type": "module",
4041
"dependencies": {
41-
"mime": "^3",
42+
"mime": "^4",
4243
"opener": "1"
4344
},
4445
"devDependencies": {
45-
"@rollup/plugin-buble": "0.21.3",
46-
"@types/node": "^18.7.15",
47-
"rollup": "2",
46+
"@rollup/plugin-buble": "^1.0.0",
47+
"@types/node": "^20.10.1",
48+
"rollup": "4",
4849
"standard": "17"
4950
}
5051
}

src/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { readFile } from 'fs'
22
import { createServer as createHttpsServer } from 'https'
33
import { createServer } from 'http'
44
import { resolve, posix } from 'path'
5+
import { Mime } from 'mime/lite'
6+
7+
import standardTypes from 'mime/types/standard.js'
8+
import otherTypes from 'mime/types/other.js'
59

6-
import mime from 'mime'
710
import opener from 'opener'
811

912
let server
@@ -13,6 +16,7 @@ let server
1316
* @param {import('..').RollupServeOptions} options
1417
*/
1518
function serve (options = { contentBase: '' }) {
19+
const mime = new Mime(standardTypes, otherTypes)
1620
if (Array.isArray(options) || typeof options === 'string') {
1721
options = { contentBase: options }
1822
}
@@ -22,7 +26,6 @@ function serve (options = { contentBase: '' }) {
2226
options.https = options.https || false
2327
options.openPage = options.openPage || ''
2428
options.onListening = options.onListening || function noop () { }
25-
mime.default_type = 'text/plain'
2629

2730
if (options.mimeTypes) {
2831
mime.define(options.mimeTypes, true)
@@ -41,7 +44,7 @@ function serve (options = { contentBase: '' }) {
4144

4245
readFileFromContentBase(options.contentBase, urlPath, function (error, content, filePath) {
4346
if (!error) {
44-
return found(response, filePath, content)
47+
return found(response, mime.getType(filePath), content)
4548
}
4649
if (error.code !== 'ENOENT') {
4750
response.writeHead(500)
@@ -57,7 +60,7 @@ function serve (options = { contentBase: '' }) {
5760
if (error) {
5861
notFound(response, filePath)
5962
} else {
60-
found(response, filePath, content)
63+
found(response, mime.getType(filePath), content)
6164
}
6265
})
6366
} else {
@@ -146,8 +149,8 @@ function notFound (response, filePath) {
146149
'\n\n(rollup-plugin-serve)', 'utf-8')
147150
}
148151

149-
function found (response, filePath, content) {
150-
response.writeHead(200, { 'Content-Type': mime.getType(filePath) })
152+
function found (response, mimeType, content) {
153+
response.writeHead(200, { 'Content-Type': mimeType || 'text/plain' })
151154
response.end(content, 'utf-8')
152155
}
153156

test/base3/test3.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!doctype html>
2+
File: /base3/test3.html
3+
<script src="dest.js"></script>
4+
<script src="mime.js"></script>

test/fixtures/demo.abc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some text

test/fixtures/demo.fgh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Some text

test/fixtures/demo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"prop": 123
3+
}

test/frames.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
width: 200px;
99
border: 1px solid #ccc;
1010
}
11+
iframe.auto-height {
12+
height: auto;
13+
}
14+
td[colspan="3"] iframe {
15+
width: 100%;
16+
}
1117
th {
1218
padding-top: 1rem;
1319
letter-spacing: 1px;
@@ -54,4 +60,12 @@
5460
<td><iframe src="./base1/test2.html"></iframe></td>
5561
<td><iframe src="./base2/test2.html"></iframe></td>
5662
</tr>
63+
<tr>
64+
<th></th>
65+
<th colspan="3">MIME types</th>
66+
</tr>
67+
<tr>
68+
<td>test3.html</td>
69+
<td colspan="3"><iframe src="./test3.html" class="auto-height"></iframe></td>
70+
</tr>
5771
</table>

test/mime.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use strict";
2+
3+
async function getMimeType(path) {
4+
const response = await fetch(path);
5+
return response.headers.get("Content-Type");
6+
}
7+
8+
async function checkMimes() {
9+
const paths = [
10+
"fixtures/demo.json",
11+
"fixtures/demo.abc",
12+
"fixtures/demo.fgh",
13+
];
14+
for (const path of paths) {
15+
const mimeType = await getMimeType(path);
16+
document.body.innerHTML += '<br>"' + path + '": ' + mimeType;
17+
}
18+
}
19+
20+
if (document.readyState === "loading") {
21+
window.addEventListener("DOMContentLoaded", checkMimes);
22+
} else {
23+
checkMimes();
24+
}

test/rollup.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ export default {
2727
open: true,
2828
openPage: '/frames.html',
2929
historyApiFallback: '/fallback.html',
30-
contentBase: ['.', 'base1', 'base2'],
30+
contentBase: ['.', 'base1', 'base2', 'base3'],
3131
onListening: testOnListening(),
32+
mimeTypes: {
33+
'text/x-abc': ['abc'],
34+
}
3235
})
3336
]
3437
}

0 commit comments

Comments
 (0)