Skip to content

Commit

Permalink
Replace @gilbarbara/esbuilder with tsup
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbarbara committed Oct 21, 2023
1 parent eb6c937 commit 7956ded
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 14 deletions.
16 changes: 14 additions & 2 deletions packages/tree-changes-hook/package.json
Expand Up @@ -48,8 +48,8 @@
"react-dom": "^18.2.0"
},
"scripts": {
"build": "npm run clean && esbuilder --cjs --esm && npm run build:types",
"build:types": "tsc --emitDeclarationOnly",
"build": "npm run clean && tsup && ts-node scripts/fix-cjs.ts",
"watch": "tsup --watch",
"clean": "del dist/*",
"lint": "eslint src test",
"test": "jest",
Expand All @@ -61,6 +61,18 @@
"size": "size-limit",
"prepublishOnly": "npm run validate"
},
"tsup": {
"dts": true,
"entry": [
"src/index.ts"
],
"format": [
"cjs",
"esm"
],
"sourcemap": true,
"splitting": false
},
"eslintConfig": {
"extends": [
"@gilbarbara/eslint-config"
Expand Down
65 changes: 65 additions & 0 deletions packages/tree-changes-hook/scripts/fix-cjs.ts
@@ -0,0 +1,65 @@
// eslint-disable-next-line import/no-relative-packages
import { replace, type ReplaceOptions } from '../../../scripts/replace';

export const fixCjsDts = async (options?: Partial<ReplaceOptions>) => {
return replace({
files: '**/*.d.{ts,cts}',
...options,
name: 'fix-cjs-dts',

callback: content => {
const { imp } = /(?<imp>import .+ from 'tree-changes';)/u.exec(content)?.groups ?? {};
const { exp, named } =
/(?<exp>export (?<named>.+) from 'tree-changes';)/u.exec(content)?.groups ?? {};
const { exportDefault } =
/(?<exportDefault>export \{ useTreeChanges as default \};)/u.exec(content)?.groups ?? {};

if (imp) {
const statement = `declare namespace UseTreeChanges {
export ${named.replace('default as treeChanges', 'treeChanges')};
${exportDefault}
}
export = UseTreeChanges;`;

return (
content
.replace(imp, `import ${named} from 'tree-changes';`)
.replace(`\n${exp}`, '')
.replace(`\n${exportDefault}`, '') + statement
);
}

return false;
},
});
};

export const fixCjsExports = async (options?: Partial<ReplaceOptions>) => {
const statement = `
// fix-cjs-exports
if (module.exports.default) {
Object.assign(module.exports.default, module.exports);
module.exports = module.exports.default;
delete module.exports.default;
}
`;

return replace({
files: '**/*.js',
...options,
name: 'fix-cjs-exports',

callback: content => {
if (content.includes('module.exports = __toCommonJS') && !content.endsWith(statement)) {
return content + statement;
}

return false;
},
});
};

fixCjsDts();
fixCjsExports();
4 changes: 3 additions & 1 deletion packages/tree-changes-hook/src/index.ts
Expand Up @@ -4,8 +4,10 @@ import treeChanges, { Data, KeyType, TreeChanges } from 'tree-changes';

export default function useTreeChanges<T extends Data>(value: T) {
const previousValue = useRef(value);

const isEqual = equal(previousValue.current, value);
const previousIsEqual = useRef(isEqual);

const instance = useRef<TreeChanges<KeyType<T, typeof previousValue.current>>>(
treeChanges(previousValue.current, value),
);
Expand All @@ -24,4 +26,4 @@ export default function useTreeChanges<T extends Data>(value: T) {

// eslint-disable-next-line unicorn/prefer-export-from
export { treeChanges };
export * from 'tree-changes';
export type { Data, KeyType, TreeChanges, Value } from 'tree-changes';
8 changes: 5 additions & 3 deletions packages/tree-changes-hook/tsconfig.json
@@ -1,8 +1,10 @@
{
"extends": "@gilbarbara/tsconfig",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"paths": {
"tree-changes": ["./packages/tree-changes"]
}
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
"include": ["src/**/*"]
}
17 changes: 15 additions & 2 deletions packages/tree-changes/package.json
Expand Up @@ -35,8 +35,8 @@
"is-lite": "^1.2.0"
},
"scripts": {
"build": "npm run clean && esbuilder --cjs --esm && npm run build:types",
"build:types": "tsc --emitDeclarationOnly",
"build": "npm run clean && tsup && ts-node scripts/fix-cjs.ts",
"watch": "tsup --watch",
"clean": "del dist/*",
"lint": "eslint src test",
"test": "jest",
Expand All @@ -48,6 +48,19 @@
"size": "size-limit",
"prepublishOnly": "npm run validate"
},
"tsup": {
"cjsInterop": true,
"dts": true,
"entry": [
"src/index.ts"
],
"format": [
"cjs",
"esm"
],
"sourcemap": true,
"splitting": false
},
"eslintConfig": {
"extends": [
"@gilbarbara/eslint-config"
Expand Down
57 changes: 57 additions & 0 deletions packages/tree-changes/scripts/fix-cjs.ts
@@ -0,0 +1,57 @@
// eslint-disable-next-line import/no-relative-packages
import { replace, type ReplaceOptions } from '../../../scripts/replace';

export const fixCjsDts = async (options?: Partial<ReplaceOptions>) => {
return replace({
files: '**/*.d.{ts,cts}',
...options,
name: 'fix-cjs-dts',

callback: content => {
const result = /(?<code>export .+)/u.exec(content);
const { code } = result?.groups ?? {};

if (code) {
const statement = `declare namespace TreeChangesModule {
${code}
}
export = TreeChangesModule;`;

if (!content.endsWith(statement)) {
return content.replace(`\n${code}`, '') + statement;
}
}

return false;
},
});
};

export const fixCjsExports = async (options?: Partial<ReplaceOptions>) => {
const statement = `
// fix-cjs-exports
if (module.exports.default) {
Object.assign(module.exports.default, module.exports);
module.exports = module.exports.default;
delete module.exports.default;
}
`;

return replace({
files: '**/*.js',
...options,
name: 'fix-cjs-exports',

callback: content => {
if (content.includes('module.exports = __toCommonJS') && !content.endsWith(statement)) {
return content + statement;
}

return false;
},
});
};

fixCjsDts();
fixCjsExports();
2 changes: 1 addition & 1 deletion packages/tree-changes/src/index.ts
Expand Up @@ -150,4 +150,4 @@ export default function treeChanges<P extends Data, D extends Data, K = KeyType<
return { added, changed, changedFrom, changedTo, decreased, emptied, filled, increased, removed };
}

export * from './types';
export type { Data, KeyType, TreeChanges, Value } from './types';
5 changes: 3 additions & 2 deletions packages/tree-changes/src/types.ts
@@ -1,9 +1,10 @@
export type ValidTypes = string | boolean | number | PlainObject;
type PlainObject = Record<string, any>;

export type Comparator = Array<string | any[]>;
export type Data = PlainObject | ValidTypes[];
export type Key = string | number;
export type KeyType<P, D> = P | D extends any[] ? Key : keyof P | keyof D;
export type PlainObject = Record<string, any>;
export type ValidTypes = string | boolean | number | PlainObject;
export type Value = ValidTypes | ValidTypes[];

export interface Options<T = Key> {
Expand Down
6 changes: 3 additions & 3 deletions packages/tree-changes/tsconfig.json
@@ -1,8 +1,8 @@
{
"extends": "@gilbarbara/tsconfig",
"compilerOptions": {
"outDir": "./dist"
"noEmit": true,
"target": "ES2020"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
"include": ["src/**/*"]
}
56 changes: 56 additions & 0 deletions scripts/replace.ts
@@ -0,0 +1,56 @@
/* eslint-disable no-await-in-loop */
import { readFile, writeFile } from 'node:fs/promises';
import { relative, resolve } from 'node:path';

import * as fg from 'fast-glob';

export type ReplaceActionResult = string | false | undefined;

export interface ReplaceOptions {
callback(content: string): Promise<ReplaceActionResult> | ReplaceActionResult;
files: fg.Pattern | fg.Pattern[];
globOptions?: fg.Options;
name?: string;
silent?: boolean;
}

export const replace = async (options: ReplaceOptions) => {
const { callback, globOptions = {}, name = 'reflect', silent } = options;
const cwd = process.cwd();

const files = await fg(options.files, {
cwd: resolve(cwd, 'dist'),
...globOptions,
absolute: true,
ignore: [...(globOptions.ignore || []), 'node_modules'],
});

const logger = (message: string) => {
if (!silent) {
// eslint-disable-next-line no-console
console.log(`[${name}]`, message);
}
};

if (!files.length) {
logger('No files matched');
}

const output = [];

for (const file of files) {
const content = await readFile(file, 'utf8');
const result = await callback(content);
const filename = relative(cwd, file);

if (result) {
await writeFile(file, result);
output.push(result);
logger(`✓ ${filename}`);
} else {
logger(`skip ${filename}`);
}
}

return output;
};

0 comments on commit 7956ded

Please sign in to comment.