@rollup-extras/plugin-serve 1.1.5 → 1.2.1

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/dist/index.cjs CHANGED
@@ -5,28 +5,97 @@ var https = require('https');
5
5
  var Koa = require('koa');
6
6
  var koaLogger = require('koa-logger');
7
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');
8
+ var logger$1 = require('@niceties/logger');
11
9
 
12
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+ function getOptions(options, defaults, field, factory) {
11
+ const newOptions = recursiveArrayOptions(options, field);
12
+ return getOptionsObject((newOptions ? newOptions : options), defaults, factory);
13
+ }
14
+ function recursiveArrayOptions(options, field) {
15
+ if (typeof options === 'string') {
16
+ return { [field]: [options] };
17
+ }
18
+ else if (Array.isArray(options)) {
19
+ return { [field]: options };
20
+ }
21
+ else if (typeof options === 'object') {
22
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
23
+ return field in options ? Object.assign(Object.assign({}, options), recursiveArrayOptions(options[field], field)) : options;
24
+ }
25
+ console.warn(`cannot process options: '${options}', reverting to defaults`);
26
+ return undefined;
27
+ }
28
+ function getOptionsObject(options, defaults, factory) {
29
+ const result = Object.assign(Object.assign({}, defaults), options);
30
+ if (factory) {
31
+ for (const [k, v] of Object.entries(factory)) {
32
+ result[k] = v(result, k);
33
+ }
34
+ }
35
+ return result;
36
+ }
37
+
38
+ function logger (options) {
39
+ return logger$1.createLogger(options.pluginName);
40
+ }
13
41
 
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);
42
+ function multiConfigPluginBase(useWriteBundle, pluginName, execute, onFinalHook) {
43
+ const finalHook = useWriteBundle ? 'writeBundle' : 'generateBundle';
44
+ let remainingOutputsCount = 0, configsCount = 0;
45
+ const configs = new Set();
46
+ const instance = {
47
+ name: pluginName,
48
+ renderStart,
49
+ [finalHook]: writeBundle,
50
+ api: { addInstance }
51
+ };
52
+ return instance;
53
+ function addInstance() {
54
+ const configId = ++configsCount;
55
+ configs.add(configId);
56
+ return {
57
+ name: `${pluginName}#${configId}`,
58
+ renderStart: () => {
59
+ configs.delete(configId);
60
+ return renderStart();
61
+ },
62
+ [finalHook]: writeBundle
63
+ };
64
+ }
65
+ function renderStart() {
66
+ ++remainingOutputsCount;
67
+ }
68
+ async function writeBundle(options, bundle) {
69
+ --remainingOutputsCount;
70
+ if (onFinalHook) {
71
+ await onFinalHook.call(this, options, bundle, configs.size, remainingOutputsCount);
72
+ }
73
+ if (configs.size === 0 && remainingOutputsCount === 0) {
74
+ // do work
75
+ try {
76
+ await execute.call(this, options, bundle);
77
+ }
78
+ finally {
79
+ // reset configs
80
+ for (let i = configsCount; i > 0; --i) {
81
+ configs.add(i);
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
18
87
 
19
88
  let globalServer;
20
- const factories = { logger: logger__default["default"] };
21
- function index (options$1 = {}) {
22
- const normalizedOptions = options.getOptions(options$1, {
89
+ const factories = { logger };
90
+ function index (options = {}) {
91
+ const normalizedOptions = getOptions(options, {
23
92
  pluginName: '@rollup-extras/plugin-serve',
24
93
  useWriteBundle: true,
25
94
  port: 8080,
26
95
  useKoaLogger: true
27
96
  }, 'dirs', factories);
28
97
  const { pluginName, useWriteBundle, port, host, https: https$1, useKoaLogger, customizeKoa, koaStaticOptions, onListen, logger } = normalizedOptions;
29
- const instance = mutliConfigPluginBase.multiConfigPluginBase(useWriteBundle, pluginName, serve);
98
+ const instance = multiConfigPluginBase(useWriteBundle, pluginName, serve);
30
99
  let { dirs } = normalizedOptions, collectDirs = false, started = false, watchMode = true;
31
100
  const pluginInstance = Object.assign(Object.assign({}, instance), { outputOptions });
32
101
  if (!dirs) {
@@ -54,15 +123,15 @@ function index (options$1 = {}) {
54
123
  await new Promise(resolve => globalServer.close(resolve));
55
124
  globalServer = undefined;
56
125
  }
57
- const app = new Koa__default["default"]();
126
+ const app = new Koa();
58
127
  if (customizeKoa) {
59
128
  customizeKoa(app);
60
129
  }
61
130
  if (useKoaLogger) {
62
- app.use(koaLogger__default["default"]());
131
+ app.use(koaLogger());
63
132
  }
64
133
  for (const dir of dirs) {
65
- app.use(serveStatic__default["default"](dir, koaStaticOptions));
134
+ app.use(serveStatic(dir, koaStaticOptions));
66
135
  }
67
136
  const server = globalServer = https$1 ? https.createServer(https$1, app.callback()) : http.createServer(app.callback());
68
137
  const listenCb = () => internalOnListen(server);
@@ -74,7 +143,7 @@ function index (options$1 = {}) {
74
143
  }
75
144
  server.on('error', (e) => {
76
145
  if (e.code === 'EADDRINUSE') {
77
- logger.finish('address in use, please try another port', 3 /* error */);
146
+ logger.finish('address in use, please try another port', 3 /* LogLevel.error */);
78
147
  }
79
148
  else {
80
149
  throw e;
@@ -84,7 +153,7 @@ function index (options$1 = {}) {
84
153
  }
85
154
  function internalOnListen(server) {
86
155
  if (!onListen || !onListen(server)) {
87
- logger.finish(`listening on ${linkFromAddress(server.address(), !!https$1)}`, 1 /* info */);
156
+ logger.finish(`listening on ${linkFromAddress(server.address(), !!https$1)}`, 1 /* LogLevel.info */);
88
157
  }
89
158
  }
90
159
  }
package/dist/index.mjs CHANGED
@@ -3,9 +3,85 @@ import { createServer } from 'https';
3
3
  import Koa from 'koa';
4
4
  import koaLogger from 'koa-logger';
5
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';
6
+ import { createLogger } from '@niceties/logger';
7
+
8
+ function getOptions(options, defaults, field, factory) {
9
+ const newOptions = recursiveArrayOptions(options, field);
10
+ return getOptionsObject((newOptions ? newOptions : options), defaults, factory);
11
+ }
12
+ function recursiveArrayOptions(options, field) {
13
+ if (typeof options === 'string') {
14
+ return { [field]: [options] };
15
+ }
16
+ else if (Array.isArray(options)) {
17
+ return { [field]: options };
18
+ }
19
+ else if (typeof options === 'object') {
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ return field in options ? Object.assign(Object.assign({}, options), recursiveArrayOptions(options[field], field)) : options;
22
+ }
23
+ console.warn(`cannot process options: '${options}', reverting to defaults`);
24
+ return undefined;
25
+ }
26
+ function getOptionsObject(options, defaults, factory) {
27
+ const result = Object.assign(Object.assign({}, defaults), options);
28
+ if (factory) {
29
+ for (const [k, v] of Object.entries(factory)) {
30
+ result[k] = v(result, k);
31
+ }
32
+ }
33
+ return result;
34
+ }
35
+
36
+ function logger (options) {
37
+ return createLogger(options.pluginName);
38
+ }
39
+
40
+ function multiConfigPluginBase(useWriteBundle, pluginName, execute, onFinalHook) {
41
+ const finalHook = useWriteBundle ? 'writeBundle' : 'generateBundle';
42
+ let remainingOutputsCount = 0, configsCount = 0;
43
+ const configs = new Set();
44
+ const instance = {
45
+ name: pluginName,
46
+ renderStart,
47
+ [finalHook]: writeBundle,
48
+ api: { addInstance }
49
+ };
50
+ return instance;
51
+ function addInstance() {
52
+ const configId = ++configsCount;
53
+ configs.add(configId);
54
+ return {
55
+ name: `${pluginName}#${configId}`,
56
+ renderStart: () => {
57
+ configs.delete(configId);
58
+ return renderStart();
59
+ },
60
+ [finalHook]: writeBundle
61
+ };
62
+ }
63
+ function renderStart() {
64
+ ++remainingOutputsCount;
65
+ }
66
+ async function writeBundle(options, bundle) {
67
+ --remainingOutputsCount;
68
+ if (onFinalHook) {
69
+ await onFinalHook.call(this, options, bundle, configs.size, remainingOutputsCount);
70
+ }
71
+ if (configs.size === 0 && remainingOutputsCount === 0) {
72
+ // do work
73
+ try {
74
+ await execute.call(this, options, bundle);
75
+ }
76
+ finally {
77
+ // reset configs
78
+ for (let i = configsCount; i > 0; --i) {
79
+ configs.add(i);
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
9
85
 
10
86
  let globalServer;
11
87
  const factories = { logger };
@@ -65,7 +141,7 @@ function index (options = {}) {
65
141
  }
66
142
  server.on('error', (e) => {
67
143
  if (e.code === 'EADDRINUSE') {
68
- logger.finish('address in use, please try another port', 3 /* error */);
144
+ logger.finish('address in use, please try another port', 3 /* LogLevel.error */);
69
145
  }
70
146
  else {
71
147
  throw e;
@@ -75,7 +151,7 @@ function index (options = {}) {
75
151
  }
76
152
  function internalOnListen(server) {
77
153
  if (!onListen || !onListen(server)) {
78
- logger.finish(`listening on ${linkFromAddress(server.address(), !!https)}`, 1 /* info */);
154
+ logger.finish(`listening on ${linkFromAddress(server.address(), !!https)}`, 1 /* LogLevel.info */);
79
155
  }
80
156
  }
81
157
  }
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.5",
2
+ "version": "1.2.1",
3
3
  "license": "MIT",
4
4
  "name": "@rollup-extras/plugin-serve",
5
5
  "author": {
@@ -38,7 +38,7 @@
38
38
  "serve"
39
39
  ],
40
40
  "scripts": {
41
- "build": "pkgbld",
41
+ "build": "pkgbld-internal",
42
42
  "test": "jest --collectCoverage",
43
43
  "lint": "eslint ./src",
44
44
  "semantic-release": "npx semantic-release"
@@ -60,19 +60,19 @@
60
60
  "ts-jest": "28.0.2",
61
61
  "typescript": "4.6.x",
62
62
  "update-monorepo-package-json": "0.2.0",
63
- "pkgbld": "1.2.12",
63
+ "pkgbld-internal": "1.0.2",
64
64
  "@types/koa": "^2.13.4",
65
65
  "@types/koa-logger": "^3.1.2",
66
66
  "@types/koa-static": "^4.0.2"
67
67
  },
68
68
  "peerDependencies": {
69
- "rollup": "^2.60.1"
69
+ "rollup": "^2.60.1 || ^3.0.0"
70
70
  },
71
71
  "dependencies": {
72
72
  "koa": "^2.13.4",
73
73
  "koa-static": "^5.0.0",
74
74
  "koa-logger": "^3.2.1",
75
- "@niceties/logger": "^1.1.3",
76
- "@rollup-extras/utils": "^1.2.4"
75
+ "@niceties/logger": "^1.1.4",
76
+ "@rollup-extras/utils": "^1.3.1"
77
77
  }
78
78
  }