Skip to content

Commit

Permalink
feat(resolveExtensions): added stripResolveExtensions option to remov…
Browse files Browse the repository at this point in the history
…e file extension prefixes
  • Loading branch information
JohnGrisham committed Dec 1, 2023
1 parent 6a97354 commit e42e7c8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { uniq } from 'ramda';

import type Serverless from 'serverless';
import type ServerlessPlugin from 'serverless/classes/Plugin';
import type { Configuration, DependencyMap, FunctionEntry } from './types';
import type { Configuration, DependencyMap, FunctionEntry, IFile } from './types';
import type { EsbuildFunctionDefinitionHandler } from './types';
import { DEFAULT_EXTENSIONS } from './constants';

Expand Down Expand Up @@ -315,3 +315,18 @@ export const buildServerlessV3LoggerFromLegacyLogger = (
verbose: legacyLogger.log.bind(legacyLogger),
success: legacyLogger.log.bind(legacyLogger),
});

export const stripResolveExtensions = (file: IFile, extensions: string[]): IFile => {
const resolveExtensionMatch = file.localPath.match(extensions.map((ext) => ext).join('|'));

if (resolveExtensionMatch?.length && !DEFAULT_EXTENSIONS.includes(resolveExtensionMatch[0])) {
const extensionParts = resolveExtensionMatch[0].split('.');

return {
...file,
localPath: file.localPath.replace(resolveExtensionMatch[0], `.${extensionParts[extensionParts.length - 1]}`),
};
}

return file;
};
13 changes: 11 additions & 2 deletions src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import semver from 'semver';
import type Serverless from 'serverless';

import { ONLY_PREFIX, SERVERLESS_FOLDER } from './constants';
import { assertIsString, doSharePath, flatDep, getDepsFromBundle, isESM } from './helper';
import { assertIsString, doSharePath, flatDep, getDepsFromBundle, isESM, stripResolveExtensions } from './helper';
import { getPackager } from './packagers';
import { humanSize, trimExtension, zip } from './utils';

Expand Down Expand Up @@ -114,7 +114,16 @@ export async function pack(this: EsbuildServerlessPlugin) {
onlyFiles: true,
})
.filter((file) => !excludedFiles.includes(file))
.map((localPath) => ({ localPath, rootPath: path.join(buildDirPath, localPath) }));
.map((localPath) => ({ localPath, rootPath: path.join(buildDirPath, localPath) }))
.map((file) => {
if (this.buildOptions?.resolveExtensions && this.buildOptions.resolveExtensions.length > 0) {
if (this.options.stripResolveExtensions) {
return stripResolveExtensions(file, this.buildOptions.resolveExtensions);
}
}

return file;
});

if (isEmpty(files)) {
this.log.verbose('Packaging: No files found. Skipping esbuild.');
Expand Down
16 changes: 14 additions & 2 deletions src/tests/helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import fs from 'fs-extra';
import os from 'os';
import path from 'path';

import { extractFunctionEntries, flatDep, getDepsFromBundle, isESM } from '../helper';
import { extractFunctionEntries, flatDep, getDepsFromBundle, isESM, stripResolveExtensions } from '../helper';

import type { Configuration, DependencyMap } from '../types';
import type { Configuration, DependencyMap, IFile } from '../types';

jest.mock('fs-extra');

Expand Down Expand Up @@ -643,3 +643,15 @@ describe('flatDeps', () => {
});
});
});

describe('stripResolveExtensions', () => {
it('should remove custom extension prefixes', () => {
const result = stripResolveExtensions({ localPath: 'test.custom.js' } as IFile, ['.custom.js']);
expect(result.localPath).toEqual('test.js');
});

it('should ignore prefixes not inside the resolve extensions list', () => {
const result = stripResolveExtensions({ localPath: 'test.other.js' } as IFile, ['.custom.js']);
expect(result.localPath).toEqual('test.other.js');
});
});
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type ReturnPluginsFn = (sls: Serverless) => Plugins;

export interface ImprovedServerlessOptions extends Serverless.Options {
package?: string;
stripResolveExtensions?: boolean;
}

export interface WatchConfiguration {
Expand Down

0 comments on commit e42e7c8

Please sign in to comment.