Skip to content
Open
5 changes: 5 additions & 0 deletions .changeset/twelve-weeks-rhyme.md
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.
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@
"description": "Official ESLint plugin for Vue.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./types": {
"types": "./dist/eslint-typegen.d.ts"
},
"./dist": "./dist/index.js",
"./dist/configs": "./dist/configs/index.js",
"./dist/utils": "./dist/utils/index.js",
Comment thread
FloEdelmann marked this conversation as resolved.
Resolved
"./dist/utils/style-variables": "./dist/utils/style-variables/index.js",
"./dist/utils/ts-utils": "./dist/utils/ts-utils/index.js",
"./dist/*.d.ts": "./dist/*.d.ts",
"./dist/*.js": "./dist/*.js",
"./dist/*": "./dist/*.js",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"types": [
"./dist/eslint-typegen.d.ts"
]
}
},
"scripts": {
"new": "node tools/new-rule.ts",
"start": "npm run test:base -- --watch",
Expand Down
80 changes: 80 additions & 0 deletions tests/lib/package-exports.test.ts
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')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why replace \ with / and then use .posix instead of just relying on path.basename itself?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion is only needed for the specifier: require.resolve() wants /, while readdirSync() returns \ on Windows. Simplified it so the filter uses path.basename() and only the specifier is converted.

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)
)
})
}
)
})
Loading