Skip to content

Commit

Permalink
feat(ruleset-bundler): skypack plugin accepts ignore list (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Oct 24, 2022
1 parent caedff1 commit 6e6d0de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
34 changes: 34 additions & 0 deletions packages/ruleset-bundler/src/plugins/__tests__/skypack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,38 @@ fs.writeFileSync(path.join(__dirname, './output.js'), 'export default {}');
`);
});
});

it('should respect ignore list', async () => {
serveAssets({
'/tmp/input.js': `import { createRulesetFunction } from '@stoplight/spectral-core/ruleset/validation';
import { parse } from '@stoplight/yaml';
import { isPlainObject } from '@stoplight/json';
export default createRulesetFunction({}, input => {
assert.ok(isPlainObject(parse(input)));
})
`,
});

const code = await bundleRuleset('/tmp/input.js', {
target: 'browser',
plugins: [
skypack({
ignoreList: [/^@stoplight\/spectral-/, '@stoplight/json'],
}),
virtualFs(io),
],
});

expect(code).toEqual(`import { createRulesetFunction } from '@stoplight/spectral-core/ruleset/validation';
import { parse } from 'https://cdn.skypack.dev/@stoplight/yaml';
import { isPlainObject } from '@stoplight/json';
var input = createRulesetFunction({}, input => {
assert.ok(isPlainObject(parse(input)));
});
export { input as default };
`);
});
});
26 changes: 16 additions & 10 deletions packages/ruleset-bundler/src/plugins/skypack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ import { isURL } from '@stoplight/path';

const DATA_URIS = /^(?:data|node|file):/;

export const skypack = (): Plugin => ({
name: '@stoplight-spectral/skypack',
resolveId(id) {
if (DATA_URIS.test(id) || isURL(id)) return;
export const skypack = (opts?: { ignoreList?: (string | RegExp)[] }): Plugin => {
return <Plugin>{
name: '@stoplight-spectral/skypack',
resolveId(id) {
if (DATA_URIS.test(id) || isURL(id)) return;

if (isPackageImport(id)) {
return `https://cdn.skypack.dev/${id}`;
}
const isIgnored =
opts?.ignoreList !== void 0 &&
opts.ignoreList.some(ignored => (typeof ignored === 'string' ? ignored === id : ignored.test(id)));

return;
},
});
if (!isIgnored && isPackageImport(id)) {
return `https://cdn.skypack.dev/${id}`;
}

return;
},
};
};

0 comments on commit 6e6d0de

Please sign in to comment.