Skip to content

Commit

Permalink
add new reverse-exports package
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Oct 26, 2023
1 parent 3934dea commit 79e377b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
/packages/vite/index.d.ts
/packages/vite/**/*.js
/packages/vite/**/*.d.ts
/packages/reverse-exports/**/*.js
/packages/reverse-exports/**/*.d.ts


# unconventional js
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
/packages/vite/index.mjs
/packages/vite/**/*.js
/packages/vite/**/*.d.ts
/packages/reverse-exports/**/*.js
/packages/reverse-exports/**/*.d.ts

# unconventional js
/blueprints/*/files/
Expand Down
7 changes: 7 additions & 0 deletions packages/reverse-exports/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules
/src/**/*.js
/src/**/*.d.ts
/src/**/*.map
/tests/**/*.js
/tests/**/*.d.ts
/tests/**/*.map
4 changes: 4 additions & 0 deletions packages/reverse-exports/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/*.test.js'],
};
14 changes: 14 additions & 0 deletions packages/reverse-exports/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "reverse-exports",
"version": "0.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
}
}
14 changes: 14 additions & 0 deletions packages/reverse-exports/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { posix } from 'path';

export default function reversePackageExports(
packageJSON: { exports?: any; name: string },
relativePath: string
): string {
// TODO add an actual matching system and don't just look for the default
if (packageJSON.exports?.['./*'] === './dist/*.js') {
return posix.join(packageJSON.name, relativePath.replace(/^.\/dist\//, `./`).replace(/\.js$/, ''));
}

// TODO figure out what the result should be if it doesn't match anything in exports
return posix.join(packageJSON.name, relativePath);
}
22 changes: 22 additions & 0 deletions packages/reverse-exports/tests/reverse-exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import reversePackageExports from '../src';

describe('reverse exports', function () {
it('correctly reversed exports', function () {
// TODO figure out what the result should be if it doesn't match anything in exports
expect(reversePackageExports({ name: 'best-addon' }, './dist/_app_/components/face.js')).toBe(
'best-addon/dist/_app_/components/face.js'
);

expect(
reversePackageExports(
{
name: 'best-addon',
exports: {
'./*': './dist/*.js',
},
},
'./dist/_app_/components/face.js'
)
).toBe('best-addon/_app_/components/face');
});
});

0 comments on commit 79e377b

Please sign in to comment.