Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: esm attempted dual #145

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
},
"modules": "commonjs"
}
],
"@babel/preset-typescript"
],
"plugins": [
[
"babel-plugin-add-import-extension", { "extension": "js" }
]
]
}
10 changes: 10 additions & 0 deletions .change-extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

# The first parameter is the desired extension
extension=$1
shift

# The other parameters are the files to be renamed with the extension
for file in "$@"; do
mv "$file" "${file%%.*}.$extension"
done
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules
coverage
cjs
esm

**/*.js
**/*.d.ts
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ index.d.ts
coverage
.eslintcache
components/*.json
.yarn/
.yarn/
dist/
esm/
cjs/
5 changes: 4 additions & 1 deletion bin/compile-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as Path from 'path';
import type { ParsedArgs } from 'minimist';
import minimist = require('minimist');
import { compileConfig } from '..';
import { compileConfig } from '../index.js';

const args: ParsedArgs = minimist(process.argv.slice(2));
if (args._.length !== 1 || args.h || args.help) {
Expand All @@ -19,6 +19,7 @@ Options:
-p The main module path, if not provided, this defaults to the working directory
-e The instance by config URI that will be exported, by default this is the provided instance URI.
-f If the exported instance should be exposed as a function, which accepts an optional hash of variables.
-m A flag to endicate that an ESM [m]odule should be generated instead of CommonJS.
--help print this help message
`);
process.exit(1);
Expand All @@ -32,13 +33,15 @@ if (args.e) {
exportVariableName = args.e;
}
const asFunction = !!args.f;
const asEsm = !!args.m;

compileConfig(
mainModulePath,
configPath,
configResourceUri,
exportVariableName,
asFunction,
asEsm,
)
.then((output: string) => process.stdout.write(`${output}\n`))
.catch(error => {
Expand Down
80 changes: 40 additions & 40 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
export * from './lib/construction/argument/ArgumentConstructorHandlerArray';
export * from './lib/construction/argument/ArgumentConstructorHandlerHash';
export * from './lib/construction/argument/ArgumentConstructorHandlerList';
export * from './lib/construction/argument/ArgumentConstructorHandlerPrimitive';
export * from './lib/construction/argument/ArgumentConstructorHandlerReference';
export * from './lib/construction/argument/ArgumentConstructorHandlerUndefined';
export * from './lib/construction/argument/ArgumentConstructorHandlerValue';
export * from './lib/construction/argument/IArgumentConstructorHandler';
export * from './lib/construction/argument/IArgumentsConstructor';
export * from './lib/construction/strategy/ConstructionStrategyCommonJs';
export * from './lib/construction/strategy/ConstructionStrategyCommonJsString';
export * from './lib/construction/strategy/IConstructionStrategy';
export * from './lib/construction/ConfigConstructor';
export * from './lib/construction/IConstructionSettings';
export * from './lib/construction/IConfigConstructorPool';
export * from './lib/construction/ConfigConstructorPool';
export * from './lib/loading/ComponentRegistry';
export * from './lib/loading/ComponentRegistryFinalizer';
export * from './lib/loading/ComponentsManagerBuilder';
export * from './lib/loading/ConfigRegistry';
export * from './lib/loading/ModuleStateBuilder';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerCollectEntries';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerElements';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerFields';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerKeyValue';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerList';
export * from './lib/preprocess/constructorargumentsmapping/IConstructorArgumentsElementMappingHandler';
export * from './lib/preprocess/constructorargumentsmapping/IConstructorArgumentsMapper';
export * from './lib/preprocess/ConfigPreprocessorComponent';
export * from './lib/preprocess/ConfigPreprocessorComponentMapped';
export * from './lib/preprocess/IConfigPreprocessor';
export * from './lib/preprocess/ParameterHandler';
export * from './lib/rdf/Iris';
export * from './lib/rdf/PrefetchedDocumentLoader';
export * from './lib/rdf/RdfParser';
export * from './lib/rdf/RdfStreamIncluder';
export * from './lib/util/CompileUtil';
export * from './lib/util/ErrorResourcesContext';
export * from './lib/util/LogLevel';
export * from './lib/ComponentsManager';
export * from './lib/construction/argument/ArgumentConstructorHandlerArray.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerHash.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerList.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerPrimitive.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerReference.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerUndefined.js';
export * from './lib/construction/argument/ArgumentConstructorHandlerValue.js';
export * from './lib/construction/argument/IArgumentConstructorHandler.js';
export * from './lib/construction/argument/IArgumentsConstructor.js';
export * from './lib/construction/strategy/ConstructionStrategyCommonJs.js';
export * from './lib/construction/strategy/ConstructionStrategyCommonJsString.js';
export * from './lib/construction/strategy/IConstructionStrategy.js';
export * from './lib/construction/ConfigConstructor.js';
export * from './lib/construction/IConstructionSettings.js';
export * from './lib/construction/IConfigConstructorPool.js';
export * from './lib/construction/ConfigConstructorPool.js';
export * from './lib/loading/ComponentRegistry.js';
export * from './lib/loading/ComponentRegistryFinalizer.js';
export * from './lib/loading/ComponentsManagerBuilder.js';
export * from './lib/loading/ConfigRegistry.js';
export * from './lib/loading/ModuleStateBuilder.js';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerCollectEntries.js';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerElements.js';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerFields.js';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerKeyValue.js';
export * from './lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerList.js';
export * from './lib/preprocess/constructorargumentsmapping/IConstructorArgumentsElementMappingHandler.js';
export * from './lib/preprocess/constructorargumentsmapping/IConstructorArgumentsMapper.js';
export * from './lib/preprocess/ConfigPreprocessorComponent.js';
export * from './lib/preprocess/ConfigPreprocessorComponentMapped.js';
export * from './lib/preprocess/IConfigPreprocessor.js';
export * from './lib/preprocess/ParameterHandler.js';
export * from './lib/rdf/Iris.js';
export * from './lib/rdf/PrefetchedDocumentLoader.js';
export * from './lib/rdf/RdfParser.js';
export * from './lib/rdf/RdfStreamIncluder.js';
export * from './lib/util/CompileUtil.js';
export * from './lib/util/ErrorResourcesContext.js';
export * from './lib/util/LogLevel.js';
export * from './lib/ComponentsManager.js';
16 changes: 8 additions & 8 deletions lib/ComponentsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import * as fs from 'fs';
import type { Resource, RdfObjectLoader } from 'rdf-object';
import { stringToTerm } from 'rdf-string';
import type { Logger } from 'winston';
import type { IConfigConstructorPool } from './construction/IConfigConstructorPool';
import type { IConstructionSettings } from './construction/IConstructionSettings';
import type { IComponentsManagerBuilderOptions } from './loading/ComponentsManagerBuilder';
import { ComponentsManagerBuilder } from './loading/ComponentsManagerBuilder';
import type { ConfigRegistry } from './loading/ConfigRegistry';
import type { IModuleState } from './loading/ModuleStateBuilder';
import { ErrorResourcesContext } from './util/ErrorResourcesContext';
import type { IConfigConstructorPool } from './construction/IConfigConstructorPool.js';
import type { IConstructionSettings } from './construction/IConstructionSettings.js';
import type { IComponentsManagerBuilderOptions } from './loading/ComponentsManagerBuilder.js';
import { ComponentsManagerBuilder } from './loading/ComponentsManagerBuilder.js';
import type { ConfigRegistry } from './loading/ConfigRegistry.js';
import type { IModuleState } from './loading/ModuleStateBuilder.js';
import { ErrorResourcesContext } from './util/ErrorResourcesContext.js';

/**
* A components manager can instantiate components.
Expand Down Expand Up @@ -38,7 +38,7 @@ export class ComponentsManager<Instance> {
* @see IComponentsManagerBuilderOptions
* @param options Settings of the new manager.
*/
public static build<I>(options: IComponentsManagerBuilderOptions<I>): Promise<ComponentsManager<I>> {
public static build<I>(options: IComponentsManagerBuilderOptions<I, I | Promise<I>>): Promise<ComponentsManager<I>> {
return new ComponentsManagerBuilder(options).build();
}

Expand Down
40 changes: 20 additions & 20 deletions lib/construction/ConfigConstructor.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Resource, RdfObjectLoader } from 'rdf-object';
import type { IModuleState } from '../loading/ModuleStateBuilder';
import { ErrorResourcesContext } from '../util/ErrorResourcesContext';
import { ArgumentConstructorHandlerArray } from './argument/ArgumentConstructorHandlerArray';
import { ArgumentConstructorHandlerHash } from './argument/ArgumentConstructorHandlerHash';
import { ArgumentConstructorHandlerList } from './argument/ArgumentConstructorHandlerList';
import { ArgumentConstructorHandlerPrimitive } from './argument/ArgumentConstructorHandlerPrimitive';
import { ArgumentConstructorHandlerReference } from './argument/ArgumentConstructorHandlerReference';
import { ArgumentConstructorHandlerUndefined } from './argument/ArgumentConstructorHandlerUndefined';
import { ArgumentConstructorHandlerValue } from './argument/ArgumentConstructorHandlerValue';
import type { IArgumentConstructorHandler } from './argument/IArgumentConstructorHandler';
import type { IArgumentsConstructor } from './argument/IArgumentsConstructor';
import type { IConfigConstructorPool } from './IConfigConstructorPool';
import type { IConstructionSettings } from './IConstructionSettings';
import type { IConstructionStrategy } from './strategy/IConstructionStrategy';
import type { IModuleState } from '../loading/ModuleStateBuilder.js';
import { ErrorResourcesContext } from '../util/ErrorResourcesContext.js';
import { ArgumentConstructorHandlerArray } from './argument/ArgumentConstructorHandlerArray.js';
import { ArgumentConstructorHandlerHash } from './argument/ArgumentConstructorHandlerHash.js';
import { ArgumentConstructorHandlerList } from './argument/ArgumentConstructorHandlerList.js';
import { ArgumentConstructorHandlerPrimitive } from './argument/ArgumentConstructorHandlerPrimitive.js';
import { ArgumentConstructorHandlerReference } from './argument/ArgumentConstructorHandlerReference.js';
import { ArgumentConstructorHandlerUndefined } from './argument/ArgumentConstructorHandlerUndefined.js';
import { ArgumentConstructorHandlerValue } from './argument/ArgumentConstructorHandlerValue.js';
import type { IArgumentConstructorHandler } from './argument/IArgumentConstructorHandler.js';
import type { IArgumentsConstructor } from './argument/IArgumentsConstructor.js';
import type { IConfigConstructorPool } from './IConfigConstructorPool.js';
import type { IConstructionSettings } from './IConstructionSettings.js';
import type { IConstructionStrategy } from './strategy/IConstructionStrategy.js';

/**
* Creates instances of raw configs using the configured creation strategy.
Expand All @@ -28,7 +28,7 @@ import type { IConstructionStrategy } from './strategy/IConstructionStrategy';
* If you want to make sure that instances are reused,
* be sure to call {@link ConfigConstructorPool} instead.
*/
export class ConfigConstructor<Instance> implements IArgumentsConstructor<Instance> {
export class ConfigConstructor<Instance, IOut = Instance> implements IArgumentsConstructor<Instance, IOut> {
private static readonly ARGS_HANDLERS: IArgumentConstructorHandler[] = [
new ArgumentConstructorHandlerUndefined(),
new ArgumentConstructorHandlerHash(),
Expand All @@ -41,10 +41,10 @@ export class ConfigConstructor<Instance> implements IArgumentsConstructor<Instan

public readonly objectLoader: RdfObjectLoader;
public readonly configConstructorPool: IConfigConstructorPool<Instance>;
public readonly constructionStrategy: IConstructionStrategy<Instance>;
public readonly constructionStrategy: IConstructionStrategy<Instance, IOut>;
private readonly moduleState: IModuleState;

public constructor(options: IConfigConstructorOptions<Instance>) {
public constructor(options: IConfigConstructorOptions<Instance, IOut>) {
this.objectLoader = options.objectLoader;
this.configConstructorPool = options.configConstructorPool;
this.constructionStrategy = options.constructionStrategy;
Expand Down Expand Up @@ -110,7 +110,7 @@ export class ConfigConstructor<Instance> implements IArgumentsConstructor<Instan
public async createInstance(
config: Resource,
settings: IConstructionSettings,
): Promise<Instance> {
): Promise<IOut> {
const args: Instance[] = await this.createArguments(config, settings);
return this.constructionStrategy.createInstance({
settings,
Expand All @@ -128,7 +128,7 @@ export class ConfigConstructor<Instance> implements IArgumentsConstructor<Instan
/**
* Options for a component factory.
*/
export interface IConfigConstructorOptions<Instance> {
export interface IConfigConstructorOptions<Instance, InstanceOut = Instance> {
/**
* The RDF object loader.
*/
Expand All @@ -140,7 +140,7 @@ export interface IConfigConstructorOptions<Instance> {
/**
* The strategy for construction.
*/
constructionStrategy: IConstructionStrategy<Instance>;
constructionStrategy: IConstructionStrategy<Instance, InstanceOut>;
/**
* The module state.
*/
Expand Down
28 changes: 14 additions & 14 deletions lib/construction/ConfigConstructorPool.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Resource, RdfObjectLoader } from 'rdf-object';
import { termToString } from 'rdf-string';
import type { IModuleState } from '../loading/ModuleStateBuilder';
import type { IConfigPreprocessor } from '../preprocess/IConfigPreprocessor';
import { ErrorResourcesContext } from '../util/ErrorResourcesContext';
import { ConfigConstructor } from './ConfigConstructor';
import type { IConfigConstructorPool } from './IConfigConstructorPool';
import type { IConstructionSettings } from './IConstructionSettings';
import type { IConstructionStrategy } from './strategy/IConstructionStrategy';
import type { IModuleState } from '../loading/ModuleStateBuilder.js';
import type { IConfigPreprocessor } from '../preprocess/IConfigPreprocessor.js';
import { ErrorResourcesContext } from '../util/ErrorResourcesContext.js';
import { ConfigConstructor } from './ConfigConstructor.js';
import type { IConfigConstructorPool } from './IConfigConstructorPool.js';
import type { IConstructionSettings } from './IConstructionSettings.js';
import type { IConstructionStrategy } from './strategy/IConstructionStrategy.js';

/**
* Manages and creates instances of components based on a given config.
Expand All @@ -16,16 +16,16 @@ import type { IConstructionStrategy } from './strategy/IConstructionStrategy';
* This will make sure that configs with the same id will only be instantiated once,
* and multiple references to configs will always reuse the same instance.
*/
export class ConfigConstructorPool<Instance> implements IConfigConstructorPool<Instance> {
export class ConfigConstructorPool<Instance, InstanceOut = Instance> implements IConfigConstructorPool<Instance> {
private readonly configPreprocessors: IConfigPreprocessor<any>[];
private readonly configConstructor: ConfigConstructor<Instance>;
private readonly constructionStrategy: IConstructionStrategy<Instance>;
private readonly configConstructor: ConfigConstructor<Instance, InstanceOut>;
private readonly constructionStrategy: IConstructionStrategy<Instance, InstanceOut>;

private instances: Record<string, Promise<any>> = {};

public constructor(options: IInstancePoolOptions<Instance>) {
public constructor(options: IInstancePoolOptions<Instance, InstanceOut>) {
this.configPreprocessors = options.configPreprocessors;
this.configConstructor = new ConfigConstructor({
this.configConstructor = new ConfigConstructor<Instance, InstanceOut>({
objectLoader: options.objectLoader,
configConstructorPool: this,
constructionStrategy: options.constructionStrategy,
Expand Down Expand Up @@ -152,7 +152,7 @@ export class ConfigConstructorPool<Instance> implements IConfigConstructorPool<I
}
}

export interface IInstancePoolOptions<Instance> {
export interface IInstancePoolOptions<Instance, InstanceOut = Instance> {
/**
* The RDF object loader.
*/
Expand All @@ -164,7 +164,7 @@ export interface IInstancePoolOptions<Instance> {
/**
* The strategy for construction.
*/
constructionStrategy: IConstructionStrategy<Instance>;
constructionStrategy: IConstructionStrategy<Instance, InstanceOut>;
/**
* The module state.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/construction/IConfigConstructorPool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Resource } from 'rdf-object';
import type { IConstructionSettings } from './IConstructionSettings';
import type { IConstructionSettings } from './IConstructionSettings.js';

/**
* Manages and creates instances of components.
Expand Down
4 changes: 4 additions & 0 deletions lib/construction/IConstructionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ export interface IConstructionSettings {
* Mapping of variable id's to values.
*/
variables?: Record<string, any>;
/**
* Whether to use the ESM module format.
*/
esm?: boolean;
}
16 changes: 8 additions & 8 deletions lib/construction/argument/ArgumentConstructorHandlerArray.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import type { Resource } from 'rdf-object';
import { ErrorResourcesContext } from '../../util/ErrorResourcesContext';
import type { IConstructionSettings } from '../IConstructionSettings';
import type { IArgumentConstructorHandler } from './IArgumentConstructorHandler';
import type { IArgumentsConstructor } from './IArgumentsConstructor';
import { ErrorResourcesContext } from '../../util/ErrorResourcesContext.js';
import type { IConstructionSettings } from '../IConstructionSettings.js';
import type { IArgumentConstructorHandler } from './IArgumentConstructorHandler.js';
import type { IArgumentsConstructor } from './IArgumentsConstructor.js';

/**
* Handles arguments with elements as array.
*/
export class ArgumentConstructorHandlerArray implements IArgumentConstructorHandler {
public canHandle<Instance>(
public canHandle<Instance, InstanceOut = Instance>(
value: Resource,
settings: IConstructionSettings,
argsCreator: IArgumentsConstructor<Instance>,
argsCreator: IArgumentsConstructor<Instance, InstanceOut>,
): boolean {
return Boolean(value.property.elements);
}

public async handle<Instance>(
public async handle<Instance, InstanceOut = Instance>(
argument: Resource,
settings: IConstructionSettings,
argsCreator: IArgumentsConstructor<Instance>,
argsCreator: IArgumentsConstructor<Instance, InstanceOut>,
): Promise<Instance> {
// Recursively handle all sub-args in the array
const elements = await Promise.all(argument.properties.elements.map(async(entry: Resource) => {
Expand Down
Loading
Loading