Skip to content

Commit abe02a0

Browse files
committed
feat: Add major features from webpack-dev-server
* compress * proxy * before * after * historyApiFallback (object format) * contentBasePublicPath * serveIndex * headers
1 parent 9571820 commit abe02a0

7 files changed

Lines changed: 999 additions & 78 deletions

File tree

README.md

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,24 @@ serve({
6565
// Multiple folders to serve from
6666
contentBase: ['dist', 'static'],
6767

68+
// URL root path to serve the static content at
69+
contentBasePublicPath: '/',
70+
71+
// Customize serving of static files, see https://expressjs.com/en/resources/middleware/serve-static.html
72+
staticOptions: {},
73+
74+
// Set to true to enable the default compression, or to an object to customize the compression
75+
// according to https://expressjs.com/en/resources/middleware/compression.html
76+
compress: false,
77+
78+
// Set to true to enable directory indexes, or to an object to customize the index generation
79+
// according to https://expressjs.com/en/resources/middleware/serve-index.html
80+
serveIndex: false,
81+
6882
// Set to true to return index.html (200) instead of error page (404)
6983
historyApiFallback: false,
7084

71-
// Path to fallback page
85+
// Path to fallback page, see also https://github.com/bripkens/connect-history-api-fallback
7286
historyApiFallback: '/200.html',
7387

7488
// Options used in setting up server
@@ -82,18 +96,38 @@ serve({
8296
ca: fs.readFileSync('/path/to/ca.pem')
8397
},
8498

85-
//set headers
99+
// Set additional headers
86100
headers: {
87101
'Access-Control-Allow-Origin': '*',
88102
foo: 'bar'
89103
},
90104

91-
// set custom mime types, usage https://github.com/broofa/mime#mimedefinetypemap-force--false
105+
// Set custom mime types, usage https://github.com/broofa/mime#mimedefinetypemap-force--false
92106
mimeTypes: {
93107
'application/javascript': ['js_commonjs-proxy']
94-
}
108+
},
109+
110+
// Let requests to /api/* be forwarded to http://localhost:3000/*
111+
// See the full documentation at https://webpack.js.org/configuration/dev-server/#devserverproxy
112+
proxy: [
113+
{
114+
context: ['/api'],
115+
target: 'http://localhost:3000',
116+
pathRewrite: { '^/api': '' },
117+
},
118+
],
119+
120+
// Customize the Express instance before all middlewares are installed
121+
before: function(app) {
122+
...
123+
},
124+
125+
// Customize the Express instance after all middlewares are installed
126+
after: function(app) {
127+
...
128+
},
95129

96-
// execute function after server has begun listening
130+
// Execute function after server has begun listening
97131
onListening: function (server) {
98132
const address = server.address()
99133
const host = address.address === '::' ? 'localhost' : address.address

index.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Plugin } from 'rollup'
22
import { IncomingHttpHeaders, OutgoingHttpHeaders, Server } from 'http'
33
import { ServerOptions } from 'https'
44
import { TypeMap } from 'mime'
5+
import { Application } from 'express'
56

67
export interface RollupServeOptions {
78
/**
@@ -26,6 +27,29 @@ export interface RollupServeOptions {
2627
*/
2728
contentBase?: string | string[]
2829

30+
/**
31+
* URL root path to serve the static content at (default: `'/'`)
32+
*/
33+
contentBasePublicPath?: string
34+
35+
/**
36+
* Options to be passed to the serve-static middleware, see the documentation
37+
* at https://expressjs.com/en/resources/middleware/serve-static.html.
38+
*/
39+
staticOptions?: object
40+
41+
/**
42+
* Enable compression of the served content. If you want to customize the default
43+
* settings, see https://expressjs.com/en/resources/middleware/compression.html.
44+
*/
45+
compress?: boolean | object
46+
47+
/**
48+
* Enable directory indexes. If you want to customize the default
49+
* settings, see https://expressjs.com/en/resources/middleware/serve-index.html.
50+
*/
51+
serveIndex?: boolean | object
52+
2953
/**
3054
* Set to `true` to return index.html (200) instead of error page (404)
3155
* or path to fallback page
@@ -63,6 +87,23 @@ export interface RollupServeOptions {
6387
*/
6488
mimeTypes?: TypeMap
6589

90+
/**
91+
* Let some requests be forwarded to other servers by their URL path. For example,
92+
* requests to /api/* be can be forwarded to http://localhost:3000/*. See the full
93+
* documentation at https://webpack.js.org/configuration/dev-server/#devserverproxy.
94+
*/
95+
proxy?: object[]
96+
97+
/**
98+
* Function to be called before all Express middlewares are installed.
99+
*/
100+
before?: (app: Application) => void
101+
102+
/**
103+
* Function to be called after all Express middlewares are installed.
104+
*/
105+
after?: (app: Application) => void
106+
66107
/**
67108
* Execute function after server has begun listening
68109
*/

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@
4040
],
4141
"type": "module",
4242
"dependencies": {
43+
"compression": "^1.8.1",
44+
"connect-history-api-fallback": "^2.0.0",
45+
"express": "^5.2.1",
46+
"http-proxy-middleware": "^3.0.5",
47+
"killable": "^1.0.1",
4348
"mime": "^4",
44-
"opener": "1"
49+
"opener": "1",
50+
"serve-index": "^1.9.1"
4551
},
4652
"devDependencies": {
4753
"@rollup/plugin-buble": "^1.0.0",

rollup.config.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,25 @@ export default {
66
{ file: 'dist/index.cjs', format: 'cjs', exports: 'default' },
77
{ file: 'dist/index.mjs', format: 'esm' }
88
],
9-
plugins: [buble()],
10-
external: ['fs', 'https', 'http', 'path', 'mime', 'opener']
9+
plugins: [
10+
buble({
11+
transforms: { forOf: false }
12+
})
13+
],
14+
external: [
15+
'fs',
16+
'https',
17+
'http',
18+
'path',
19+
'mime/lite',
20+
'mime/types/standard.js',
21+
'mime/types/other.js',
22+
'opener',
23+
'express',
24+
'killable',
25+
'compression',
26+
'serve-index',
27+
'connect-history-api-fallback',
28+
'http-proxy-middleware'
29+
]
1130
}

0 commit comments

Comments
 (0)