@isograph/babel-plugin 0.3.0 → 0.3.1
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/compileTag.js +41 -18
- package/package.json +5 -2
- package/stub.ts +1 -0
- package/tsconfig.json +1 -2
package/compileTag.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
+
const { addDefault } = require('@babel/helper-module-imports');
|
3
4
|
const pathModule = require('path');
|
4
5
|
const os = require('os');
|
5
6
|
|
@@ -17,11 +18,15 @@ function compileTag(t, path, config) {
|
|
17
18
|
// This throws if the tag is invalid
|
18
19
|
compileImportStatement(t, path, type, field, 'entrypoint', config);
|
19
20
|
} else if (keyword === 'field') {
|
20
|
-
if (
|
21
|
-
|
22
|
-
path.parentPath.node.arguments.length === 1
|
23
|
-
|
24
|
-
|
21
|
+
if (t.isCallExpression(path.parentPath.node)) {
|
22
|
+
const firstArg = path.parentPath.node.arguments[0];
|
23
|
+
if (path.parentPath.node.arguments.length === 1 && firstArg != null) {
|
24
|
+
path.parentPath.replaceWith(firstArg);
|
25
|
+
} else {
|
26
|
+
throw new Error(
|
27
|
+
'Invalid iso tag usage. The iso function should be passed at most one argument.',
|
28
|
+
);
|
29
|
+
}
|
25
30
|
} else {
|
26
31
|
path.replaceWith(
|
27
32
|
t.arrowFunctionExpression([t.identifier('x')], t.identifier('x')),
|
@@ -45,26 +50,28 @@ const typeAndFieldRegex = new RegExp(
|
|
45
50
|
* @param {babel.NodePath<babel.types.CallExpression>} path
|
46
51
|
* */
|
47
52
|
function getTypeAndField(path) {
|
48
|
-
|
53
|
+
const firstArg = path.node.arguments[0];
|
54
|
+
if (path.node.arguments.length !== 1 || firstArg == null) {
|
49
55
|
throw new Error(
|
50
56
|
`BabelPluginIsograph: Iso invocation require one parameter, found ${path.node.arguments.length}`,
|
51
57
|
);
|
52
58
|
}
|
53
59
|
|
54
|
-
if (
|
60
|
+
if (firstArg.type !== 'TemplateLiteral') {
|
55
61
|
throw new Error(
|
56
62
|
'BabelPluginIsograph: Only template literals are allowed in iso fragments.',
|
57
63
|
);
|
58
64
|
}
|
59
65
|
|
60
|
-
const quasis =
|
61
|
-
|
66
|
+
const quasis = firstArg.quasis;
|
67
|
+
const firstQuasi = quasis[0];
|
68
|
+
if (quasis.length !== 1 || firstQuasi == null) {
|
62
69
|
throw new Error(
|
63
70
|
'BabelPluginIsograph: Substitutions are not allowed in iso fragments.',
|
64
71
|
);
|
65
72
|
}
|
66
73
|
|
67
|
-
const content =
|
74
|
+
const content = firstQuasi.value.raw;
|
68
75
|
const typeAndField = typeAndFieldRegex.exec(content);
|
69
76
|
|
70
77
|
const keyword = typeAndField?.[1];
|
@@ -95,6 +102,7 @@ function compileImportStatement(t, path, type, field, artifactType, config) {
|
|
95
102
|
cwd,
|
96
103
|
config.config['artifact_directory'] ?? config.config['project_root'],
|
97
104
|
);
|
105
|
+
const module = config.config['options']?.['module'];
|
98
106
|
|
99
107
|
const fileToArtifactDir = pathModule.relative(folder, artifactDirectory);
|
100
108
|
const artifactDirToArtifact = `/__isograph/${type}/${field}/${artifactType}.ts`;
|
@@ -115,14 +123,29 @@ function compileImportStatement(t, path, type, field, artifactType, config) {
|
|
115
123
|
fileToArtifact = '.' + fileToArtifact;
|
116
124
|
}
|
117
125
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
)
|
125
|
-
|
126
|
+
if (module === 'esmodule') {
|
127
|
+
const program = path.scope.getProgramParent();
|
128
|
+
const imports = /** @type {Map<string, string>} */ (
|
129
|
+
program.data['imports'] ??= new Map()
|
130
|
+
);
|
131
|
+
|
132
|
+
let id = imports.get(fileToArtifact);
|
133
|
+
if (id == null) {
|
134
|
+
id = addDefault(path, fileToArtifact, { nameHint: field }).name;
|
135
|
+
imports.set(fileToArtifact, id);
|
136
|
+
}
|
137
|
+
|
138
|
+
path.replaceWith(t.identifier(id));
|
139
|
+
} else {
|
140
|
+
path.replaceWith(
|
141
|
+
t.memberExpression(
|
142
|
+
t.callExpression(t.identifier('require'), [
|
143
|
+
t.stringLiteral(fileToArtifact),
|
144
|
+
]),
|
145
|
+
t.identifier('default'),
|
146
|
+
),
|
147
|
+
);
|
148
|
+
}
|
126
149
|
}
|
127
150
|
|
128
151
|
module.exports = compileTag;
|
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.3.
|
5
|
+
"version": "0.3.1",
|
6
6
|
"keywords": [
|
7
7
|
"graphql",
|
8
8
|
"isograph",
|
@@ -16,9 +16,11 @@
|
|
16
16
|
"directory": "libs/isograph-babel-plugin"
|
17
17
|
},
|
18
18
|
"scripts": {
|
19
|
-
"tsc": "tsc"
|
19
|
+
"tsc": "tsc",
|
20
|
+
"tsc-force": "tsc --build --clean && tsc --build --force"
|
20
21
|
},
|
21
22
|
"dependencies": {
|
23
|
+
"@babel/helper-module-imports": "^7.0.0",
|
22
24
|
"babel-plugin-macros": "^2.0.0",
|
23
25
|
"cosmiconfig": "^5.0.5",
|
24
26
|
"graphql": "15.3.0"
|
@@ -27,6 +29,7 @@
|
|
27
29
|
"@babel/core": "^7.20.0",
|
28
30
|
"@babel/types": "^7.25.6",
|
29
31
|
"@types/babel__core": "^7.20.5",
|
32
|
+
"@types/babel__helper-module-imports": "^7.18.3",
|
30
33
|
"@types/cosmiconfig": "^5.0.3",
|
31
34
|
"prettier": "2.8.8",
|
32
35
|
"prettier-plugin-hermes-parser": "0.16.0"
|
package/stub.ts
CHANGED