From a7a925da367d88184058719a56af55882e7c7aff Mon Sep 17 00:00:00 2001 From: Moritz Kornher Date: Fri, 12 Mar 2021 20:13:33 +0000 Subject: [PATCH] feat: set sensible defaults for website deployment --- lib/source.ts | 9 ++++++++ test/source.test.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/source.ts b/lib/source.ts index f0655e2b..b7be6714 100644 --- a/lib/source.ts +++ b/lib/source.ts @@ -34,6 +34,15 @@ abstract class Source< constructor(entrypoint: string, props: Props) { const defaultOptions: BuildOptions = { platform: "browser", + ...(!props.buildOptions?.define + ? { + define: { + "process.env.NODE_ENV": `"${ + process.env.NODE_ENV ?? "production" + }"`, + }, + } + : {}), }; this.props = { diff --git a/test/source.test.ts b/test/source.test.ts index d7495fb9..bd9e26db 100644 --- a/test/source.test.ts +++ b/test/source.test.ts @@ -145,4 +145,57 @@ describe("source", () => { }).not.toThrow(); }); }); + + describe("default build options", () => { + it("does not override provided values", () => { + const source = new JavaScriptSource("fixtures/handlers/js-handler.js", { + buildOptions: { + platform: "neutral", + define: {}, + }, + }) as any; + + expect(source.props.buildOptions.platform).toBe("neutral"); + expect(Object.keys(source.props.buildOptions.define).length).toBe(0); + }); + + it("platform=browser", () => { + const source = new JavaScriptSource( + "fixtures/handlers/js-handler.js" + ) as any; + + expect(source.props.buildOptions.platform).toBe("browser"); + }); + + describe("NODE_ENV is set", () => { + it("defines NODE_ENV as that value", () => { + const source = new JavaScriptSource( + "fixtures/handlers/js-handler.js" + ) as any; + + expect(source.props.buildOptions.define?.["process.env.NODE_ENV"]).toBe( + '"test"' + ); + }); + }); + + describe("NODE_ENV not set", () => { + beforeEach(() => { + delete process.env.NODE_ENV; + }); + + afterEach(() => { + process.env.NODE_ENV = "test"; + }); + it("defines NODE_ENV as production", () => { + const source = new JavaScriptSource( + "fixtures/handlers/js-handler.js" + ) as any; + + expect(source.props.buildOptions.define?.["process.env.NODE_ENV"]).toBe( + '"production"' + ); + }); + }); + }); });