Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[code-infra] Babel plugin to fully resolve imported paths #43294

Merged
merged 29 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e26e204
Babel plugin to fully resolve imported paths
Janpot Aug 14, 2024
a2e9fe3
Update babel.config.js
Janpot Aug 14, 2024
0cf1f75
Update babel.config.js
Janpot Aug 14, 2024
e3a9915
Update babel.config.js
Janpot Aug 14, 2024
d8a432e
fw
Janpot Aug 14, 2024
08785b3
improvements
Janpot Aug 14, 2024
399ffcc
Add types
Janpot Aug 14, 2024
a1cf4e8
Update babel.config.js
Janpot Aug 14, 2024
8ceb06b
Update babel.config.js
Janpot Aug 14, 2024
25bdc7d
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
d3574fd
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
e26fa02
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
70b8261
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
16bc6cd
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
3f5915c
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
73bb2ce
Update babel.config.js
Janpot Aug 14, 2024
ff2c258
Update babel.config.js
Janpot Aug 14, 2024
baa5247
Update babel.config.js
Janpot Aug 14, 2024
6406fdb
fixes
Janpot Aug 14, 2024
fed743d
win
Janpot Aug 14, 2024
ef2d0fc
Update babelPluginResolveRelativeImports.js
Janpot Aug 14, 2024
baa646d
Merge branch 'next' into babel-resolve-imports
Janpot Aug 16, 2024
7e2636d
Merge branch 'next' into babel-resolve-imports
Janpot Aug 19, 2024
0f21e37
move to internal package
Janpot Aug 21, 2024
7280c93
Update README.md
Janpot Aug 21, 2024
eb63fd3
Update ci.json
Janpot Aug 21, 2024
d2c6ef5
Update ci.json
Janpot Aug 21, 2024
28db8b7
Update ci.json
Janpot Aug 21, 2024
86fae26
Update ci.json
Janpot Aug 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
improvements
  • Loading branch information
Janpot committed Aug 14, 2024
commit 08785b320b5385419421b59a1895f22106bca388
2 changes: 1 addition & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module.exports = function getBabelConfig(api) {
// Make sure the aliasing happens before we resolve the imports to the actual file.
// This plugin will be enabled by overrides in certain environments.
['babel-plugin-module-resolver', false],
[importResolverPlugin, { outExtension: '.js' }],
[importResolverPlugin, useESModules ? { outExtension: '.js' } : false],
];

if (process.env.NODE_ENV === 'production') {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@
"@octokit/rest": "^20.1.1",
"@pigment-css/react": "0.0.20",
"@playwright/test": "1.46.0",
"@types/babel__core": "^7.20.5",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.17.7",
"@types/mocha": "^10.0.7",
"@types/node": "^18.19.44",
"@types/react": "^18.3.3",
"@types/resolve": "^1.20.6",
"@types/yargs": "^17.0.33",
"@typescript-eslint/eslint-plugin": "^7.16.1",
"@typescript-eslint/parser": "^7.16.1",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 29 additions & 3 deletions scripts/babelPluginResolveRelativeImports.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
/// <reference path="./resolve.d.ts" />
// @ts-check

const nodePath = require('path');
const resolve = require('resolve/sync');

/**
*
* @param {string} absolutePath
* @param {string} relativeTo
* @returns {string}
*/
function toRelativeImportSpecifier(absolutePath, relativeTo) {
const relative = nodePath.posix.relative(relativeTo, absolutePath);
return relative.startsWith('.') ? relative : `./${relative}`;
}

/**
* @typedef {import('@babel/core')} babel
*/
Expand All @@ -19,8 +33,20 @@ module.exports = function plugin({ types: t }, { outExtension = '.js' }) {
const cache = new Map();
return {
visitor: {
ImportDeclaration(path, state) {
const source = path.get('source');
ImportOrExportDeclaration(path, state) {
if (path.isExportDefaultDeclaration()) {
return;
}

const source =
/** @type {babel.NodePath<babel.types.StringLiteral | null | undefined> } */ (
path.get('source')
);

if (!source.node) {
return;
}

const importedPath = source.node.value;

if (!importedPath.startsWith('.')) {
Expand Down Expand Up @@ -51,7 +77,7 @@ module.exports = function plugin({ types: t }, { outExtension = '.js' }) {
nodePath.basename(resolvedPath, nodePath.extname(resolvedPath)) + outExtension,
);
// get relative path (posix style, because will be used as an import)
resolvedPath = nodePath.posix.relative(dir, resolvedPath);
resolvedPath = toRelativeImportSpecifier(resolvedPath, dir);
cache.set(absoluteImportPath, resolvedPath);
}

Expand Down
6 changes: 6 additions & 0 deletions scripts/resolve.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'resolve/sync' {
import { Opts } from 'resolve';

function resolve(id: string, options?: Opts): string;
export = resolve;
}
Loading