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(externals): add option to disable packing of external modules #119

Open
wants to merge 1 commit 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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ custom:
packagePath: absolute/path/to/package.json # optional - by default it looks for a package.json in the working directory
```

This behavior can be disabled via `packExternals`:

```yml
custom:
esbuild:
packExternals: false
```

### Usign esbuild plugins

*Note that the plugins API is still experimental : see [the documentation page](https://esbuild.github.io/plugins/)*
_Note that the plugins API is still experimental : see [the documentation page](https://esbuild.github.io/plugins/)_

You can configure esbuild plugins by passing a plugins' configuration file:

Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface Configuration extends Omit<BuildOptions, 'watch' | 'plugins'> {
exclude: string[];
watch: WatchConfiguration;
plugins?: string;
packExternals?: boolean;
}

const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
Expand All @@ -40,6 +41,7 @@ const DEFAULT_BUILD_OPTIONS: Partial<Configuration> = {
external: [],
exclude: ['aws-sdk'],
packager: 'npm',
packExternals: true,
watch: {
pattern: './**/*.(js|ts)',
ignore: [WORK_FOLDER, 'dist', 'node_modules', SERVERLESS_FOLDER],
Expand Down Expand Up @@ -179,7 +181,7 @@ export class EsbuildPlugin implements Plugin {
...(this.serverless.service.package?.patterns || []),
'!node_modules/serverless-esbuild',
]),
]
],
};

for (const fnName in this.functions) {
Expand All @@ -192,7 +194,7 @@ export class EsbuildPlugin implements Plugin {
...(fn.package?.exclude || []).map(concat('!')),
...(fn.package?.patterns || []),
]),
]
],
};
}
}
Expand Down Expand Up @@ -221,6 +223,7 @@ export class EsbuildPlugin implements Plugin {
delete config['packagePath'];
delete config['watch'];
delete config['pugins'];
delete config['packExternals'];

const result = await build(config);

Expand Down Expand Up @@ -264,7 +267,9 @@ export class EsbuildPlugin implements Plugin {
}
const files = await globby(fn.package.patterns);
for (const filename of files) {
const destFileName = path.resolve(path.join(this.buildDirPath, `__only_${fn.name}`, filename));
const destFileName = path.resolve(
path.join(this.buildDirPath, `__only_${fn.name}`, filename)
);
const dirname = path.dirname(destFileName);

if (!fs.existsSync(dirname)) {
Expand Down
3 changes: 1 addition & 2 deletions src/pack-externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function getProdModules(
export async function packExternalModules(this: EsbuildPlugin) {
const externals = without(this.buildOptions.exclude, this.buildOptions.external);

if (!externals || !externals.length) {
if (!externals || !externals.length || !this.buildOptions.packExternals) {
return;
}

Expand Down Expand Up @@ -294,7 +294,6 @@ export async function packExternalModules(this: EsbuildPlugin) {
this.serverless.cli.log('Packing external modules: ' + compositeModules.join(', '));
await packager.install(compositeModulePath, exists);
this.options.verbose && this.serverless.cli.log(`Package took [${Date.now() - start} ms]`);

// Prune extraneous packages - removes not needed ones
const startPrune = Date.now();
await packager.prune(compositeModulePath);
Expand Down