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

pass config #8

Merged
merged 1 commit into from
Dec 12, 2022
Merged
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
121 changes: 87 additions & 34 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,24 @@ interface Config {
* custom cache key
* if not set, I will combine all transform's cahceKey.
*/
getCacheKey?: Transformer<Config>['getCacheKey'] | Transformer<Config>['getCacheKeyAsync'];
getCacheKey?:
| Transformer<Config>['getCacheKey']
| Transformer<Config>['getCacheKeyAsync'];
}

const requireTransformer = (path: string) => {
const transformer = (require(path).default || require(path));
const requireTransformer = (path: string, config: Record<string, any>) => {
const transformer = require(path).default || require(path);

if (!transformer.process && !transformer.processAsync) {
if (transformer.createTransformer) {
return transformer.createTransformer();
return transformer.createTransformer(config || {});
}

return null;
}

return transformer;
}
};

const flatTransformers = (transformers: Config['transformers']) => {
const containers: any[] = [];
Expand All @@ -41,25 +43,24 @@ const flatTransformers = (transformers: Config['transformers']) => {
let transformerModule;

if (typeof transformer === 'string') {
transformerModule = requireTransformer(transformer);
transformerModule = requireTransformer(transformer, {});

if (!transformerModule) {
console.error(`cant load ${transformer} as a transformer, so skip it`);
break;
}
}
else if (Array.isArray(transformer)) {
} else if (Array.isArray(transformer)) {
transformerModule = {
...requireTransformer(transformer[0]),
transformerConfig: transformer[1]
...requireTransformer(transformer[0], transformer[1]),
transformerConfig: transformer[1],
};
}

containers.push(transformerModule);
}

return containers;
}
};

const createTransformer = (): Transformer<Config> => {
let flattenTransformers: any = null;
Expand All @@ -69,49 +70,101 @@ const createTransformer = (): Transformer<Config> => {
return flattenTransformers;
}

flattenTransformers = flatTransformers(options.transformerConfig.transformers);
flattenTransformers = flatTransformers(
options.transformerConfig.transformers
);

return flattenTransformers;
}
};

const constructOptions = <T>(options: TransformOptions<Config>, config: T): TransformOptions<T> => {
const constructOptions = <T>(
options: TransformOptions<Config>,
config: T
): TransformOptions<T> => {
return {
...options,
transformerConfig: config
transformerConfig: config,
};
}
};

return {
canInstrument: true,
getCacheKey: (sourceText: string, sourcePath: string, options: TransformOptions<Config>): string => {
getCacheKey: (
sourceText: string,
sourcePath: string,
options: TransformOptions<Config>
): string => {
const transformers = getFlattenTransformers(options);

return transformers.reduce((res: string, transformer: any) => {
return res + transformer.getCacheKey?.(sourceText, sourcePath, constructOptions(options, transformer.transformerConfig)) || ''
return (
res +
transformer.getCacheKey?.(
sourceText,
sourcePath,
constructOptions(options, transformer.transformerConfig)
) || ''
);
}, '');
},
process: (sourceText: string, sourcePath: string, options: TransformOptions<Config>): string => {
process: (
sourceText: string,
sourcePath: string,
options: TransformOptions<Config>
): string => {
const transformers = getFlattenTransformers(options);

return transformers.reduce((res: { code: string }, transformer: any) => {
return transformer.process?.(res.code ? res.code : res, sourcePath, constructOptions(options, transformer.transformerConfig))
}, { code: sourceText });
return transformers.reduce(
(res: { code: string }, transformer: any) => {
return transformer.process?.(
res.code ? res.code : res,
sourcePath,
constructOptions(options, transformer.transformerConfig)
);
},
{ code: sourceText }
);
},
getCacheKeyAsync: async (sourceText: string, sourcePath: string, options: TransformOptions<Config>) => {
getCacheKeyAsync: async (
sourceText: string,
sourcePath: string,
options: TransformOptions<Config>
) => {
const transformers = getFlattenTransformers(options);

return await transformers.reduce(async (res: string, transformer: any) => {
return res + await transformer.getCacheKeyAsync?.(sourceText, sourcePath, constructOptions(options, transformer.transformerConfig)) || ''
}, '');
return await transformers.reduce(
async (res: string, transformer: any) => {
return (
res +
(await transformer.getCacheKeyAsync?.(
sourceText,
sourcePath,
constructOptions(options, transformer.transformerConfig)
)) || ''
);
},
''
);
},
processAsync: async (sourceText: string, sourcePath: string, options: TransformOptions<Config>) => {
processAsync: async (
sourceText: string,
sourcePath: string,
options: TransformOptions<Config>
) => {
const transformers = getFlattenTransformers(options);

return await transformers.reduce(async (res: { code: string }, transformer: any) => {
return await transformer.process?.(res.code ? res.code : res, sourcePath, constructOptions(options, transformer.transformerConfig));
}, { code: sourceText });
}
}
}
return await transformers.reduce(
async (res: { code: string }, transformer: any) => {
return await transformer.process?.(
res.code ? res.code : res,
sourcePath,
constructOptions(options, transformer.transformerConfig)
);
},
{ code: sourceText }
);
},
};
};

export default createTransformer();
export default createTransformer();