Skip to content

Commit

Permalink
Merge pull request #8 from beforeyoubid/feature/force-include
Browse files Browse the repository at this point in the history
added force include
  • Loading branch information
alice-byb authored Apr 18, 2023
2 parents 398dae3 + b7b812a commit 91b5f2c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ custom:
packager: 'auto' # can be specified as 'npm' | 'yarn' | 'pnpm';
forceExclude:
- some-library
forceInclude:
- some-other-library
```

[serverless]: https://www.serverless.com/
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const DEFAULT_CONFIG: Config = {
clean: true,
minify: false,
forceExclude: [],
forceInclude: [],
};

export const DEFAULT_AWS_MODULES: string[] = ['aws-sdk'];
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class EsbuildLayersPlugin implements Plugin {
async fetchModulesForLayer(layerName: string) {
const layerRefName = `${layerName.replace(/^./, x => x.toUpperCase())}LambdaLayer`;

const dependencies = await getExternalModules(this.serverless, this.config.forceExclude, layerRefName);
const dependencies = await getExternalModules(this.serverless, this.config, layerRefName);
if (dependencies.length === 0) return {};
const packageJsonText = await fs.promises.readFile(path.join(basePath, 'package.json'), { encoding: 'utf-8' });
const packageJson = JSON.parse(packageJsonText) as PackageJsonFile;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type Config = {
clean: boolean;
minify: boolean;
forceExclude: string[];
forceInclude: string[];
};

export enum Level {
Expand Down
7 changes: 5 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,13 @@ export function getLayers(serverless: Serverless): { [key: string]: Layer } {
/**
* locate all external modules attached to a particular layer
* @param serverless the serverless instance to check
* @param config the plugin config
* @param layerRefName the name of the layer to check
* @returns an array of strings containing the layer's modules
*/
export async function getExternalModules(
serverless: Serverless,
forceExclude: string[],
config: Config,
layerRefName: string
): Promise<string[]> {
const entries = await resolvedEntries(serverless, layerRefName);
Expand Down Expand Up @@ -166,7 +167,9 @@ export async function getExternalModules(
.reduce((list, listsOfMods) => list.concat(...listsOfMods), [])
.filter(module => !isBuiltinModule(module))
);
return Array.from(imports).filter(dep => !DEFAULT_AWS_MODULES.includes(dep) && !forceExclude.includes(dep));
return Array.from(imports)
.filter(dep => !DEFAULT_AWS_MODULES.includes(dep) && !config.forceExclude.includes(dep))
.concat(config.forceInclude);
}

export function compileConfig(userConfig: Partial<Config>): Config {
Expand Down

0 comments on commit 91b5f2c

Please sign in to comment.