Skip to content

Commit e13eb93

Browse files
authored
feat: native ES module (#51)
BREAKING CHANGE: `javascript-plugin-architecture-with-typescript-definitions` is now a native ES Module
1 parent 88c84d6 commit e13eb93

File tree

12 files changed

+1013
-12167
lines changed

12 files changed

+1013
-12167
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/release.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ jobs:
1212
- uses: actions/checkout@v2
1313
- uses: actions/setup-node@v2
1414
- run: npm ci
15-
- run: npm run build
1615
- run: npx semantic-release
1716
env:
1817
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/test.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ on:
33
push:
44
branches:
55
- master
6-
- "greenkeeper/**"
76
pull_request:
87
types: [opened, synchronize]
98

index.d.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,56 @@
11
declare type Options = {
2-
[key: string]: unknown;
2+
[key: string]: unknown;
33
};
44
declare type ApiExtension = {
5-
[key: string]: unknown;
5+
[key: string]: unknown;
66
};
7-
declare type TestPlugin = (instance: Base, options: Options) => ApiExtension | undefined;
7+
declare type Plugin = (instance: Base, options: Options) => ApiExtension | void;
8+
89
declare type Constructor<T> = new (...args: any[]) => T;
910
/**
1011
* @author https://stackoverflow.com/users/2887218/jcalz
1112
* @see https://stackoverflow.com/a/50375286/10325032
1213
*/
13-
declare type UnionToIntersection<Union> = (Union extends any ? (argument: Union) => void : never) extends (argument: infer Intersection) => void ? Intersection : never;
14+
declare type UnionToIntersection<Union> = (
15+
Union extends any ? (argument: Union) => void : never
16+
) extends (argument: infer Intersection) => void
17+
? Intersection
18+
: never;
1419
declare type AnyFunction = (...args: any) => any;
15-
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> = T extends AnyFunction ? ReturnType<T> : T extends AnyFunction[] ? UnionToIntersection<ReturnType<T[number]>> : never;
20+
declare type ReturnTypeOf<T extends AnyFunction | AnyFunction[]> =
21+
T extends AnyFunction
22+
? ReturnType<T>
23+
: T extends AnyFunction[]
24+
? UnionToIntersection<ReturnType<T[number]>>
25+
: never;
26+
1627
export declare class Base<TOptions extends Options = Options> {
17-
static plugins: TestPlugin[];
18-
static plugin<S extends Constructor<any> & {
19-
plugins: any[];
20-
}, T1 extends TestPlugin, T2 extends TestPlugin[]>(this: S, plugin1: T1, ...additionalPlugins: T2): S & {
21-
plugins: any[];
22-
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
23-
static defaults<TDefaults extends Options, S extends Constructor<Base<TDefaults>>>(this: S, defaults: TDefaults): {
24-
new (...args: any[]): {
25-
options: TDefaults;
26-
};
27-
} & S;
28-
constructor(options?: TOptions);
29-
options: TOptions;
28+
static plugins: Plugin[];
29+
static plugin<
30+
S extends Constructor<any> & {
31+
plugins: any[];
32+
},
33+
T1 extends Plugin,
34+
T2 extends Plugin[]
35+
>(
36+
this: S,
37+
plugin1: T1,
38+
...additionalPlugins: T2
39+
): S & {
40+
plugins: any[];
41+
} & Constructor<UnionToIntersection<ReturnTypeOf<T1> & ReturnTypeOf<T2>>>;
42+
static defaults<
43+
TDefaults extends Options,
44+
S extends Constructor<Base<TDefaults>>
45+
>(
46+
this: S,
47+
defaults: TDefaults
48+
): {
49+
new (...args: any[]): {
50+
options: TDefaults;
51+
};
52+
} & S;
53+
constructor(options?: TOptions);
54+
options: TOptions;
3055
}
3156
export {};

index.js

Lines changed: 31 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,32 @@
1-
"use strict";
2-
var __extends = (this && this.__extends) || (function () {
3-
var extendStatics = function (d, b) {
4-
extendStatics = Object.setPrototypeOf ||
5-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7-
return extendStatics(d, b);
1+
export class Base {
2+
constructor(options = {}) {
3+
this.options = options;
4+
// apply plugins
5+
// https://stackoverflow.com/a/16345172
6+
const classConstructor = this.constructor;
7+
classConstructor.plugins.forEach((plugin) => {
8+
Object.assign(this, plugin(this, options));
9+
});
10+
}
11+
static plugin(plugin1, ...additionalPlugins) {
12+
var _a;
13+
const currentPlugins = this.plugins;
14+
let newPlugins = [plugin1, ...additionalPlugins];
15+
const BaseWithPlugins =
16+
((_a = class extends this {}),
17+
(_a.plugins = currentPlugins.concat(
18+
newPlugins.filter((plugin) => !currentPlugins.includes(plugin))
19+
)),
20+
_a);
21+
return BaseWithPlugins;
22+
}
23+
static defaults(defaults) {
24+
const BaseWitDefaults = class extends this {
25+
constructor(...args) {
26+
super(Object.assign({}, defaults, args[0] || {}));
27+
}
828
};
9-
return function (d, b) {
10-
extendStatics(d, b);
11-
function __() { this.constructor = d; }
12-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13-
};
14-
})();
15-
var __spreadArrays = (this && this.__spreadArrays) || function () {
16-
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
17-
for (var r = Array(s), k = 0, i = 0; i < il; i++)
18-
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
19-
r[k] = a[j];
20-
return r;
21-
};
22-
exports.__esModule = true;
23-
exports.Base = void 0;
24-
var Base = /** @class */ (function () {
25-
function Base(options) {
26-
var _this = this;
27-
if (options === void 0) { options = {}; }
28-
this.options = options;
29-
// apply plugins
30-
// https://stackoverflow.com/a/16345172
31-
var classConstructor = this.constructor;
32-
classConstructor.plugins.forEach(function (plugin) {
33-
Object.assign(_this, plugin(_this, options));
34-
});
35-
}
36-
Base.plugin = function (plugin1) {
37-
var _a;
38-
var additionalPlugins = [];
39-
for (var _i = 1; _i < arguments.length; _i++) {
40-
additionalPlugins[_i - 1] = arguments[_i];
41-
}
42-
var currentPlugins = this.plugins;
43-
var newPlugins = __spreadArrays([
44-
plugin1
45-
], additionalPlugins);
46-
var BaseWithPlugins = (_a = /** @class */ (function (_super) {
47-
__extends(class_1, _super);
48-
function class_1() {
49-
return _super !== null && _super.apply(this, arguments) || this;
50-
}
51-
return class_1;
52-
}(this)),
53-
_a.plugins = currentPlugins.concat(newPlugins.filter(function (plugin) { return !currentPlugins.includes(plugin); })),
54-
_a);
55-
return BaseWithPlugins;
56-
};
57-
Base.defaults = function (defaults) {
58-
var BaseWitDefaults = /** @class */ (function (_super) {
59-
__extends(class_2, _super);
60-
function class_2() {
61-
var args = [];
62-
for (var _i = 0; _i < arguments.length; _i++) {
63-
args[_i] = arguments[_i];
64-
}
65-
return _super.call(this, Object.assign({}, defaults, args[0] || {})) || this;
66-
}
67-
return class_2;
68-
}(this));
69-
return BaseWitDefaults;
70-
};
71-
Base.plugins = [];
72-
return Base;
73-
}());
74-
exports.Base = Base;
29+
return BaseWitDefaults;
30+
}
31+
}
32+
Base.plugins = [];

index.test-d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { expectType } from "tsd";
2+
import { Base } from "./index.js";
3+
4+
const fooPlugin = () => {
5+
return {
6+
foo: () => "foo",
7+
};
8+
};
9+
10+
const MyBase = Base.plugin(fooPlugin).defaults({
11+
default: "value",
12+
});
13+
const base = new MyBase({
14+
option: "value",
15+
});
16+
17+
expectType<string>(base.options.default);
18+
expectType<string>(base.options.option);
19+
expectType<string>(base.foo());
20+
21+
// @ts-expect-error unknown properties cannot be used, see #31
22+
base.unknown;

0 commit comments

Comments
 (0)