Skip to content

Commit

Permalink
chore: Update tools
Browse files Browse the repository at this point in the history
  • Loading branch information
nuintun committed Dec 18, 2024
1 parent a090a7a commit 3d2a125
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 421 deletions.
26 changes: 12 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
"sideEffects": false,
"main": "cjs/index.cjs",
"module": "esm/index.js",
"types": "types/index.d.ts",
"exports": {
"import": "./esm/index.js",
"require": "./cjs/index.cjs",
"types": "./types/index.d.ts"
"require": "./cjs/index.cjs"
},
"files": [
"cjs",
"esm",
"types"
"esm"
],
"repository": {
"type": "git",
Expand All @@ -39,23 +36,24 @@
"prepublishOnly": "npm run build",
"build:cjs": "rollup -c tools/rollup.cjs.js",
"build:esm": "rollup -c tools/rollup.esm.js",
"build:fix-types": "node tools/fix-types.js",
"format": "prettier --write . --ignore-path .prettierignore",
"build": "npm run clean && npm run build:main && npm run format",
"build:main": "npm run build:cjs && npm run build:esm && npm run build:types",
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir types && node tools/fix-types.js"
"build:main": "npm run build:cjs && npm run build:esm && npm run build:fix-types"
},
"dependencies": {
"tslib": "^2.8.1"
},
"devDependencies": {
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-typescript": "^12.1.1",
"@types/react": "^18.3.12",
"magic-string": "^0.30.14",
"prettier": "^3.4.1",
"@rollup/plugin-replace": "^6.0.2",
"@rollup/plugin-typescript": "^12.1.2",
"@types/react": "^18.3.17",
"dts-paths": "^1.0.1",
"magic-string": "^0.30.17",
"prettier": "^3.4.2",
"react": "^18.3.1",
"rimraf": "^6.0.1",
"rollup": "^4.28.0",
"tsc-alias": "^1.8.10",
"rollup": "^4.28.1",
"typescript": "^5.7.2"
},
"peerDependencies": {
Expand Down
554 changes: 180 additions & 374 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type { Item, Options, ScrollToItemOptions, ScrollToOptions, Virtual } fro
* @description [hook] 虚列表
* @param options 配置参数
*/
export default function useVirtual<T extends HTMLElement, U extends HTMLElement>(options: Options): Virtual<T, U> {
export function useVirtual<T extends HTMLElement, U extends HTMLElement>(options: Options): Virtual<T, U> {
const { size, count, horizontal } = options;

if (__DEV__) {
Expand Down
2 changes: 1 addition & 1 deletion tools/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

import { rimraf } from 'rimraf';

rimraf.sync(['cjs', 'esm', 'types']);
await rimraf(['cjs', 'esm']);
79 changes: 74 additions & 5 deletions tools/fix-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,78 @@
* @module fix-types
*/

import { replaceTscAliasPaths } from 'tsc-alias';
import { join } from 'node:path';
import { resolvePaths } from 'dts-paths';
import { readdir, rename } from 'node:fs/promises';

replaceTscAliasPaths({
verbose: true,
outDir: 'types'
});
/**
* @function renameDts
* @description Rename `.d.ts` files in a directory to a specified extension.
* @param {string} dir The target directory to process.
* @param {string} ext The new extension to replace `.d.ts`.
* @returns {Promise<void>}
*/
async function renameDts(dir, ext) {
// Stack to manage directories.
const stack = [dir];

while (stack.length > 0) {
const currentDir = stack.pop();

// Read items in the current directory.
const items = await readdir(currentDir, { withFileTypes: true });

// Collect all tasks for the current directory.
const tasks = items.map(async item => {
const sourcePath = join(currentDir, item.name);

if (item.isDirectory()) {
// If it's a directory, push it onto the stack.
stack.push(sourcePath);
} else if (item.isFile()) {
const re = /\.d\.ts$/i;

// If the file matches `.d.ts`, rename it.
if (re.test(sourcePath)) {
const targetPath = sourcePath.replace(re, ext);

await rename(sourcePath, targetPath);
}
}
});

// Process all tasks concurrently for the current directory.
await Promise.all(tasks);
}
}

renameDts('cjs', '.d.cts')
.then(async () => {
return Promise.all([
resolvePaths('cjs', {
compilerOptions: {
paths: {
'/*': ['cjs/*']
}
},
extensions: ['.d.cts']
}),
resolvePaths('esm', {
compilerOptions: {
paths: {
'/*': ['esm/*']
}
},
extensions: ['.d.ts']
})
]);
})
.then(
([cjs, esm]) => {
console.log(`fix cjs types: ${cjs.size} files`);
console.log(`fix esm types: ${esm.size} files`);
},
error => {
console.error(error);
}
);
4 changes: 2 additions & 2 deletions tools/plugins/tree-shake.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MagicString from 'magic-string';

/***
* @function treeShake
* @description Fixed tree shaking for typescript and rollup preserve modules
* @description Fixed tree shaking for typescript and rollup preserve modules.
* @return {import('rollup').Plugin}
*/
export default function treeShake() {
Expand All @@ -16,7 +16,7 @@ export default function treeShake() {
const files = Object.entries(bundle);

for (const [, file] of files) {
if (!file.isAsset) {
if (file.type !== 'asset') {
const code = new MagicString(file.code);

this.parse(file.code, {
Expand Down
34 changes: 12 additions & 22 deletions tools/rollup.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
* @module rollup.base
*/

import replace from '@rollup/plugin-replace';
import treeShake from './plugins/tree-shake.js';
import typescript from '@rollup/plugin-typescript';
import { createRequire, isBuiltin } from 'node:module';

const pkg = createRequire(import.meta.url)('../package.json');

const externals = [
// Dependencies
// Dependencies.
...Object.keys(pkg.dependencies || {}),
// Peer dependencies
// Peer dependencies.
...Object.keys(pkg.peerDependencies || {})
];

Expand All @@ -26,22 +25,9 @@ const banner = `/**
*/
`;

/**
* @function env
* @return {import('rollup').Plugin}
*/
function env() {
return replace({
preventAssignment: true,
values: {
__DEV__: `process.env.NODE_ENV !== 'production'`
}
});
}

/**
* @function rollup
* @param {boolean} [esnext]
* @param {boolean} [esnext] Is esnext.
* @return {import('rollup').RollupOptions}
*/
export default function rollup(esnext) {
Expand All @@ -50,16 +36,20 @@ export default function rollup(esnext) {
output: {
banner,
interop: 'auto',
exports: 'auto',
esModule: false,
preserveModules: true,
dir: esnext ? 'esm' : 'cjs',
format: esnext ? 'esm' : 'cjs',
generatedCode: { constBindings: true },
entryFileNames: `[name].${esnext ? 'js' : 'cjs'}`,
chunkFileNames: `[name].${esnext ? 'js' : 'cjs'}`
chunkFileNames: `[name].${esnext ? 'js' : 'cjs'}`,
entryFileNames: `[name].${esnext ? 'js' : 'cjs'}`
},
plugins: [env(), typescript(), treeShake()],
plugins: [
typescript({
declaration: true,
declarationDir: esnext ? 'esm' : 'cjs'
}),
treeShake()
],
onwarn(error, warn) {
if (error.code !== 'CIRCULAR_DEPENDENCY') {
warn(error);
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"noUnusedLocals": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "Node",
"noImplicitOverride": true,
"noUnusedParameters": true,
"moduleResolution": "bundler",
"forceConsistentCasingInFileNames": true,
"paths": {
"/*": [
Expand All @@ -27,6 +27,6 @@
"global.d.ts"
],
"exclude": [
"node_modules/**/*"
"node_modules"
]
}

0 comments on commit 3d2a125

Please sign in to comment.