From 7a1fa6ce07eabf8e207ae35c42ee34ae772403f6 Mon Sep 17 00:00:00 2001 From: mwcz Date: Wed, 29 Sep 2021 12:57:40 -0400 Subject: [PATCH] add support for esbuild's 'define' option --- README.md | 1 + examples/esbuild-define/index.ts | 1 + src/index.ts | 3 ++- src/options.ts | 3 ++- tests/index.spec.ts | 13 ++++++++++++- 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 examples/esbuild-define/index.ts diff --git a/README.md b/README.md index 924278e..aca3552 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ export interface Options { }, target?: string format?: string + define?: { [key: string]: string } } ``` diff --git a/examples/esbuild-define/index.ts b/examples/esbuild-define/index.ts new file mode 100644 index 0000000..41b8682 --- /dev/null +++ b/examples/esbuild-define/index.ts @@ -0,0 +1 @@ +export default EXAMPLE_VAR; diff --git a/src/index.ts b/src/index.ts index e2bca0f..dd9d37b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -46,6 +46,7 @@ const createTransformer = (options?: Options) => ({ target: options?.target || 'es2018', ...(options?.jsxFactory ? { jsxFactory: options.jsxFactory }: {}), ...(options?.jsxFragment ? { jsxFragment: options.jsxFragment }: {}), + ...(options?.define ? { define: options.define }: {}), ...sourcemaps }) @@ -74,4 +75,4 @@ const transformer: Pick = { export * from './options' -export default transformer \ No newline at end of file +export default transformer diff --git a/src/options.ts b/src/options.ts index 992e457..4d5e659 100644 --- a/src/options.ts +++ b/src/options.ts @@ -9,4 +9,5 @@ export interface Options { }, target?: string format?: string -} \ No newline at end of file + define?: { [key: string]: string } +} diff --git a/tests/index.spec.ts b/tests/index.spec.ts index a3a7726..bbbe2f6 100644 --- a/tests/index.spec.ts +++ b/tests/index.spec.ts @@ -25,6 +25,17 @@ const process = (sourcePath: string, options?: Options) => { return { ...output } } +test('esbuild transform option', () => { + const output = process('./examples/esbuild-define/index.ts', { + define: { + "EXAMPLE_VAR": "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + } + }) + + expect(output.code).toEqual(expect.stringContaining("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); +}) + + test('ts file', () => { const names = display() expect(names.includes('Jane')).toBeTruthy() @@ -94,4 +105,4 @@ test('should have sourcemap without [jest.mock]', () => { test('should not have sourcemap [default]', () => { const output = process('./examples/names-ts/index.ts', { sourcemap: false }) expect(output.map).toBeNull() -}) \ No newline at end of file +})