Skip to content

Commit

Permalink
feat: set sensible defaults for website deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Mar 12, 2021
1 parent efe25c1 commit a7a925d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
53 changes: 53 additions & 0 deletions test/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
);
});
});
});
});

0 comments on commit a7a925d

Please sign in to comment.