Skip to content

Commit

Permalink
Start work on production builds (#1)
Browse files Browse the repository at this point in the history
Note that this is about reducing the runtime overhead of dev-mode
constructs, not (yet) about eliminating them from the build.

1. use import.meta.env.PROD in user code
2. add `pnpm test:prod`, which runs the tests in prod mode
3. Make verify() and verified() noops in production
4. Update rollup.config.js to mirror how vite replaces import.meta.env

Note that the reason we have rollup.config.ts is because we need the
feature of rollup that allows us to create separate, distinct configs,
because we don't want the packages to share chunks with each other.
See [this vite issue].

[this vite issue]: vitejs/vite#1736
  • Loading branch information
wycats committed Jun 24, 2022
1 parent 15a3f75 commit 829d4c9
Show file tree
Hide file tree
Showing 17 changed files with 256 additions and 38 deletions.
15 changes: 15 additions & 0 deletions .build/import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createReplacePlugin } from "./replace.js";

const MODE = process.env.MODE ?? "development";
const DEV = MODE === "development";
const PROD = MODE === "production";

export default createReplacePlugin(
(id) => /\.(j|t)sx?$/.test(id),
{
"import.meta.env.MODE": process.env.MODE ?? "development",
"import.meta.env.DEV": DEV ? "true" : "false",
"import.meta.env.PROD": PROD ? "true" : "false",
},
true
);
58 changes: 58 additions & 0 deletions .build/replace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// originally from: https://github.com/vitejs/vite/blob/51e9c83458e30e3ce70abead14e02a7b353322d9/src/node/build/buildPluginReplace.ts

// import type { Plugin, TransformResult } from "rollup";
import MagicString from "magic-string";

/**
* @param {(id: string) => boolean} test
* @param {Record<string, string>} replacements
* @param {boolean} sourcemap
* @returns {import("rollup").Plugin}
*/
export function createReplacePlugin(test, replacements, sourcemap) {
const pattern = new RegExp(
"\\b(" +
Object.keys(replacements)
.map((str) => {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&");
})
.join("|") +
")\\b",
"g"
);

return {
name: "starbeam:replace",
/**
* @param {string} code
* @param {string} id
* @returns {import("rollup").TransformResult}
*/
transform(code, id) {
if (test(id)) {
const s = new MagicString(code);
let hasReplaced = false;
let match;

while ((match = pattern.exec(code))) {
hasReplaced = true;
const start = match.index;
const end = start + match[0].length;
const replacement = replacements[match[1]];
s.overwrite(start, end, replacement);
}

if (!hasReplaced) {
return null;
}

/** @type { import("rollup").TransformResult} */
const result = { code: s.toString() };
if (sourcemap) {
result.map = s.generateMap({ hires: true });
}
return result;
}
},
};
}
11 changes: 5 additions & 6 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"root": true,
"plugins": [
"prettier",
"unused-imports",
"simple-import-sort"
],
"plugins": ["prettier", "unused-imports", "simple-import-sort"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": false,
Expand Down Expand Up @@ -107,7 +103,10 @@
}
},
{
"files": ["framework/react/use-resource/scripts/**/*"],
"files": [
"framework/react/use-resource/scripts/**/*",
"rollup.config.js"
],
"env": {
"node": true
}
Expand Down
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"*.jsx": "${capture}.js",
"*.tsx": "${capture}.ts",
"tsconfig.json": "tsconfig.*",
"package.json": "package-lock.json, yarn.lock, pnpm-lock.yaml, .editorconfig, .eslintrc.json, .gitignore, .npmrc, .quokka, pnpm-workspace.yaml, vite.config.ts"
"package.json": "package-lock.json, yarn.lock, pnpm-lock.yaml, .pnpm-debug.log, .editorconfig, .eslintrc.json, .gitignore, .npmrc, .quokka, pnpm-workspace.yaml",
"vite.config.ts": "rollup.config.js, .env.*"
},
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"vitest.enable": true,
Expand All @@ -30,5 +31,8 @@
},
"[ignore]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
},
"[dotenv]": {
"editor.defaultFormatter": "foxundermoon.shell-format"
}
}
47 changes: 47 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
MIT License

Copyright (c) 20222-present, Yehuda Katz and Starbeam contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---

Some additional code with the following licenses is included.

MIT License

Copyright (c) 2019-present, Yuxi (Evan) You

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 3 additions & 3 deletions framework/react/use-resource/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,16 @@ export function useResource(): {
options?: LifecycleOptions
) => Resource<T, void>;
} {
return useResource.with(undefined as void);
return useResource.withState(undefined as void);
}

useResource.create = <T>(
create: CreateResource<T, void>
): Resource<T, void> => {
return useResource.with(undefined as void).create(create);
return useResource.withState(undefined as void).create(create);
};

useResource.with = <A>(
useResource.withState = <A>(
state: A
): {
create: <T>(
Expand Down
6 changes: 3 additions & 3 deletions framework/react/use-resource/tests/use-resource.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ testModes("useResource", (mode) => {
const [count, setCount] = useState(0);

const resource = useResource
.with({ count })
.withState({ count })
.create(({ count }) => TestResource.initial(count))
.update((resource, { count }) => resource.transition("updated", count))
.on({
Expand Down Expand Up @@ -74,7 +74,7 @@ testModes("useResource (nested)", (mode) => {
const [count, setCount] = useState(0);

const resource = useResource
.with({ count })
.withState({ count })
.create(({ count }) => TestResource.initial(count))
.update((resource, { count }) => resource.transition("updated", count))
.on({
Expand Down Expand Up @@ -136,7 +136,7 @@ testModes("useResource (nested, stability across remounting)", (mode) => {
const [count, setCount] = useState(0);

const resource = useResource
.with({ count })
.withState({ count })
.create(({ count }) => TestResource.initial(count))
.update((resource, { count }) => resource.transition("updated", count))
.on({
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"types": "src/index.ts",
"main": "src/index.ts",
"private": true,
"license": "MIT",
"publishConfig": {
"registry": "http://localhost:4873/"
},
Expand All @@ -15,6 +16,7 @@
"serve": "vite preview",
"test": "vitest",
"typecheck": "tsc --build",
"test:prod": "vitest --mode production",
"demos:react:store": "vite --port 3001 -c ./demos/react-store/vite.config.ts"
},
"devDependencies": {
Expand All @@ -23,6 +25,7 @@
"@domtree/any": "workspace:*",
"@domtree/flavors": "workspace:*",
"@domtree/minimal": "workspace:*",
"@import-meta-env/unplugin": "^0.1.8",
"@rollup/plugin-sucrase": "^4.0.4",
"@rollup/plugin-typescript": "^8.3.3",
"@swc/core": "^1.2.203",
Expand All @@ -33,6 +36,7 @@
"@typescript-eslint/eslint-plugin": "^5.27.0",
"@typescript-eslint/parser": "^5.27.0",
"@vitest/ui": "^0.14.1",
"dotenv": "^16.0.1",
"esbuild": "^0.14.46",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
Expand All @@ -45,6 +49,7 @@
"esno": "^0.16.3",
"fast-glob": "^3.2.11",
"jsdom": "^19.0.0",
"magic-string": "^0.26.2",
"postcss": "^8.4.14",
"prettier": "^2.6.2",
"rollup": "^2.75.6",
Expand Down
5 changes: 5 additions & 0 deletions packages/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="vite/client" />

interface ImportMeta {
readonly env: ImportMetaEnv;
}
18 changes: 11 additions & 7 deletions packages/verify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ export {
} from "./src/assertions/basic.js";
export { isOneOf } from "./src/assertions/multi.js";
export { type TypeOf, hasType } from "./src/assertions/types.js";
export {
type Expectation,
expected,
VerificationError,
verified,
verify,
} from "./src/verify.js";
export { type Expectation, expected, VerificationError } from "./src/verify.js";

import { verify as verifyDev } from "./src/verify.js";
export const verify: typeof verifyDev["noop"] = import.meta.env.DEV
? verifyDev
: verifyDev.noop;

import { verified as verifiedDev } from "./src/verify.js";
export const verified: typeof verifiedDev["noop"] = import.meta.env.DEV
? verifiedDev
: verifiedDev.noop;
16 changes: 16 additions & 0 deletions packages/verify/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export function verify<T, U extends T>(
}
}

verify.noop = <T, U extends T>(
value: T,
_check: ((input: T) => input is U) | ((input: T) => boolean),
_error?: Expectation<T>
): asserts value is U => {
return;
};

export function verified<T, U extends T>(
value: T,
check: (input: T) => input is U,
Expand All @@ -44,6 +52,14 @@ export function verified<T, U extends T>(
return value;
}

verified.noop = <T, U extends T>(
value: T,
_check: (input: T) => input is U,
_error?: Expectation<T>
): U => {
return value as U;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class Expectation<In = any> {
static create(description?: string) {
Expand Down
4 changes: 3 additions & 1 deletion packages/verify/tests/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import "./support.js";
import { expected, isEqual, isPresent, verify } from "@starbeam/verify";
import { describe, expect, test } from "vitest";

describe("basic verification", () => {
const isProd = import.meta.env.PROD;

describe.skipIf(isProd)("basic verification", () => {
test("isPresent", () => {
expect((value: unknown) => verify(value, isPresent)).toFail(
null,
Expand Down
4 changes: 3 additions & 1 deletion packages/verify/tests/verify.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expected, verify } from "@starbeam/verify";
import { describe, expect, test } from "vitest";

describe("verify", () => {
const isProd = import.meta.env.PROD;

describe.skipIf(isProd)("verify", () => {
test("default verify message", () => {
const isPresent = <T>(input: T | null | undefined): input is T => {
return input !== null && input !== undefined;
Expand Down
Loading

0 comments on commit 829d4c9

Please sign in to comment.