Repository navigation
feat: add exports to package.json and expose eslint-plugin-vue/types
#3252
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
c8d4d4c
54d8cc7
a67accd
a4033ab
eaceea4
3dde9a2
56d88d4
15e1e37
7276104
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'eslint-plugin-vue': minor | ||
| --- | ||
|
|
||
| Added an `exports` map to `package.json`. It adds the `eslint-plugin-vue/types` entry with the generated lint rule types (also resolved with `moduleResolution: node`), while deep imports of files in `dist` keep working with or without the `.js` extension, including directories like `eslint-plugin-vue/dist/utils`. Paths outside `dist` can no longer be imported. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * Entry points of the built package, resolved through `package.json#exports`. | ||
| * https://github.com/vuejs/eslint-plugin-vue/issues/3010 | ||
| */ | ||
| import { strict as assert } from 'node:assert' | ||
| import fs from 'node:fs' | ||
| import { createRequire } from 'node:module' | ||
| import path from 'node:path' | ||
| import ts from 'typescript' | ||
|
|
||
| const ROOT = path.resolve(__dirname, '../..') | ||
| // Resolving the package's own name from inside it goes through its `exports`. | ||
| const require = createRequire(path.join(ROOT, 'package.json')) | ||
|
|
||
| describe('package.json exports', () => { | ||
| it.each([ | ||
| ['eslint-plugin-vue', 'dist/index.js'], | ||
| ['eslint-plugin-vue/package.json', 'package.json'], | ||
| ['eslint-plugin-vue/dist/index.js', 'dist/index.js'], | ||
| ['eslint-plugin-vue/dist/utils/index.js', 'dist/utils/index.js'], | ||
| ['eslint-plugin-vue/dist/utils/index', 'dist/utils/index.js'], | ||
| ['eslint-plugin-vue/dist/utils', 'dist/utils/index.js'], | ||
| ['eslint-plugin-vue/dist/utils/casing', 'dist/utils/casing.js'], | ||
| ['eslint-plugin-vue/dist/processor', 'dist/processor.js'], | ||
| ['eslint-plugin-vue/dist/configs/flat/base', 'dist/configs/flat/base.js'], | ||
| ['eslint-plugin-vue/dist/eslint-typegen.d.ts', 'dist/eslint-typegen.d.ts'] | ||
| ])('should resolve %s', (specifier, expected) => { | ||
| assert.strictEqual(require.resolve(specifier), path.join(ROOT, expected)) | ||
| }) | ||
|
|
||
| it('should resolve every `dist` directory to its `index.js`', () => { | ||
| const indexFiles = fs | ||
| .readdirSync(path.join(ROOT, 'dist'), { recursive: true }) | ||
| .map((file) => `dist/${String(file).replaceAll('\\', '/')}`) | ||
| .filter((file) => path.posix.basename(file) === 'index.js') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why replace
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The conversion is only needed for the specifier: |
||
| assert.ok(indexFiles.includes('dist/configs/index.js')) | ||
|
|
||
| for (const indexFile of indexFiles) { | ||
| const specifier = `eslint-plugin-vue/${path.posix.dirname(indexFile)}` | ||
| assert.strictEqual( | ||
| require.resolve(specifier), | ||
| path.join(ROOT, indexFile), | ||
| specifier | ||
| ) | ||
| } | ||
| }) | ||
|
|
||
| it('should not expose `eslint-plugin-vue/types` at runtime', () => { | ||
| assert.throws(() => require.resolve('eslint-plugin-vue/types'), { | ||
| code: 'ERR_PACKAGE_PATH_NOT_EXPORTED' | ||
| }) | ||
| }) | ||
|
|
||
| describe.each([ | ||
| ['node16', ts.ModuleResolutionKind.Node16, ts.ModuleKind.Node16], | ||
| ['nodenext', ts.ModuleResolutionKind.NodeNext, ts.ModuleKind.NodeNext], | ||
| ['bundler', ts.ModuleResolutionKind.Bundler, ts.ModuleKind.ESNext] | ||
| ])( | ||
| 'with TypeScript moduleResolution %s', | ||
| (_name, moduleResolution, module) => { | ||
| it.each([ | ||
| ['eslint-plugin-vue', 'dist/index.d.ts'], | ||
| ['eslint-plugin-vue/types', 'dist/eslint-typegen.d.ts'], | ||
| ['eslint-plugin-vue/dist/eslint-typegen', 'dist/eslint-typegen.d.ts'], | ||
| ['eslint-plugin-vue/dist/eslint-typegen.js', 'dist/eslint-typegen.d.ts'] | ||
| ])('should resolve the types of %s', (specifier, expected) => { | ||
| const { resolvedModule } = ts.resolveModuleName( | ||
| specifier, | ||
| path.join(ROOT, 'tests/lib/package-exports.test.ts'), | ||
| { module, moduleResolution }, | ||
| ts.sys | ||
| ) | ||
| assert.strictEqual( | ||
| resolvedModule && path.resolve(resolvedModule.resolvedFileName), | ||
| path.join(ROOT, expected) | ||
| ) | ||
| }) | ||
| } | ||
| ) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.