Repository navigation
Expand file tree
/
Copy pathpinefile.ts
More file actions
56 lines (49 loc) · 1.66 KB
/
Copy pathpinefile.ts
File metadata and controls
56 lines (49 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { run, log, series } from '@pinefile/pine';
import isCI from 'is-ci';
import { build } from 'esbuild';
import { copyFileSync, readFileSync, rmSync, writeFileSync } from 'fs';
const buildOptions = (format: 'cjs' | 'esm') => ({
entryPoints: ['./src/index.ts'],
bundle: true,
format,
target: 'es2022',
outfile: `./dist/${format}/index.${format === 'esm' ? 'mjs' : 'js'}`,
// Keep `require('personnummer')` returning the class, not `{ default }`.
...(format === 'cjs' && {
footer: { js: 'module.exports = module.exports.default;' },
}),
});
export default {
build: async () => {
rmSync('./dist', { recursive: true, force: true });
log.info('Building types');
await run('tsc --emitDeclarationOnly');
log.info('Building cjs');
await build(buildOptions('cjs'));
log.info('Building esm');
await build(buildOptions('esm'));
// Node16/NodeNext reads `.d.ts` next to a CJS file as CJS, so each format
// gets its own declarations matching what it exports at runtime.
log.info('Building per-format types');
const types = './dist/types/index.d.ts';
copyFileSync(types, './dist/esm/index.d.mts');
writeFileSync(
'./dist/cjs/index.d.ts',
readFileSync(types, 'utf8').replace(
/export declare const valid[^]*$/,
'export = Personnummer;\n',
),
);
},
test: async (args: { _: string[] }) => {
const files = isCI
? [__dirname + '/dist/cjs', __dirname + '/dist/esm']
: [__dirname + '/src'];
await series(
files.map((file) => async () => {
log.info(`Running tests with ${file}\n`);
await run(`FILE=${file} vitest ${args._}`);
}),
);
},
};