vite-plugin-gas-hoist 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.ja.md +54 -0
- package/README.md +54 -0
- package/package.json +46 -0
- package/src/index.js +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Taiki Watanabe
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ja.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# vite-plugin-gas-hoist
|
|
2
|
+
|
|
3
|
+
[English](README.md)
|
|
4
|
+
|
|
5
|
+
エントリーポイントの `export` を Google Apps Script (GAS) のグローバルスコープに公開する Vite プラグイン。
|
|
6
|
+
|
|
7
|
+
## なぜ必要?
|
|
8
|
+
|
|
9
|
+
GAS はグローバルスコープの関数しか実行できないが、Vite でバンドルすると全てのコードが IIFE 内に閉じ込められる。このプラグインは、エントリーポイントの `export` を自動的にグローバル関数としてラップすることで、この問題を解決する。
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
// ビルド前(エントリーポイント)
|
|
13
|
+
export const sayHello = (name) => `Hello, ${name}!`;
|
|
14
|
+
|
|
15
|
+
// ビルド後(プラグインが自動追加)
|
|
16
|
+
function sayHello(...args) { return lib_.sayHello(...args) }
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## インストール
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -D vite-plugin-gas-hoist
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 使い方
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
// vite.config.js
|
|
29
|
+
import { defineConfig } from 'vite';
|
|
30
|
+
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [vitePluginGasHoist()],
|
|
34
|
+
build: {
|
|
35
|
+
lib: {
|
|
36
|
+
entry: 'src/main.js',
|
|
37
|
+
formats: ['iife'],
|
|
38
|
+
name: 'lib_',
|
|
39
|
+
fileName: (_, entryName) => `${entryName}.js`,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`build.lib.name` に指定した変数名(上記例では `lib_`)を介して、エクスポートされた関数がグローバルスコープに公開される。
|
|
46
|
+
|
|
47
|
+
## 動作条件
|
|
48
|
+
|
|
49
|
+
- `build.lib.formats` に `'iife'` を含むこと
|
|
50
|
+
- エントリーポイントで関数を `export` していること
|
|
51
|
+
|
|
52
|
+
## ライセンス
|
|
53
|
+
|
|
54
|
+
[MIT](LICENSE)
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# vite-plugin-gas-hoist
|
|
2
|
+
|
|
3
|
+
[日本語](README.ja.md)
|
|
4
|
+
|
|
5
|
+
A Vite plugin that hoists entry point `export`s to the global scope for Google Apps Script (GAS).
|
|
6
|
+
|
|
7
|
+
## Why?
|
|
8
|
+
|
|
9
|
+
GAS can only execute functions in the global scope, but Vite bundles all code inside an IIFE. This plugin automatically wraps entry point exports as global functions, bridging the gap.
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
// Before build (entry point)
|
|
13
|
+
export const sayHello = (name) => `Hello, ${name}!`;
|
|
14
|
+
|
|
15
|
+
// After build (plugin adds this)
|
|
16
|
+
function sayHello(...args) { return lib_.sayHello(...args) }
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install -D vite-plugin-gas-hoist
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
// vite.config.js
|
|
29
|
+
import { defineConfig } from 'vite';
|
|
30
|
+
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [vitePluginGasHoist()],
|
|
34
|
+
build: {
|
|
35
|
+
lib: {
|
|
36
|
+
entry: 'src/main.js',
|
|
37
|
+
formats: ['iife'],
|
|
38
|
+
name: 'lib_',
|
|
39
|
+
fileName: (_, entryName) => `${entryName}.js`,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Exported functions are hoisted to the global scope via the variable name specified in `build.lib.name` (e.g. `lib_`).
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
- `build.lib.formats` must include `'iife'`
|
|
50
|
+
- Entry point must have at least one `export`
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
[MIT](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-gas-hoist",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A Vite plugin that hoists entry point exports to the global scope for Google Apps Script",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"vite-plugin",
|
|
7
|
+
"vite",
|
|
8
|
+
"google-apps-script",
|
|
9
|
+
"gas",
|
|
10
|
+
"clasp"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Taiki Watanabe",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/hd140283/vite-plugin-gas-hoist.git"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./src/index.js",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.js"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"vite": "^8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@biomejs/biome": "^2.3.6",
|
|
31
|
+
"vite": "^8.0.3",
|
|
32
|
+
"vite-plugin-static-copy": "^3.1.4"
|
|
33
|
+
},
|
|
34
|
+
"volta": {
|
|
35
|
+
"node": "22.21.1",
|
|
36
|
+
"pnpm": "10.23.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"lint": "biome check --write",
|
|
40
|
+
"lint:ci": "biome check",
|
|
41
|
+
"example:dev": "cd example && vite build --mode development",
|
|
42
|
+
"example:build": "cd example && vite build",
|
|
43
|
+
"example:login": "pnpx @google/clasp@2.5 login",
|
|
44
|
+
"example:push": "cd example && pnpx @google/clasp@2.5 push"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vite plugin for Google Apps Script.
|
|
3
|
+
* Hoists entry point exports to the GAS global scope.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* // vite.config.js
|
|
7
|
+
* import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
8
|
+
*
|
|
9
|
+
* export default defineConfig({
|
|
10
|
+
* plugins: [vitePluginGasHoist()],
|
|
11
|
+
* build: {
|
|
12
|
+
* lib: {
|
|
13
|
+
* entry: 'src/main.js',
|
|
14
|
+
* formats: ['iife'],
|
|
15
|
+
* name: 'lib_',
|
|
16
|
+
* },
|
|
17
|
+
* },
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* @returns {import('vite').Plugin}
|
|
21
|
+
*/
|
|
22
|
+
export const vitePluginGasHoist = () => {
|
|
23
|
+
/** @type {string} IIFE variable name from build.lib.name */
|
|
24
|
+
let varName;
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
name: 'vite-plugin-gas-hoist',
|
|
28
|
+
|
|
29
|
+
/** @param {import('vite').ResolvedConfig} config */
|
|
30
|
+
configResolved(config) {
|
|
31
|
+
varName = config.build.lib.name;
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
renderChunk: {
|
|
35
|
+
order: 'post',
|
|
36
|
+
/**
|
|
37
|
+
* Appends global wrapper functions for each export.
|
|
38
|
+
*
|
|
39
|
+
* Given `export { sayHello }` with `lib.name = 'lib_'`, produces:
|
|
40
|
+
* ```js
|
|
41
|
+
* function sayHello(...args){return lib_.sayHello(...args)}
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @param {string} code
|
|
45
|
+
* @param {import('rollup').RenderedChunk} chunk
|
|
46
|
+
* @param {import('rollup').NormalizedOutputOptions} options
|
|
47
|
+
*/
|
|
48
|
+
handler(code, chunk, options) {
|
|
49
|
+
if (options.format !== 'iife' || !chunk.isEntry) return null;
|
|
50
|
+
|
|
51
|
+
const { exports } = chunk;
|
|
52
|
+
if (exports.length === 0) return null;
|
|
53
|
+
|
|
54
|
+
const wrappers = exports
|
|
55
|
+
.map((name) => `function ${name}(...args){return ${varName}.${name}(...args)}`)
|
|
56
|
+
.join('\n');
|
|
57
|
+
|
|
58
|
+
console.log(`vite-plugin-gas-hoist: hoisted ${exports.length} function(s)`);
|
|
59
|
+
return `${code}\n${wrappers}\n`;
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
};
|