Skip to content

Commit

Permalink
improve cloudflare worker examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Feb 16, 2022
1 parent 6c4abd9 commit b10c42a
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 85 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from '../cloudflare-workers/.testRun'
testRun('npm run preview:miniflare', { hasStarWarsPage: false })

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from '../cloudflare-workers/.testRun'
testRun('npm run preview:wrangler', { hasStarWarsPage: false })
20 changes: 9 additions & 11 deletions examples/cloudflare-workers-vue-html-streaming/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"scripts": {
"// For increased dev speed we use an Express.js dev server instead of wrangler": "",
"dev": "node ./dev-server",
"// Use miniflare to simulate Cloudflare Workers environment": "",
"dev:miniflare": "npm run build:vite && miniflare --port 3000 worker/worker/script.js",
"// Build and try the worker locally": "",
"prod": "npm run build:vite && wrangler dev --port 3000",
"// Build and deploy the worker to Cloudflare Workers": "",
"// Development server": "",
"dev": "node ./devServer",
"// Preview worker locally": "",
"preview:": "npm run preview:miniflare",
"preview:miniflare": "npm run build:vite && miniflare --port 3000 worker/worker/script.js",
"preview:wrangler": "npm run build:vite && wrangler dev --port 3000",
"// Deploy worker to Cloudflare": "",
"deploy": "npm run build:vite && wrangler publish",
"// Build scripts": "",
"// Build": "",
"build": "npm run build:vite && npm run build:worker",
"build:vite": "vite build && vite build --ssr",
"build:worker": "wrangler build",
"// To sustain the vite-plugin-ssr dev link, we need to do this as Wrangler breaks links upon `$ wrangler dev`": "",
"// postinstall": "(cd ../../vite-plugin-ssr/ && yarn link) && yarn link vite-plugin-ssr"
"build:worker": "wrangler build"
},
"dependencies": {
"@cloudflare/kv-asset-handler": "~0.2.0",
Expand Down
55 changes: 30 additions & 25 deletions examples/cloudflare-workers-vue-html-streaming/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ Example of deploying to [Cloudflare Workers](https://workers.cloudflare.com/) wi
See [vite-plugin-ssr.com/cloudflare-workers](https://vite-plugin-ssr.com/cloudflare-workers).


## Run

To run the example:
1. ```bash
git clone git@github.com:brillout/vite-plugin-ssr
cd vite-plugin-ssr/examples/cloudflare-workers-vue-html-streaming/
```

2. Create a Cloudflare account and paste your account id in `wrangler.toml#account_id`.

3. ```bash
npm install
```
To develop (for increased dev speed we use an Express.js dev server instead of `wrangler`):
```bash
npm run dev
```
To build and try the worker locally:
```bash
npm run prod
```
To build and deploy the worker to Cloudflare Workers:
```bash
npm run deploy
```
## Run example

Setup:
```bash
git clone git@github.com:brillout/vite-plugin-ssr
cd vite-plugin-ssr/examples/cloudflare-workers-vue-html-streaming/
npm install
```

To develop: (For increased development speed, we use an Express.js development server instead of a worker.)
```bash
npm run dev
```

To try the worker locally with miniflare: (No account needed.)
```bash
npm run preview
```

To be able to use `wrangler`, create a Cloudflare account and paste your account id in `wrangler.toml#account_id`.

To try the worker locally with wrangler:
```bash
npm run preview:wrangler
```

To deploy the worker to Cloudflare:
```bash
npm run deploy
```


## `dist/server/importBuild.js`
Expand Down
2 changes: 0 additions & 2 deletions examples/cloudflare-workers/.test-dev-miniflare.spec.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/cloudflare-workers/.test-miniflare.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('npm run preview:miniflare', { hasStarWarsPage: true })
2 changes: 0 additions & 2 deletions examples/cloudflare-workers/.test-prod.spec.ts

This file was deleted.

2 changes: 2 additions & 0 deletions examples/cloudflare-workers/.test-wrangler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { testRun } from './.testRun'
testRun('npm run preview:wrangler', { hasStarWarsPage: true })
8 changes: 4 additions & 4 deletions examples/cloudflare-workers/.testRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import assert from 'assert'
export { testRun }

function testRun(
cmd: 'npm run dev' | 'npm run prod' | 'npm run dev:miniflare',
cmd: 'npm run dev' | 'npm run preview:miniflare' | 'npm run preview:wrangler',
{ hasStarWarsPage }: { hasStarWarsPage: boolean },
) {
const isMiniflare = cmd === 'npm run dev:miniflare'
const isWrangler = cmd === 'npm run prod'
const isMiniflare = cmd === 'npm run preview:miniflare'
const isWrangler = cmd === 'npm run preview:wrangler'
const isWorker = isMiniflare || isWrangler

if ((isWindows() || isNode12()) && isWorker) {
test('SKIPED: miniflare and wrangler', () => {})
return
}

if (cmd === 'npm run prod') {
if (isWrangler) {
if (!isGithubAction() || process.env['GIT_BRANCH'] !== 'master') {
test('SKIPED: wrangler test is not run locally nor in Pull Requests', () => {})
return
Expand Down
20 changes: 9 additions & 11 deletions examples/cloudflare-workers/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"scripts": {
"// For increased dev speed we use an Express.js dev server instead of wrangler": "",
"dev": "node ./dev-server",
"// Use miniflare to simulate Cloudflare Workers environment": "",
"dev:miniflare": "npm run build:vite && miniflare --port 3000 worker/worker/script.js",
"// Build and try the worker locally": "",
"prod": "npm run build:vite && wrangler dev --port 3000",
"// Build and deploy the worker to Cloudflare Workers": "",
"// Development server": "",
"dev": "node ./devServer",
"// Preview worker locally": "",
"preview:": "npm run preview:miniflare",
"preview:miniflare": "npm run build:vite && miniflare --port 3000 worker/worker/script.js",
"preview:wrangler": "npm run build:vite && wrangler dev --port 3000",
"// Deploy worker to Cloudflare": "",
"deploy": "npm run build:vite && wrangler publish",
"// Build scripts": "",
"// Build": "",
"build": "npm run build:vite && npm run build:worker",
"build:vite": "vite build && vite build --ssr",
"build:worker": "wrangler build",
"// To sustain the vite-plugin-ssr dev link, we need to do this as Wrangler breaks links upon `$ wrangler dev`": "",
"// postinstall": "(cd ../../vite-plugin-ssr/ && yarn link) && yarn link vite-plugin-ssr"
"build:worker": "wrangler build"
},
"dependencies": {
"@cloudflare/kv-asset-handler": "~0.2.0",
Expand Down
56 changes: 30 additions & 26 deletions examples/cloudflare-workers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,36 @@ Example of deploying to [Cloudflare Workers](https://workers.cloudflare.com/) wi
See [vite-plugin-ssr.com/cloudflare-workers](https://vite-plugin-ssr.com/cloudflare-workers).


## Run

To run the example:

1. ```bash
git clone git@github.com:brillout/vite-plugin-ssr
cd vite-plugin-ssr/examples/cloudflare-workers/
```

2. Create a Cloudflare account and paste your account id in `wrangler.toml#account_id`.

3. ```bash
npm install
```
To develop (for increased dev speed we use an Express.js dev server instead of `wrangler`):
```bash
npm run dev
```
To build and try the worker locally:
```bash
npm run prod
```
To build and deploy the worker to Cloudflare Workers:
```bash
npm run deploy
```
## Run example

Setup:
```bash
git clone git@github.com:brillout/vite-plugin-ssr
cd vite-plugin-ssr/examples/cloudflare-workers/
npm install
```

To develop: (For increased development speed, we use an Express.js development server instead of a worker.)
```bash
npm run dev
```

To try the worker locally with miniflare: (No account needed.)
```bash
npm run preview
```

To be able to use `wrangler`, create a Cloudflare account and paste your account id in `wrangler.toml#account_id`.

To try the worker locally with wrangler:
```bash
npm run preview:wrangler
```

To deploy the worker to Cloudflare:
```bash
npm run deploy
```


## `dist/server/importBuild.js`
Expand Down

0 comments on commit b10c42a

Please sign in to comment.