Skip to content

Commit 499d9f5

Browse files
committed
add a Vite plugin
1 parent bf12933 commit 499d9f5

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

packages/vite-plugin/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
yarn.lock
2+
node_modules

packages/vite-plugin/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# @ruby2js/rollup-plugin
2+
3+
Integration between Vite and Ruby2JS
4+
5+
## Description
6+
7+
Since Vite supports both rollup and vite plugins, this plugin can be used
8+
interchangeably with the [Ruby2JS Rollup
9+
plugin](https://www.npmjs.com/package/@ruby2js/rollup-plugin) with if there
10+
the use of a refresh plugin is not needed.
11+
12+
What this plugin does is integrate Ruby2JS with the refresh process. In order
13+
to do this, the refresh plugin you would normally use needs to be passed as an
14+
option to the Ruby2JS plugin rather than included as a separate plugin. An
15+
example of this usage follows below.
16+
17+
## Installation
18+
19+
```bash
20+
npm install --save-dev @ruby2js/vite-plugin
21+
# or
22+
yarn add -D @ruby2js/vite-plugin
23+
```
24+
25+
## Usage
26+
27+
The following is a example of a `vite.config.js` file configured for use with
28+
the React refresh plugin. Note the addition of `.rb` and `.js.rb` extensions
29+
to `resolve.extensions` and the passing of the `reactRefresh` plugin as a
30+
`refresh` option to the `@ruby2js/vite-plugin`.
31+
32+
```javascript
33+
import { defineConfig } from 'vite'
34+
import reactRefresh from '@vitejs/plugin-react-refresh'
35+
import ruby2js from '@ruby2js/vite-plugin';
36+
37+
export default defineConfig({
38+
resolve: {
39+
extensions: ['.rb', '.js.rb'].concat(
40+
['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json']
41+
)
42+
},
43+
44+
plugins: [
45+
ruby2js({
46+
refresh: reactRefresh(),
47+
eslevel: 2021,
48+
autoexports: 'default',
49+
filters: ['react', 'esm', 'functions']
50+
})
51+
]
52+
})
53+
```

packages/vite-plugin/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Ruby2JS = require('@ruby2js/ruby2js');
2+
const { extname } = require('path');
3+
4+
module.exports = options => {
5+
let extensions = options.extensions || ['.rb'];
6+
7+
return {
8+
transform(code, id) {
9+
if (!extensions.includes(extname(id))) return;
10+
11+
js = Ruby2JS.convert(code, {...options, file: id});
12+
13+
return {
14+
code: js.toString(),
15+
map: js.sourcemap
16+
}
17+
}
18+
}
19+
}

packages/vite-plugin/package.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@ruby2js/vite-plugin",
3+
"version": "0.0.1",
4+
"description": "ruby2js plugin for vite",
5+
"author": "Sam Ruby <rubys@intertwingly.net>",
6+
"license": "MIT",
7+
"main": "index.js",
8+
"homepage": "https://github.com/ruby2js/ruby2js/tree/master/packages/rollup-plugin#readme",
9+
"keywords": [
10+
"ruby2js",
11+
"vite",
12+
"vite-plugin"
13+
],
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/ruby2js/ruby2js"
17+
},
18+
"publishConfig": {
19+
"access": "public"
20+
},
21+
"dependencies": {
22+
"@ruby2js/rollup-plugin": ">= 0.0.2",
23+
"btoa": "~1.2.1"
24+
}
25+
}

0 commit comments

Comments
 (0)