@meteor-vite/plugin-zodern-relay 1.0.5 → 1.1.0-alpha.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.md +54 -54
- package/dist/Plugin.js +6 -2
- package/dist/Plugin.js.map +1 -1
- package/dist/Plugin.mjs +6 -2
- package/dist/Plugin.mjs.map +1 -1
- package/package.json +5 -7
- package/src/Plugin.ts +127 -121
- package/stubs/babel-plugin.js +444 -444
- package/stubs/relay-client.js +22 -22
package/README.md
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
# Vite plugin `zodern:relay`
|
|
2
|
-
|
|
3
|
-
This is a Vite compatability package for
|
|
4
|
-
[`zodern:relay`](https://github.com/zodern/meteor-relay#readme) - type safe
|
|
5
|
-
[Meteor](https://meteor.com/) methods and publications.
|
|
6
|
-
|
|
7
|
-
This plugin acts as partial replacement for the Babel `@zodern/babel-plugin-meteor-relay` plugin required by
|
|
8
|
-
`zodern:relay`. You still need the Babel plugin as it might still be required on the server.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
## Installation
|
|
12
|
-
```sh
|
|
13
|
-
npm i -D @meteor-vite/plugin-zodern-relay
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
## Configuration
|
|
17
|
-
Add the plugin to your Vite config and you're all set. If your methods and publications reside outside of `imports/api/<methods|publications>`, specify those paths when calling the plugin.
|
|
18
|
-
```ts
|
|
19
|
-
// vite.config.ts
|
|
20
|
-
import zodernRelay from '@meteor-vite/plugin-zodern-relay';
|
|
21
|
-
import { meteor } from 'meteor-vite/plugin';
|
|
22
|
-
|
|
23
|
-
export default defineConfig({
|
|
24
|
-
plugins: [
|
|
25
|
-
meteor({
|
|
26
|
-
clientEntry: '...',
|
|
27
|
-
}),
|
|
28
|
-
zodernRelay({
|
|
29
|
-
directories: {
|
|
30
|
-
/**
|
|
31
|
-
* Path to directories where your zodern:relay methods live
|
|
32
|
-
* @default ['./imports/methods']
|
|
33
|
-
*/
|
|
34
|
-
methods: ['./imports/methods'],
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Path to the directories where your zodern:relay publications live.
|
|
38
|
-
* @default ['./imports/publications']
|
|
39
|
-
*/
|
|
40
|
-
publications: ['./imports/publications'],
|
|
41
|
-
}
|
|
42
|
-
}),
|
|
43
|
-
]
|
|
44
|
-
})
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Usage & Documentation
|
|
48
|
-
You can use [`zodern:relay`](https://github.com/zodern/meteor-relay#readme) like you normally would. Consult their
|
|
49
|
-
readme for documentation.
|
|
50
|
-
|
|
51
|
-
- `zodern:relay` - https://github.com/zodern/meteor-relay#readme
|
|
52
|
-
- `meteor-vite` - https://github.com/JorgenVatle/meteor-vite#readme
|
|
53
|
-
|
|
54
|
-
## License
|
|
1
|
+
# Vite plugin `zodern:relay`
|
|
2
|
+
|
|
3
|
+
This is a Vite compatability package for
|
|
4
|
+
[`zodern:relay`](https://github.com/zodern/meteor-relay#readme) - type safe
|
|
5
|
+
[Meteor](https://meteor.com/) methods and publications.
|
|
6
|
+
|
|
7
|
+
This plugin acts as partial replacement for the Babel `@zodern/babel-plugin-meteor-relay` plugin required by
|
|
8
|
+
`zodern:relay`. You still need the Babel plugin as it might still be required on the server.
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
```sh
|
|
13
|
+
npm i -D @meteor-vite/plugin-zodern-relay
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Configuration
|
|
17
|
+
Add the plugin to your Vite config and you're all set. If your methods and publications reside outside of `imports/api/<methods|publications>`, specify those paths when calling the plugin.
|
|
18
|
+
```ts
|
|
19
|
+
// vite.config.ts
|
|
20
|
+
import zodernRelay from '@meteor-vite/plugin-zodern-relay';
|
|
21
|
+
import { meteor } from 'meteor-vite/plugin';
|
|
22
|
+
|
|
23
|
+
export default defineConfig({
|
|
24
|
+
plugins: [
|
|
25
|
+
meteor({
|
|
26
|
+
clientEntry: '...',
|
|
27
|
+
}),
|
|
28
|
+
zodernRelay({
|
|
29
|
+
directories: {
|
|
30
|
+
/**
|
|
31
|
+
* Path to directories where your zodern:relay methods live
|
|
32
|
+
* @default ['./imports/methods']
|
|
33
|
+
*/
|
|
34
|
+
methods: ['./imports/methods'],
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Path to the directories where your zodern:relay publications live.
|
|
38
|
+
* @default ['./imports/publications']
|
|
39
|
+
*/
|
|
40
|
+
publications: ['./imports/publications'],
|
|
41
|
+
}
|
|
42
|
+
}),
|
|
43
|
+
]
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage & Documentation
|
|
48
|
+
You can use [`zodern:relay`](https://github.com/zodern/meteor-relay#readme) like you normally would. Consult their
|
|
49
|
+
readme for documentation.
|
|
50
|
+
|
|
51
|
+
- `zodern:relay` - https://github.com/zodern/meteor-relay#readme
|
|
52
|
+
- `meteor-vite` - https://github.com/JorgenVatle/meteor-vite#readme
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
55
|
MIT
|
package/dist/Plugin.js
CHANGED
|
@@ -64,7 +64,7 @@ async function zodernRelay(options) {
|
|
|
64
64
|
}
|
|
65
65
|
return {
|
|
66
66
|
name: "zodern-relay",
|
|
67
|
-
async load(filename) {
|
|
67
|
+
async load(filename, fileOptions) {
|
|
68
68
|
const relay = resolveRelay(filename || "");
|
|
69
69
|
if (!relay) {
|
|
70
70
|
return;
|
|
@@ -73,6 +73,10 @@ async function zodernRelay(options) {
|
|
|
73
73
|
if (!config.shouldTransform({ content: code, id: filename })) {
|
|
74
74
|
return;
|
|
75
75
|
}
|
|
76
|
+
let arch = "web.browser.vite";
|
|
77
|
+
if (fileOptions?.ssr || this.environment.name === "server") {
|
|
78
|
+
arch = "os.vite.ssr";
|
|
79
|
+
}
|
|
76
80
|
const transform = await (0, import_core.transformAsync)(code, {
|
|
77
81
|
configFile: false,
|
|
78
82
|
babelrc: false,
|
|
@@ -83,7 +87,7 @@ async function zodernRelay(options) {
|
|
|
83
87
|
caller: {
|
|
84
88
|
name: "@meteor-vite/plugin-zodern-relay",
|
|
85
89
|
// @ts-expect-error No type definition for this, but it's required by the Babel plugin.
|
|
86
|
-
arch
|
|
90
|
+
arch
|
|
87
91
|
}
|
|
88
92
|
});
|
|
89
93
|
if (!transform) {
|
package/dist/Plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Plugin.ts"],"sourcesContent":["import { transformAsync } from '@babel/core';\
|
|
1
|
+
{"version":3,"sources":["../src/Plugin.ts"],"sourcesContent":["import { transformAsync } from '@babel/core';\nimport FS from 'fs';\nimport Path from 'path';\nimport { type Plugin } from 'vite';\n\nconst cwd = process.cwd();\n\nexport default async function zodernRelay(options?: Options): Promise<Plugin> {\n const config = {\n directories: {\n methods: options?.directories?.methods || ['./imports/methods'],\n publications: options?.directories?.publications || ['./imports/publications'],\n },\n shouldTransform: options?.shouldTransform || (({ content }) => content.includes(options?.relayPackageId || 'meteor/zodern:relay')),\n } satisfies Options;\n \n const directories = [\n ...config.directories.methods.map((path) => ['methods', Path.relative(cwd, path)]),\n ...config.directories.publications.map((path) => ['publications', Path.relative(cwd, path)])\n ] as [RelayInfo['type'], string][];\n \n function resolveRelay(id: string): RelayInfo | undefined {\n const relativePath = Path.relative(cwd, id);\n for (const [type, directory] of directories) {\n if (!relativePath.startsWith(directory)) {\n continue;\n }\n return {\n id,\n type,\n relativePath,\n }\n }\n }\n \n return {\n name: 'zodern-relay',\n async load(filename, fileOptions) {\n const relay = resolveRelay(filename || '');\n \n if (!relay) {\n return;\n }\n \n const code = FS.readFileSync(filename, 'utf-8');\n \n // Prevent transforming files that don't use zodern:relay\n if (!config.shouldTransform({ content: code, id: filename })) {\n return;\n }\n \n let arch = 'web.browser.vite';\n \n if (fileOptions?.ssr || this.environment.name === 'server') {\n arch = 'os.vite.ssr';\n }\n \n const transform = await transformAsync(code, {\n configFile: false,\n babelrc: false,\n filename,\n presets: ['@babel/preset-typescript'], // Add TypeScript preset\n plugins: ['@zodern/babel-plugin-meteor-relay'],\n caller: {\n name: '@meteor-vite/plugin-zodern-relay',\n \n // @ts-expect-error No type definition for this, but it's required by the Babel plugin.\n arch,\n }\n });\n \n if (!transform) {\n return;\n }\n \n let newCode = transform.code ?? '';\n \n // Rewrite any newly added imports to use the correct package ID.\n // Only applicable when using a fork of zodern:relay.\n if (options?.relayPackageId) {\n newCode = newCode.replace('meteor/zodern:relay', options.relayPackageId);\n }\n \n return {\n code: newCode,\n }\n }\n }\n \n \n}\nexport interface Options {\n directories?: {\n /**\n * Path to directories where your zodern:relay methods live\n * @default ['./imports/methods']\n */\n methods?: string[],\n \n /**\n * Path to the directories where your zodern:relay publications live.\n * @default ['./imports/publications']\n */\n publications?: string[],\n }\n /**\n * Specify a custom filter to determine whether a module should be transformed with zodern:relay.\n * Used to prevent unnecessary Babel transformations.\n * @default ({ content }) => content.includes('meteor/zodern:relay')\n * @param id\n */\n shouldTransform?: (file: { content: string, id: string }) => boolean;\n \n /**\n * If you're using a fork of zodern:relay, you should specify its Meteor import string here so imports\n * can be substituted with your fork. The Babel plugin currently injects a hardcoded import string for\n * client-code `zodern:relay/client`, so it's important we correct that before finishing the transform process.\n * @default 'meteor/zodern:relay'\n */\n relayPackageId?: string;\n}\n\ntype RelayInfo = {\n type: 'methods' | 'publications';\n id: string;\n relativePath: string;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA+B;AAC/B,gBAAe;AACf,kBAAiB;AAGjB,IAAM,MAAM,QAAQ,IAAI;AAExB,eAAO,YAAmC,SAAoC;AAC1E,QAAM,SAAS;AAAA,IACX,aAAa;AAAA,MACT,SAAS,SAAS,aAAa,WAAW,CAAC,mBAAmB;AAAA,MAC9D,cAAc,SAAS,aAAa,gBAAgB,CAAC,wBAAwB;AAAA,IACjF;AAAA,IACA,iBAAiB,SAAS,oBAAoB,CAAC,EAAE,QAAQ,MAAM,QAAQ,SAAS,SAAS,kBAAkB,qBAAqB;AAAA,EACpI;AAEA,QAAM,cAAc;AAAA,IAChB,GAAG,OAAO,YAAY,QAAQ,IAAI,CAAC,SAAS,CAAC,WAAW,YAAAA,QAAK,SAAS,KAAK,IAAI,CAAC,CAAC;AAAA,IACjF,GAAG,OAAO,YAAY,aAAa,IAAI,CAAC,SAAS,CAAC,gBAAgB,YAAAA,QAAK,SAAS,KAAK,IAAI,CAAC,CAAC;AAAA,EAC/F;AAEA,WAAS,aAAa,IAAmC;AACrD,UAAM,eAAe,YAAAA,QAAK,SAAS,KAAK,EAAE;AAC1C,eAAW,CAAC,MAAM,SAAS,KAAK,aAAa;AACzC,UAAI,CAAC,aAAa,WAAW,SAAS,GAAG;AACrC;AAAA,MACJ;AACA,aAAO;AAAA,QACH;AAAA,QACA;AAAA,QACA;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,MAAM;AAAA,IACN,MAAM,KAAK,UAAU,aAAa;AAC9B,YAAM,QAAQ,aAAa,YAAY,EAAE;AAEzC,UAAI,CAAC,OAAO;AACR;AAAA,MACJ;AAEA,YAAM,OAAO,UAAAC,QAAG,aAAa,UAAU,OAAO;AAG9C,UAAI,CAAC,OAAO,gBAAgB,EAAE,SAAS,MAAM,IAAI,SAAS,CAAC,GAAG;AAC1D;AAAA,MACJ;AAEA,UAAI,OAAO;AAEX,UAAI,aAAa,OAAO,KAAK,YAAY,SAAS,UAAU;AACxD,eAAO;AAAA,MACX;AAEA,YAAM,YAAY,UAAM,4BAAe,MAAM;AAAA,QACzC,YAAY;AAAA,QACZ,SAAS;AAAA,QACT;AAAA,QACA,SAAS,CAAC,0BAA0B;AAAA;AAAA,QACpC,SAAS,CAAC,mCAAmC;AAAA,QAC7C,QAAQ;AAAA,UACJ,MAAM;AAAA;AAAA,UAGN;AAAA,QACJ;AAAA,MACJ,CAAC;AAED,UAAI,CAAC,WAAW;AACZ;AAAA,MACJ;AAEA,UAAI,UAAU,UAAU,QAAQ;AAIhC,UAAI,SAAS,gBAAgB;AACzB,kBAAU,QAAQ,QAAQ,uBAAuB,QAAQ,cAAc;AAAA,MAC3E;AAEA,aAAO;AAAA,QACH,MAAM;AAAA,MACV;AAAA,IACJ;AAAA,EACJ;AAGJ;","names":["Path","FS"]}
|
package/dist/Plugin.mjs
CHANGED
|
@@ -30,7 +30,7 @@ async function zodernRelay(options) {
|
|
|
30
30
|
}
|
|
31
31
|
return {
|
|
32
32
|
name: "zodern-relay",
|
|
33
|
-
async load(filename) {
|
|
33
|
+
async load(filename, fileOptions) {
|
|
34
34
|
const relay = resolveRelay(filename || "");
|
|
35
35
|
if (!relay) {
|
|
36
36
|
return;
|
|
@@ -39,6 +39,10 @@ async function zodernRelay(options) {
|
|
|
39
39
|
if (!config.shouldTransform({ content: code, id: filename })) {
|
|
40
40
|
return;
|
|
41
41
|
}
|
|
42
|
+
let arch = "web.browser.vite";
|
|
43
|
+
if (fileOptions?.ssr || this.environment.name === "server") {
|
|
44
|
+
arch = "os.vite.ssr";
|
|
45
|
+
}
|
|
42
46
|
const transform = await transformAsync(code, {
|
|
43
47
|
configFile: false,
|
|
44
48
|
babelrc: false,
|
|
@@ -49,7 +53,7 @@ async function zodernRelay(options) {
|
|
|
49
53
|
caller: {
|
|
50
54
|
name: "@meteor-vite/plugin-zodern-relay",
|
|
51
55
|
// @ts-expect-error No type definition for this, but it's required by the Babel plugin.
|
|
52
|
-
arch
|
|
56
|
+
arch
|
|
53
57
|
}
|
|
54
58
|
});
|
|
55
59
|
if (!transform) {
|
package/dist/Plugin.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Plugin.ts"],"sourcesContent":["import { transformAsync } from '@babel/core';\
|
|
1
|
+
{"version":3,"sources":["../src/Plugin.ts"],"sourcesContent":["import { transformAsync } from '@babel/core';\nimport FS from 'fs';\nimport Path from 'path';\nimport { type Plugin } from 'vite';\n\nconst cwd = process.cwd();\n\nexport default async function zodernRelay(options?: Options): Promise<Plugin> {\n const config = {\n directories: {\n methods: options?.directories?.methods || ['./imports/methods'],\n publications: options?.directories?.publications || ['./imports/publications'],\n },\n shouldTransform: options?.shouldTransform || (({ content }) => content.includes(options?.relayPackageId || 'meteor/zodern:relay')),\n } satisfies Options;\n \n const directories = [\n ...config.directories.methods.map((path) => ['methods', Path.relative(cwd, path)]),\n ...config.directories.publications.map((path) => ['publications', Path.relative(cwd, path)])\n ] as [RelayInfo['type'], string][];\n \n function resolveRelay(id: string): RelayInfo | undefined {\n const relativePath = Path.relative(cwd, id);\n for (const [type, directory] of directories) {\n if (!relativePath.startsWith(directory)) {\n continue;\n }\n return {\n id,\n type,\n relativePath,\n }\n }\n }\n \n return {\n name: 'zodern-relay',\n async load(filename, fileOptions) {\n const relay = resolveRelay(filename || '');\n \n if (!relay) {\n return;\n }\n \n const code = FS.readFileSync(filename, 'utf-8');\n \n // Prevent transforming files that don't use zodern:relay\n if (!config.shouldTransform({ content: code, id: filename })) {\n return;\n }\n \n let arch = 'web.browser.vite';\n \n if (fileOptions?.ssr || this.environment.name === 'server') {\n arch = 'os.vite.ssr';\n }\n \n const transform = await transformAsync(code, {\n configFile: false,\n babelrc: false,\n filename,\n presets: ['@babel/preset-typescript'], // Add TypeScript preset\n plugins: ['@zodern/babel-plugin-meteor-relay'],\n caller: {\n name: '@meteor-vite/plugin-zodern-relay',\n \n // @ts-expect-error No type definition for this, but it's required by the Babel plugin.\n arch,\n }\n });\n \n if (!transform) {\n return;\n }\n \n let newCode = transform.code ?? '';\n \n // Rewrite any newly added imports to use the correct package ID.\n // Only applicable when using a fork of zodern:relay.\n if (options?.relayPackageId) {\n newCode = newCode.replace('meteor/zodern:relay', options.relayPackageId);\n }\n \n return {\n code: newCode,\n }\n }\n }\n \n \n}\nexport interface Options {\n directories?: {\n /**\n * Path to directories where your zodern:relay methods live\n * @default ['./imports/methods']\n */\n methods?: string[],\n \n /**\n * Path to the directories where your zodern:relay publications live.\n * @default ['./imports/publications']\n */\n publications?: string[],\n }\n /**\n * Specify a custom filter to determine whether a module should be transformed with zodern:relay.\n * Used to prevent unnecessary Babel transformations.\n * @default ({ content }) => content.includes('meteor/zodern:relay')\n * @param id\n */\n shouldTransform?: (file: { content: string, id: string }) => boolean;\n \n /**\n * If you're using a fork of zodern:relay, you should specify its Meteor import string here so imports\n * can be substituted with your fork. The Babel plugin currently injects a hardcoded import string for\n * client-code `zodern:relay/client`, so it's important we correct that before finishing the transform process.\n * @default 'meteor/zodern:relay'\n */\n relayPackageId?: string;\n}\n\ntype RelayInfo = {\n type: 'methods' | 'publications';\n id: string;\n relativePath: string;\n};\n"],"mappings":";AAAA,SAAS,sBAAsB;AAC/B,OAAO,QAAQ;AACf,OAAO,UAAU;AAGjB,IAAM,MAAM,QAAQ,IAAI;AAExB,eAAO,YAAmC,SAAoC;AAC1E,QAAM,SAAS;AAAA,IACX,aAAa;AAAA,MACT,SAAS,SAAS,aAAa,WAAW,CAAC,mBAAmB;AAAA,MAC9D,cAAc,SAAS,aAAa,gBAAgB,CAAC,wBAAwB;AAAA,IACjF;AAAA,IACA,iBAAiB,SAAS,oBAAoB,CAAC,EAAE,QAAQ,MAAM,QAAQ,SAAS,SAAS,kBAAkB,qBAAqB;AAAA,EACpI;AAEA,QAAM,cAAc;AAAA,IAChB,GAAG,OAAO,YAAY,QAAQ,IAAI,CAAC,SAAS,CAAC,WAAW,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC;AAAA,IACjF,GAAG,OAAO,YAAY,aAAa,IAAI,CAAC,SAAS,CAAC,gBAAgB,KAAK,SAAS,KAAK,IAAI,CAAC,CAAC;AAAA,EAC/F;AAEA,WAAS,aAAa,IAAmC;AACrD,UAAM,eAAe,KAAK,SAAS,KAAK,EAAE;AAC1C,eAAW,CAAC,MAAM,SAAS,KAAK,aAAa;AACzC,UAAI,CAAC,aAAa,WAAW,SAAS,GAAG;AACrC;AAAA,MACJ;AACA,aAAO;AAAA,QACH;AAAA,QACA;AAAA,QACA;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAEA,SAAO;AAAA,IACH,MAAM;AAAA,IACN,MAAM,KAAK,UAAU,aAAa;AAC9B,YAAM,QAAQ,aAAa,YAAY,EAAE;AAEzC,UAAI,CAAC,OAAO;AACR;AAAA,MACJ;AAEA,YAAM,OAAO,GAAG,aAAa,UAAU,OAAO;AAG9C,UAAI,CAAC,OAAO,gBAAgB,EAAE,SAAS,MAAM,IAAI,SAAS,CAAC,GAAG;AAC1D;AAAA,MACJ;AAEA,UAAI,OAAO;AAEX,UAAI,aAAa,OAAO,KAAK,YAAY,SAAS,UAAU;AACxD,eAAO;AAAA,MACX;AAEA,YAAM,YAAY,MAAM,eAAe,MAAM;AAAA,QACzC,YAAY;AAAA,QACZ,SAAS;AAAA,QACT;AAAA,QACA,SAAS,CAAC,0BAA0B;AAAA;AAAA,QACpC,SAAS,CAAC,mCAAmC;AAAA,QAC7C,QAAQ;AAAA,UACJ,MAAM;AAAA;AAAA,UAGN;AAAA,QACJ;AAAA,MACJ,CAAC;AAED,UAAI,CAAC,WAAW;AACZ;AAAA,MACJ;AAEA,UAAI,UAAU,UAAU,QAAQ;AAIhC,UAAI,SAAS,gBAAgB;AACzB,kBAAU,QAAQ,QAAQ,uBAAuB,QAAQ,cAAc;AAAA,MAC3E;AAEA,aAAO;AAAA,QACH,MAAM;AAAA,MACV;AAAA,IACJ;AAAA,EACJ;AAGJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meteor-vite/plugin-zodern-relay",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.1.0-alpha.0",
|
|
4
4
|
"description": "Vite compatability plugin for zodern:relay - typed Meteor methods and publications",
|
|
5
5
|
"main": "dist/Plugin.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
+
"types": "./dist/Plugin.d.ts",
|
|
8
9
|
"require": "./dist/Plugin.js",
|
|
9
|
-
"import": "./dist/Plugin.mjs"
|
|
10
|
-
"types": "./dist/Plugin.d.ts"
|
|
10
|
+
"import": "./dist/Plugin.mjs"
|
|
11
11
|
},
|
|
12
12
|
"./stubs/*": [
|
|
13
13
|
"./stubs/*.js"
|
|
@@ -44,11 +44,9 @@
|
|
|
44
44
|
"@babel/preset-typescript": "^7.24.7",
|
|
45
45
|
"@types/babel__core": "^7.20.5",
|
|
46
46
|
"tsup": "^8.0.2",
|
|
47
|
-
"typescript": "^5.4.2"
|
|
48
|
-
"vite": "^5.1.6"
|
|
47
|
+
"typescript": "^5.4.2"
|
|
49
48
|
},
|
|
50
49
|
"peerDependencies": {
|
|
51
|
-
"@babel/core": "^7.0.0"
|
|
52
|
-
"vite": ">=3.0.0"
|
|
50
|
+
"@babel/core": "^7.0.0"
|
|
53
51
|
}
|
|
54
52
|
}
|
package/src/Plugin.ts
CHANGED
|
@@ -1,121 +1,127 @@
|
|
|
1
|
-
import { transformAsync } from '@babel/core';
|
|
2
|
-
import FS from 'fs';
|
|
3
|
-
import Path from 'path';
|
|
4
|
-
import { type Plugin } from 'vite';
|
|
5
|
-
|
|
6
|
-
const cwd = process.cwd();
|
|
7
|
-
|
|
8
|
-
export default async function zodernRelay(options?: Options): Promise<Plugin> {
|
|
9
|
-
const config = {
|
|
10
|
-
directories: {
|
|
11
|
-
methods: options?.directories?.methods || ['./imports/methods'],
|
|
12
|
-
publications: options?.directories?.publications || ['./imports/publications'],
|
|
13
|
-
},
|
|
14
|
-
shouldTransform: options?.shouldTransform || (({ content }) => content.includes(options?.relayPackageId || 'meteor/zodern:relay')),
|
|
15
|
-
} satisfies Options;
|
|
16
|
-
|
|
17
|
-
const directories = [
|
|
18
|
-
...config.directories.methods.map((path) => ['methods', Path.relative(cwd, path)]),
|
|
19
|
-
...config.directories.publications.map((path) => ['publications', Path.relative(cwd, path)])
|
|
20
|
-
] as [RelayInfo['type'], string][];
|
|
21
|
-
|
|
22
|
-
function resolveRelay(id: string): RelayInfo | undefined {
|
|
23
|
-
const relativePath = Path.relative(cwd, id);
|
|
24
|
-
for (const [type, directory] of directories) {
|
|
25
|
-
if (!relativePath.startsWith(directory)) {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
return {
|
|
29
|
-
id,
|
|
30
|
-
type,
|
|
31
|
-
relativePath,
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
name: 'zodern-relay',
|
|
38
|
-
async load(filename) {
|
|
39
|
-
const relay = resolveRelay(filename || '');
|
|
40
|
-
|
|
41
|
-
if (!relay) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const code = FS.readFileSync(filename, 'utf-8');
|
|
46
|
-
|
|
47
|
-
// Prevent transforming files that don't use zodern:relay
|
|
48
|
-
if (!config.shouldTransform({ content: code, id: filename })) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Path to
|
|
96
|
-
* @default ['./imports/
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
1
|
+
import { transformAsync } from '@babel/core';
|
|
2
|
+
import FS from 'fs';
|
|
3
|
+
import Path from 'path';
|
|
4
|
+
import { type Plugin } from 'vite';
|
|
5
|
+
|
|
6
|
+
const cwd = process.cwd();
|
|
7
|
+
|
|
8
|
+
export default async function zodernRelay(options?: Options): Promise<Plugin> {
|
|
9
|
+
const config = {
|
|
10
|
+
directories: {
|
|
11
|
+
methods: options?.directories?.methods || ['./imports/methods'],
|
|
12
|
+
publications: options?.directories?.publications || ['./imports/publications'],
|
|
13
|
+
},
|
|
14
|
+
shouldTransform: options?.shouldTransform || (({ content }) => content.includes(options?.relayPackageId || 'meteor/zodern:relay')),
|
|
15
|
+
} satisfies Options;
|
|
16
|
+
|
|
17
|
+
const directories = [
|
|
18
|
+
...config.directories.methods.map((path) => ['methods', Path.relative(cwd, path)]),
|
|
19
|
+
...config.directories.publications.map((path) => ['publications', Path.relative(cwd, path)])
|
|
20
|
+
] as [RelayInfo['type'], string][];
|
|
21
|
+
|
|
22
|
+
function resolveRelay(id: string): RelayInfo | undefined {
|
|
23
|
+
const relativePath = Path.relative(cwd, id);
|
|
24
|
+
for (const [type, directory] of directories) {
|
|
25
|
+
if (!relativePath.startsWith(directory)) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
id,
|
|
30
|
+
type,
|
|
31
|
+
relativePath,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
name: 'zodern-relay',
|
|
38
|
+
async load(filename, fileOptions) {
|
|
39
|
+
const relay = resolveRelay(filename || '');
|
|
40
|
+
|
|
41
|
+
if (!relay) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const code = FS.readFileSync(filename, 'utf-8');
|
|
46
|
+
|
|
47
|
+
// Prevent transforming files that don't use zodern:relay
|
|
48
|
+
if (!config.shouldTransform({ content: code, id: filename })) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let arch = 'web.browser.vite';
|
|
53
|
+
|
|
54
|
+
if (fileOptions?.ssr || this.environment.name === 'server') {
|
|
55
|
+
arch = 'os.vite.ssr';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const transform = await transformAsync(code, {
|
|
59
|
+
configFile: false,
|
|
60
|
+
babelrc: false,
|
|
61
|
+
filename,
|
|
62
|
+
presets: ['@babel/preset-typescript'], // Add TypeScript preset
|
|
63
|
+
plugins: ['@zodern/babel-plugin-meteor-relay'],
|
|
64
|
+
caller: {
|
|
65
|
+
name: '@meteor-vite/plugin-zodern-relay',
|
|
66
|
+
|
|
67
|
+
// @ts-expect-error No type definition for this, but it's required by the Babel plugin.
|
|
68
|
+
arch,
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
if (!transform) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let newCode = transform.code ?? '';
|
|
77
|
+
|
|
78
|
+
// Rewrite any newly added imports to use the correct package ID.
|
|
79
|
+
// Only applicable when using a fork of zodern:relay.
|
|
80
|
+
if (options?.relayPackageId) {
|
|
81
|
+
newCode = newCode.replace('meteor/zodern:relay', options.relayPackageId);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
code: newCode,
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
}
|
|
92
|
+
export interface Options {
|
|
93
|
+
directories?: {
|
|
94
|
+
/**
|
|
95
|
+
* Path to directories where your zodern:relay methods live
|
|
96
|
+
* @default ['./imports/methods']
|
|
97
|
+
*/
|
|
98
|
+
methods?: string[],
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Path to the directories where your zodern:relay publications live.
|
|
102
|
+
* @default ['./imports/publications']
|
|
103
|
+
*/
|
|
104
|
+
publications?: string[],
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Specify a custom filter to determine whether a module should be transformed with zodern:relay.
|
|
108
|
+
* Used to prevent unnecessary Babel transformations.
|
|
109
|
+
* @default ({ content }) => content.includes('meteor/zodern:relay')
|
|
110
|
+
* @param id
|
|
111
|
+
*/
|
|
112
|
+
shouldTransform?: (file: { content: string, id: string }) => boolean;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* If you're using a fork of zodern:relay, you should specify its Meteor import string here so imports
|
|
116
|
+
* can be substituted with your fork. The Babel plugin currently injects a hardcoded import string for
|
|
117
|
+
* client-code `zodern:relay/client`, so it's important we correct that before finishing the transform process.
|
|
118
|
+
* @default 'meteor/zodern:relay'
|
|
119
|
+
*/
|
|
120
|
+
relayPackageId?: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
type RelayInfo = {
|
|
124
|
+
type: 'methods' | 'publications';
|
|
125
|
+
id: string;
|
|
126
|
+
relativePath: string;
|
|
127
|
+
};
|