mirror of
https://github.com/withastro/astro.git
synced 2025-03-10 23:01:26 -05:00
feat: add resolveEntrypoint utility
This commit is contained in:
parent
60dc8da71b
commit
1cf1198941
4 changed files with 78 additions and 4 deletions
|
@ -29,10 +29,15 @@
|
|||
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
|
||||
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
||||
"postbuild": "astro-scripts copy \"src/**/*.js\"",
|
||||
"dev": "astro-scripts dev \"src/**/*.ts\""
|
||||
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
||||
"test": "mocha --exit --timeout 20000"
|
||||
},
|
||||
"devDependencies": {
|
||||
"astro-scripts": "workspace:*"
|
||||
"@types/chai": "^4.3.10",
|
||||
"@types/mocha": "^10.0.4",
|
||||
"astro-scripts": "workspace:*",
|
||||
"chai": "^4.3.7",
|
||||
"mocha": "^10.2.0"
|
||||
},
|
||||
"keywords": [
|
||||
"astro",
|
||||
|
|
|
@ -56,6 +56,23 @@ export function isRelativePath(path: string) {
|
|||
return startsWithDotDotSlash(path) || startsWithDotSlash(path);
|
||||
}
|
||||
|
||||
function createRootRelativePath(value: URL, root: URL) {
|
||||
return '/' + removeLeadingForwardSlash(value.toString().replace(root.toString(), ''));
|
||||
}
|
||||
|
||||
export function resolveEntrypoint(entrypoint: string | URL, root: URL) {
|
||||
if (entrypoint instanceof URL) {
|
||||
return createRootRelativePath(entrypoint, root);
|
||||
}
|
||||
if (startsWithForwardSlash(entrypoint)) {
|
||||
return entrypoint;
|
||||
}
|
||||
if (isRelativePath(entrypoint)) {
|
||||
return createRootRelativePath(new URL(entrypoint, root), root);
|
||||
}
|
||||
return entrypoint;
|
||||
}
|
||||
|
||||
function isString(path: unknown): path is string {
|
||||
return typeof path === 'string' || path instanceof String;
|
||||
}
|
||||
|
|
41
packages/internal-helpers/test/path.test.js
Normal file
41
packages/internal-helpers/test/path.test.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { resolveEntrypoint } from '../dist/path.js';
|
||||
import { expect } from 'chai';
|
||||
|
||||
describe('resolveEntrypoint', () => {
|
||||
it('handles fully resolved URLs', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = new URL('./src/item.ts', root);
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal('/src/item.ts');
|
||||
})
|
||||
it('handles relative paths', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = './src/item.ts';
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal('/src/item.ts');
|
||||
})
|
||||
it('handles root-relative specifiers', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = '/src/item.ts';
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal('/src/item.ts');
|
||||
})
|
||||
it('handles virtual modules', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = 'virtual:test-entrypoint';
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal(entrypoint);
|
||||
})
|
||||
it('handles fully resolved node_modules paths', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = new URL('file://test/path/usr/node_modules/@astrojs/preact/dist/server.js');
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal('/node_modules/@astrojs/preact/dist/server.js');
|
||||
})
|
||||
it('handles package specifier', () => {
|
||||
const root = new URL('file://test/path/usr/');
|
||||
const entrypoint = '@astrojs/preact/server';
|
||||
const output = resolveEntrypoint(entrypoint, root);
|
||||
expect(output).to.equal(entrypoint);
|
||||
})
|
||||
});
|
15
pnpm-lock.yaml
generated
15
pnpm-lock.yaml
generated
|
@ -4871,9 +4871,21 @@ importers:
|
|||
|
||||
packages/internal-helpers:
|
||||
devDependencies:
|
||||
'@types/chai':
|
||||
specifier: ^4.3.10
|
||||
version: 4.3.10
|
||||
'@types/mocha':
|
||||
specifier: ^10.0.4
|
||||
version: 10.0.4
|
||||
astro-scripts:
|
||||
specifier: workspace:*
|
||||
version: link:../../scripts
|
||||
chai:
|
||||
specifier: ^4.3.7
|
||||
version: 4.3.10
|
||||
mocha:
|
||||
specifier: ^10.2.0
|
||||
version: 10.2.0
|
||||
|
||||
packages/markdown/remark:
|
||||
dependencies:
|
||||
|
@ -7308,12 +7320,11 @@ packages:
|
|||
/@types/chai-subset@1.3.4:
|
||||
resolution: {integrity: sha512-CCWNXrJYSUIojZ1149ksLl3AN9cmZ5djf+yUoVVV+NuYrtydItQVlL2ZDqyC6M6O9LWRnVf8yYDxbXHO2TfQZg==}
|
||||
dependencies:
|
||||
'@types/chai': 4.3.9
|
||||
'@types/chai': 4.3.10
|
||||
dev: false
|
||||
|
||||
/@types/chai@4.3.10:
|
||||
resolution: {integrity: sha512-of+ICnbqjmFCiixUnqRulbylyXQrPqIGf/B3Jax1wIF3DvSheysQxAWvqHhZiW3IQrycvokcLcFQlveGp+vyNg==}
|
||||
dev: true
|
||||
|
||||
/@types/chai@4.3.9:
|
||||
resolution: {integrity: sha512-69TtiDzu0bcmKQv3yg1Zx409/Kd7r0b5F1PfpYJfSHzLGtB53547V4u+9iqKYsTu/O2ai6KTb0TInNpvuQ3qmg==}
|
||||
|
|
Loading…
Add table
Reference in a new issue