@isograph/babel-plugin 0.0.0-main-43a453c5

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.
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ const compileTag = require("./compileTag");
4
+ const cosmiconfig = require("cosmiconfig");
5
+
6
+ const configExplorer = cosmiconfig("isograph", {
7
+ searchPlaces: ["isograph.config.json"],
8
+ loaders: {
9
+ ".json": cosmiconfig.loadJson,
10
+ },
11
+ });
12
+
13
+ let IsographConfig;
14
+ const result = configExplorer.searchSync();
15
+ if (result) {
16
+ IsographConfig = result.config;
17
+ } else {
18
+ throw new Error(
19
+ "No config found. Do you have a isograph.config.json file somewhere?"
20
+ );
21
+ }
22
+
23
+ module.exports = function BabelPluginIsograph(context) {
24
+ const { types: t } = context;
25
+ if (!t) {
26
+ throw new Error(
27
+ 'BabelPluginIsograph: Expected plugin context to include "types", but got:' +
28
+ String(context)
29
+ );
30
+ }
31
+
32
+ const visitor = {
33
+ TaggedTemplateExpression(path) {
34
+ compileTag(t, path, IsographConfig);
35
+ },
36
+ };
37
+
38
+ return {
39
+ visitor: {
40
+ Program(path, state) {
41
+ path.traverse(visitor, state);
42
+ },
43
+ },
44
+ };
45
+ };
package/compileTag.js ADDED
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+
3
+ const pathModule = require("path");
4
+
5
+ function compileTag(t, path, config) {
6
+ const tag = path.get("tag");
7
+
8
+ if (tag.isIdentifier({ name: "iso" })) {
9
+ // Don't do anything for iso tags
10
+ }
11
+ if (tag.isIdentifier({ name: "isoFetch" })) {
12
+ return compileIsoFetchTag(t, path, config);
13
+ }
14
+
15
+ return false;
16
+ }
17
+
18
+ const typeAndFieldRegex = new RegExp("\\s*([^\\.\\s]+)\\.([^\\s\\(]+)", "m");
19
+
20
+ function compileIsoFetchTag(t, path, config) {
21
+ // This throws if the tag is invalid
22
+ const { type, field } = getTypeAndField(path);
23
+ compileImportStatement(t, path, type, field, "entrypoint", config);
24
+ }
25
+
26
+ function getTypeAndField(path) {
27
+ const quasis = path.node.quasi.quasis;
28
+
29
+ if (quasis.length !== 1) {
30
+ throw new Error(
31
+ "BabelPluginIsograph: Substitutions are not allowed in iso fragments."
32
+ );
33
+ }
34
+
35
+ const content = path.node.quasi.quasis[0].value.raw;
36
+ const typeAndField = typeAndFieldRegex.exec(content);
37
+ const type = typeAndField[1];
38
+ const field = typeAndField[2];
39
+
40
+ if (type == null || field == null) {
41
+ throw new Error(
42
+ "Malformed iso literal. I hope the iso compiler failed to accept this literal!"
43
+ );
44
+ }
45
+ return { type, field };
46
+ }
47
+
48
+ function compileImportStatement(t, path, type, field, artifactType, config) {
49
+ const filename = path.state.filename;
50
+ const folder = pathModule.dirname(filename);
51
+ const cwd = path.state.cwd;
52
+ const artifactDirectory = pathModule.join(cwd, config.artifact_directory);
53
+
54
+ const fileToArtifactDir = pathModule.relative(folder, artifactDirectory);
55
+ const artifactDirToArtifact = `/__isograph/${type}/${field}/${artifactType}.isograph.ts`;
56
+ const fileToArtifact = pathModule.join(
57
+ fileToArtifactDir,
58
+ artifactDirToArtifact
59
+ );
60
+
61
+ path.replaceWith(
62
+ t.memberExpression(
63
+ t.CallExpression(t.Identifier("require"), [
64
+ t.StringLiteral(fileToArtifact),
65
+ ]),
66
+ t.Identifier("default")
67
+ )
68
+ );
69
+ }
70
+
71
+ module.exports = compileTag;
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require("./BabelPluginIsograph.js");
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@isograph/babel-plugin",
3
+ "description": "A Babel plugin for use with Isograph applications.",
4
+ "version": "0.0.0-main-43a453c5",
5
+ "keywords": [
6
+ "graphql",
7
+ "isograph",
8
+ "babel",
9
+ "babel-plugin"
10
+ ],
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/isographlabs/isograph",
15
+ "directory": "libs/isograph-babel"
16
+ },
17
+ "dependencies": {
18
+ "babel-plugin-macros": "^2.0.0",
19
+ "cosmiconfig": "^5.0.5",
20
+ "graphql": "15.3.0"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/core": "^7.20.0",
24
+ "prettier": "2.8.8",
25
+ "prettier-plugin-hermes-parser": "0.16.0"
26
+ }
27
+ }