File: build-addons.mjs

package info (click to toggle)
node-react-toastify 9.1.2%2B~1.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,088 kB
  • sloc: javascript: 260; makefile: 28
file content (63 lines) | stat: -rwxr-xr-x 1,875 bytes parent folder | download
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
57
58
59
60
61
62
63
import { existsSync } from 'node:fs';
import { readdir, readFile, writeFile, rename } from 'node:fs/promises';
import { exec } from 'node:child_process';
import path from 'node:path';
import { promisify } from 'node:util';

const asyncExec = promisify(exec);

const packageJson = JSON.parse(
  await readFile(new URL('./package.json', import.meta.url))
);

const BASE_DIR = './src/addons';
const MICRO_BUNDLE = './node_modules/.bin/microbundle';

try {
  const dirs = await readdir(BASE_DIR);

  for (const dir of dirs) {
    let entryPoint = path.join(BASE_DIR, dir, 'index.ts');
    const exportKey = `./addons/${dir}`;
    const exportValues = {
      require: '',
      import: '',
      types: `./addons/${dir}/index.d.ts`
    };

    if (!existsSync(entryPoint)) {
      if (existsSync(`${entryPoint}x`)) {
        entryPoint = `${entryPoint}x`;
      } else {
        throw new Error(`${entryPoint} does not exist`);
      }
    }

    for (const moduleType of ['cjs', 'esm']) {
      const filename = moduleType === 'esm' ? 'index.esm.mjs' : 'index.js';
      const out = `./dist/addons/${dir}/${filename}`;
      const exportOut = `./addons/${dir}/${filename}`;

      const { stdout, stderr } = await asyncExec(
        `${MICRO_BUNDLE} -i ${entryPoint} -o ${out} --no-pkg-main -f ${moduleType} --jsx React.createElement --external react-toastify --compress false`
      );
      console.log(stdout);
      console.log(stderr);

      if (moduleType === 'cjs') {
        exportValues.require = exportOut;
      } else if (moduleType === 'esm') {
        exportValues.import = exportOut;
      }
    }

    packageJson.exports = Object.assign(packageJson.exports, {
      [exportKey]: exportValues
    });
  }

  await writeFile('./package.json', JSON.stringify(packageJson, null, 2));
  await rename('./dist/addons', './addons');
} catch (error) {
  throw error;
}