Skip to content

Commit

Permalink
feat(packager): ability to run custom scripts after packager install
Browse files Browse the repository at this point in the history
  • Loading branch information
vamche committed Jul 1, 2021
1 parent b9be9c7 commit 6f872aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ custom:
esbuild:
packager: yarn # optional - npm or yarn, default is npm
packagePath: absolute/path/to/package.json # optional - by default it looks for a package.json in the working directory
packagerOptions: # optional - packager related options, currently supports only 'scripts' for both npm and yarn
scripts: # scripts to be executed, can be a string or array of strings
- echo 'Hello World!'
- rm -rf node_modules
```

To easily mark all the `dependencies` in `package.json` as `external`, you can utilize `esbuild-node-externals` [plugin](https://www.npmjs.com/package/esbuild-node-externals).
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ export interface WatchConfiguration {
ignore?: string[] | string;
}

export interface PackagerOptions {
scripts?: string[] | string;
}

export interface Configuration extends Omit<BuildOptions, 'watch' | 'plugins'> {
packager: 'npm' | 'yarn';
packagePath: string;
exclude: string[];
watch: WatchConfiguration;
plugins?: string;
keepOutputDirectory?: boolean;
packagerOptions?: PackagerOptions;
}

const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
Expand All @@ -46,7 +51,8 @@ const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
pattern: './**/*.(js|ts)',
ignore: [WORK_FOLDER, 'dist', 'node_modules', SERVERLESS_FOLDER],
},
keepOutputDirectory: false
keepOutputDirectory: false,
packagerOptions: {},
};

export class EsbuildPlugin implements Plugin {
Expand Down Expand Up @@ -238,6 +244,7 @@ export class EsbuildPlugin implements Plugin {
delete config['packagePath'];
delete config['watch'];
delete config['keepOutputDirectory'];
delete config['packagerOptions'];

const bundlePath = entry.substr(0, entry.lastIndexOf('.')) + '.js';

Expand Down
18 changes: 18 additions & 0 deletions src/pack-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ export async function packExternalModules(this: EsbuildPlugin) {
// Fetch needed original package.json sections
const sectionNames = packager.copyPackageSectionNames;

// Get scripts from packager options
const packagerScripts = this.buildOptions.packagerOptions
? ([].concat(this.buildOptions.packagerOptions.scripts || []))
.reduce((scripts, script, index) => {
scripts[`script${index}`] = script;
return scripts;
}, {})
: {};

const rootPackageJson: Record<string, any> = this.serverless.utils.readFileSync(
rootPackageJsonPath
);
Expand Down Expand Up @@ -267,6 +276,7 @@ export async function packExternalModules(this: EsbuildPlugin) {
version: '1.0.0',
description: `Packaged externals for ${this.serverless.service.service}`,
private: true,
scripts: packagerScripts,
},
packageSections
);
Expand Down Expand Up @@ -314,4 +324,12 @@ export async function packExternalModules(this: EsbuildPlugin) {
await packager.prune(compositeModulePath);
this.options.verbose &&
this.serverless.cli.log(`Prune: ${compositeModulePath} [${Date.now() - startPrune} ms]`);

// Run packager scripts
if (Object.keys(packagerScripts).length > 0) {
const startScripts = Date.now();
await packager.runScripts(this.buildDirPath, Object.keys(packagerScripts));
this.options.verbose &&
this.serverless.cli.log(`Packager scripts took [${Date.now() - startScripts} ms].\nExecuted scripts: ${Object.values(packagerScripts).map(script => `\n ${script}`)}`);
}
}

0 comments on commit 6f872aa

Please sign in to comment.