Skip to content

Commit

Permalink
fix: new regex based bootstrap import/export source location transform (
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l authored Jul 19, 2022
1 parent 02a1e11 commit e096322
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 165 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-pugs-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bob-the-bundler": patch
---

Replace babel based export/import source location transform with an improved regex based transform that reduces code change noise and preserves the original formatting.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
"jest-resolver.js"
],
"dependencies": {
"@babel/core": "^7.18.9",
"@babel/generator": "^7.18.9",
"@babel/parser": "^7.18.9",
"@babel/traverse": "^7.18.9",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@vercel/ncc": "^0.34.0",
Expand Down
44 changes: 36 additions & 8 deletions src/utils/rewrite-code-imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,75 @@ const fixturePath = path.join(__dirname, "__fixtures__", "index.ts");
it("ignores module import statement", () => {
const fixture = "import foo from 'foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual("import foo from 'foo';");
expect(result).toEqual("import foo from 'foo'");
});

it("handles type statements", () => {
const fixture = "import type foo from 'foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual("import type foo from 'foo';");
expect(result).toEqual("import type foo from 'foo'");
});

it("rewrites relative import statement", () => {
const fixture = "import foo from './bar'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual(`import foo from "./bar.js";`);
expect(result).toEqual(`import foo from './bar.js'`);
});

it("rewrites relative import statement for folder", () => {
const fixture = "import foo from './foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual(`import foo from "./foo/index.js";`);
expect(result).toEqual(`import foo from './foo/index.js'`);
});

it("rewrites relative import statement", () => {
const fixture = "import foo from '../foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual(`import foo from "../foo.js";`);
expect(result).toEqual(`import foo from '../foo.js'`);
});

it("ignores module export statement", () => {
const fixture = "export {foo} from 'foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual("export { foo } from 'foo';");
expect(result).toEqual("export {foo} from 'foo'");
});

it("rewrites relative export statement", () => {
const fixture = "export {foo} from './bar'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual(`export { foo } from "./bar.js";`);
expect(result).toEqual(`export {foo} from './bar.js'`);
});

it("rewrites relative export statement for folder", () => {
const fixture = "export {foo} from './foo'";
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toEqual(`export { foo } from "./foo/index.js";`);
expect(result).toEqual(`export {foo} from './foo/index.js'`);
});

it("complex example", () => {
const fixture = `
import { GraphQLError } from '../../../error/GraphQLError';
import type { FieldNode } from '../../../language/ast';
import type { ASTVisitor } from '../../../language/visitor';
import { getNamedType } from '../../../type/definition';
import { isIntrospectionType } from '../../../type/introspection';
import type { ValidationContext } from '../../ValidationContext';
`;
const result = rewriteCodeImports(fixture, fixturePath);
expect(result).toMatchInlineSnapshot(`
"
import { GraphQLError } from '../../../error/GraphQLError.js';
import type { FieldNode } from '../../../language/ast.js';
import type { ASTVisitor } from '../../../language/visitor.js';
import { getNamedType } from '../../../type/definition.js';
import { isIntrospectionType } from '../../../type/introspection.js';
import type { ValidationContext } from '../../ValidationContext.js';
"
`);
});
38 changes: 9 additions & 29 deletions src/utils/rewrite-code-imports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import traverse from "@babel/traverse";
import * as fse from "fs-extra";
import * as path from "path";

Expand Down Expand Up @@ -31,32 +28,15 @@ export function rewriteCodeImports(
fileContents: string,
absoluteFilePath: string
): string {
const ast = parse(fileContents, {
sourceType: "module",
plugins: ["typescript"],
});

const relativeDirname = path.dirname(absoluteFilePath);

traverse(ast, {
ImportDeclaration(nodePath) {
nodePath.node.source.value = rewriteSourceValue(
nodePath.node.source.value,
relativeDirname
);
},
ExportDeclaration(nodePath) {
if (
nodePath.node.type !== "ExportDefaultDeclaration" &&
nodePath.node.source
) {
nodePath.node.source.value = rewriteSourceValue(
nodePath.node.source.value,
relativeDirname
);
}
},
});

return generate(ast).code;
return fileContents.replace(
/* this regex should hopefully catch all kind of import/export expressions that are relative. */
/((?:import|export)\s+[\s\w,{}*]*\s+from\s+["'])((?:\.\/|\.\.\/)(?:(?!\.js).)+)(["'])/g,
(_, importFromPart, modulePath, hyphenEndPart) => {
console.log(`${importFromPart}${modulePath}`);
const sourcePath = rewriteSourceValue(modulePath, relativeDirname);
return `${importFromPart}${sourcePath}${hyphenEndPart}`;
}
);
}
124 changes: 0 additions & 124 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.18.6.tgz#8b37d24e88e8e21c499d4328db80577d8882fa53"
integrity sha512-tzulrgDT0QD6U7BJ4TKVk2SDDg7wlP39P9yAx1RfLy7vP/7rsDRlWVfbWxElslu56+r7QOhB2NSDsabYYruoZQ==

"@babel/compat-data@^7.18.8":
version "7.18.8"
resolved "https://registry.yarnpkg.com/@babel/compat-data/-/compat-data-7.18.8.tgz#2483f565faca607b8535590e84e7de323f27764d"
integrity sha512-HSmX4WZPPK3FUxYp7g2T6EyO8j96HlZJlxmKPSh6KAcqwyDrfx7hKjXpAW/0FhFfTJsR0Yt4lAjLI2coMptIHQ==

"@babel/core@^7.11.6", "@babel/core@^7.12.3":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.6.tgz#54a107a3c298aee3fe5e1947a6464b9b6faca03d"
Expand All @@ -62,27 +57,6 @@
json5 "^2.2.1"
semver "^6.3.0"

"@babel/core@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.9.tgz#805461f967c77ff46c74ca0460ccf4fe933ddd59"
integrity sha512-1LIb1eL8APMy91/IMW+31ckrfBM4yCoLaVzoDhZUKSM4cu1L1nIidyxkCgzPAgrC5WEz36IPEr/eSeSF9pIn+g==
dependencies:
"@ampproject/remapping" "^2.1.0"
"@babel/code-frame" "^7.18.6"
"@babel/generator" "^7.18.9"
"@babel/helper-compilation-targets" "^7.18.9"
"@babel/helper-module-transforms" "^7.18.9"
"@babel/helpers" "^7.18.9"
"@babel/parser" "^7.18.9"
"@babel/template" "^7.18.6"
"@babel/traverse" "^7.18.9"
"@babel/types" "^7.18.9"
convert-source-map "^1.7.0"
debug "^4.1.0"
gensync "^1.0.0-beta.2"
json5 "^2.2.1"
semver "^6.3.0"

"@babel/generator@^7.18.6", "@babel/generator@^7.7.2":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.6.tgz#9ab2d46d3cbf631f0e80f72e72874a04c3fc12a9"
Expand All @@ -92,15 +66,6 @@
"@jridgewell/gen-mapping" "^0.3.0"
jsesc "^2.5.1"

"@babel/generator@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.9.tgz#68337e9ea8044d6ddc690fb29acae39359cca0a5"
integrity sha512-wt5Naw6lJrL1/SGkipMiFxJjtyczUWTP38deiP1PO60HsBjDeKk08CGC3S8iVuvf0FmTdgKwU1KIXzSKL1G0Ug==
dependencies:
"@babel/types" "^7.18.9"
"@jridgewell/gen-mapping" "^0.3.2"
jsesc "^2.5.1"

"@babel/helper-compilation-targets@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.18.6.tgz#18d35bfb9f83b1293c22c55b3d576c1315b6ed96"
Expand All @@ -111,26 +76,11 @@
browserslist "^4.20.2"
semver "^6.3.0"

"@babel/helper-compilation-targets@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/helper-compilation-targets/-/helper-compilation-targets-7.18.9.tgz#69e64f57b524cde3e5ff6cc5a9f4a387ee5563bf"
integrity sha512-tzLCyVmqUiFlcFoAPLA/gL9TeYrF61VLNtb+hvkuVaB5SUjW7jcfrglBIX1vUIoT7CLP3bBlIMeyEsIl2eFQNg==
dependencies:
"@babel/compat-data" "^7.18.8"
"@babel/helper-validator-option" "^7.18.6"
browserslist "^4.20.2"
semver "^6.3.0"

"@babel/helper-environment-visitor@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.6.tgz#b7eee2b5b9d70602e59d1a6cad7dd24de7ca6cd7"
integrity sha512-8n6gSfn2baOY+qlp+VSzsosjCVGFqWKmDF0cCWOybh52Dw3SEyoWR1KrhMJASjLwIEkkAufZ0xvr+SxLHSpy2Q==

"@babel/helper-environment-visitor@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/helper-environment-visitor/-/helper-environment-visitor-7.18.9.tgz#0c0cee9b35d2ca190478756865bb3528422f51be"
integrity sha512-3r/aACDJ3fhQ/EVgFy0hpj8oHyHpQc+LPtJoY9SzTThAsStm4Ptegq92vqKoE3vD706ZVFWITnMnxucw+S9Ipg==

"@babel/helper-function-name@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.18.6.tgz#8334fecb0afba66e6d87a7e8c6bb7fed79926b83"
Expand All @@ -139,14 +89,6 @@
"@babel/template" "^7.18.6"
"@babel/types" "^7.18.6"

"@babel/helper-function-name@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/helper-function-name/-/helper-function-name-7.18.9.tgz#940e6084a55dee867d33b4e487da2676365e86b0"
integrity sha512-fJgWlZt7nxGksJS9a0XdSaI4XvpExnNIgRP+rVefWh5U7BL8pPuir6SJUmFKRfjWQ51OtWSzwOxhaH/EBWWc0A==
dependencies:
"@babel/template" "^7.18.6"
"@babel/types" "^7.18.9"

"@babel/helper-hoist-variables@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/helper-hoist-variables/-/helper-hoist-variables-7.18.6.tgz#d4d2c8fb4baeaa5c68b99cc8245c56554f926678"
Expand Down Expand Up @@ -175,20 +117,6 @@
"@babel/traverse" "^7.18.6"
"@babel/types" "^7.18.6"

"@babel/helper-module-transforms@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.18.9.tgz#5a1079c005135ed627442df31a42887e80fcb712"
integrity sha512-KYNqY0ICwfv19b31XzvmI/mfcylOzbLtowkw+mfvGPAQ3kfCnMLYbED3YecL5tPd8nAYFQFAd6JHp2LxZk/J1g==
dependencies:
"@babel/helper-environment-visitor" "^7.18.9"
"@babel/helper-module-imports" "^7.18.6"
"@babel/helper-simple-access" "^7.18.6"
"@babel/helper-split-export-declaration" "^7.18.6"
"@babel/helper-validator-identifier" "^7.18.6"
"@babel/template" "^7.18.6"
"@babel/traverse" "^7.18.9"
"@babel/types" "^7.18.9"

"@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":
version "7.14.5"
resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.14.5.tgz#5ac822ce97eec46741ab70a517971e443a70c5a9"
Expand Down Expand Up @@ -227,15 +155,6 @@
"@babel/traverse" "^7.18.6"
"@babel/types" "^7.18.6"

"@babel/helpers@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/helpers/-/helpers-7.18.9.tgz#4bef3b893f253a1eced04516824ede94dcfe7ff9"
integrity sha512-Jf5a+rbrLoR4eNdUmnFu8cN5eNJT6qdTdOg5IHIzq87WwyRw9PwguLFOWYgktN/60IP4fgDUawJvs7PjQIzELQ==
dependencies:
"@babel/template" "^7.18.6"
"@babel/traverse" "^7.18.9"
"@babel/types" "^7.18.9"

"@babel/highlight@^7.18.6":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.18.6.tgz#81158601e93e2563795adcbfbdf5d64be3f2ecdf"
Expand All @@ -250,11 +169,6 @@
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.6.tgz#845338edecad65ebffef058d3be851f1d28a63bc"
integrity sha512-uQVSa9jJUe/G/304lXspfWVpKpK4euFLgGiMQFOCpM/bgcAdeoHwi/OQz23O9GK2osz26ZiXRRV9aV+Yl1O8tw==

"@babel/parser@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.9.tgz#f2dde0c682ccc264a9a8595efd030a5cc8fd2539"
integrity sha512-9uJveS9eY9DJ0t64YbIBZICtJy8a5QrDEVdiLCG97fVLpDTpGX7t8mMSb6OWw6Lrnjqj4O8zwjELX3dhoMgiBg==

"@babel/plugin-syntax-async-generators@^7.8.4":
version "7.8.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-async-generators/-/plugin-syntax-async-generators-7.8.4.tgz#a983fb1aeb2ec3f6ed042a210f640e90e786fe0d"
Expand Down Expand Up @@ -378,22 +292,6 @@
debug "^4.1.0"
globals "^11.1.0"

"@babel/traverse@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.18.9.tgz#deeff3e8f1bad9786874cb2feda7a2d77a904f98"
integrity sha512-LcPAnujXGwBgv3/WHv01pHtb2tihcyW1XuL9wd7jqh1Z8AQkTd+QVjMrMijrln0T7ED3UXLIy36P9Ao7W75rYg==
dependencies:
"@babel/code-frame" "^7.18.6"
"@babel/generator" "^7.18.9"
"@babel/helper-environment-visitor" "^7.18.9"
"@babel/helper-function-name" "^7.18.9"
"@babel/helper-hoist-variables" "^7.18.6"
"@babel/helper-split-export-declaration" "^7.18.6"
"@babel/parser" "^7.18.9"
"@babel/types" "^7.18.9"
debug "^4.1.0"
globals "^11.1.0"

"@babel/types@^7.0.0", "@babel/types@^7.18.6", "@babel/types@^7.3.0", "@babel/types@^7.3.3":
version "7.18.6"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.6.tgz#5d781dd10a3f0c9f1f931bd19de5eb26ec31acf0"
Expand All @@ -402,14 +300,6 @@
"@babel/helper-validator-identifier" "^7.18.6"
to-fast-properties "^2.0.0"

"@babel/types@^7.18.9":
version "7.18.9"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.9.tgz#7148d64ba133d8d73a41b3172ac4b83a1452205f"
integrity sha512-WwMLAg2MvJmt/rKEVQBBhIVffMmnilX4oe0sRe7iPOHIGsqpruFHHdrfj4O1CMMtgMtCU4oPafZjDPCRgO57Wg==
dependencies:
"@babel/helper-validator-identifier" "^7.18.6"
to-fast-properties "^2.0.0"

"@bcoe/v8-coverage@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
Expand Down Expand Up @@ -840,15 +730,6 @@
"@jridgewell/sourcemap-codec" "^1.4.10"
"@jridgewell/trace-mapping" "^0.3.9"

"@jridgewell/gen-mapping@^0.3.2":
version "0.3.2"
resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.2.tgz#c1aedc61e853f2bb9f5dfe6d4442d3b565b253b9"
integrity sha512-mh65xKQAzI6iBcFzwv28KVWSmCkdRBWoOh+bYQGW3+6OZvbbN3TqMGo5hqYxQniRcH9F2VZIoJCm4pa3BPDK/A==
dependencies:
"@jridgewell/set-array" "^1.0.1"
"@jridgewell/sourcemap-codec" "^1.4.10"
"@jridgewell/trace-mapping" "^0.3.9"

"@jridgewell/resolve-uri@^3.0.3":
version "3.0.7"
resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.0.7.tgz#30cd49820a962aff48c8fffc5cd760151fca61fe"
Expand All @@ -859,11 +740,6 @@
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.1.tgz#36a6acc93987adcf0ba50c66908bd0b70de8afea"
integrity sha512-Ct5MqZkLGEXTVmQYbGtx9SVqD2fqwvdubdps5D3djjAkgkKwT918VNOz65pEHFaYTeWcukmJmH5SwsA9Tn2ObQ==

"@jridgewell/set-array@^1.0.1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@jridgewell/set-array/-/set-array-1.1.2.tgz#7c6cf998d6d20b914c0a55a91ae928ff25965e72"
integrity sha512-xnkseuNADM0gt2bs+BvhO0p78Mk762YnZdsuzFV018NoG1Sj1SCQvpSqa7XUaTam5vAGasABV9qXASMKnFMwMw==

"@jridgewell/sourcemap-codec@^1.4.10":
version "1.4.13"
resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.13.tgz#b6461fb0c2964356c469e115f504c95ad97ab88c"
Expand Down

0 comments on commit e096322

Please sign in to comment.