├── .github └── workflows │ └── semgrep.yml ├── .gitignore ├── README.md ├── package.json ├── scripts └── open-preview ├── src ├── all.js └── time.js └── webpack.config.js /.github/workflows/semgrep.yml: -------------------------------------------------------------------------------- 1 | 2 | on: 3 | pull_request: {} 4 | workflow_dispatch: {} 5 | push: 6 | branches: 7 | - main 8 | - master 9 | schedule: 10 | - cron: '0 0 * * *' 11 | name: Semgrep config 12 | jobs: 13 | semgrep: 14 | name: semgrep/ci 15 | runs-on: ubuntu-20.04 16 | env: 17 | SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} 18 | SEMGREP_URL: https://cloudflare.semgrep.dev 19 | SEMGREP_APP_URL: https://cloudflare.semgrep.dev 20 | SEMGREP_VERSION_CHECK_URL: https://cloudflare.semgrep.dev/api/check-version 21 | container: 22 | image: returntocorp/semgrep 23 | steps: 24 | - uses: actions/checkout@v3 25 | - run: semgrep ci 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist/ 3 | .DS_Store 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Workers Webpack Example 2 | ==== 3 | 4 | [Cloudflare Workers](http://developers.cloudflare.com/workers/) allow you to write JavaScript which runs on all of Cloudflare's 5 | 160+ global data centers. This repo is an example of how to combine multiple files and dependencies to create a Worker using 6 | the [Webpack](https://webpack.js.org/) build tool. 7 | 8 | The actual Worker replaces the content of your site with a Worker which returns the current time in the timezone of the caller's 9 | choice. 10 | 11 | ### Dependencies 12 | 13 | - [npm](https://www.npmjs.com/get-npm) 14 | - [jq](https://stedolan.github.io/jq/) (for the preview script) 15 | - [cURL](https://curl.haxx.se/) (for the preview script) 16 | 17 | ### Instructions 18 | 19 | - `npm install` 20 | - `npm run build` 21 | 22 | To open the Workers preview with the built Worker: 23 | 24 | - `npm run preview` 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "workers-webpack", 3 | "version": "1.0.0", 4 | "description": "", 5 | "scripts": { 6 | "build": "webpack -p --progress --colors", 7 | "preview": "./scripts/open-preview < dist/worker.js" 8 | }, 9 | "main": "dist/worker.js", 10 | "author": "", 11 | "license": "MIT", 12 | "dependencies": { 13 | "moment-timezone": "^0.5.17", 14 | "webpack": "^4.10.2", 15 | "webpack-cli": "^2.1.4" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /scripts/open-preview: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | url=https://google.com?tz=America/New_York 5 | url=${1:-$url} 6 | 7 | id=$(curl 'https://cloudflareworkers.com/script' -H 'pragma: no-cache' -H 'origin: https://cloudflareworkers.com' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' -H 'content-type: text/javascript; charset=UTF-8' -H 'accept: */*' -H 'cache-control: no-cache' -H 'authority: cloudflareworkers.com' -H 'referer: https://cloudflareworkers.com/' --compressed --data-binary @- <&0 | jq -r '.id') 8 | open "https://cloudflareworkers.com/#$id:$url" 9 | -------------------------------------------------------------------------------- /src/all.js: -------------------------------------------------------------------------------- 1 | import { handleRequest } from './time' 2 | 3 | addEventListener('fetch', event => { 4 | event.respondWith(handleRequest(event.request)) 5 | }) 6 | -------------------------------------------------------------------------------- /src/time.js: -------------------------------------------------------------------------------- 1 | import moment from 'moment' 2 | import 'moment-timezone' 3 | 4 | export async function handleRequest(request) { 5 | const time = moment(new Date) 6 | const url = new URL(request.url) 7 | 8 | let timezone = url.searchParams.get("tz") 9 | if (!timezone) { 10 | timezone = "America/Los_Angeles" 11 | } 12 | 13 | return new Response(time.tz(timezone).format()) 14 | } 15 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | entry: "./src/all.js", 3 | mode: "development", 4 | optimization: { 5 | minimize: false 6 | }, 7 | performance: { 8 | hints: false 9 | }, 10 | output: { 11 | path: __dirname + "/dist", 12 | publicPath: "dist", 13 | filename: "worker.js" 14 | } 15 | }; 16 | --------------------------------------------------------------------------------