Skip to content

Commit

Permalink
cloudflare platform and dwrangler
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Dec 12, 2024
1 parent a0c9079 commit 162e8cc
Show file tree
Hide file tree
Showing 21 changed files with 1,689 additions and 90 deletions.
5 changes: 5 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
"import": "./dist/globals-injector-standalone/edge/injector.js",
"default": "./dist/globals-injector-standalone/edge/injector.cjs"
},
"./injector-standalone/edge-auto": {
"ts-src": "./src/globals-injector/auto-inject.ts",
"import": "./dist/globals-injector-standalone/edge/auto-inject.js",
"default": "./dist/globals-injector-standalone/edge/auto-inject.cjs"
},
"./tsconfigs/*.json": "./tsconfigs/*",
"./tsconfigs/*": "./tsconfigs/*.json"
},
Expand Down
49 changes: 36 additions & 13 deletions packages/core/src/config-engine/authoring-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,54 @@ import { DmnoDataTypeMetadata } from './configraph-adapter';

// export function pickFromSchemaObject<S extends Record<string, any>>(schemaObj: S, keys: Array<keyof S>);
// export function pickFromSchemaObject<S extends Record<string, any>>(schemaObj: S, ...keys: Array<keyof S>);
export function pickFromSchemaObject<S extends Record<string, any>>(
export function pickFromSchemaObject<
S extends Record<string, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>,
>(
schemaObj: S,
firstKeyOrKeyArray: keyof S | Array<keyof S>,
pickSettings: (
keyof S |
Array<keyof S> |
Partial<Record<keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>>
),
...keysRest: Array<keyof S>
) {
const keyArray = _.isArray(firstKeyOrKeyArray) ? firstKeyOrKeyArray : [firstKeyOrKeyArray, ...keysRest];
return Object.fromEntries(keyArray.map((k) => [k, schemaObj[k]]));
): Partial<Record<keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>> {
if (_.isArray(pickSettings) || _.isString(pickSettings)) {
const keyArray = _.isArray(pickSettings)
? pickSettings
: [pickSettings, ...keysRest];
return _.pick(schemaObj, keyArray);
} else if (_.isObject(pickSettings)) {
return _.mapValues(pickSettings, (itemOverrides, key) => {
return {
...schemaObj[key],
...itemOverrides,
};
});
} else {
throw new Error('Invalid pick from schema');
}
}


export function createVendorSchema(
schema: Record<string, ConfigraphDataTypeDefinitionOrShorthand<DmnoDataTypeMetadata>>,
export function createVendorSchema<
S extends Record<string, ConfigraphDataTypeDefinitionOrShorthand<DmnoDataTypeMetadata>>,
>(
schema: S,
commonTraits?: Partial<ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>>,
) {
if (!commonTraits) return schema;
): Record< keyof S, ConfigraphDataTypeDefinition<unknown, DmnoDataTypeMetadata>> {
return _.mapValues(schema, (item, _itemKey) => {
if (item instanceof ConfigraphDataType || _.isString(item) || _.isFunction(item)) {
return {
extends: item,
...commonTraits,
};
} else {
return {
// TODO: not sure why I need this any :(
...item as any,
...commonTraits,
};
}
return {
...item,
...commonTraits,
};
// throw Error('invalid vendor schema');
});
}
2 changes: 2 additions & 0 deletions packages/core/src/config-loader/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export async function setupViteServer(opts: {
},

transform(code, id, options) {
// TODO: need to detect file types more broadly and can do better detection of if we need to inject at all
if (id.endsWith('.json')) return;
// fairly naive way of doing this... but for now we are replacing `DMNO_CONFIG.SOME_KEY` with `getResolverCtx().get('SOME_KEY')`
// TODO: we probably should limit which files this applies in
const fixedCode = new MagicString(code);
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/globals-injector/auto-inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import { injectDmnoGlobals as _injectDmnoGlobals } from './injector';

_injectDmnoGlobals();
let defineInjectedConfig;
try {
// @ts-ignore
defineInjectedConfig = __DMNO_INJECTED_CONFIG__;
} catch (err) {}
if (defineInjectedConfig) _injectDmnoGlobals({ injectedConfig: defineInjectedConfig });
else _injectDmnoGlobals();

export const injectDmnoGlobals = _injectDmnoGlobals;

1 change: 1 addition & 0 deletions packages/core/tsup.inject-standalone.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const outDir = `dist/globals-injector-standalone${process.env.DMNO_EDGE_COMPAT ?
export default defineConfig({
entry: [ // Entry point(s)
'src/globals-injector/injector.ts', // function used to inject dmno globals
'src/globals-injector/auto-inject.ts', // exports inject function and automatically calls it once
],

esbuildPlugins: [
Expand Down
4 changes: 4 additions & 0 deletions packages/docs-site/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ export default defineConfig({
label: 'Overview',
link: '/docs/platforms/overview/',
},
{
label: 'Cloudflare',
link: '/docs/platforms/cloudflare/',
},
{
label: 'Netlify',
link: '/docs/platforms/netlify/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@ Even if you are not writing TypeScript, these days you are still likely relying
// ...
"include": [
"**/*.js",
+ ".dmno/.typegen/global.d.ts"
+ ".dmno/.typegen/global.d.ts",
+ ".dmno/.typegen/global-public.d.ts"
]
}
```

:::note[Invalid keys are not strict]
JavaScript is by default not strict - so while you will get nice autocompletion and IntelliSense on your config, your IDE will not give you the handy red squiggle if you use an _invalid_ key. Depending on your setup you may still get a build/runtime error.
:::



#### Injecting `DMNO_CONFIG` vs `DMNO_PUBLIC_CONFIG`

Expand Down
Loading

0 comments on commit 162e8cc

Please sign in to comment.