@isograph/babel-plugin 0.0.0-main-24737b7d → 0.0.0-main-421d6faf

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.
Files changed (2) hide show
  1. package/compileTag.js +25 -8
  2. package/package.json +3 -1
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
 
@@ -101,6 +102,7 @@ function compileImportStatement(t, path, type, field, artifactType, config) {
101
102
  cwd,
102
103
  config.config['artifact_directory'] ?? config.config['project_root'],
103
104
  );
105
+ const module = config.config['options']?.['module'];
104
106
 
105
107
  const fileToArtifactDir = pathModule.relative(folder, artifactDirectory);
106
108
  const artifactDirToArtifact = `/__isograph/${type}/${field}/${artifactType}.ts`;
@@ -121,14 +123,29 @@ function compileImportStatement(t, path, type, field, artifactType, config) {
121
123
  fileToArtifact = '.' + fileToArtifact;
122
124
  }
123
125
 
124
- path.replaceWith(
125
- t.memberExpression(
126
- t.callExpression(t.identifier('require'), [
127
- t.stringLiteral(fileToArtifact),
128
- ]),
129
- t.identifier('default'),
130
- ),
131
- );
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
+ }
132
149
  }
133
150
 
134
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.0.0-main-24737b7d",
5
+ "version": "0.0.0-main-421d6faf",
6
6
  "keywords": [
7
7
  "graphql",
8
8
  "isograph",
@@ -19,6 +19,7 @@
19
19
  "tsc": "tsc"
20
20
  },
21
21
  "dependencies": {
22
+ "@babel/helper-module-imports": "^7.0.0",
22
23
  "babel-plugin-macros": "^2.0.0",
23
24
  "cosmiconfig": "^5.0.5",
24
25
  "graphql": "15.3.0"
@@ -27,6 +28,7 @@
27
28
  "@babel/core": "^7.20.0",
28
29
  "@babel/types": "^7.25.6",
29
30
  "@types/babel__core": "^7.20.5",
31
+ "@types/babel__helper-module-imports": "^7.18.3",
30
32
  "@types/cosmiconfig": "^5.0.3",
31
33
  "prettier": "2.8.8",
32
34
  "prettier-plugin-hermes-parser": "0.16.0"