Skip to content

Commit

Permalink
fix(ruleset-bundler): improve package import heuristics (#1985)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Dec 10, 2021
1 parent d5a7560 commit 5c013e9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
5 changes: 2 additions & 3 deletions packages/ruleset-bundler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rollup, Plugin } from 'rollup';
import { isURL } from '@stoplight/path';
import { isValidPackageName } from './utils/isValidPackageName';
import { isPackageImport } from './utils/isPackageImport';

export type BundleOptions = {
plugins: Plugin[];
Expand Down Expand Up @@ -33,8 +33,7 @@ export async function bundleRuleset(
: target === 'browser'
? id => isURL(id)
: (id, importer) =>
id.startsWith('node:') ||
(!isURL(id) && isValidPackageName(id) && (importer === void 0 || !isURL(importer))),
id.startsWith('node:') || (!isURL(id) && isPackageImport(id) && (importer === void 0 || !isURL(importer))),
});

return (await bundle.generate({ format: format ?? (target === 'runtime' ? 'iife' : 'esm'), exports: 'auto' }))
Expand Down
6 changes: 2 additions & 4 deletions packages/ruleset-bundler/src/plugins/skypack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Plugin } from 'rollup';
import { isValidPackageName } from '../utils/isValidPackageName';
import { isPackageImport } from '../utils/isPackageImport';
import { isURL } from '@stoplight/path';

const DATA_URIS = /^(?:data|node|file):/;
Expand All @@ -9,9 +9,7 @@ export const skypack = (): Plugin => ({
resolveId(id) {
if (DATA_URIS.test(id) || isURL(id)) return;

const path = id.split('/');

if (path.length > 0 && isValidPackageName(path[0])) {
if (isPackageImport(id)) {
return `https://cdn.skypack.dev/${id}`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isPackageImport } from '../isPackageImport';

describe('isPackageImport util', () => {
it.each([
'nimma/legacy',
'nimma',
'lodash',
'lodash/get',
'lodash/get.js',
'@stoplight/path',
'@stoplight/spectral-core',
'@stoplight/spectral-core/dist/file.js',
])('given valid %s package import, should return true', input => {
expect(isPackageImport(input)).toBe(true);
});

it.each(['', '/nimma/legacy', 'path', 'https://cdn.skypack.dev/@stoplight/spectral-core'])(
'given invalid %s import, should return false',
input => {
expect(isPackageImport(input)).toBe(false);
},
);
});
12 changes: 12 additions & 0 deletions packages/ruleset-bundler/src/utils/isPackageImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as validate from 'validate-npm-package-name';

const isValidPackageName = (packageName: string): boolean => validate(packageName).validForNewPackages;

export const isPackageImport = (packageName: string): boolean => {
const fragments = packageName.split('/');
if (packageName.startsWith('@') && fragments.length >= 2) {
fragments.splice(0, 2, `${fragments[0]}/${fragments[1]}`);
}

return fragments.every(isValidPackageName);
};
3 changes: 0 additions & 3 deletions packages/ruleset-bundler/src/utils/isValidPackageName.ts

This file was deleted.

0 comments on commit 5c013e9

Please sign in to comment.