@rollup-extras/plugin-serve 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Konstantin Shutkin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # Plugin Serve
2
+
3
+ Rollup plugin for dev server.
4
+
5
+ Points:
6
+
7
+ - Uses `koa`, customizable through koa middleware
8
+ - Zero config by default (works if you have output.dir defined)
9
+
10
+ Uses [`@niceties/logger`](https://github.com/kshutkin/niceties/blob/main/logger/README.md) to log messages, can be configured through `@niceties/logger` API.
11
+
12
+ [Changlelog](./CHANGELOG.md)
13
+
14
+ ## Installation
15
+
16
+ Using npm:
17
+ ```
18
+ npm install --save-dev @rollup-extras/plugin-serve
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```javascript
24
+ import serve from '@rollup-extras/plugin-serve';
25
+
26
+ export default {
27
+ input: 'src/index.js',
28
+
29
+ output: {
30
+ format: 'es',
31
+ dir: 'dest'
32
+ },
33
+
34
+ plugins: [serve()],
35
+ }
36
+ ```
37
+
38
+ ## Providing options
39
+
40
+ Just pass options to the plugin function. Returned object is the plugin instance which can be passed to rollup.
41
+
42
+ ```javascript
43
+ serve({option: value, option2: value2})
44
+ ```
45
+
46
+ For additional plugin instances (in case of multiple configs) please use: `firstInstance.api.addInstance()`
47
+
48
+ ## Options
49
+
50
+ ### pluginName
51
+
52
+ Optional, `string`.
53
+
54
+ For debugging purposes, so many instances of a plugin can be differenciated in debugging output.
55
+
56
+ ### useWriteBundle
57
+
58
+ Optional, `boolean`, default: `true`.
59
+
60
+ Option to use `writeBundle` hook instead of `generateBundle`.
61
+
62
+ ### dirs
63
+
64
+ Optional, `string` | `string[]`, default: `output.dir`.
65
+
66
+ Defines what dir to serve using `koa-static` middleware. If you want to disable `koa-static` you can use `[]` (empty array) as `dirs`.
67
+
68
+ ### port
69
+
70
+ Optional, `number`, default: `8080`.
71
+
72
+ Port to use for server.
73
+
74
+ ### host
75
+
76
+ Optional, `string`.
77
+
78
+ Host to use, by default it does not provide host to createServer and lets nodejs to decide.
79
+
80
+ ### useKoaLogger
81
+
82
+ Optional, `boolean`, default: `true`.
83
+
84
+ If plugin should use koa-logger middleware.
85
+
86
+ ### koaStaticOptions
87
+
88
+ Optional.
89
+
90
+ Please check [`koa-static`](https://github.com/koajs/static) for options.
91
+
92
+ ### https
93
+
94
+ Optional, `{ cert: string, key: string, ca?: string; }`.
95
+
96
+ Key and certificate to use for https. Best way to generate cert and key (and to install ca) is [`mkcert`](https://github.com/FiloSottile/mkcert).
97
+
98
+ ### customizeKoa
99
+
100
+ Optional, `(koa: Koa) => void`
101
+
102
+ Extention point to customize `koa`.
103
+
104
+ ### onListen
105
+
106
+ Optional, `(server: Server) => void | true`
107
+
108
+ Extention point after server is live. Please return true to suppress default banner.
109
+
110
+ ## Configuration
111
+
112
+ ```typescript
113
+ type ServePluginOptions = {
114
+ pluginName?: string;
115
+ useWriteBundle?: boolean;
116
+ dirs?: string | string[];
117
+ port?: number;
118
+ useKoaLogger?: boolean;
119
+ koaStaticOptions?: 'koa-static'.Options;
120
+ host?: string;
121
+ https?: {
122
+ cert: string;
123
+ key: string;
124
+ ca?: string;
125
+ },
126
+ customizeKoa?: (koa: Koa) => void;
127
+ onListen?: (server: Server) => void | true;
128
+ } | string | string[]
129
+ ```
130
+
131
+ ## Prior Art
132
+
133
+ - https://github.com/thgh/rollup-plugin-serve
134
+ - https://github.com/pearofducks/rollup-plugin-dev
135
+ - https://github.com/modernweb-dev/web/tree/master/packages/dev-server-rollup
136
+ - https://github.com/lukeed/sirv (and any another standalone server)
137
+
138
+ # License
139
+
140
+ [MIT](./LICENSE)
package/dist/index.cjs ADDED
@@ -0,0 +1,103 @@
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
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
+
14
+ var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
15
+ var koaLogger__default = /*#__PURE__*/_interopDefaultLegacy(koaLogger);
16
+ var serveStatic__default = /*#__PURE__*/_interopDefaultLegacy(serveStatic);
17
+ var logger__default = /*#__PURE__*/_interopDefaultLegacy(logger);
18
+
19
+ let globalServer;
20
+ const factories = { logger: logger__default["default"] };
21
+ function index (options$1 = {}) {
22
+ const normalizedOptions = options.getOptions(options$1, {
23
+ pluginName: '@rollup-extras/plugin-serve',
24
+ useWriteBundle: true,
25
+ port: 8080,
26
+ useKoaLogger: true
27
+ }, 'dirs', factories);
28
+ const { pluginName, useWriteBundle, port, host, https: https$1, useKoaLogger, customizeKoa, koaStaticOptions, onListen, logger } = normalizedOptions;
29
+ const instance = mutliConfigPluginBase.multiConfigPluginBase(useWriteBundle, pluginName, serve);
30
+ let { dirs } = normalizedOptions, collectDirs = false, started = false, watchMode = true;
31
+ const pluginInstance = Object.assign(Object.assign({}, instance), { outputOptions });
32
+ if (!dirs) {
33
+ dirs = [];
34
+ collectDirs = true;
35
+ pluginInstance.renderStart = renderStart;
36
+ }
37
+ return pluginInstance;
38
+ function renderStart(outputOptions, inputOptions) {
39
+ instance.renderStart.call(this, outputOptions, inputOptions);
40
+ if (collectDirs) {
41
+ if (outputOptions.dir) {
42
+ dirs.push(outputOptions.dir);
43
+ }
44
+ }
45
+ }
46
+ function outputOptions() {
47
+ watchMode = this.meta.watchMode;
48
+ return null;
49
+ }
50
+ async function serve() {
51
+ if (!started && watchMode) {
52
+ started = true;
53
+ if (globalServer) {
54
+ await new Promise(resolve => globalServer.close(resolve));
55
+ globalServer = undefined;
56
+ }
57
+ const app = new Koa__default["default"]();
58
+ if (customizeKoa) {
59
+ customizeKoa(app);
60
+ }
61
+ if (useKoaLogger) {
62
+ app.use(koaLogger__default["default"]());
63
+ }
64
+ for (const dir of dirs) {
65
+ app.use(serveStatic__default["default"](dir, koaStaticOptions));
66
+ }
67
+ const server = globalServer = https$1 ? https.createServer(https$1, app.callback()) : http.createServer(app.callback());
68
+ const listenCb = () => internalOnListen(server);
69
+ if (host) {
70
+ server.listen(port, host, listenCb);
71
+ }
72
+ else {
73
+ server.listen(port, listenCb);
74
+ }
75
+ server.on('error', (e) => {
76
+ if (e.code === 'EADDRINUSE') {
77
+ logger.finish('address in use, please try another port', 3 /* error */);
78
+ }
79
+ else {
80
+ throw e;
81
+ }
82
+ });
83
+ }
84
+ }
85
+ function internalOnListen(server) {
86
+ if (!onListen || !onListen(server)) {
87
+ logger.finish(`listening on ${linkFromAddress(server.address(), !!https$1)}`, 1 /* info */);
88
+ }
89
+ }
90
+ }
91
+ function linkFromAddress(address, https) {
92
+ if (address && typeof address !== 'string') {
93
+ return linkFromAddressInfo(address, https);
94
+ }
95
+ return `${address}`;
96
+ }
97
+ function linkFromAddressInfo({ address, port, family }, https) {
98
+ const serverName = family === 'IPv6' ? `[${address}]` : address;
99
+ const protocol = `http${https ? 's' : ''}://`;
100
+ return `${protocol}${serverName}:${port}`;
101
+ }
102
+
103
+ module.exports = index;
@@ -0,0 +1,34 @@
1
+ import { NormalizedInputOptions, NormalizedOutputOptions, PluginContext } from 'rollup';
2
+ import { ServePluginOptions } from './types';
3
+ export default function (options?: ServePluginOptions): {
4
+ outputOptions: (this: PluginContext) => null;
5
+ buildEnd?: ((this: PluginContext, err?: Error | undefined) => void | Promise<void>) | undefined;
6
+ buildStart?: ((this: PluginContext, options: NormalizedInputOptions) => void | Promise<void>) | undefined;
7
+ closeBundle?: ((this: PluginContext) => void | Promise<void>) | undefined;
8
+ closeWatcher?: ((this: PluginContext) => void) | undefined;
9
+ load?: import("rollup").LoadHook | undefined;
10
+ moduleParsed?: import("rollup").ModuleParsedHook | undefined;
11
+ options?: ((this: import("rollup").MinimalPluginContext, options: import("rollup").InputOptions) => import("rollup").InputOptions | Promise<import("rollup").InputOptions | null | undefined> | null | undefined) | undefined;
12
+ resolveDynamicImport?: import("rollup").ResolveDynamicImportHook | undefined;
13
+ resolveId?: import("rollup").ResolveIdHook | undefined;
14
+ transform?: import("rollup").TransformHook | undefined;
15
+ watchChange?: import("rollup").WatchChangeHook | undefined;
16
+ augmentChunkHash?: ((this: PluginContext, chunk: import("rollup").PreRenderedChunk) => string | void) | undefined;
17
+ generateBundle?: ((this: PluginContext, options: NormalizedOutputOptions, bundle: import("rollup").OutputBundle, isWrite: boolean) => void | Promise<void>) | undefined;
18
+ renderChunk?: import("rollup").RenderChunkHook | undefined;
19
+ renderDynamicImport?: ((this: PluginContext, options: {
20
+ customResolution: string | null;
21
+ format: import("rollup").InternalModuleFormat;
22
+ moduleId: string;
23
+ targetModuleId: string | null;
24
+ }) => {
25
+ left: string;
26
+ right: string;
27
+ } | null | undefined) | undefined;
28
+ renderError?: ((this: PluginContext, err?: Error | undefined) => void | Promise<void>) | undefined;
29
+ renderStart?: ((this: PluginContext, outputOptions: NormalizedOutputOptions, inputOptions: NormalizedInputOptions) => void | Promise<void>) | undefined;
30
+ resolveAssetUrl?: import("rollup").ResolveAssetUrlHook | undefined;
31
+ resolveFileUrl?: import("rollup").ResolveFileUrlHook | undefined;
32
+ resolveImportMeta?: import("rollup").ResolveImportMetaHook | undefined;
33
+ writeBundle?: ((this: PluginContext, options: NormalizedOutputOptions, bundle: import("rollup").OutputBundle) => void | Promise<void>) | undefined;
34
+ };
package/dist/index.mjs ADDED
@@ -0,0 +1,94 @@
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 = Object.assign(Object.assign({}, 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 /* 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 /* 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
+ const serverName = family === 'IPv6' ? `[${address}]` : address;
90
+ const protocol = `http${https ? 's' : ''}://`;
91
+ return `${protocol}${serverName}:${port}`;
92
+ }
93
+
94
+ export { index as default };
@@ -0,0 +1,20 @@
1
+ /// <reference types="node" />
2
+ import Koa from 'koa';
3
+ import serve from 'koa-static';
4
+ import { Server } from 'http';
5
+ export declare 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[];
package/package.json ADDED
@@ -0,0 +1,85 @@
1
+ {
2
+ "version": "1.0.0",
3
+ "license": "MIT",
4
+ "name": "@rollup-extras/plugin-serve",
5
+ "author": {
6
+ "name": "Konstantin Shutkin"
7
+ },
8
+ "funding": "https://www.donationalerts.com/r/excitingcode",
9
+ "type": "module",
10
+ "exports": {
11
+ ".": {
12
+ "require": "./dist/index.cjs",
13
+ "default": "./dist/index.mjs"
14
+ }
15
+ },
16
+ "main": "./dist/index.cjs",
17
+ "module": "./dist/index.mjs",
18
+ "typings": "./dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "engines": {
23
+ "node": ">=15"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "git+https://github.com/kshutkin/rollup-extras.git"
28
+ },
29
+ "bugs": {
30
+ "url": "https://github.com/kshutkin/rollup-extras/issues"
31
+ },
32
+ "homepage": "https://github.com/kshutkin/rollup-extras/plugin-clean/README.md",
33
+ "readme": "README.md",
34
+ "keywords": [
35
+ "rollup-plugin"
36
+ ],
37
+ "scripts": {
38
+ "build": "rimraf ./dist && rollup -c",
39
+ "dev": "rimraf ./dist && rollup -c -w",
40
+ "test": "jest --collectCoverage",
41
+ "lint": "eslint ./src",
42
+ "semantic-release": "npx semantic-release"
43
+ },
44
+ "devDependencies": {
45
+ "@rollup/plugin-commonjs": "21.0.1",
46
+ "@rollup/plugin-node-resolve": "13.0.6",
47
+ "@semantic-release/changelog": "6.0.1",
48
+ "@semantic-release/commit-analyzer": "9.0.1",
49
+ "@semantic-release/git": "10.0.1",
50
+ "@semantic-release/npm": "8.0.2",
51
+ "@semantic-release/release-notes-generator": "10.0.2",
52
+ "@types/jest": "27.0.2",
53
+ "@typescript-eslint/eslint-plugin": "5.3.0",
54
+ "@typescript-eslint/parser": "5.3.0",
55
+ "conventional-changelog-angular": "5.0.13",
56
+ "eslint": "8.1.0",
57
+ "jest": "27.3.1",
58
+ "lodash": "4.17.21",
59
+ "rimraf": "3.0.2",
60
+ "is-builtin-module": "3.1.0",
61
+ "rollup": "2.60.1",
62
+ "rollup-plugin-terser": "7.0.2",
63
+ "rollup-plugin-typescript2": "0.31.1",
64
+ "rollup-plugin-preprocess": "0.0.4",
65
+ "semantic-release": "18.0.0",
66
+ "semantic-release-monorepo": "7.0.5",
67
+ "ts-jest": "27.0.7",
68
+ "typescript": "4.5.x",
69
+ "update-monorepo-package-json": "0.1.6",
70
+ "@types/koa": "^2.13.4",
71
+ "@types/koa-logger": "^3.1.2",
72
+ "@types/koa-static": "^4.0.2"
73
+ },
74
+ "peerDependencies": {
75
+ "rollup": "^2.60.2"
76
+ },
77
+ "dependencies": {
78
+ "koa": "^2.13.4",
79
+ "koa-static": "^5.0.0",
80
+ "koa-logger": "^3.2.1",
81
+ "@niceties/logger": "^1.0.4",
82
+ "@rollup-extras/utils": "^1.0.0"
83
+ },
84
+ "description": "README.md"
85
+ }