Skip to content

Commit

Permalink
docs: update (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest authored Oct 9, 2023
1 parent 6336ecb commit 347e586
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 60 deletions.
25 changes: 21 additions & 4 deletions docs/guide/getting-started/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

> In this guide, we will use Webpack as an example, but you can also use other build tools. All supported build tools can be found on the [compile-time transform plugin](/guide/getting-started/compile-time-transform.html)s page.
![](/glance.png)

### Write your code

1. To prevent accidentally leaking environment variables to the client, you need to list public environment variables in an example file (e.g., `.env.example`):
Expand Down Expand Up @@ -54,7 +56,7 @@ $ npm i -D dotenv

```ini
# .env
NAME=world
NAME=development
```

See the [`.env` file](#env-file) section for details.
Expand Down Expand Up @@ -87,7 +89,7 @@ $ npm i -D dotenv
```diff
// dist/main.js
- const name = import.meta.env.NAME
+ const name = "world"
+ const name = "developemnt"
// ...
```

Expand All @@ -107,6 +109,12 @@ $ npm i -D dotenv
env: ".env",
example: ".env.example",
- transformMode: "compile-time",
+ // If you are using popular packagers such as Webpack and Vite,
+ // @import-meta-env/unplugin will automatically switch the `transformMode` for you:
+ // - for development mode, `transformMode` will be `"compile-time"`
+ // - for production mode, `transformMode` will be `"runtime"`
+
+ // Otherwise, you need to set `transformMode` according to your needs:
+ transformMode: "runtime",
}),
],
Expand Down Expand Up @@ -143,13 +151,22 @@ $ npm i -D dotenv

See the [special expression](#special-expression) section for details.

1. Define environment variables:

```ini
# .env
NAME=production
```

See the [`.env` file](#env-file) section for details.

1. Install [runtime transform tool](/guide/getting-started/runtime-transform.html).

```sh
$ npm i -D @import-meta-env/cli
```

1. Transform it again using the packaged executable:
1. Transform it again:

```sh
npx import-meta-env -x .env.example -p dist/index.html
Expand All @@ -163,7 +180,7 @@ $ npm i -D dotenv
<meta charset="UTF-8" />
<script>
- globalThis.import_meta_env = JSON.parse('"import_meta_env_placeholder"')
+ globalThis.import_meta_env = JSON.parse('{"NAME":"world"}');
+ globalThis.import_meta_env = JSON.parse('{"NAME":"production"}');
</script>
<script defer="defer" src="main.js"></script>
</head>
Expand Down
56 changes: 0 additions & 56 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,3 @@ features:
- title: Save Time & Money
details: Speed up your CI/CD pipeline, you no longer need to build multiple bundles for different stages.
---

<style>
#app .container > .image > .image-container > .image-src:hover {
cursor: zoom-in;
}

#app .container > .image > .image-container.zoom-in {
cursor: zoom-out;
z-index: 9999;
position: fixed;
display: flex;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
max-width: unset;
max-height: unset;
background-color: black;
transform: unset;
}

#app .container > .image > .image-container.zoom-in > .image-src {
cursor: zoom-out;
max-width: 100vw;
max-height: 100vh;
object-fit: cover;
}
</style>

<script>
const doc = typeof document === 'object' ? document : undefined
function addZoomFeature() {
if (doc === undefined) return
let shouldZoomIn = true;
const imageContainer = doc.querySelector(".image-container");
const imageSrc = doc.querySelector(".image-src");
if (imageContainer === null || imageSrc === null) {
requestAnimationFrame(addZoomFeature);
return;
}
imageContainer.addEventListener("click", (e) => {
if (shouldZoomIn) {
if (e.target !== imageSrc) {
return;
}
imageContainer.classList.add("zoom-in");
doc.body.style.overflow = "hidden";
} else {
imageContainer.classList.remove("zoom-in");
doc.body.style.overflow = "unset";
}
shouldZoomIn = !shouldZoomIn;
});
}
addZoomFeature();
</script>
40 changes: 40 additions & 0 deletions docs/public/glance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Code

```ini
# .env.example
HELLO=
```

```ini
# .env
HELLO=world
```

```ts
// src/index.js
const name = import.meta.env.NAME;
document.querySelector("body").innerHTML = `<h1>Hello, ${name}</h1>`;
```

```html
<!-- public/index.html -->
<script>
globalThis.import_meta_env = JSON.parse('"import_meta_env_placeholder"');
</script>
```

# Development

```sh
$ npx webpack --mode=development
```

# Production

```sh
$ npx webpack --mode=production
```

```sh
$ npx import-meta-env -x .env.example -p dist/index.html
```
Binary file modified docs/public/glance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 347e586

Please sign in to comment.