-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start work on production builds (#1)
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
Showing
17 changed files
with
256 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface ImportMeta { | ||
readonly env: ImportMetaEnv; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.