Skip to content

Commit 65dfc0e

Browse files
authored
Merge 0692002 into 0aa3bd8
2 parents 0aa3bd8 + 0692002 commit 65dfc0e

File tree

15 files changed

+308
-314
lines changed

15 files changed

+308
-314
lines changed

.changeset/fresh-beers-dream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@inox-tools/inline-mod": minor
3+
---
4+
5+
Implement support for async magic factory

.changeset/pre.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"mode": "pre",
3+
"tag": "smart-factory",
4+
"initialVersions": {
5+
"docs": "0.0.1",
6+
"@examples/custom-routing": "0.0.1",
7+
"@example/inline-mod": "0.0.0",
8+
"inline-mod-astro": "0.0.1",
9+
"@inox-tools/custom-routing": "0.2.0",
10+
"@inox-tools/inline-mod": "1.0.1",
11+
"@inox-tools/velox-luna": "0.0.1",
12+
"turbo": "1.0.0",
13+
"templates": "1.0.0"
14+
},
15+
"changesets": [
16+
"fresh-beers-dream",
17+
"thick-plums-prove"
18+
]
19+
}

.changeset/thick-plums-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@inox-tools/inline-mod": minor
3+
---
4+
5+
Add new factory utility

.github/workflows/release.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ on:
44
push:
55
branches:
66
- main
7+
pull_request:
8+
types:
9+
- opened
10+
- reopened
11+
- synchronize
12+
- labeled
713

814
defaults:
915
run:
@@ -18,6 +24,7 @@ jobs:
1824
changelog:
1925
name: Changelog PR or Release
2026
runs-on: ubuntu-latest
27+
if: ${{ github.event_name == 'push' || contains(github.event.pull_request.labels.*.name, 'preview') }}
2128
permissions:
2229
contents: write
2330
id-token: write
@@ -46,8 +53,8 @@ jobs:
4653
# Note: pnpm install after versioning is necessary to refresh lockfile
4754
version: pnpm run version
4855
publish: pnpm exec changeset publish
49-
commit: "[ci] release"
50-
title: "[ci] release"
56+
commit: ${{ github.event_name == 'push' && '[ci] release' || '[pre] release' }}
57+
title: ${{ github.event_name == 'push' && '[ci] release' || '[pre] release' }}
5158
env:
5259
# Needs access to push to main
5360
GITHUB_TOKEN: ${{ secrets.COMMIT_TOKEN }}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"lint": "eslint . --report-unused-disable-directives --fix",
2222
"release": "pnpm run build && changeset publish",
2323
"test": "turbo run test --concurrency=1",
24-
"version": "changeset version"
24+
"version": "changeset version && pnpm install && pnpm format"
2525
},
2626
"devDependencies": {
2727
"@changesets/changelog-github": "^0.5.0",

packages/inline-mod/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @inox-tools/inline-mod
22

3+
## 1.1.0-smart-factory.0
4+
5+
### Minor Changes
6+
7+
- 8aca13a: Implement support for async magic factory
8+
- db4e594: Add new factory utility
9+
310
## 1.0.1
411

512
### Patch Changes

packages/inline-mod/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@inox-tools/inline-mod",
3-
"version": "1.0.1",
3+
"version": "1.1.0-smart-factory.2",
44
"description": "Define a virtual module inline with any reference to buildtime values",
55
"keywords": [
66
"vite-plugin"
@@ -18,7 +18,8 @@
1818
},
1919
"files": [
2020
"README.md",
21-
"src"
21+
"src",
22+
"dist"
2223
],
2324
"scripts": {
2425
"build": "tsup",
@@ -42,7 +43,7 @@
4243
"vitest": "^1.2.2"
4344
},
4445
"peerDependencies": {
45-
"vite": "^3.2.0"
46+
"vite": "^4 || ^5"
4647
},
4748
"peerDependenciesMeta": {
4849
"vite": {

packages/inline-mod/src/closure/entry.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ type EntryMap = {
1010
// A closure we are dependent on.
1111
function: InspectedFunction;
1212

13+
factory: {
14+
isAsync: boolean;
15+
factory: Entry<'function'>;
16+
};
17+
1318
// An object which may contain nested closures.
1419
// Can include an optional proto if the user is not using the default Object.prototype.
1520
object: InspectedObject;
@@ -44,8 +49,10 @@ type EntryMap = {
4449
pending: never;
4550
};
4651

47-
export type Entry<T extends keyof EntryMap = keyof EntryMap> = {
48-
[K in keyof EntryMap]: {
52+
export type EntryType = keyof EntryMap;
53+
54+
export type Entry<T extends EntryType = EntryType> = {
55+
[K in EntryType]: {
4956
type: K;
5057
value: EntryMap[K];
5158
};

packages/inline-mod/src/closure/inspectCode.ts

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ class Inspector {
133133

134134
log('Error during inspection:', error);
135135

136-
throw new InternalInspectionError(
137-
error instanceof Error ? error.message : `${error}`,
138-
this.frames,
139-
error.stack
140-
);
136+
if (!(error instanceof Error)) throw error;
137+
138+
throw new InternalInspectionError(error.message, this.frames, error.stack);
141139
}
142140
}
143141

@@ -275,6 +273,17 @@ class Inspector {
275273
value: object,
276274
capturedProperties?: CapturedPropertyChain[]
277275
): Promise<Entry> {
276+
const factoryInfo = tryExtractMagicFactory(value);
277+
if (factoryInfo) {
278+
return {
279+
type: 'factory',
280+
value: {
281+
isAsync: factoryInfo.isAsync,
282+
factory: (await this.inspect(factoryInfo.factory)) as Entry<'function'>,
283+
},
284+
};
285+
}
286+
278287
if (this.doNotCapture(value)) {
279288
log('Value should skip capture');
280289
return Entry.json();
@@ -995,6 +1004,89 @@ function isDerivedNoCaptureConstructor(func: Function): boolean {
9951004
return false;
9961005
}
9971006

1007+
const factorySymbol = Symbol('inox-tool/inline-factory');
1008+
1009+
type MagicPlaceholder = {
1010+
[factorySymbol]: {
1011+
initialized: boolean;
1012+
isAsync: boolean;
1013+
factory: () => Record<any, any>;
1014+
};
1015+
};
1016+
1017+
function ensureFactoryInitialized(magicValue: MagicPlaceholder): void {
1018+
if (!magicValue[factorySymbol].initialized) {
1019+
Object.assign(magicValue, magicValue[factorySymbol].factory());
1020+
magicValue[factorySymbol].initialized = true;
1021+
}
1022+
}
1023+
1024+
const magicFactoryProxyHandler: ProxyHandler<MagicPlaceholder> = {
1025+
has: (target, key) => {
1026+
if (key === factorySymbol) return true;
1027+
ensureFactoryInitialized(target);
1028+
1029+
return Reflect.has(target, key);
1030+
},
1031+
get: (target, key) => {
1032+
if (key === factorySymbol) {
1033+
return {
1034+
isAsync: target[factorySymbol].isAsync,
1035+
factory: target[factorySymbol].factory,
1036+
};
1037+
}
1038+
1039+
ensureFactoryInitialized(target);
1040+
1041+
return Reflect.get(target, key);
1042+
},
1043+
set: (target, key, value) => {
1044+
if (key === factorySymbol) {
1045+
return false;
1046+
}
1047+
1048+
ensureFactoryInitialized(target);
1049+
1050+
return Reflect.set(target, key, value);
1051+
},
1052+
ownKeys: (target) => {
1053+
ensureFactoryInitialized(target);
1054+
1055+
return Reflect.ownKeys(target).filter((k) => k !== factorySymbol);
1056+
},
1057+
};
1058+
1059+
export function magicFactory<T extends Record<any, any>>({
1060+
isAsync,
1061+
fn,
1062+
}: {
1063+
isAsync: boolean;
1064+
fn: T | (() => T);
1065+
}): T {
1066+
if (typeof fn !== 'function') {
1067+
return fn;
1068+
}
1069+
1070+
return new Proxy<T & MagicPlaceholder>(
1071+
{
1072+
// Trick for TS
1073+
...({} as T),
1074+
[factorySymbol]: {
1075+
initialized: false,
1076+
isAsync,
1077+
factory: fn,
1078+
},
1079+
},
1080+
magicFactoryProxyHandler
1081+
);
1082+
}
1083+
1084+
function tryExtractMagicFactory(value: any): MagicPlaceholder[typeof factorySymbol] | undefined {
1085+
if (factorySymbol in value) {
1086+
return value[factorySymbol];
1087+
}
1088+
}
1089+
9981090
/**
9991091
* Cache of global entries
10001092
*/

0 commit comments

Comments
 (0)