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(cjs)!: remove .default suffix from d.ts and d.cts files #3750

Merged
merged 2 commits into from May 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/unocss/astro.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/index.d.ts

This file was deleted.

11 changes: 9 additions & 2 deletions packages/unocss/package.json
Expand Up @@ -94,9 +94,16 @@
},
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "index.d.ts",
"types": "dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./*"
]
}
},
"files": [
"*.d.ts",
"dist"
],
"engines": {
Expand Down
2 changes: 0 additions & 2 deletions packages/unocss/postcss.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-attributify.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-icons.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-mini.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-tagify.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-typography.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-uno.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-web-fonts.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/preset-wind.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/vite.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/unocss/webpack.d.ts

This file was deleted.

13 changes: 9 additions & 4 deletions scripts/dist-verify.ts
Expand Up @@ -3,21 +3,26 @@ import process from 'node:process'
import fg from 'fast-glob'

export async function verifyDist() {
const cjsFiles = await fg('packages/*/dist/**/*.cjs', {
const cjsFiles = await fg(['packages/*/dist/**/*.d.ts', 'packages/*/dist/**/*.d.cts'], {
ignore: ['**/node_modules/**'],
})
// const cjsFiles = await fg('packages/*/dist/**/*.cjs', {
// ignore: ['**/node_modules/**'],
// })

console.log(`${cjsFiles.length} cjs files found`)
console.log(`${cjsFiles.length} dts files found`)
// console.log(`${cjsFiles.length} cjs files found`)
console.log(cjsFiles.map(i => ` - ${i}`).join('\n'))

const forbidden = [
// Make sure no CJS is importing UnoCSS packages as they are ESM only
/require\(['"]@?unocss(\/core)?['"]\)/,
// Use `exports.default` instead, should be patched by postbuild.ts
'module.exports',
// 'module.exports',
]

const exportsDefault = 'exports.default'
const exportsDefault = 'as default'
// const exportsDefault = 'exports.default'

let error = false
await Promise.all(cjsFiles.map(async (file) => {
Expand Down
78 changes: 64 additions & 14 deletions scripts/postbuild.ts
Expand Up @@ -2,28 +2,78 @@ import { readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { verifyDist } from './dist-verify'

function patchCjs(cjsModulePath: string, name: string) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
cjsModule
.replace(`'use strict';`, `'use strict';Object.defineProperty(exports, '__esModule', {value: true});`)
.replace(`module.exports = ${name};`, `exports.default = ${name};`),
{ encoding: 'utf-8' },
)
const regexp = /export\s*\{\s*(.*)\s*};/

function parseExports(dtsModulePath: string, defaultExport: string, content: string) {
const exportAsDefault = `${defaultExport} as default`
const entries = content.split('\n').reduce((acc, line) => {
// skip LF if last line
if (acc.content.length && acc.content.at(-1) === '\n' && line.trim().length === 0)
return acc

const match = line.match(regexp)
if (match?.length && match[0].includes(exportAsDefault)) {
const exportsArray = match[1].split(',').map(e => e.trim())
const removeImport = `${defaultExport} as default`
// Filtering out the default export
const nonDefaultExports = exportsArray.filter(item => item !== removeImport).map(e => e.trim())
acc.matched = true
acc.content = nonDefaultExports.length > 0
? `${acc.content}\nexport = ${defaultExport};\nexport { ${nonDefaultExports.join(', ')} };\n`
: `${acc.content}\nexport = ${defaultExport};\n`
}
else {
// avoid adding LF at first line
acc.content = acc.content.length > 0
? `${acc.content}\n${line}`
: line
}

return acc
}, { content: '', matched: false })

if (entries.matched)
return entries.content
else
throw new Error(`UPPS, no match found for ${defaultExport} in ${dtsModulePath}!`)
}

function patchDefaultCjsExport(dtsModuleName: string, defaultExport: string = '_default') {
for (const path of [`${dtsModuleName}.d.ts`, `${dtsModuleName}.d.cts`]) {
writeFileSync(
path,
parseExports(path, defaultExport, readFileSync(path, 'utf-8')),
{ encoding: 'utf-8' },
)
}
}

function patchUnoCSSPostcssCjsExport(dtsModuleName: string) {
for (const path of [`${dtsModuleName}.d.ts`, `${dtsModuleName}.d.cts`]) {
const content = readFileSync(path, 'utf-8')
writeFileSync(
path,
content.replace('export { default } from \'@unocss/postcss\';', '\nexport = postcss;'),
{ encoding: 'utf-8' },
)
}
}

// @unocss/eslint-config
patchCjs(resolve('./packages/eslint-config/dist/flat.cjs'), 'flat')
patchCjs(resolve('./packages/eslint-config/dist/index.cjs'), 'index')
patchDefaultCjsExport(resolve('./packages/eslint-config/dist/flat'))
patchDefaultCjsExport(resolve('./packages/eslint-config/dist/index'))

// @unocss/eslint-plugin
patchCjs(resolve('./packages/eslint-plugin/dist/index.cjs'), 'index')
patchDefaultCjsExport(resolve('./packages/eslint-plugin/dist/index'))

// @unocss/postcss
patchCjs(resolve('./packages/postcss/dist/index.cjs'), 'unocss')
patchDefaultCjsExport(resolve('./packages/postcss/dist/index'), 'unocss')

// @unocss/webpack
patchDefaultCjsExport(resolve('./packages/webpack/dist/index'), 'WebpackPlugin')

// unocss
patchCjs(resolve('./packages/unocss/dist/postcss.cjs'), 'postcss__default')
patchUnoCSSPostcssCjsExport(resolve('./packages/unocss/dist/postcss'))
patchDefaultCjsExport(resolve('./packages/unocss/dist/webpack'), 'UnocssWebpackPlugin')

await verifyDist()