Skip to content

Commit

Permalink
Vite Plugin - Watch Workspace (#1154)
Browse files Browse the repository at this point in the history
* move plugin and add docs

* Remove references and add dep

* Comments

* Fix package name

* package-lock + add missing index.ts

* add header

* Make it work for tsx. Ignore dist folders when using

* Fix dependencies

* add CJS build command and tsconfig

* Make JS default for switch statements

* Extend wait for provider image and try to debug on fail

* Enclose bash statement in brackets

* package-lock.json

* package-lock.json
  • Loading branch information
forgetso authored Apr 22, 2024
1 parent 1e3608a commit e4645ce
Show file tree
Hide file tree
Showing 13 changed files with 487 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/provider_image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:
- name: Check provider bundle runs
run: |
CONTAINER=$(docker run -d -v "./.env.production:/usr/src/app/.env.production" prosopo/provider:dev /bin/sh -c 'NODE_ENV=production npx provider version')
sleep 10s
sleep 20s
docker logs $CONTAINER >& provider.log
echo $(cat provider.log)
grep -oE "Version: \".*\"" provider.log || exit 1
grep -oE "Version: \".*\"" provider.log || (echo $(cat provider.log) && exit 1)
2 changes: 2 additions & 0 deletions demos/client-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@polkadot/extension-inject": "0.46.6",
"@prosopo/common": "0.3.38",
"@prosopo/procaptcha": "0.3.38",
"@prosopo/procaptcha-frictionless": "0.3.38",
"@prosopo/procaptcha-react": "0.3.38",
"@prosopo/types": "0.3.38",
"@types/react-dom": "^18.2.4",
Expand All @@ -27,6 +28,7 @@
"devDependencies": {
"@prosopo/cli": "0.3.38",
"@prosopo/config": "0.3.38",
"@prosopo/vite-plugin-watch-workspace": "^0.3.38",
"@types/node": "^20.3.1",
"css-loader": "^6.8.1",
"eslint-config-react-app": "^7.0.1",
Expand Down
3 changes: 3 additions & 0 deletions demos/client-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
},
{
"path": "../../dev/config"
},
{
"path": "../../dev/vite-plugin-watch-workspace"
}
]
}
11 changes: 9 additions & 2 deletions demos/client-example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.
import * as path from 'path'
import { VitePluginCloseAndCopy } from '@prosopo/config'
import { VitePluginWatchWorkspace } from '@prosopo/vite-plugin-watch-workspace'
import { defineConfig } from 'vite'
import { getLogger } from '@prosopo/common'
import { loadEnv } from '@prosopo/cli'
Expand All @@ -21,7 +22,7 @@ const logger = getLogger(`Info`, `vite.config.js`)
const dir = path.resolve('.')
loadEnv(dir)
// https://vitejs.dev/config/
export default defineConfig(function ({ command, mode }) {
export default defineConfig(async function ({ command, mode }) {
logger.info(`Running at ${dir} in ${mode} mode`)
// NODE_ENV must be wrapped in quotes. We just set it to the mode and ignore what's in the env file, otherwise the
// mode and NODE_ENV can end up out of sync (one set to development and the other set to production, which causes
Expand All @@ -46,7 +47,6 @@ export default defineConfig(function ({ command, mode }) {
'process.env._DEV_ONLY_WATCH_EVENTS': JSON.stringify(process.env._DEV_ONLY_WATCH_EVENTS),
}
logger.debug('define', JSON.stringify(define))

return {
watch: false,
mode: 'development',
Expand All @@ -69,6 +69,13 @@ export default defineConfig(function ({ command, mode }) {
// Closes the bundler and copies the bundle to the client-bundle-example project unless we're in serve
// mode, in which case we don't want to close the bundler because it will close the server
command !== 'serve' ? VitePluginCloseAndCopy() : undefined,
// Watches external files (workspace packages) and rebuilds them when they change
await VitePluginWatchWorkspace({
workspaceRoot: path.resolve('../..'),
currentPackage: `${path.resolve('.')}/**/*`,
format: 'esm',
ignorePaths: [`${path.resolve('../..')}/demos/*`, `${path.resolve('../..')}/dev/*`, `**/dist/**/*`],
}),
],
server: { port: process.env.PROSOPO_PORT ? Number(process.env.PROSOPO_PORT) : 9230 },
}
Expand Down
52 changes: 52 additions & 0 deletions dev/vite-plugin-watch-workspace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Vite Plugin Watch Workspace

Vite Plugin Watch Workspace is a Vite plugin that watches for changes in local dependencies in a monorepo and rebuilds
them when they change.

## Installation

```bash
npm install @prosopo/vite-plugin-watch-workspace
```

## Usage

Add the plugin to your Vite config:

```typescript
import { defineConfig } from 'vite'
import { VitePluginWatchWorkspace } from '@prosopo/vite-plugin-watch-workspace'

export default defineConfig(async function ({ command, mode }) {
return {
plugins: [
VitePluginWatchWorkspace({
workspaceRoot: '/path/to/your/workspace',
currentPackage: '/path/to/your/current/package',
format: 'esm', // or 'cjs'
fileTypes: ['ts', 'tsx', 'js', 'jsx'], // optional - file types to watch. default is ['ts', 'tsx']
ignorePaths: ['node_modules', 'dist'], // optional - globs to ignore
}),
],
}
})
```

#### Options

- `workspaceRoot` - The root of your monorepo.
- `currentPackage` - The path to the package that is currently being built. Can be a path or a glob.
- `format` - The module format to use. Can be `'esm'` or `'cjs'`.
- `fileTypes` - An array of file types to watch. Optional. Default is `['ts', 'tsx']`.
- `ignorePaths` - An array of globs to ignore. Optional but it is recommended to add a wildcard to ignore your dist
folders, e.g. `**/dist/**`.

## How it works

The plugin takes your `workspaceRoot` and searches for packages by reading the `workspaces` field in your workspace
root `package.json`. It then searches for files in the packages that match the `fileTypes` you provided and adds the
files to Vite's watch list. When one of the discovered files changes, the plugin loads the tsconfig associated with it
and builds the file using esbuild.

See [this article](https://prosopo.io/articles/using-vite-to-rebuild-local-dependencies-in-an-npm-workspace) for more
details.
52 changes: 52 additions & 0 deletions dev/vite-plugin-watch-workspace/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@prosopo/vite-plugin-watch-workspace",
"version": "0.3.38",
"description": "Vite plugin for watching and rebuilding external files",
"main": "./dist/index.js",
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/cjs/index.cjs"
}
},
"scripts": {
"test": "echo \"No test specified\"",
"clean": "tsc --build --clean",
"build": "tsc --build --verbose",
"build:cjs": "npx vite --config vite.cjs.config.ts build",
"eslint": "npx eslint . --no-error-on-unmatched-pattern --ignore-path ../../.eslintignore",
"eslint:fix": "npm run eslint -- --fix",
"prettier": "npx prettier . --check --no-error-on-unmatched-pattern --ignore-path ../../.eslintignore",
"prettier:fix": "npm run prettier -- --write",
"lint": "npm run eslint && npm run prettier",
"lint:fix": "npm run eslint:fix && npm run prettier:fix"
},
"engines": {
"node": ">=18",
"npm": ">=9"
},
"author": "Prosopo Limited",
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.3.4",
"vite": "^5.1.7"
},
"devDependencies": {
"@types/debug": "^4.1.12",
"tslib": "2.6.2",
"typescript": "5.1.6"
},
"repository": {
"type": "git",
"url": "git+https://github.com/prosopo/captcha.git"
},
"bugs": {
"url": "https://github.com/prosopo/captcha/issues"
},
"homepage": "https://github.com/prosopo/captcha/blob/main/dev/vite-plugin-watch-workspace/README.md",
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"sideEffects": false
}
14 changes: 14 additions & 0 deletions dev/vite-plugin-watch-workspace/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export * from './vite-plugin-watch-workspace.js'
Loading

0 comments on commit e4645ce

Please sign in to comment.