Skip to content

Commit

Permalink
feat(plugin): support after declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Pong420 committed Nov 28, 2020
1 parent 4bd55fe commit 9f5f107
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/compiler/compiler.ts
Expand Up @@ -52,6 +52,9 @@ export class Compiler {
: ((program as any) as ts.Program);
const before = plugins.beforeHooks.map((hook) => hook(programRef));
const after = plugins.afterHooks.map((hook) => hook(programRef));
const afterDeclarations = plugins.afterDeclarationsHooks.map((hook) =>
hook(programRef),
);
const emitResult = program.emit(
undefined,
undefined,
Expand All @@ -60,7 +63,7 @@ export class Compiler {
{
before: before.concat(tsconfigPathsPlugin),
after,
afterDeclarations: [],
afterDeclarations,
},
);

Expand Down
7 changes: 6 additions & 1 deletion lib/compiler/defaults/webpack-defaults.ts
Expand Up @@ -35,6 +35,9 @@ export const webpackDefaultsFactory = (
getCustomTransformers: (program: any) => ({
before: plugins.beforeHooks.map((hook) => hook(program)),
after: plugins.afterHooks.map((hook) => hook(program)),
afterDeclaratqions: plugins.afterDeclarationsHooks.map((hook) =>
hook(program),
),
}),
},
},
Expand Down Expand Up @@ -92,6 +95,8 @@ export const webpackDefaultsFactory = (
function isAnyPluginRegistered(plugins: MultiNestCompilerPlugins) {
return (
(plugins.afterHooks && plugins.afterHooks.length > 0) ||
(plugins.beforeHooks && plugins.beforeHooks.length > 0)
(plugins.beforeHooks && plugins.beforeHooks.length > 0) ||
(plugins.afterDeclarationsHooks &&
plugins.afterDeclarationsHooks.length > 0)
);
}
13 changes: 12 additions & 1 deletion lib/compiler/plugins-loader.ts
Expand Up @@ -16,11 +16,16 @@ interface PluginAndOptions {
export interface NestCompilerPlugin {
before?: (options?: Record<string, any>, program?: ts.Program) => Transformer;
after?: (options?: Record<string, any>, program?: ts.Program) => Transformer;
afterDeclarations?: (
options?: Record<string, any>,
program?: ts.Program,
) => Transformer;
}

export interface MultiNestCompilerPlugins {
beforeHooks: Array<(program?: ts.Program) => Transformer>;
afterHooks: Array<(program?: ts.Program) => Transformer>;
afterDeclarationsHooks: Array<(program?: ts.Program) => Transformer>;
}

export class PluginsLoader {
Expand Down Expand Up @@ -52,8 +57,9 @@ export class PluginsLoader {
});
const beforeHooks: MultiNestCompilerPlugins['afterHooks'] = [];
const afterHooks: MultiNestCompilerPlugins['beforeHooks'] = [];
const afterDeclarationsHooks: MultiNestCompilerPlugins['afterDeclarationsHooks'] = [];
pluginRefs.forEach((plugin, index) => {
if (!plugin.before && !plugin.after) {
if (!plugin.before && !plugin.after && !plugin.afterDeclarations) {
throw new Error(CLI_ERRORS.WRONG_PLUGIN(pluginNames[index]));
}
const options = isObject(plugins[index])
Expand All @@ -62,10 +68,15 @@ export class PluginsLoader {
plugin.before &&
beforeHooks.push(plugin.before.bind(plugin.before, options));
plugin.after && afterHooks.push(plugin.after.bind(plugin.after, options));
plugin.afterDeclarations &&
afterDeclarationsHooks.push(
plugin.afterDeclarations.bind(plugin.afterDeclarations, options),
);
});
return {
beforeHooks,
afterHooks,
afterDeclarationsHooks,
};
}
}
6 changes: 6 additions & 0 deletions lib/compiler/watch-compiler.ts
Expand Up @@ -92,10 +92,16 @@ export class WatchCompiler {
const after = plugins.afterHooks.map((hook) =>
hook(program.getProgram()),
);
const afterDeclarations = plugins.afterDeclarationsHooks.map((hook) =>
hook(program.getProgram()),
);
before.unshift(tsconfigPathsPlugin);

transforms.before = before.concat(transforms.before || []);
transforms.after = after.concat(transforms.after || []);
transforms.afterDeclarations = afterDeclarations.concat(
transforms.afterDeclarations || [],
);

return origProgramEmit(
targetSourceFile,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/errors.ts
Expand Up @@ -4,5 +4,5 @@ export const CLI_ERRORS = {
MISSING_TYPESCRIPT: (path: string) =>
`Could not find TypeScript configuration file "${path}". Please, ensure that you are running this command in the appropriate directory (inside Nest workspace).`,
WRONG_PLUGIN: (name: string) =>
`The "${name}" plugin is not compatible with Nest CLI. Neither "after()" nor "before()" function have been provided.`,
`The "${name}" plugin is not compatible with Nest CLI. Neither "after()" nor "before()" nor "afterDeclarations()" function have been provided.`,
};

0 comments on commit 9f5f107

Please sign in to comment.