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

Copy types for ES #15

Merged
merged 1 commit into from
Aug 31, 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
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
"module": "./dist/es/index.js",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/es/index.js",
"require": "./dist/cjs/index.js"
"import": {
"types": "./dist/types/index.d.mts",
"default": "./dist/es/index.js"
},
"require": {
"types": "./dist/types/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"types": "./dist/index.d.ts",
"types": "./dist/types/index.d.ts",
"files": [
"dist"
],
Expand Down
39 changes: 36 additions & 3 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import { copyFile, lstat, readdir, writeFile } from 'fs';
import { dirname, parse } from 'path';

import typescript from '@rollup/plugin-typescript';
import terser from '@rollup/plugin-terser';
Expand Down Expand Up @@ -31,6 +32,38 @@ export default defineConfig({
clean('dist'),
typescript({ sourceMap: local }),
terser(),
(() => ({
name: 'copyDeclarationFiles',
writeBundle: {
sequential: true,
order: 'post',
handler: ({ format }) => {
if (format === 'cjs') return;

readdir('./dist/types', { recursive: true }, (err, paths) => {
if (err) return console.error(err);

for (const path of paths) {
lstat(`./dist/types/${path}`, (err, stats) => {
if (err) return console.error(err);

if (stats.isFile()) {
const dir = dirname(path.toString()).replace(/^\./, '');

copyFile(
`./dist/types/${path}`,
`./dist/types/${dir ? `${dir}/` : dir}${parse(path.toString()).name}.mts`,
(err) => {
if (err) return console.error(err);
},
);
}
});
}
});
},
}
}))(),
(() => ({
name: 'createPackageFiles',
writeBundle: {
Expand All @@ -39,14 +72,14 @@ export default defineConfig({
handler: ({ format }) => {
switch (format) {
case 'cjs': {
fs.writeFile('./dist/cjs/package.json', '{\n\t"type": "commonjs"\n}', (err) => {
writeFile('./dist/cjs/package.json', '{\n\t"type": "commonjs"\n}', (err) => {
if (err) console.error(err);
});

break;
}
case 'es': {
fs.writeFile('./dist/es/package.json', '{\n\t"type": "module"\n}', (err) => {
writeFile('./dist/es/package.json', '{\n\t"type": "module"\n}', (err) => {
if (err) console.error(err);
});

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"skipLibCheck": true,
"strict": true,
"declaration": true,
"declarationDir": "./dist",
"declarationDir": "./dist/types",
"rootDir": "./src",
"outDir": "./dist"
},
Expand Down
Loading