@isograph/babel-plugin 0.0.0-main-2eacee40 → 0.0.0-main-feb39d09
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/BabelPluginIsograph.js +12 -0
- package/compileTag.js +31 -7
- package/package.json +7 -1
- package/stub.ts +6 -0
- package/tsconfig.json +10 -0
package/BabelPluginIsograph.js
CHANGED
@@ -10,6 +10,7 @@ const configExplorer = cosmiconfig('isograph', {
|
|
10
10
|
},
|
11
11
|
});
|
12
12
|
|
13
|
+
/** @type {import("cosmiconfig").Config} */
|
13
14
|
let IsographConfig;
|
14
15
|
const result = configExplorer.searchSync();
|
15
16
|
if (result) {
|
@@ -20,6 +21,16 @@ if (result) {
|
|
20
21
|
);
|
21
22
|
}
|
22
23
|
|
24
|
+
/** @typedef {import("@babel/core")} babel*/
|
25
|
+
|
26
|
+
/**
|
27
|
+
* @typedef {Object} Context
|
28
|
+
* @property {typeof babel.types} [types]
|
29
|
+
* */
|
30
|
+
|
31
|
+
/**
|
32
|
+
* @param {Context} context
|
33
|
+
* @returns {babel.PluginObj} */
|
23
34
|
module.exports = function BabelPluginIsograph(context) {
|
24
35
|
const { types } = context;
|
25
36
|
if (!types) {
|
@@ -29,6 +40,7 @@ module.exports = function BabelPluginIsograph(context) {
|
|
29
40
|
);
|
30
41
|
}
|
31
42
|
|
43
|
+
/** @type {babel.Visitor<babel.PluginPass>} */
|
32
44
|
const visitor = {
|
33
45
|
CallExpression(path) {
|
34
46
|
compileTag(types, path, IsographConfig);
|
package/compileTag.js
CHANGED
@@ -3,6 +3,12 @@
|
|
3
3
|
const pathModule = require('path');
|
4
4
|
const os = require('os');
|
5
5
|
|
6
|
+
/**
|
7
|
+
* @typedef {import("@babel/core")} babel
|
8
|
+
* @param {typeof babel.types} t
|
9
|
+
* @param {babel.NodePath<babel.types.CallExpression>} path
|
10
|
+
* @param {import("cosmiconfig").Config} config
|
11
|
+
*/
|
6
12
|
function compileTag(t, path, config) {
|
7
13
|
const callee = path.node.callee;
|
8
14
|
if (t.isIdentifier(callee) && callee.name === 'iso' && path.node.arguments) {
|
@@ -27,12 +33,22 @@ const typeAndFieldRegex = new RegExp(
|
|
27
33
|
'm',
|
28
34
|
);
|
29
35
|
|
36
|
+
/**
|
37
|
+
* @param {babel.NodePath<babel.types.CallExpression>} path
|
38
|
+
* */
|
30
39
|
function getTypeAndField(path) {
|
31
40
|
if (path.node.arguments.length !== 1) {
|
32
41
|
throw new Error(
|
33
42
|
`BabelPluginIsograph: Iso invocation require one parameter, found ${path.node.arguments.length}`,
|
34
43
|
);
|
35
44
|
}
|
45
|
+
|
46
|
+
if (path.node.arguments[0].type !== 'TemplateLiteral') {
|
47
|
+
throw new Error(
|
48
|
+
'BabelPluginIsograph: Only template literals are allowed in iso fragments.',
|
49
|
+
);
|
50
|
+
}
|
51
|
+
|
36
52
|
const quasis = path.node.arguments[0].quasis;
|
37
53
|
if (quasis.length !== 1) {
|
38
54
|
throw new Error(
|
@@ -43,9 +59,9 @@ function getTypeAndField(path) {
|
|
43
59
|
const content = quasis[0].value.raw;
|
44
60
|
const typeAndField = typeAndFieldRegex.exec(content);
|
45
61
|
|
46
|
-
const keyword = typeAndField[1];
|
47
|
-
const type = typeAndField[2];
|
48
|
-
const field = typeAndField[3];
|
62
|
+
const keyword = typeAndField?.[1];
|
63
|
+
const type = typeAndField?.[2];
|
64
|
+
const field = typeAndField?.[3];
|
49
65
|
|
50
66
|
if (keyword == null || type == null || field == null) {
|
51
67
|
throw new Error(
|
@@ -55,13 +71,21 @@ function getTypeAndField(path) {
|
|
55
71
|
return { keyword, type, field };
|
56
72
|
}
|
57
73
|
|
74
|
+
/**
|
75
|
+
* @param {typeof babel.types} t
|
76
|
+
* @param {babel.NodePath<babel.types.CallExpression>} path
|
77
|
+
* @param {string} type
|
78
|
+
* @param {string} field
|
79
|
+
* @param {string} artifactType
|
80
|
+
* @param {import("cosmiconfig").Config} config
|
81
|
+
*/
|
58
82
|
function compileImportStatement(t, path, type, field, artifactType, config) {
|
59
83
|
const filename = path.state.filename;
|
60
84
|
const folder = pathModule.dirname(filename);
|
61
85
|
const cwd = path.state.cwd;
|
62
86
|
const artifactDirectory = pathModule.join(
|
63
87
|
cwd,
|
64
|
-
config
|
88
|
+
config['artifact_directory'] ?? config['project_root'],
|
65
89
|
);
|
66
90
|
|
67
91
|
const fileToArtifactDir = pathModule.relative(folder, artifactDirectory);
|
@@ -85,10 +109,10 @@ function compileImportStatement(t, path, type, field, artifactType, config) {
|
|
85
109
|
|
86
110
|
path.replaceWith(
|
87
111
|
t.memberExpression(
|
88
|
-
t.
|
89
|
-
t.
|
112
|
+
t.callExpression(t.identifier('require'), [
|
113
|
+
t.stringLiteral(fileToArtifact),
|
90
114
|
]),
|
91
|
-
t.
|
115
|
+
t.identifier('default'),
|
92
116
|
),
|
93
117
|
);
|
94
118
|
}
|
package/package.json
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
"name": "@isograph/babel-plugin",
|
3
3
|
"description": "A Babel plugin for use with Isograph applications.",
|
4
4
|
"homepage": "https://isograph.dev",
|
5
|
-
"version": "0.0.0-main-
|
5
|
+
"version": "0.0.0-main-feb39d09",
|
6
6
|
"keywords": [
|
7
7
|
"graphql",
|
8
8
|
"isograph",
|
@@ -15,6 +15,9 @@
|
|
15
15
|
"url": "git+https://github.com/isographlabs/isograph.git",
|
16
16
|
"directory": "libs/isograph-babel-plugin"
|
17
17
|
},
|
18
|
+
"scripts": {
|
19
|
+
"tsc": "tsc"
|
20
|
+
},
|
18
21
|
"dependencies": {
|
19
22
|
"babel-plugin-macros": "^2.0.0",
|
20
23
|
"cosmiconfig": "^5.0.5",
|
@@ -22,6 +25,9 @@
|
|
22
25
|
},
|
23
26
|
"devDependencies": {
|
24
27
|
"@babel/core": "^7.20.0",
|
28
|
+
"@babel/types": "^7.25.6",
|
29
|
+
"@types/babel__core": "^7.20.5",
|
30
|
+
"@types/cosmiconfig": "^5.0.3",
|
25
31
|
"prettier": "2.8.8",
|
26
32
|
"prettier-plugin-hermes-parser": "0.16.0"
|
27
33
|
},
|
package/stub.ts
ADDED