Skip to content

Commit e096322

Browse files
authored
fix: new regex based bootstrap import/export source location transform (#105)
1 parent 02a1e11 commit e096322

File tree

5 files changed

+50
-165
lines changed

5 files changed

+50
-165
lines changed

.changeset/warm-pugs-impress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"bob-the-bundler": patch
3+
---
4+
5+
Replace babel based export/import source location transform with an improved regex based transform that reduces code change noise and preserves the original formatting.

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
"jest-resolver.js"
2323
],
2424
"dependencies": {
25-
"@babel/core": "^7.18.9",
26-
"@babel/generator": "^7.18.9",
27-
"@babel/parser": "^7.18.9",
28-
"@babel/traverse": "^7.18.9",
2925
"@rollup/plugin-json": "^4.1.0",
3026
"@rollup/plugin-node-resolve": "^13.3.0",
3127
"@vercel/ncc": "^0.34.0",

src/utils/rewrite-code-imports.spec.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,75 @@ const fixturePath = path.join(__dirname, "__fixtures__", "index.ts");
66
it("ignores module import statement", () => {
77
const fixture = "import foo from 'foo'";
88
const result = rewriteCodeImports(fixture, fixturePath);
9-
expect(result).toEqual("import foo from 'foo';");
9+
expect(result).toEqual("import foo from 'foo'");
1010
});
1111

1212
it("handles type statements", () => {
1313
const fixture = "import type foo from 'foo'";
1414
const result = rewriteCodeImports(fixture, fixturePath);
15-
expect(result).toEqual("import type foo from 'foo';");
15+
expect(result).toEqual("import type foo from 'foo'");
1616
});
1717

1818
it("rewrites relative import statement", () => {
1919
const fixture = "import foo from './bar'";
2020
const result = rewriteCodeImports(fixture, fixturePath);
21-
expect(result).toEqual(`import foo from "./bar.js";`);
21+
expect(result).toEqual(`import foo from './bar.js'`);
2222
});
2323

2424
it("rewrites relative import statement for folder", () => {
2525
const fixture = "import foo from './foo'";
2626
const result = rewriteCodeImports(fixture, fixturePath);
27-
expect(result).toEqual(`import foo from "./foo/index.js";`);
27+
expect(result).toEqual(`import foo from './foo/index.js'`);
2828
});
2929

3030
it("rewrites relative import statement", () => {
3131
const fixture = "import foo from '../foo'";
3232
const result = rewriteCodeImports(fixture, fixturePath);
33-
expect(result).toEqual(`import foo from "../foo.js";`);
33+
expect(result).toEqual(`import foo from '../foo.js'`);
3434
});
3535

3636
it("ignores module export statement", () => {
3737
const fixture = "export {foo} from 'foo'";
3838
const result = rewriteCodeImports(fixture, fixturePath);
39-
expect(result).toEqual("export { foo } from 'foo';");
39+
expect(result).toEqual("export {foo} from 'foo'");
4040
});
4141

4242
it("rewrites relative export statement", () => {
4343
const fixture = "export {foo} from './bar'";
4444
const result = rewriteCodeImports(fixture, fixturePath);
45-
expect(result).toEqual(`export { foo } from "./bar.js";`);
45+
expect(result).toEqual(`export {foo} from './bar.js'`);
4646
});
4747

4848
it("rewrites relative export statement for folder", () => {
4949
const fixture = "export {foo} from './foo'";
5050
const result = rewriteCodeImports(fixture, fixturePath);
51-
expect(result).toEqual(`export { foo } from "./foo/index.js";`);
51+
expect(result).toEqual(`export {foo} from './foo/index.js'`);
52+
});
53+
54+
it("complex example", () => {
55+
const fixture = `
56+
import { GraphQLError } from '../../../error/GraphQLError';
57+
58+
import type { FieldNode } from '../../../language/ast';
59+
import type { ASTVisitor } from '../../../language/visitor';
60+
61+
import { getNamedType } from '../../../type/definition';
62+
import { isIntrospectionType } from '../../../type/introspection';
63+
64+
import type { ValidationContext } from '../../ValidationContext';
65+
`;
66+
const result = rewriteCodeImports(fixture, fixturePath);
67+
expect(result).toMatchInlineSnapshot(`
68+
"
69+
import { GraphQLError } from '../../../error/GraphQLError.js';
70+
71+
import type { FieldNode } from '../../../language/ast.js';
72+
import type { ASTVisitor } from '../../../language/visitor.js';
73+
74+
import { getNamedType } from '../../../type/definition.js';
75+
import { isIntrospectionType } from '../../../type/introspection.js';
76+
77+
import type { ValidationContext } from '../../ValidationContext.js';
78+
"
79+
`);
5280
});

src/utils/rewrite-code-imports.ts

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { parse } from "@babel/parser";
2-
import generate from "@babel/generator";
3-
import traverse from "@babel/traverse";
41
import * as fse from "fs-extra";
52
import * as path from "path";
63

@@ -31,32 +28,15 @@ export function rewriteCodeImports(
3128
fileContents: string,
3229
absoluteFilePath: string
3330
): string {
34-
const ast = parse(fileContents, {
35-
sourceType: "module",
36-
plugins: ["typescript"],
37-
});
38-
3931
const relativeDirname = path.dirname(absoluteFilePath);
4032

41-
traverse(ast, {
42-
ImportDeclaration(nodePath) {
43-
nodePath.node.source.value = rewriteSourceValue(
44-
nodePath.node.source.value,
45-
relativeDirname
46-
);
47-
},
48-
ExportDeclaration(nodePath) {
49-
if (
50-
nodePath.node.type !== "ExportDefaultDeclaration" &&
51-
nodePath.node.source
52-
) {
53-
nodePath.node.source.value = rewriteSourceValue(
54-
nodePath.node.source.value,
55-
relativeDirname
56-
);
57-
}
58-
},
59-
});
60-
61-
return generate(ast).code;
33+
return fileContents.replace(
34+
/* this regex should hopefully catch all kind of import/export expressions that are relative. */
35+
/((?:import|export)\s+[\s\w,{}*]*\s+from\s+["'])((?:\.\/|\.\.\/)(?:(?!\.js).)+)(["'])/g,
36+
(_, importFromPart, modulePath, hyphenEndPart) => {
37+
console.log(`${importFromPart}${modulePath}`);
38+
const sourcePath = rewriteSourceValue(modulePath, relativeDirname);
39+
return `${importFromPart}${sourcePath}${hyphenEndPart}`;
40+
}
41+
);
6242
}

yarn.lock

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.18.6.tgz#8b37d24e88e8e21c499d4328db80577d8882fa53"
3737
integrity sha512-tzulrgDT0QD6U7BJ4TKVk2SDDg7wlP39P9yAx1RfLy7vP/7rsDRlWVfbWxElslu56+r7QOhB2NSDsabYYruoZQ==
3838

39-
"@babel/compat-data@^7.18.8":
40-
version "7.18.8"
41-
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.18.8.tgz#2483f565faca607b8535590e84e7de323f27764d"
42-
integrity sha512-HSmX4WZPPK3FUxYp7g2T6EyO8j96HlZJlxmKPSh6KAcqwyDrfx7hKjXpAW/0FhFfTJsR0Yt4lAjLI2coMptIHQ==
43-
4439
"@babel/core@^7.11.6", "@babel/core@^7.12.3":
4540
version "7.18.6"
4641
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.6.tgz#54a107a3c298aee3fe5e1947a6464b9b6faca03d"
@@ -62,27 +57,6 @@
6257
json5 "^2.2.1"
6358
semver "^6.3.0"
6459

65-
"@babel/core@^7.18.9":
66-
version "7.18.9"
67-
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.9.tgz#805461f967c77ff46c74ca0460ccf4fe933ddd59"
68-
integrity sha512-1LIb1eL8APMy91/IMW+31ckrfBM4yCoLaVzoDhZUKSM4cu1L1nIidyxkCgzPAgrC5WEz36IPEr/eSeSF9pIn+g==
69-
dependencies:
70-
"@ampproject/remapping" "^2.1.0"
71-
"@babel/code-frame" "^7.18.6"
72-
"@babel/generator" "^7.18.9"
73-
"@babel/helper-compilation-targets" "^7.18.9"
74-
"@babel/helper-module-transforms" "^7.18.9"
75-
"@babel/helpers" "^7.18.9"
76-
"@babel/parser" "^7.18.9"
77-
"@babel/template" "^7.18.6"
78-
"@babel/traverse" "^7.18.9"
79-
"@babel/types" "^7.18.9"
80-
convert-source-map "^1.7.0"
81-
debug "^4.1.0"
82-
gensync "^1.0.0-beta.2"
83-
json5 "^2.2.1"
84-
semver "^6.3.0"
85-
8660
"@babel/generator@^7.18.6", "@babel/generator@^7.7.2":
8761
version "7.18.6"
8862
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.6.tgz#9ab2d46d3cbf631f0e80f72e72874a04c3fc12a9"
@@ -92,15 +66,6 @@
9266
"@jridgewell/gen-mapping" "^0.3.0"
9367
jsesc "^2.5.1"
9468

95-
"@babel/generator@^7.18.9":
96-
version "7.18.9"
97-
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.9.tgz#68337e9ea8044d6ddc690fb29acae39359cca0a5"
98-
integrity sha512-wt5Naw6lJrL1/SGkipMiFxJjtyczUWTP38deiP1PO60HsBjDeKk08CGC3S8iVuvf0FmTdgKwU1KIXzSKL1G0Ug==
99-
dependencies:
100-
"@babel/types" "^7.18.9"
101-
"@jridgewell/gen-mapping" "^0.3.2"
102-
jsesc "^2.5.1"
103-
10469
"@babel/helper-compilation-targets@^7.18.6":
10570
version "7.18.6"
10671
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.18.6.tgz#18d35bfb9f83b1293c22c55b3d576c1315b6ed96"
@@ -111,26 +76,11 @@
11176
browserslist "^4.20.2"
11277
semver "^6.3.0"
11378

114-
"@babel/helper-compilation-targets@^7.18.9":
115-
version "7.18.9"
116-
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.18.9.tgz#69e64f57b524cde3e5ff6cc5a9f4a387ee5563bf"
117-
integrity sha512-tzLCyVmqUiFlcFoAPLA/gL9TeYrF61VLNtb+hvkuVaB5SUjW7jcfrglBIX1vUIoT7CLP3bBlIMeyEsIl2eFQNg==
118-
dependencies:
119-
"@babel/compat-data" "^7.18.8"
120-
"@babel/helper-validator-option" "^7.18.6"
121-
browserslist "^4.20.2"
122-
semver "^6.3.0"
123-
12479
"@babel/helper-environment-visitor@^7.18.6":
12580
version "7.18.6"
12681
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.6.tgz#b7eee2b5b9d70602e59d1a6cad7dd24de7ca6cd7"
12782
integrity sha512-8n6gSfn2baOY+qlp+VSzsosjCVGFqWKmDF0cCWOybh52Dw3SEyoWR1KrhMJASjLwIEkkAufZ0xvr+SxLHSpy2Q==
12883

129-
"@babel/helper-environment-visitor@^7.18.9":
130-
version "7.18.9"
131-
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.9.tgz#0c0cee9b35d2ca190478756865bb3528422f51be"
132-
integrity sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==
133-
13484
"@babel/helper-function-name@^7.18.6":
13585
version "7.18.6"
13686
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.18.6.tgz#8334fecb0afba66e6d87a7e8c6bb7fed79926b83"
@@ -139,14 +89,6 @@
13989
"@babel/template" "^7.18.6"
14090
"@babel/types" "^7.18.6"
14191

142-
"@babel/helper-function-name@^7.18.9":
143-
version "7.18.9"
144-
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.18.9.tgz#940e6084a55dee867d33b4e487da2676365e86b0"
145-
integrity sha512-fJgWlZt7nxGksJS9a0XdSaI4XvpExnNIgRP+rVefWh5U7BL8pPuir6SJUmFKRfjWQ51OtWSzwOxhaH/EBWWc0A==
146-
dependencies:
147-
"@babel/template" "^7.18.6"
148-
"@babel/types" "^7.18.9"
149-
15092
"@babel/helper-hoist-variables@^7.18.6":
15193
version "7.18.6"
15294
resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.18.6.tgz#d4d2c8fb4baeaa5c68b99cc8245c56554f926678"
@@ -175,20 +117,6 @@
175117
"@babel/traverse" "^7.18.6"
176118
"@babel/types" "^7.18.6"
177119

178-
"@babel/helper-module-transforms@^7.18.9":
179-
version "7.18.9"
180-
resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.18.9.tgz#5a1079c005135ed627442df31a42887e80fcb712"
181-
integrity sha512-KYNqY0ICwfv19b31XzvmI/mfcylOzbLtowkw+mfvGPAQ3kfCnMLYbED3YecL5tPd8nAYFQFAd6JHp2LxZk/J1g==
182-
dependencies:
183-
"@babel/helper-environment-visitor" "^7.18.9"
184-
"@babel/helper-module-imports" "^7.18.6"
185-
"@babel/helper-simple-access" "^7.18.6"
186-
"@babel/helper-split-export-declaration" "^7.18.6"
187-
"@babel/helper-validator-identifier" "^7.18.6"
188-
"@babel/template" "^7.18.6"
189-
"@babel/traverse" "^7.18.9"
190-
"@babel/types" "^7.18.9"
191-
192120
"@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.8.0":
193121
version "7.14.5"
194122
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.14.5.tgz#5ac822ce97eec46741ab70a517971e443a70c5a9"
@@ -227,15 +155,6 @@
227155
"@babel/traverse" "^7.18.6"
228156
"@babel/types" "^7.18.6"
229157

230-
"@babel/helpers@^7.18.9":
231-
version "7.18.9"
232-
resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.18.9.tgz#4bef3b893f253a1eced04516824ede94dcfe7ff9"
233-
integrity sha512-Jf5a+rbrLoR4eNdUmnFu8cN5eNJT6qdTdOg5IHIzq87WwyRw9PwguLFOWYgktN/60IP4fgDUawJvs7PjQIzELQ==
234-
dependencies:
235-
"@babel/template" "^7.18.6"
236-
"@babel/traverse" "^7.18.9"
237-
"@babel/types" "^7.18.9"
238-
239158
"@babel/highlight@^7.18.6":
240159
version "7.18.6"
241160
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.18.6.tgz#81158601e93e2563795adcbfbdf5d64be3f2ecdf"
@@ -250,11 +169,6 @@
250169
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.6.tgz#845338edecad65ebffef058d3be851f1d28a63bc"
251170
integrity sha512-uQVSa9jJUe/G/304lXspfWVpKpK4euFLgGiMQFOCpM/bgcAdeoHwi/OQz23O9GK2osz26ZiXRRV9aV+Yl1O8tw==
252171

253-
"@babel/parser@^7.18.9":
254-
version "7.18.9"
255-
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.9.tgz#f2dde0c682ccc264a9a8595efd030a5cc8fd2539"
256-
integrity sha512-9uJveS9eY9DJ0t64YbIBZICtJy8a5QrDEVdiLCG97fVLpDTpGX7t8mMSb6OWw6Lrnjqj4O8zwjELX3dhoMgiBg==
257-
258172
"@babel/plugin-syntax-async-generators@^7.8.4":
259173
version "7.8.4"
260174
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.8.4.tgz#a983fb1aeb2ec3f6ed042a210f640e90e786fe0d"
@@ -378,22 +292,6 @@
378292
debug "^4.1.0"
379293
globals "^11.1.0"
380294

381-
"@babel/traverse@^7.18.9":
382-
version "7.18.9"
383-
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.18.9.tgz#deeff3e8f1bad9786874cb2feda7a2d77a904f98"
384-
integrity sha512-LcPAnujXGwBgv3/WHv01pHtb2tihcyW1XuL9wd7jqh1Z8AQkTd+QVjMrMijrln0T7ED3UXLIy36P9Ao7W75rYg==
385-
dependencies:
386-
"@babel/code-frame" "^7.18.6"
387-
"@babel/generator" "^7.18.9"
388-
"@babel/helper-environment-visitor" "^7.18.9"
389-
"@babel/helper-function-name" "^7.18.9"
390-
"@babel/helper-hoist-variables" "^7.18.6"
391-
"@babel/helper-split-export-declaration" "^7.18.6"
392-
"@babel/parser" "^7.18.9"
393-
"@babel/types" "^7.18.9"
394-
debug "^4.1.0"
395-
globals "^11.1.0"
396-
397295
"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
398296
version "7.18.6"
399297
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.6.tgz#5d781dd10a3f0c9f1f931bd19de5eb26ec31acf0"
@@ -402,14 +300,6 @@
402300
"@babel/helper-validator-identifier" "^7.18.6"
403301
to-fast-properties "^2.0.0"
404302

405-
"@babel/types@^7.18.9":
406-
version "7.18.9"
407-
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.9.tgz#7148d64ba133d8d73a41b3172ac4b83a1452205f"
408-
integrity sha512-WwMLAg2MvJmt/rKEVQBBhIVffMmnilX4oe0sRe7iPOHIGsqpruFHHdrfj4O1CMMtgMtCU4oPafZjDPCRgO57Wg==
409-
dependencies:
410-
"@babel/helper-validator-identifier" "^7.18.6"
411-
to-fast-properties "^2.0.0"
412-
413303
"@bcoe/v8-coverage@^0.2.3":
414304
version "0.2.3"
415305
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
@@ -840,15 +730,6 @@
840730
"@jridgewell/sourcemap-codec" "^1.4.10"
841731
"@jridgewell/trace-mapping" "^0.3.9"
842732

843-
"@jridgewell/gen-mapping@^0.3.2":
844-
version "0.3.2"
845-
resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz#c1aedc61e853f2bb9f5dfe6d4442d3b565b253b9"
846-
integrity sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==
847-
dependencies:
848-
"@jridgewell/set-array" "^1.0.1"
849-
"@jridgewell/sourcemap-codec" "^1.4.10"
850-
"@jridgewell/trace-mapping" "^0.3.9"
851-
852733
"@jridgewell/resolve-uri@^3.0.3":
853734
version "3.0.7"
854735
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.0.7.tgz#30cd49820a962aff48c8fffc5cd760151fca61fe"
@@ -859,11 +740,6 @@
859740
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.1.tgz#36a6acc93987adcf0ba50c66908bd0b70de8afea"
860741
integrity sha512-Ct5MqZkLGEXTVmQYbGtx9SVqD2fqwvdubdps5D3djjAkgkKwT918VNOz65pEHFaYTeWcukmJmH5SwsA9Tn2ObQ==
861742

862-
"@jridgewell/set-array@^1.0.1":
863-
version "1.1.2"
864-
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72"
865-
integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==
866-
867743
"@jridgewell/sourcemap-codec@^1.4.10":
868744
version "1.4.13"
869745
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.13.tgz#b6461fb0c2964356c469e115f504c95ad97ab88c"

0 commit comments

Comments
 (0)