Skip to content
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

Fix lint for string exports #448

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ export function lint(pkg: PackageMetadata) {
}
}
} else {
// Validate CJS package
if (main && path.extname(main) === '.mjs') {
state.badMainExtension = true
}
// Validate CJS package
if (exports) {
if (typeof exports === 'string') {
if (!hasCjsExtension(exports)) {
if (path.extname(exports) === '.mjs') {
state.badMainExport = true
}
}
Expand Down Expand Up @@ -127,9 +127,15 @@ export function lint(pkg: PackageMetadata) {
)
}
if (state.badMainExport) {
logger.warn(
'Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed',
)
if (isESM) {
logger.warn(
'Cannot export `exports` field with .cjs extension in ESM package, only .mjs and .js extensions are allowed',
)
} else {
logger.warn(
'Cannot export `exports` field with .mjs extension in CJS package, only .js and .cjs extensions are allowed',
)
}
}

if (state.invalidExportsFieldType) {
Expand Down
13 changes: 4 additions & 9 deletions test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const testCases: {
},
{
name: 'externals',
args: ['index.js', '-o', './dist/index.js'],
async expected(dir) {
const distFile = join(dir, './dist/index.js')
const content = await fsp.readFile(distFile, { encoding: 'utf-8' })
Expand All @@ -55,7 +54,6 @@ const testCases: {
},
{
name: 'duplicate-entry',
args: [],
async expected(dir, { stdout }) {
const distFiles = [
'dist/index.js',
Expand All @@ -72,7 +70,6 @@ const testCases: {
},
{
name: 'ts-error',
args: ['index.ts', '-o', './dist/index.js'],
async expected(dir, { stdout, stderr }) {
const distFile = join(dir, './dist/index.js')
expect(stderr).toMatch(/Could not load TypeScript compiler/)
Expand All @@ -89,16 +86,14 @@ const testCases: {
},
},
{
name: 'no-ts-require-for-js',
args: ['index.js', '-o', './dist/index.js'],
name: 'js-only',
async expected(dir) {
const distFile = join(dir, './dist/index.js')
expect(await existsFile(distFile)).toBe(true)
},
},
{
name: 'entry-index-index',
args: [],
async expected(dir) {
await assertFilesContent(dir, {
'./dist/index.js': /'index'/,
Expand All @@ -108,7 +103,6 @@ const testCases: {
},
{
name: 'pkg-exports',
args: ['index.js'],
async expected(dir) {
const distFiles = [
'dist/index.cjs',
Expand Down Expand Up @@ -240,8 +234,7 @@ const testCases: {
},
{
name: 'single-entry',
args: [],
async expected(dir, { stdout }) {
async expected(dir, { stdout, stderr }) {
const distFiles = [
join(dir, './dist/index.js'),
join(dir, './dist/index.d.ts'),
Expand All @@ -260,6 +253,8 @@ const testCases: {
dist/index.d.ts
dist/index.js`

expect(stderr).not.toContain('Cannot export `exports` field with')

const rawStdout = stripANSIColor(stdout)
log.split('\n').forEach((line: string) => {
expect(rawStdout).toContain(line.trim())
Expand Down
3 changes: 3 additions & 0 deletions test/integration/js-only/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exports": "./dist/index.js"
}
Loading