vite-plugin-gas-hoist 0.2.0 → 0.3.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/README.ja.md +44 -20
- package/README.md +44 -20
- package/index.js +60 -0
- package/lib/classify-variable.js +15 -0
- package/lib/collect-module-exports.js +75 -0
- package/lib/emit-wrapper.js +23 -0
- package/lib/hoist-exports.js +54 -0
- package/lib/resolve-export-kind.js +35 -0
- package/package.json +45 -15
- package/src/index.js +0 -189
package/README.ja.md
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/vite-plugin-gas-hoist)
|
|
4
4
|
[](https://registry.vite.dev)
|
|
5
|
+
[](https://registry.vite.dev)
|
|
6
|
+
[](https://registry.vite.dev)
|
|
5
7
|
|
|
6
8
|
[English](README.md)
|
|
7
9
|
|
|
8
|
-
エントリーポイントの `export` を、ESM の宣言種別(`function` / `const` / `let` / `var`)を保ったまま、Google Apps Script (GAS) のグローバルスコープに公開する Vite プラグインです。
|
|
10
|
+
エントリーポイントの `export` を、ESM の宣言種別(`function` / `const` / `let` / `var`)を保ったまま、Google Apps Script (GAS) のグローバルスコープに公開する Vite プラグインです。Rollup と Rolldown でも使えます。
|
|
9
11
|
|
|
10
12
|
## デモ
|
|
11
13
|
|
|
@@ -24,7 +26,9 @@ GAS 向けに Vite でバンドルするときは、複数モジュールを 1
|
|
|
24
26
|
export const sayHello = (name) => `Hello, ${name}!`;
|
|
25
27
|
|
|
26
28
|
// ビルド後
|
|
27
|
-
var lib_ = (function()
|
|
29
|
+
var lib_ = (function () {
|
|
30
|
+
/* バンドルされた sayHello を含む IIFE */
|
|
31
|
+
})({});
|
|
28
32
|
const sayHello = (...args) => lib_.sayHello(...args); // プラグインが追加
|
|
29
33
|
```
|
|
30
34
|
|
|
@@ -42,20 +46,39 @@ import { defineConfig } from 'vite';
|
|
|
42
46
|
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
43
47
|
|
|
44
48
|
export default defineConfig({
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
plugins: [vitePluginGasHoist()],
|
|
50
|
+
build: {
|
|
51
|
+
lib: {
|
|
52
|
+
entry: 'src/main.js',
|
|
53
|
+
formats: ['iife'],
|
|
54
|
+
name: 'lib_',
|
|
55
|
+
fileName: (_, entryName) => `${entryName}.js`,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
54
58
|
});
|
|
55
59
|
```
|
|
56
60
|
|
|
57
61
|
`build.lib.name` に指定した変数名(上記例では `lib_`)を介して、エクスポートが GAS のグローバルスコープに公開されます。
|
|
58
62
|
|
|
63
|
+
### Rollup / Rolldown
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
// rollup.config.js または rolldown.config.js
|
|
67
|
+
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
68
|
+
|
|
69
|
+
export default {
|
|
70
|
+
input: 'src/main.js',
|
|
71
|
+
plugins: [vitePluginGasHoist()],
|
|
72
|
+
output: {
|
|
73
|
+
file: 'dist/main.js',
|
|
74
|
+
format: 'iife',
|
|
75
|
+
name: 'lib_',
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
変数名は `output.name` の指定が使われます。
|
|
81
|
+
|
|
59
82
|
## 対応パターン
|
|
60
83
|
|
|
61
84
|
GAS でグローバルに公開するときの目的は、大きく 2 つに分かれます。
|
|
@@ -65,14 +88,14 @@ GAS でグローバルに公開するときの目的は、大きく 2 つに分
|
|
|
65
88
|
|
|
66
89
|
ESM の宣言種別(`function` / `const` / `let` / `var`)と右辺の形をそのまま保つことで、この 2 つの公開を組み合わせて表現できます。GAS のライブラリ可視性ルール(`function` / `var` は外部公開、`const` / `let` は非公開)がそのまま反映される挙動です。
|
|
67
90
|
|
|
68
|
-
| ESM 宣言
|
|
69
|
-
|
|
70
|
-
| `export function foo() {}`
|
|
71
|
-
| `export const FOO = 42`
|
|
72
|
-
| `export const foo = (x) => x`
|
|
73
|
-
| `export const foo = function(x){...}` | `const foo = function(...args){return lib_.foo(...args)}` | 非公開(同上)
|
|
74
|
-
| `export let foo = ...`
|
|
75
|
-
| `export var foo = ...`
|
|
91
|
+
| ESM 宣言 | 出力 | GAS ライブラリ可視性 |
|
|
92
|
+
| ------------------------------------- | --------------------------------------------------------- | ------------------------------------------------ |
|
|
93
|
+
| `export function foo() {}` | `function foo(...args){return lib_.foo(...args)}` | 公開 |
|
|
94
|
+
| `export const FOO = 42` | `const FOO = lib_.FOO` | 非公開 |
|
|
95
|
+
| `export const foo = (x) => x` | `const foo = (...args) => lib_.foo(...args)` | 非公開(エディタの "実行" ドロップダウンに表示) |
|
|
96
|
+
| `export const foo = function(x){...}` | `const foo = function(...args){return lib_.foo(...args)}` | 非公開(同上) |
|
|
97
|
+
| `export let foo = ...` | `let foo = ...`(右辺は const と同形) | 非公開 |
|
|
98
|
+
| `export var foo = ...` | `var foo = ...`(右辺は const と同形) | 公開 |
|
|
76
99
|
|
|
77
100
|
`export { foo }` や `export { foo as bar }` のような specifier 形式の再エクスポートも、ローカル宣言の種別を引き継ぎます。表に当てはまらないパターン(`export default` や `export class`、宣言種別を判定できない再エクスポートなど)はビルド時に警告を出してスキップします。
|
|
78
101
|
|
|
@@ -80,7 +103,8 @@ ESM の宣言種別(`function` / `const` / `let` / `var`)と右辺の形を
|
|
|
80
103
|
|
|
81
104
|
## 動作条件
|
|
82
105
|
|
|
83
|
-
-
|
|
106
|
+
- Vite 6・7・8、Rollup 4、Rolldown 1 のいずれか
|
|
107
|
+
- `build.lib.formats` に `'iife'` を含むこと(Rollup / Rolldown では `output.format` が `'iife'` であること)
|
|
84
108
|
- エントリーポイントで何かを `export` していること
|
|
85
109
|
|
|
86
110
|
## 0.1.x からの移行
|
package/README.md
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/vite-plugin-gas-hoist)
|
|
4
4
|
[](https://registry.vite.dev)
|
|
5
|
+
[](https://registry.vite.dev)
|
|
6
|
+
[](https://registry.vite.dev)
|
|
5
7
|
|
|
6
8
|
[日本語](README.ja.md)
|
|
7
9
|
|
|
8
|
-
A Vite plugin that hoists entry point `export`s to the global scope for Google Apps Script (GAS), preserving the original ESM declaration kind (`function` / `const` / `let` / `var`).
|
|
10
|
+
A Vite plugin that hoists entry point `export`s to the global scope for Google Apps Script (GAS), preserving the original ESM declaration kind (`function` / `const` / `let` / `var`). It also works with Rollup and Rolldown.
|
|
9
11
|
|
|
10
12
|
## Try it
|
|
11
13
|
|
|
@@ -24,7 +26,9 @@ The catch: that also hides everything you actually want to expose — both funct
|
|
|
24
26
|
export const sayHello = (name) => `Hello, ${name}!`;
|
|
25
27
|
|
|
26
28
|
// After build
|
|
27
|
-
var lib_ = (function()
|
|
29
|
+
var lib_ = (function () {
|
|
30
|
+
/* IIFE bundle containing sayHello */
|
|
31
|
+
})({});
|
|
28
32
|
const sayHello = (...args) => lib_.sayHello(...args); // added by the plugin
|
|
29
33
|
```
|
|
30
34
|
|
|
@@ -42,20 +46,39 @@ import { defineConfig } from 'vite';
|
|
|
42
46
|
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
43
47
|
|
|
44
48
|
export default defineConfig({
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
plugins: [vitePluginGasHoist()],
|
|
50
|
+
build: {
|
|
51
|
+
lib: {
|
|
52
|
+
entry: 'src/main.js',
|
|
53
|
+
formats: ['iife'],
|
|
54
|
+
name: 'lib_',
|
|
55
|
+
fileName: (_, entryName) => `${entryName}.js`,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
54
58
|
});
|
|
55
59
|
```
|
|
56
60
|
|
|
57
61
|
Exports are hoisted to the global scope via the variable name specified in `build.lib.name` (e.g. `lib_`).
|
|
58
62
|
|
|
63
|
+
### Rollup / Rolldown
|
|
64
|
+
|
|
65
|
+
```js
|
|
66
|
+
// rollup.config.js or rolldown.config.js
|
|
67
|
+
import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
68
|
+
|
|
69
|
+
export default {
|
|
70
|
+
input: 'src/main.js',
|
|
71
|
+
plugins: [vitePluginGasHoist()],
|
|
72
|
+
output: {
|
|
73
|
+
file: 'dist/main.js',
|
|
74
|
+
format: 'iife',
|
|
75
|
+
name: 'lib_',
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The variable name comes from `output.name`.
|
|
81
|
+
|
|
59
82
|
## What gets hoisted
|
|
60
83
|
|
|
61
84
|
Exposing something globally in GAS serves one of two purposes:
|
|
@@ -65,14 +88,14 @@ Exposing something globally in GAS serves one of two purposes:
|
|
|
65
88
|
|
|
66
89
|
By preserving the original ESM declaration kind (`function` / `const` / `let` / `var`) and the right-hand-side shape, the plugin lets you express both axes naturally. GAS's library visibility rules apply directly: `function` and `var` are externally visible, `const` and `let` are not.
|
|
67
90
|
|
|
68
|
-
| ESM declaration
|
|
69
|
-
|
|
70
|
-
| `export function foo() {}`
|
|
71
|
-
| `export const FOO = 42`
|
|
72
|
-
| `export const foo = (x) => x`
|
|
73
|
-
| `export const foo = function(x){...}` | `const foo = function(...args){return lib_.foo(...args)}` | internal only (same)
|
|
74
|
-
| `export let foo = ...`
|
|
75
|
-
| `export var foo = ...`
|
|
91
|
+
| ESM declaration | Output | GAS library visibility |
|
|
92
|
+
| ------------------------------------- | --------------------------------------------------------- | ------------------------------------------------------ |
|
|
93
|
+
| `export function foo() {}` | `function foo(...args){return lib_.foo(...args)}` | external |
|
|
94
|
+
| `export const FOO = 42` | `const FOO = lib_.FOO` | internal only |
|
|
95
|
+
| `export const foo = (x) => x` | `const foo = (...args) => lib_.foo(...args)` | internal only (appears in the editor's "Run" dropdown) |
|
|
96
|
+
| `export const foo = function(x){...}` | `const foo = function(...args){return lib_.foo(...args)}` | internal only (same) |
|
|
97
|
+
| `export let foo = ...` | `let foo = ...` (right-hand side as above) | internal only |
|
|
98
|
+
| `export var foo = ...` | `var foo = ...` (right-hand side as above) | external |
|
|
76
99
|
|
|
77
100
|
Specifier-form re-exports like `export { foo }` and `export { foo as bar }` inherit the kind of the local declaration. Patterns not covered above (`export default`, `export class`, re-exports whose kind cannot be determined, etc.) are skipped with a build-time warning.
|
|
78
101
|
|
|
@@ -80,7 +103,8 @@ Specifier-form re-exports like `export { foo }` and `export { foo as bar }` inhe
|
|
|
80
103
|
|
|
81
104
|
## Requirements
|
|
82
105
|
|
|
83
|
-
-
|
|
106
|
+
- Vite 6, 7, or 8, Rollup 4, or Rolldown 1
|
|
107
|
+
- `build.lib.formats` must include `'iife'` (`output.format` must be `'iife'` for Rollup / Rolldown)
|
|
84
108
|
- Entry point must have at least one `export`
|
|
85
109
|
|
|
86
110
|
## Migration from 0.1.x
|
package/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { collectModuleExports } from './lib/collect-module-exports.js';
|
|
2
|
+
import { hoistExports } from './lib/hoist-exports.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Vite plugin for Google Apps Script.
|
|
6
|
+
* Hoists entry point exports to the GAS global scope.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // vite.config.js
|
|
10
|
+
* import { vitePluginGasHoist } from 'vite-plugin-gas-hoist';
|
|
11
|
+
*
|
|
12
|
+
* export default defineConfig({
|
|
13
|
+
* plugins: [vitePluginGasHoist()],
|
|
14
|
+
* build: {
|
|
15
|
+
* lib: {
|
|
16
|
+
* entry: 'src/main.js',
|
|
17
|
+
* formats: ['iife'],
|
|
18
|
+
* name: 'lib_',
|
|
19
|
+
* },
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* @returns {import('vite').Plugin}
|
|
24
|
+
*/
|
|
25
|
+
export const vitePluginGasHoist = () => {
|
|
26
|
+
/** @type {Map<string, ModuleRecord>} */
|
|
27
|
+
const modules = new Map();
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
name: 'vite-plugin-gas-hoist',
|
|
31
|
+
apply: 'build',
|
|
32
|
+
|
|
33
|
+
buildStart() {
|
|
34
|
+
modules.clear();
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
/** @param {import('rollup').ModuleInfo} moduleInfo */
|
|
38
|
+
async moduleParsed({ id, code }) {
|
|
39
|
+
const moduleExports = collectModuleExports(this.parse(code));
|
|
40
|
+
const resolutions = await Promise.all(moduleExports.sources.map((source) => this.resolve(source, id)));
|
|
41
|
+
modules.set(id, { ...moduleExports, resolutions });
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
renderChunk: {
|
|
45
|
+
order: 'post',
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} code
|
|
48
|
+
* @param {import('rollup').RenderedChunk} chunk
|
|
49
|
+
* @param {import('rollup').NormalizedOutputOptions} options
|
|
50
|
+
*/
|
|
51
|
+
handler(code, chunk, options) {
|
|
52
|
+
const { output, logs } = hoistExports(code, chunk, options, modules);
|
|
53
|
+
for (const { level, message } of logs) {
|
|
54
|
+
this[level](message);
|
|
55
|
+
}
|
|
56
|
+
return output;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @typedef {'function' | 'const' | 'let' | 'var'} ExportKind */
|
|
2
|
+
/** @typedef {{ kind: ExportKind, isFn: boolean, isArrow: boolean }} ExportInfo */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Classifies a `VariableDeclaration` declarator as a value or function expression.
|
|
6
|
+
*
|
|
7
|
+
* @param {ExportKind} kind
|
|
8
|
+
* @param {import('estree').Expression | null | undefined} init
|
|
9
|
+
* @returns {ExportInfo}
|
|
10
|
+
*/
|
|
11
|
+
export const classifyVariable = (kind, init) => {
|
|
12
|
+
const isArrow = init?.type === 'ArrowFunctionExpression';
|
|
13
|
+
const isFn = isArrow || init?.type === 'FunctionExpression';
|
|
14
|
+
return { kind, isFn, isArrow };
|
|
15
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { classifyVariable } from './classify-variable.js';
|
|
2
|
+
|
|
3
|
+
/** @typedef {{ source: string, name: string }} ExportLink */
|
|
4
|
+
/** @typedef {{ bindings: Map<string, ExportInfo | ExportLink>, stars: string[], sources: string[] }} ModuleExports */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Collects the top-level named exports of a module. Each export is bound to
|
|
8
|
+
* its declaration kind, or to a link naming the module it is re-exported from.
|
|
9
|
+
*
|
|
10
|
+
* @param {import('estree').Program} ast
|
|
11
|
+
* @returns {ModuleExports}
|
|
12
|
+
*/
|
|
13
|
+
export const collectModuleExports = (ast) => {
|
|
14
|
+
/** @type {Map<string, ExportInfo | ExportLink>} */
|
|
15
|
+
const locals = new Map();
|
|
16
|
+
for (const node of ast.body) {
|
|
17
|
+
if (node.type === 'FunctionDeclaration' && node.id) {
|
|
18
|
+
locals.set(node.id.name, { kind: 'function', isFn: true, isArrow: false });
|
|
19
|
+
} else if (node.type === 'VariableDeclaration') {
|
|
20
|
+
for (const decl of node.declarations) {
|
|
21
|
+
if (decl.id.type === 'Identifier') {
|
|
22
|
+
locals.set(decl.id.name, classifyVariable(node.kind, decl.init));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
} else if (node.type === 'ImportDeclaration') {
|
|
26
|
+
for (const spec of node.specifiers) {
|
|
27
|
+
if (spec.type === 'ImportSpecifier') {
|
|
28
|
+
locals.set(spec.local.name, { source: node.source.value, name: spec.imported.name });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @type {Map<string, ExportInfo | ExportLink>} */
|
|
35
|
+
const bindings = new Map();
|
|
36
|
+
/** @type {string[]} */
|
|
37
|
+
const stars = [];
|
|
38
|
+
for (const node of ast.body) {
|
|
39
|
+
if (node.type === 'ExportAllDeclaration') {
|
|
40
|
+
if (!node.exported) {
|
|
41
|
+
stars.push(node.source.value);
|
|
42
|
+
}
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (node.type !== 'ExportNamedDeclaration') continue;
|
|
46
|
+
|
|
47
|
+
if (node.declaration?.type === 'VariableDeclaration') {
|
|
48
|
+
const kind = node.declaration.kind;
|
|
49
|
+
for (const decl of node.declaration.declarations) {
|
|
50
|
+
if (decl.id.type === 'Identifier') {
|
|
51
|
+
bindings.set(decl.id.name, classifyVariable(kind, decl.init));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
} else if (node.declaration?.type === 'FunctionDeclaration') {
|
|
55
|
+
if (node.declaration.id?.type === 'Identifier') {
|
|
56
|
+
bindings.set(node.declaration.id.name, { kind: 'function', isFn: true, isArrow: false });
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
for (const spec of node.specifiers) {
|
|
60
|
+
if (spec.type !== 'ExportSpecifier') continue;
|
|
61
|
+
const binding = node.source
|
|
62
|
+
? { source: node.source.value, name: spec.local.name }
|
|
63
|
+
: locals.get(spec.local.name);
|
|
64
|
+
if (binding) {
|
|
65
|
+
bindings.set(spec.exported.name, binding);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const linked = [...bindings.values()].flatMap((binding) => ('source' in binding ? [binding.source] : []));
|
|
72
|
+
const sources = [...new Set([...linked, ...stars])];
|
|
73
|
+
|
|
74
|
+
return { bindings, stars, sources };
|
|
75
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders the hoisted binding for a single export, preserving the original
|
|
3
|
+
* declaration kind and (for `const`/`let` function expressions) the function
|
|
4
|
+
* shape (arrow vs `function` expression) so callers see a callable wrapper
|
|
5
|
+
* that GAS recognizes as a function.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} name
|
|
8
|
+
* @param {ExportInfo} info
|
|
9
|
+
* @param {string} varName
|
|
10
|
+
* @returns {string}
|
|
11
|
+
*/
|
|
12
|
+
export const emitWrapper = (name, info, varName) => {
|
|
13
|
+
if (info.kind === 'function') {
|
|
14
|
+
return `function ${name}(...args){return ${varName}.${name}(...args)}`;
|
|
15
|
+
}
|
|
16
|
+
if (info.isFn) {
|
|
17
|
+
if (info.isArrow) {
|
|
18
|
+
return `${info.kind} ${name} = (...args) => ${varName}.${name}(...args)`;
|
|
19
|
+
}
|
|
20
|
+
return `${info.kind} ${name} = function(...args){return ${varName}.${name}(...args)}`;
|
|
21
|
+
}
|
|
22
|
+
return `${info.kind} ${name} = ${varName}.${name}`;
|
|
23
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { emitWrapper } from './emit-wrapper.js';
|
|
2
|
+
import { resolveExportKind } from './resolve-export-kind.js';
|
|
3
|
+
|
|
4
|
+
/** @typedef {{ level: 'info' | 'warn', message: string }} HoistLog */
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Appends kind-appropriate global hoist bindings for each entry export,
|
|
8
|
+
* skipping unsupported patterns (e.g. `export default`, `export class`)
|
|
9
|
+
* whose declaration kind cannot be resolved.
|
|
10
|
+
*
|
|
11
|
+
* @param {string} code
|
|
12
|
+
* @param {import('rollup').RenderedChunk} chunk
|
|
13
|
+
* @param {import('rollup').NormalizedOutputOptions} options
|
|
14
|
+
* @param {Map<string, ModuleRecord>} modules
|
|
15
|
+
* @returns {{ output: string | null, logs: HoistLog[] }}
|
|
16
|
+
*/
|
|
17
|
+
export const hoistExports = (code, chunk, options, modules) => {
|
|
18
|
+
if (options.format !== 'iife' || !chunk.isEntry) return { output: null, logs: [] };
|
|
19
|
+
|
|
20
|
+
/** @type {Array<{ name: string, info: ExportInfo }>} */
|
|
21
|
+
const known = [];
|
|
22
|
+
/** @type {string[]} */
|
|
23
|
+
const skipped = [];
|
|
24
|
+
|
|
25
|
+
for (const name of chunk.exports) {
|
|
26
|
+
const info = resolveExportKind(modules, chunk.facadeModuleId, name);
|
|
27
|
+
if (info) {
|
|
28
|
+
known.push({ name, info });
|
|
29
|
+
} else {
|
|
30
|
+
skipped.push(name);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @type {HoistLog[]} */
|
|
35
|
+
const logs = [];
|
|
36
|
+
|
|
37
|
+
if (known.length > 0) {
|
|
38
|
+
const label = known.length === 1 ? 'export' : 'exports';
|
|
39
|
+
const list = known.map(({ name, info }) => ` - ${name} (${info.kind})`).join('\n');
|
|
40
|
+
logs.push({ level: 'info', message: `Hoisted ${known.length} ${label} to global scope:\n${list}` });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (skipped.length > 0) {
|
|
44
|
+
const label = skipped.length === 1 ? 'export' : 'exports';
|
|
45
|
+
const list = skipped.map((name) => ` - ${name}`).join('\n');
|
|
46
|
+
logs.push({ level: 'warn', message: `Skipped ${skipped.length} ${label} (unsupported pattern):\n${list}` });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (known.length === 0) return { output: null, logs };
|
|
50
|
+
|
|
51
|
+
const wrappers = known.map(({ name, info }) => emitWrapper(name, info, options.name)).join('\n');
|
|
52
|
+
|
|
53
|
+
return { output: `${code}\n${wrappers}\n`, logs };
|
|
54
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** @typedef {ModuleExports & { resolutions: Array<{ id: string } | null> }} ModuleRecord */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves the declaration kind of a module's export, following re-exports
|
|
5
|
+
* to the module that declares it.
|
|
6
|
+
*
|
|
7
|
+
* @param {Map<string, ModuleRecord>} modules
|
|
8
|
+
* @param {string | null | undefined} id
|
|
9
|
+
* @param {string} name
|
|
10
|
+
* @param {Set<string>} [seen]
|
|
11
|
+
* @returns {ExportInfo | undefined}
|
|
12
|
+
*/
|
|
13
|
+
export const resolveExportKind = (modules, id, name, seen = new Set()) => {
|
|
14
|
+
const record = modules.get(id);
|
|
15
|
+
const key = `${id}\0${name}`;
|
|
16
|
+
if (!record || seen.has(key)) return undefined;
|
|
17
|
+
seen.add(key);
|
|
18
|
+
|
|
19
|
+
/** @param {string} source */
|
|
20
|
+
const idOf = (source) => record.resolutions[record.sources.indexOf(source)]?.id;
|
|
21
|
+
|
|
22
|
+
const binding = record.bindings.get(name);
|
|
23
|
+
if (binding) {
|
|
24
|
+
return 'source' in binding ? resolveExportKind(modules, idOf(binding.source), binding.name, seen) : binding;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// `export *` never re-exports the default export.
|
|
28
|
+
if (name === 'default') return undefined;
|
|
29
|
+
|
|
30
|
+
for (const source of record.stars) {
|
|
31
|
+
const info = resolveExportKind(modules, idOf(source), name, seen);
|
|
32
|
+
if (info) return info;
|
|
33
|
+
}
|
|
34
|
+
return undefined;
|
|
35
|
+
};
|
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-gas-hoist",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Vite plugin that hoists entry point exports to the global scope for Google Apps Script",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vite-plugin",
|
|
7
|
+
"rollup-plugin",
|
|
8
|
+
"rolldown-plugin",
|
|
7
9
|
"vite",
|
|
8
10
|
"google-apps-script",
|
|
9
11
|
"gas",
|
|
@@ -16,41 +18,69 @@
|
|
|
16
18
|
"url": "git+https://github.com/hd140283/vite-plugin-gas-hoist.git"
|
|
17
19
|
},
|
|
18
20
|
"type": "module",
|
|
19
|
-
"main": "./
|
|
21
|
+
"main": "./index.js",
|
|
20
22
|
"exports": {
|
|
21
|
-
".": "./
|
|
23
|
+
".": "./index.js"
|
|
22
24
|
},
|
|
23
25
|
"files": [
|
|
24
|
-
"
|
|
26
|
+
"index.js",
|
|
27
|
+
"lib"
|
|
25
28
|
],
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22"
|
|
31
|
+
},
|
|
32
|
+
"devEngines": {
|
|
33
|
+
"runtime": {
|
|
34
|
+
"name": "node",
|
|
35
|
+
"version": "^22 || ^24 || >=26",
|
|
36
|
+
"onFail": "error"
|
|
37
|
+
},
|
|
38
|
+
"packageManager": {
|
|
39
|
+
"name": "pnpm",
|
|
40
|
+
"version": "12",
|
|
41
|
+
"onFail": "error"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
26
44
|
"compatiblePackages": {
|
|
27
45
|
"schemaVersion": 1,
|
|
46
|
+
"vite": {
|
|
47
|
+
"type": "compatible",
|
|
48
|
+
"versions": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
49
|
+
},
|
|
28
50
|
"rollup": {
|
|
29
|
-
"type": "
|
|
30
|
-
"
|
|
51
|
+
"type": "compatible",
|
|
52
|
+
"versions": "^4.0.0"
|
|
31
53
|
},
|
|
32
54
|
"rolldown": {
|
|
33
|
-
"type": "
|
|
34
|
-
"
|
|
55
|
+
"type": "compatible",
|
|
56
|
+
"versions": "^1.0.0"
|
|
35
57
|
}
|
|
36
58
|
},
|
|
37
59
|
"peerDependencies": {
|
|
60
|
+
"rolldown": "^1.0.0",
|
|
61
|
+
"rollup": "^4.0.0",
|
|
38
62
|
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
39
63
|
},
|
|
64
|
+
"peerDependenciesMeta": {
|
|
65
|
+
"rolldown": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"rollup": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"vite": {
|
|
72
|
+
"optional": true
|
|
73
|
+
}
|
|
74
|
+
},
|
|
40
75
|
"devDependencies": {
|
|
41
|
-
"@biomejs/biome": "^2.
|
|
76
|
+
"@biomejs/biome": "^2.6.8",
|
|
42
77
|
"acorn": "^8.16.0",
|
|
43
78
|
"vite": "^8.0.3",
|
|
44
|
-
"vitest": "^
|
|
45
|
-
},
|
|
46
|
-
"volta": {
|
|
47
|
-
"node": "22.21.1",
|
|
48
|
-
"pnpm": "10.23.0"
|
|
79
|
+
"vitest": "^5.1.1"
|
|
49
80
|
},
|
|
50
81
|
"scripts": {
|
|
51
82
|
"test": "vitest run",
|
|
52
83
|
"lint": "biome check --write",
|
|
53
|
-
"lint:ci": "biome check",
|
|
54
84
|
"example:dev": "cd example && npm run dev",
|
|
55
85
|
"example:build": "cd example && npm run build",
|
|
56
86
|
"example:login": "pnpx @google/clasp@2.5 login",
|
package/src/index.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
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
|
-
/** @typedef {'function' | 'const' | 'let' | 'var'} ExportKind */
|
|
23
|
-
/** @typedef {{ kind: ExportKind, isFn: boolean, isArrow: boolean }} ExportInfo */
|
|
24
|
-
export const vitePluginGasHoist = () => {
|
|
25
|
-
/** @type {string} IIFE variable name from build.lib.name */
|
|
26
|
-
let varName;
|
|
27
|
-
|
|
28
|
-
/** @type {Map<string, ExportInfo>} */
|
|
29
|
-
const exportKinds = new Map();
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
name: 'vite-plugin-gas-hoist',
|
|
33
|
-
apply: 'build',
|
|
34
|
-
|
|
35
|
-
/** @param {import('vite').ResolvedConfig} config */
|
|
36
|
-
configResolved(config) {
|
|
37
|
-
varName = config.build.lib?.name;
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
buildStart() {
|
|
41
|
-
exportKinds.clear();
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Records the declaration kind of each top-level named export so that
|
|
46
|
-
* renderChunk can emit a binding that preserves the original visibility.
|
|
47
|
-
*
|
|
48
|
-
* @param {string} code
|
|
49
|
-
* @param {string} _id
|
|
50
|
-
*/
|
|
51
|
-
transform(code, _id) {
|
|
52
|
-
// Cheap pre-filter: skip modules that obviously have no exports (node_modules, virtual modules, CSS-as-JS, etc.) before paying acorn's cost.
|
|
53
|
-
if (!code.includes('export')) return null;
|
|
54
|
-
|
|
55
|
-
let ast;
|
|
56
|
-
try {
|
|
57
|
-
ast = this.parse(code);
|
|
58
|
-
} catch {
|
|
59
|
-
// Parse errors are surfaced by Vite/Rollup elsewhere; staying silent here avoids double-reporting.
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** @type {Map<string, ExportInfo>} */
|
|
64
|
-
const localKinds = new Map();
|
|
65
|
-
for (const node of ast.body) {
|
|
66
|
-
if (node.type === 'FunctionDeclaration' && node.id) {
|
|
67
|
-
localKinds.set(node.id.name, { kind: 'function', isFn: true, isArrow: false });
|
|
68
|
-
} else if (node.type === 'VariableDeclaration') {
|
|
69
|
-
for (const decl of node.declarations) {
|
|
70
|
-
if (decl.id.type === 'Identifier') {
|
|
71
|
-
localKinds.set(decl.id.name, classifyVariable(node.kind, decl.init));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
for (const node of ast.body) {
|
|
78
|
-
if (node.type !== 'ExportNamedDeclaration') continue;
|
|
79
|
-
|
|
80
|
-
if (node.declaration?.type === 'VariableDeclaration') {
|
|
81
|
-
const kind = node.declaration.kind;
|
|
82
|
-
for (const decl of node.declaration.declarations) {
|
|
83
|
-
if (decl.id.type === 'Identifier') {
|
|
84
|
-
exportKinds.set(decl.id.name, classifyVariable(kind, decl.init));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
} else if (node.declaration?.type === 'FunctionDeclaration') {
|
|
88
|
-
if (node.declaration.id?.type === 'Identifier') {
|
|
89
|
-
exportKinds.set(node.declaration.id.name, { kind: 'function', isFn: true, isArrow: false });
|
|
90
|
-
}
|
|
91
|
-
} else if (node.specifiers.length > 0 && !node.source) {
|
|
92
|
-
for (const spec of node.specifiers) {
|
|
93
|
-
if (spec.type !== 'ExportSpecifier') continue;
|
|
94
|
-
const info = localKinds.get(spec.local.name);
|
|
95
|
-
if (info) {
|
|
96
|
-
exportKinds.set(spec.exported.name, info);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return null;
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
renderChunk: {
|
|
105
|
-
order: 'post',
|
|
106
|
-
/**
|
|
107
|
-
* Appends kind-appropriate global hoist bindings for each entry export,
|
|
108
|
-
* skipping unsupported patterns (e.g. `export default`, `export class`,
|
|
109
|
-
* re-exports) whose declaration kind was not recorded by `transform`.
|
|
110
|
-
*
|
|
111
|
-
* @param {string} code
|
|
112
|
-
* @param {import('rollup').RenderedChunk} chunk
|
|
113
|
-
* @param {import('rollup').NormalizedOutputOptions} options
|
|
114
|
-
*/
|
|
115
|
-
handler(code, chunk, options) {
|
|
116
|
-
if (options.format !== 'iife' || !chunk.isEntry) return null;
|
|
117
|
-
|
|
118
|
-
/** @type {Array<{ name: string, info: ExportInfo }>} */
|
|
119
|
-
const known = [];
|
|
120
|
-
/** @type {string[]} */
|
|
121
|
-
const skipped = [];
|
|
122
|
-
|
|
123
|
-
for (const name of chunk.exports) {
|
|
124
|
-
const info = exportKinds.get(name);
|
|
125
|
-
if (info) {
|
|
126
|
-
known.push({ name, info });
|
|
127
|
-
} else {
|
|
128
|
-
skipped.push(name);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (known.length > 0) {
|
|
133
|
-
const label = known.length === 1 ? 'export' : 'exports';
|
|
134
|
-
const list = known.map(({ name, info }) => ` - ${name} (${info.kind})`).join('\n');
|
|
135
|
-
console.log(`[vite-plugin-gas-hoist] Hoisted ${known.length} ${label} to global scope:\n${list}`);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (skipped.length > 0) {
|
|
139
|
-
const label = skipped.length === 1 ? 'export' : 'exports';
|
|
140
|
-
const list = skipped.map((name) => ` - ${name}`).join('\n');
|
|
141
|
-
this.warn(`Skipped ${skipped.length} ${label} (unsupported pattern):\n${list}`);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (known.length === 0) return null;
|
|
145
|
-
|
|
146
|
-
const wrappers = known.map(({ name, info }) => emitWrapper(name, info, varName)).join('\n');
|
|
147
|
-
|
|
148
|
-
return `${code}\n${wrappers}\n`;
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Classifies a `VariableDeclaration` declarator as a value or function expression.
|
|
156
|
-
*
|
|
157
|
-
* @param {ExportKind} kind
|
|
158
|
-
* @param {import('estree').Expression | null | undefined} init
|
|
159
|
-
* @returns {ExportInfo}
|
|
160
|
-
*/
|
|
161
|
-
const classifyVariable = (kind, init) => {
|
|
162
|
-
const isArrow = init?.type === 'ArrowFunctionExpression';
|
|
163
|
-
const isFn = isArrow || init?.type === 'FunctionExpression';
|
|
164
|
-
return { kind, isFn, isArrow };
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Renders the hoisted binding for a single export, preserving the original
|
|
169
|
-
* declaration kind and (for `const`/`let` function expressions) the function
|
|
170
|
-
* shape (arrow vs `function` expression) so callers see a callable wrapper
|
|
171
|
-
* that GAS recognizes as a function.
|
|
172
|
-
*
|
|
173
|
-
* @param {string} name
|
|
174
|
-
* @param {ExportInfo} info
|
|
175
|
-
* @param {string} varName
|
|
176
|
-
* @returns {string}
|
|
177
|
-
*/
|
|
178
|
-
const emitWrapper = (name, info, varName) => {
|
|
179
|
-
if (info.kind === 'function') {
|
|
180
|
-
return `function ${name}(...args){return ${varName}.${name}(...args)}`;
|
|
181
|
-
}
|
|
182
|
-
if (info.isFn) {
|
|
183
|
-
if (info.isArrow) {
|
|
184
|
-
return `${info.kind} ${name} = (...args) => ${varName}.${name}(...args)`;
|
|
185
|
-
}
|
|
186
|
-
return `${info.kind} ${name} = function(...args){return ${varName}.${name}(...args)}`;
|
|
187
|
-
}
|
|
188
|
-
return `${info.kind} ${name} = ${varName}.${name}`;
|
|
189
|
-
};
|