@rollup-extras/plugin-serve 1.3.4 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -26
- package/exports.d.ts +26 -0
- package/index.d.ts +28 -0
- package/index.js +130 -0
- package/package.json +15 -30
- package/dist/index.cjs +0 -99
- package/dist/index.d.ts +0 -3
- package/dist/index.mjs +0 -97
- package/dist/types.d.ts +0 -20
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Rollup plugin for a dev server.
|
|
|
4
4
|
|
|
5
5
|
Points:
|
|
6
6
|
|
|
7
|
-
- Uses `
|
|
7
|
+
- Uses `hono`, customizable through Hono middleware
|
|
8
8
|
- Zero-config by default (works if you have `output.dir` defined)
|
|
9
9
|
|
|
10
10
|
Uses [`@niceties/logger`](https://github.com/kshutkin/niceties/blob/main/logger/README.md) to log messages, which can be configured through `@niceties/logger` API.
|
|
@@ -14,6 +14,7 @@ Uses [`@niceties/logger`](https://github.com/kshutkin/niceties/blob/main/logger/
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
16
16
|
Using npm:
|
|
17
|
+
|
|
17
18
|
```
|
|
18
19
|
npm install --save-dev @rollup-extras/plugin-serve
|
|
19
20
|
```
|
|
@@ -21,18 +22,18 @@ npm install --save-dev @rollup-extras/plugin-serve
|
|
|
21
22
|
## Usage
|
|
22
23
|
|
|
23
24
|
```javascript
|
|
24
|
-
import serve from
|
|
25
|
+
import serve from "@rollup-extras/plugin-serve";
|
|
25
26
|
|
|
26
27
|
export default {
|
|
27
|
-
|
|
28
|
+
input: "src/index.js",
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
output: {
|
|
31
|
+
format: "es",
|
|
32
|
+
dir: "dest",
|
|
33
|
+
},
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
}
|
|
35
|
+
plugins: [serve()],
|
|
36
|
+
};
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
## Providing options
|
|
@@ -40,7 +41,7 @@ export default {
|
|
|
40
41
|
Just pass options to the plugin function. The returned object is the plugin instance which can be passed to rollup.
|
|
41
42
|
|
|
42
43
|
```javascript
|
|
43
|
-
serve({option: value, option2: value2})
|
|
44
|
+
serve({ option: value, option2: value2 });
|
|
44
45
|
```
|
|
45
46
|
|
|
46
47
|
For additional plugin instances (in case of multiple configs) please use `firstInstance.api.addInstance()`
|
|
@@ -63,7 +64,7 @@ Option to use `writeBundle` hook instead of `generateBundle`.
|
|
|
63
64
|
|
|
64
65
|
Optional, `string` | `string[]`, default: `output.dir`.
|
|
65
66
|
|
|
66
|
-
Defines what dir to serve using
|
|
67
|
+
Defines what dir to serve using Hono static middleware. If you want to disable static serving you can use `[]` (empty array) as `dirs`.
|
|
67
68
|
|
|
68
69
|
### port
|
|
69
70
|
|
|
@@ -75,19 +76,19 @@ Port to use for the server.
|
|
|
75
76
|
|
|
76
77
|
Optional, `string`.
|
|
77
78
|
|
|
78
|
-
Host to use, by default, does not provide a host to createServer and lets
|
|
79
|
+
Host to use, by default, does not provide a host to createServer and lets Node.js decide.
|
|
79
80
|
|
|
80
|
-
###
|
|
81
|
+
### useLogger
|
|
81
82
|
|
|
82
83
|
Optional, `boolean`, default: `true`.
|
|
83
84
|
|
|
84
|
-
If the plugin should use
|
|
85
|
+
If the plugin should use Hono logger middleware.
|
|
85
86
|
|
|
86
|
-
###
|
|
87
|
+
### staticOptions
|
|
87
88
|
|
|
88
89
|
Optional.
|
|
89
90
|
|
|
90
|
-
Please check [
|
|
91
|
+
Please check [`@hono/node-server/serve-static`](https://hono.dev/docs/getting-started/nodejs#serve-static-files) for options.
|
|
91
92
|
|
|
92
93
|
### https
|
|
93
94
|
|
|
@@ -95,11 +96,11 @@ Optional, `{ cert: string, key: string, ca?: string; }`.
|
|
|
95
96
|
|
|
96
97
|
Key and certificate to use for https. The best way to generate a certificate and key (and to install ca) is [`mkcert`](https://github.com/FiloSottile/mkcert).
|
|
97
98
|
|
|
98
|
-
###
|
|
99
|
+
### customize
|
|
99
100
|
|
|
100
|
-
Optional, `(
|
|
101
|
+
Optional, `(app: Hono) => void`
|
|
101
102
|
|
|
102
|
-
Extension point to customize
|
|
103
|
+
Extension point to customize the Hono app.
|
|
103
104
|
|
|
104
105
|
### onListen
|
|
105
106
|
|
|
@@ -115,15 +116,15 @@ type ServePluginOptions = {
|
|
|
115
116
|
useWriteBundle?: boolean;
|
|
116
117
|
dirs?: string | string[];
|
|
117
118
|
port?: number;
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
useLogger?: boolean;
|
|
120
|
+
staticOptions?: object;
|
|
120
121
|
host?: string;
|
|
121
122
|
https?: {
|
|
122
|
-
|
|
123
|
-
|
|
123
|
+
cert: string;
|
|
124
|
+
key: string;
|
|
124
125
|
ca?: string;
|
|
125
|
-
}
|
|
126
|
-
|
|
126
|
+
};
|
|
127
|
+
customize?: (app: Hono) => void;
|
|
127
128
|
onListen?: (server: Server) => void | true;
|
|
128
129
|
} | string | string[]
|
|
129
130
|
```
|
|
@@ -137,4 +138,4 @@ type ServePluginOptions = {
|
|
|
137
138
|
|
|
138
139
|
# License
|
|
139
140
|
|
|
140
|
-
[MIT](https://github.com/kshutkin/rollup-extras/blob/main/LICENSE)
|
|
141
|
+
[MIT](https://github.com/kshutkin/rollup-extras/blob/main/LICENSE)
|
package/exports.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Server } from 'node:http';
|
|
2
|
+
|
|
3
|
+
import type { ServeStaticOptions } from '@hono/node-server/serve-static';
|
|
4
|
+
import type { Hono } from 'hono';
|
|
5
|
+
import type { Plugin } from 'rollup';
|
|
6
|
+
|
|
7
|
+
export type ServePluginOptionsObject = {
|
|
8
|
+
pluginName?: string;
|
|
9
|
+
useWriteBundle?: boolean;
|
|
10
|
+
dirs?: string | string[];
|
|
11
|
+
port?: number;
|
|
12
|
+
useLogger?: boolean;
|
|
13
|
+
staticOptions?: Omit<ServeStaticOptions, 'root' | 'path'>;
|
|
14
|
+
host?: string;
|
|
15
|
+
https?: {
|
|
16
|
+
cert: string;
|
|
17
|
+
key: string;
|
|
18
|
+
ca?: string;
|
|
19
|
+
};
|
|
20
|
+
customize?: (app: Hono) => void;
|
|
21
|
+
onListen?: (server: Server) => undefined | true;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type ServePluginOptions = ServePluginOptionsObject | string | string[];
|
|
25
|
+
|
|
26
|
+
export default function serve(options?: ServePluginOptions): Plugin;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
declare module '@rollup-extras/plugin-serve' {
|
|
2
|
+
import type { Server } from 'node:http';
|
|
3
|
+
import type { ServeStaticOptions } from '@hono/node-server/serve-static';
|
|
4
|
+
import type { Hono } from 'hono';
|
|
5
|
+
import type { Plugin } from 'rollup';
|
|
6
|
+
export type ServePluginOptionsObject = {
|
|
7
|
+
pluginName?: string;
|
|
8
|
+
useWriteBundle?: boolean;
|
|
9
|
+
dirs?: string | string[];
|
|
10
|
+
port?: number;
|
|
11
|
+
useLogger?: boolean;
|
|
12
|
+
staticOptions?: Omit<ServeStaticOptions, 'root' | 'path'>;
|
|
13
|
+
host?: string;
|
|
14
|
+
https?: {
|
|
15
|
+
cert: string;
|
|
16
|
+
key: string;
|
|
17
|
+
ca?: string;
|
|
18
|
+
};
|
|
19
|
+
customize?: (app: Hono) => void;
|
|
20
|
+
onListen?: (server: Server) => undefined | true;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type ServePluginOptions = ServePluginOptionsObject | string | string[];
|
|
24
|
+
|
|
25
|
+
export default function serve(options?: ServePluginOptions): Plugin;
|
|
26
|
+
|
|
27
|
+
export {};
|
|
28
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createServer as createHttpsServer } from 'node:https';
|
|
2
|
+
|
|
3
|
+
import { createAdaptorServer } from '@hono/node-server';
|
|
4
|
+
import { serveStatic } from '@hono/node-server/serve-static';
|
|
5
|
+
import { Hono } from 'hono';
|
|
6
|
+
import { logger as honoLogger } from 'hono/logger';
|
|
7
|
+
|
|
8
|
+
import { LogLevel } from '@niceties/logger';
|
|
9
|
+
import logger from '@rollup-extras/utils/logger';
|
|
10
|
+
import { multiConfigPluginBase } from '@rollup-extras/utils/multi-config-plugin-base';
|
|
11
|
+
import { getOptions } from '@rollup-extras/utils/options';
|
|
12
|
+
|
|
13
|
+
let globalServer;
|
|
14
|
+
|
|
15
|
+
const factories = { logger };
|
|
16
|
+
|
|
17
|
+
export default function (options = {}) {
|
|
18
|
+
const normalizedOptions = getOptions(
|
|
19
|
+
options,
|
|
20
|
+
{
|
|
21
|
+
pluginName: '@rollup-extras/plugin-serve',
|
|
22
|
+
useWriteBundle: true,
|
|
23
|
+
port: 8080,
|
|
24
|
+
useLogger: true,
|
|
25
|
+
},
|
|
26
|
+
'dirs',
|
|
27
|
+
factories
|
|
28
|
+
);
|
|
29
|
+
const { pluginName, useWriteBundle, port, host, https, useLogger, customize, staticOptions, onListen, logger } = normalizedOptions;
|
|
30
|
+
const instance = multiConfigPluginBase(useWriteBundle, pluginName, serve);
|
|
31
|
+
|
|
32
|
+
let { dirs } = normalizedOptions,
|
|
33
|
+
collectDirs = false,
|
|
34
|
+
started = false,
|
|
35
|
+
watchMode = true;
|
|
36
|
+
|
|
37
|
+
const pluginInstance = { ...instance, outputOptions };
|
|
38
|
+
|
|
39
|
+
if (!dirs) {
|
|
40
|
+
dirs = [];
|
|
41
|
+
collectDirs = true;
|
|
42
|
+
pluginInstance.renderStart = renderStart;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return (pluginInstance);
|
|
46
|
+
|
|
47
|
+
function renderStart( outputOptions, inputOptions) {
|
|
48
|
+
|
|
49
|
+
( (instance).renderStart).call(this, outputOptions, inputOptions);
|
|
50
|
+
if (collectDirs) {
|
|
51
|
+
if (outputOptions.dir) {
|
|
52
|
+
(dirs).push(outputOptions.dir);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function outputOptions() {
|
|
58
|
+
watchMode = this.meta.watchMode;
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function serve() {
|
|
63
|
+
if (!started && watchMode) {
|
|
64
|
+
started = true;
|
|
65
|
+
|
|
66
|
+
if (globalServer) {
|
|
67
|
+
await new Promise(resolve => (globalServer).close(resolve));
|
|
68
|
+
globalServer = undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const app = new Hono();
|
|
72
|
+
|
|
73
|
+
if (customize) {
|
|
74
|
+
customize(app);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (useLogger) {
|
|
78
|
+
app.use('*', honoLogger());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
for (const dir of (dirs)) {
|
|
82
|
+
app.use('*', serveStatic({ root: dir, ...staticOptions }));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const server = createAdaptorServer({
|
|
86
|
+
fetch: app.fetch,
|
|
87
|
+
...(https ? { createServer: createHttpsServer, serverOptions: https } : {}),
|
|
88
|
+
});
|
|
89
|
+
globalServer = server;
|
|
90
|
+
|
|
91
|
+
const listenCb = () => internalOnListen(server);
|
|
92
|
+
|
|
93
|
+
if (host) {
|
|
94
|
+
server.listen(port, host, listenCb);
|
|
95
|
+
} else {
|
|
96
|
+
server.listen(port, listenCb);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
server.on('error', ( e) => {
|
|
100
|
+
if (e.code === 'EADDRINUSE') {
|
|
101
|
+
logger.finish('address in use, please try another port', LogLevel.error);
|
|
102
|
+
} else {
|
|
103
|
+
throw e;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function internalOnListen(server) {
|
|
110
|
+
if (!onListen || !onListen( (server))) {
|
|
111
|
+
logger.finish(`listening on ${linkFromAddress(server.address(), !!https)}`, LogLevel.info);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function linkFromAddress(address, https) {
|
|
117
|
+
if (address && typeof address !== 'string') {
|
|
118
|
+
return linkFromAddressInfo(address, https);
|
|
119
|
+
}
|
|
120
|
+
return `${address}`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function linkFromAddressInfo({ address, port, family }, https) {
|
|
124
|
+
let serverName = family === 'IPv6' ? `[${address}]` : address;
|
|
125
|
+
if (serverName === '[::]') {
|
|
126
|
+
serverName = 'localhost';
|
|
127
|
+
}
|
|
128
|
+
const protocol = `http${https ? 's' : ''}://`;
|
|
129
|
+
return `${protocol}${serverName}:${port}`;
|
|
130
|
+
}
|
package/package.json
CHANGED
|
@@ -1,25 +1,21 @@
|
|
|
1
1
|
{
|
|
2
|
-
"
|
|
3
|
-
"version": "1.3.4",
|
|
4
|
-
"name": "@rollup-extras/plugin-serve",
|
|
5
|
-
"description": "Rollup plugin for dev server based on koa.",
|
|
2
|
+
"version": "2.0.0",
|
|
6
3
|
"license": "MIT",
|
|
4
|
+
"name": "@rollup-extras/plugin-serve",
|
|
7
5
|
"author": "Konstantin Shutkin",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./index.d.ts",
|
|
10
8
|
"exports": {
|
|
11
9
|
".": {
|
|
12
|
-
"
|
|
13
|
-
"default": "./
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
14
12
|
},
|
|
15
13
|
"./package.json": "./package.json"
|
|
16
14
|
},
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"LICENSE"
|
|
20
|
-
],
|
|
15
|
+
"main": "./index.js",
|
|
16
|
+
"module": "./index.js",
|
|
21
17
|
"engines": {
|
|
22
|
-
"node": ">=
|
|
18
|
+
"node": ">=18.14.1"
|
|
23
19
|
},
|
|
24
20
|
"repository": {
|
|
25
21
|
"type": "git",
|
|
@@ -29,35 +25,24 @@
|
|
|
29
25
|
"bugs": "https://github.com/kshutkin/rollup-extras/issues",
|
|
30
26
|
"homepage": "https://github.com/kshutkin/rollup-extras/blob/main/plugin-serve/README.md",
|
|
31
27
|
"readme": "README.md",
|
|
28
|
+
"description": "Rollup plugin for dev server based on Hono.",
|
|
32
29
|
"keywords": [
|
|
33
30
|
"rollup-plugin",
|
|
34
31
|
"server",
|
|
35
32
|
"serve"
|
|
36
33
|
],
|
|
37
34
|
"dependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"@
|
|
42
|
-
"@rollup-extras/utils": "^1.4.4"
|
|
43
|
-
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"@types/koa": "^2.13.4",
|
|
46
|
-
"@types/koa-logger": "^3.1.2",
|
|
47
|
-
"@types/koa-static": "^4.0.2"
|
|
35
|
+
"@niceties/logger": "^2.1.0",
|
|
36
|
+
"@hono/node-server": "^1.19.11",
|
|
37
|
+
"hono": "^4.12.8",
|
|
38
|
+
"@rollup-extras/utils": "^2.0.0"
|
|
48
39
|
},
|
|
49
40
|
"peerDependencies": {
|
|
50
|
-
"rollup": "^
|
|
41
|
+
"rollup": "^4.0.0"
|
|
51
42
|
},
|
|
52
43
|
"peerDependenciesMeta": {
|
|
53
44
|
"rollup": {
|
|
54
45
|
"optional": true
|
|
55
46
|
}
|
|
56
|
-
},
|
|
57
|
-
"types": "./dist/index.d.ts",
|
|
58
|
-
"scripts": {
|
|
59
|
-
"build": "pkgbld-internal",
|
|
60
|
-
"test": "jest --collectCoverage",
|
|
61
|
-
"lint": "eslint ./src"
|
|
62
47
|
}
|
|
63
48
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var http = require('http');
|
|
4
|
-
var https = require('https');
|
|
5
|
-
var Koa = require('koa');
|
|
6
|
-
var koaLogger = require('koa-logger');
|
|
7
|
-
var serveStatic = require('koa-static');
|
|
8
|
-
var options = require('@rollup-extras/utils/options');
|
|
9
|
-
var logger = require('@rollup-extras/utils/logger');
|
|
10
|
-
var mutliConfigPluginBase = require('@rollup-extras/utils/mutli-config-plugin-base');
|
|
11
|
-
|
|
12
|
-
let globalServer;
|
|
13
|
-
const factories = { logger };
|
|
14
|
-
function index (options$1 = {}) {
|
|
15
|
-
const normalizedOptions = options.getOptions(options$1, {
|
|
16
|
-
pluginName: '@rollup-extras/plugin-serve',
|
|
17
|
-
useWriteBundle: true,
|
|
18
|
-
port: 8080,
|
|
19
|
-
useKoaLogger: true
|
|
20
|
-
}, 'dirs', factories);
|
|
21
|
-
const { pluginName, useWriteBundle, port, host, https: https$1, useKoaLogger, customizeKoa, koaStaticOptions, onListen, logger } = normalizedOptions;
|
|
22
|
-
const instance = mutliConfigPluginBase.multiConfigPluginBase(useWriteBundle, pluginName, serve);
|
|
23
|
-
let { dirs } = normalizedOptions, collectDirs = false, started = false, watchMode = true;
|
|
24
|
-
const pluginInstance = { ...instance, outputOptions };
|
|
25
|
-
if (!dirs) {
|
|
26
|
-
dirs = [];
|
|
27
|
-
collectDirs = true;
|
|
28
|
-
pluginInstance.renderStart = renderStart;
|
|
29
|
-
}
|
|
30
|
-
return pluginInstance;
|
|
31
|
-
function renderStart(outputOptions, inputOptions) {
|
|
32
|
-
instance.renderStart.call(this, outputOptions, inputOptions);
|
|
33
|
-
if (collectDirs) {
|
|
34
|
-
if (outputOptions.dir) {
|
|
35
|
-
dirs.push(outputOptions.dir);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
function outputOptions() {
|
|
40
|
-
watchMode = this.meta.watchMode;
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
async function serve() {
|
|
44
|
-
if (!started && watchMode) {
|
|
45
|
-
started = true;
|
|
46
|
-
if (globalServer) {
|
|
47
|
-
await new Promise(resolve => globalServer.close(resolve));
|
|
48
|
-
globalServer = undefined;
|
|
49
|
-
}
|
|
50
|
-
const app = new Koa();
|
|
51
|
-
if (customizeKoa) {
|
|
52
|
-
customizeKoa(app);
|
|
53
|
-
}
|
|
54
|
-
if (useKoaLogger) {
|
|
55
|
-
app.use(koaLogger());
|
|
56
|
-
}
|
|
57
|
-
for (const dir of dirs) {
|
|
58
|
-
app.use(serveStatic(dir, koaStaticOptions));
|
|
59
|
-
}
|
|
60
|
-
const server = globalServer = https$1 ? https.createServer(https$1, app.callback()) : http.createServer(app.callback());
|
|
61
|
-
const listenCb = () => internalOnListen(server);
|
|
62
|
-
if (host) {
|
|
63
|
-
server.listen(port, host, listenCb);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
server.listen(port, listenCb);
|
|
67
|
-
}
|
|
68
|
-
server.on('error', (e) => {
|
|
69
|
-
if (e.code === 'EADDRINUSE') {
|
|
70
|
-
logger.finish('address in use, please try another port', 3 /* LogLevel.error */);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
throw e;
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
function internalOnListen(server) {
|
|
79
|
-
if (!onListen || !onListen(server)) {
|
|
80
|
-
logger.finish(`listening on ${linkFromAddress(server.address(), !!https$1)}`, 1 /* LogLevel.info */);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
function linkFromAddress(address, https) {
|
|
85
|
-
if (address && typeof address !== 'string') {
|
|
86
|
-
return linkFromAddressInfo(address, https);
|
|
87
|
-
}
|
|
88
|
-
return `${address}`;
|
|
89
|
-
}
|
|
90
|
-
function linkFromAddressInfo({ address, port, family }, https) {
|
|
91
|
-
let serverName = family === 'IPv6' ? `[${address}]` : address;
|
|
92
|
-
if (serverName === '[::]') {
|
|
93
|
-
serverName = 'localhost';
|
|
94
|
-
}
|
|
95
|
-
const protocol = `http${https ? 's' : ''}://`;
|
|
96
|
-
return `${protocol}${serverName}:${port}`;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = index;
|
package/dist/index.d.ts
DELETED
package/dist/index.mjs
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { createServer as createServer$1 } from 'http';
|
|
2
|
-
import { createServer } from 'https';
|
|
3
|
-
import Koa from 'koa';
|
|
4
|
-
import koaLogger from 'koa-logger';
|
|
5
|
-
import serveStatic from 'koa-static';
|
|
6
|
-
import { getOptions } from '@rollup-extras/utils/options';
|
|
7
|
-
import logger from '@rollup-extras/utils/logger';
|
|
8
|
-
import { multiConfigPluginBase } from '@rollup-extras/utils/mutli-config-plugin-base';
|
|
9
|
-
|
|
10
|
-
let globalServer;
|
|
11
|
-
const factories = { logger };
|
|
12
|
-
function index (options = {}) {
|
|
13
|
-
const normalizedOptions = getOptions(options, {
|
|
14
|
-
pluginName: '@rollup-extras/plugin-serve',
|
|
15
|
-
useWriteBundle: true,
|
|
16
|
-
port: 8080,
|
|
17
|
-
useKoaLogger: true
|
|
18
|
-
}, 'dirs', factories);
|
|
19
|
-
const { pluginName, useWriteBundle, port, host, https, useKoaLogger, customizeKoa, koaStaticOptions, onListen, logger } = normalizedOptions;
|
|
20
|
-
const instance = multiConfigPluginBase(useWriteBundle, pluginName, serve);
|
|
21
|
-
let { dirs } = normalizedOptions, collectDirs = false, started = false, watchMode = true;
|
|
22
|
-
const pluginInstance = { ...instance, outputOptions };
|
|
23
|
-
if (!dirs) {
|
|
24
|
-
dirs = [];
|
|
25
|
-
collectDirs = true;
|
|
26
|
-
pluginInstance.renderStart = renderStart;
|
|
27
|
-
}
|
|
28
|
-
return pluginInstance;
|
|
29
|
-
function renderStart(outputOptions, inputOptions) {
|
|
30
|
-
instance.renderStart.call(this, outputOptions, inputOptions);
|
|
31
|
-
if (collectDirs) {
|
|
32
|
-
if (outputOptions.dir) {
|
|
33
|
-
dirs.push(outputOptions.dir);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
function outputOptions() {
|
|
38
|
-
watchMode = this.meta.watchMode;
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
async function serve() {
|
|
42
|
-
if (!started && watchMode) {
|
|
43
|
-
started = true;
|
|
44
|
-
if (globalServer) {
|
|
45
|
-
await new Promise(resolve => globalServer.close(resolve));
|
|
46
|
-
globalServer = undefined;
|
|
47
|
-
}
|
|
48
|
-
const app = new Koa();
|
|
49
|
-
if (customizeKoa) {
|
|
50
|
-
customizeKoa(app);
|
|
51
|
-
}
|
|
52
|
-
if (useKoaLogger) {
|
|
53
|
-
app.use(koaLogger());
|
|
54
|
-
}
|
|
55
|
-
for (const dir of dirs) {
|
|
56
|
-
app.use(serveStatic(dir, koaStaticOptions));
|
|
57
|
-
}
|
|
58
|
-
const server = globalServer = https ? createServer(https, app.callback()) : createServer$1(app.callback());
|
|
59
|
-
const listenCb = () => internalOnListen(server);
|
|
60
|
-
if (host) {
|
|
61
|
-
server.listen(port, host, listenCb);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
server.listen(port, listenCb);
|
|
65
|
-
}
|
|
66
|
-
server.on('error', (e) => {
|
|
67
|
-
if (e.code === 'EADDRINUSE') {
|
|
68
|
-
logger.finish('address in use, please try another port', 3 /* LogLevel.error */);
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
throw e;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
function internalOnListen(server) {
|
|
77
|
-
if (!onListen || !onListen(server)) {
|
|
78
|
-
logger.finish(`listening on ${linkFromAddress(server.address(), !!https)}`, 1 /* LogLevel.info */);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function linkFromAddress(address, https) {
|
|
83
|
-
if (address && typeof address !== 'string') {
|
|
84
|
-
return linkFromAddressInfo(address, https);
|
|
85
|
-
}
|
|
86
|
-
return `${address}`;
|
|
87
|
-
}
|
|
88
|
-
function linkFromAddressInfo({ address, port, family }, https) {
|
|
89
|
-
let serverName = family === 'IPv6' ? `[${address}]` : address;
|
|
90
|
-
if (serverName === '[::]') {
|
|
91
|
-
serverName = 'localhost';
|
|
92
|
-
}
|
|
93
|
-
const protocol = `http${https ? 's' : ''}://`;
|
|
94
|
-
return `${protocol}${serverName}:${port}`;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export { index as default };
|
package/dist/types.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import Koa from 'koa';
|
|
3
|
-
import serve from 'koa-static';
|
|
4
|
-
import { Server } from 'http';
|
|
5
|
-
export type ServePluginOptions = {
|
|
6
|
-
pluginName?: string;
|
|
7
|
-
useWriteBundle?: boolean;
|
|
8
|
-
dirs?: string | string[];
|
|
9
|
-
port?: number;
|
|
10
|
-
useKoaLogger?: boolean;
|
|
11
|
-
koaStaticOptions?: serve.Options;
|
|
12
|
-
host?: string;
|
|
13
|
-
https?: {
|
|
14
|
-
cert: string;
|
|
15
|
-
key: string;
|
|
16
|
-
ca?: string;
|
|
17
|
-
};
|
|
18
|
-
customizeKoa?: (koa: Koa) => void;
|
|
19
|
-
onListen?: (server: Server) => void | true;
|
|
20
|
-
} | string | string[];
|