Skip to content

Commit

Permalink
fix!: remove .default suffix from d.ts and d.cts files
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 22, 2024
1 parent 3827444 commit 9c4de9e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 10 deletions.
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
68 changes: 62 additions & 6 deletions scripts/postbuild.ts
Expand Up @@ -2,7 +2,7 @@ import { readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { verifyDist } from './dist-verify'

function patchCjs(cjsModulePath: string, name: string) {
function _patchCjs(cjsModulePath: string, name: string) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
Expand All @@ -13,17 +13,73 @@ function patchCjs(cjsModulePath: string, name: string) {
)
}

function patchCjsDts(cjsModulePath: string, name: string, useName = false) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
cjsModule
.replace(`export { ${name} as default };`, `export = ${useName ? name : '_default'};`),
{ encoding: 'utf-8' },
)
}

function patchPostcssCjsDts(cjsModulePath: string) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
cjsModule
.replace(`export { type UnoPostcssPluginOptions, unocss as default };`, `export = unocss;\nexport { type UnoPostcssPluginOptions };`),
{ encoding: 'utf-8' },
)
}

function patchWebpackCjsDts(cjsModulePath: string) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
cjsModule
.replace(`export { type WebpackPluginOptions, WebpackPlugin as default, defineConfig };`, `export = WebpackPlugin;\nexport { type WebpackPluginOptions, defineConfig };`),
{ encoding: 'utf-8' },
)
}

function patchPostcss2CjsDts(cjsModulePath: string) {
const cjsModule = readFileSync(cjsModulePath, 'utf-8')
writeFileSync(
cjsModulePath,
cjsModule
.replace(`export { default } from '@unocss/postcss';`, `export = 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')
patchCjsDts(resolve('./packages/eslint-config/dist/flat.d.ts'), '_default')
patchCjsDts(resolve('./packages/eslint-config/dist/flat.d.cts'), '_default')
patchCjsDts(resolve('./packages/eslint-config/dist/index.d.ts'), '_default')
patchCjsDts(resolve('./packages/eslint-config/dist/index.d.cts'), '_default')
// patchCjs(resolve('./packages/eslint-config/dist/flat.cjs'), 'flat')
// patchCjs(resolve('./packages/eslint-config/dist/index.cjs'), 'index')

// @unocss/eslint-plugin
patchCjs(resolve('./packages/eslint-plugin/dist/index.cjs'), 'index')
patchCjsDts(resolve('./packages/eslint-plugin/dist/index.d.ts'), '_default')
patchCjsDts(resolve('./packages/eslint-plugin/dist/index.d.cts'), '_default')
// patchCjs(resolve('./packages/eslint-plugin/dist/index.cjs'), 'index')

// @unocss/postcss
patchCjs(resolve('./packages/postcss/dist/index.cjs'), 'unocss')
patchPostcssCjsDts(resolve('./packages/postcss/dist/index.d.ts'))
patchPostcssCjsDts(resolve('./packages/postcss/dist/index.d.cts'))
// patchCjs(resolve('./packages/postcss/dist/index.cjs'), 'unocss')

// @unocss/webpack
patchWebpackCjsDts(resolve('./packages/webpack/dist/index.d.ts'))
patchWebpackCjsDts(resolve('./packages/webpack/dist/index.d.cts'))

// unocss
patchCjs(resolve('./packages/unocss/dist/postcss.cjs'), 'postcss__default')
patchPostcss2CjsDts(resolve('./packages/unocss/dist/postcss.d.ts'))
patchPostcss2CjsDts(resolve('./packages/unocss/dist/postcss.d.cts'))
patchCjsDts(resolve('./packages/unocss/dist/webpack.d.ts'), 'UnocssWebpackPlugin', true)
patchCjsDts(resolve('./packages/unocss/dist/webpack.d.cts'), 'UnocssWebpackPlugin', true)
// patchCjs(resolve('./packages/unocss/dist/postcss.cjs'), 'postcss__default')

await verifyDist()

0 comments on commit 9c4de9e

Please sign in to comment.