@hypernym/bundler 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import process, { stdout, cwd } from 'node:process';
3
3
  import { createArgs } from '@hypernym/args';
4
- import { readFile, stat } from 'node:fs/promises';
4
+ import { readFile, mkdir, writeFile, stat } from 'node:fs/promises';
5
5
  import { resolve, parse } from 'node:path';
6
6
  import { exists } from '@hypernym/utils/node';
7
7
  import { dim, magenta, red, cyan, green } from '@hypernym/colors';
@@ -25,7 +25,7 @@ const externals = [
25
25
  ];
26
26
 
27
27
  const name = "bundler";
28
- const version = `0.1.0`;
28
+ const version = `0.1.2`;
29
29
 
30
30
  const cl = console.log;
31
31
  const log = (...args) => {
@@ -96,7 +96,7 @@ function getOutputPath(outDir, input, types = false) {
96
96
  return `./${output}`;
97
97
  }
98
98
 
99
- async function loadConfig(filePath, defaults) {
99
+ async function loadConfig(cwd, filePath, defaults) {
100
100
  const result = await build$1({
101
101
  entryPoints: [filePath],
102
102
  bundle: true,
@@ -106,8 +106,11 @@ async function loadConfig(filePath, defaults) {
106
106
  packages: "external"
107
107
  });
108
108
  const code = result.outputFiles[0].text;
109
- const buffer = Buffer.from(code).toString("base64");
110
- const content = await import(`data:text/javascript;base64,${buffer}`);
109
+ const tempDir = resolve(cwd, "node_modules", ".hypernym", "bundler");
110
+ const tempConfig = resolve(tempDir, "config.mjs");
111
+ await mkdir(tempDir, { recursive: true });
112
+ await writeFile(tempConfig, code, "utf-8");
113
+ const content = await import(tempConfig);
111
114
  const config = {
112
115
  ...defaults,
113
116
  ...content.default
@@ -129,7 +132,7 @@ async function createConfigLoader(cwd, args) {
129
132
  const path = resolve(cwd, args.config);
130
133
  const isConfig = await exists(path);
131
134
  if (isConfig)
132
- return await loadConfig(path, defaults);
135
+ return await loadConfig(cwd, path, defaults);
133
136
  else
134
137
  return logger.exit(warnMessage);
135
138
  }
@@ -139,7 +142,7 @@ async function createConfigLoader(cwd, args) {
139
142
  const path = resolve(cwd, `${configName}${ext}`);
140
143
  const isConfig = await exists(path);
141
144
  if (isConfig)
142
- return await loadConfig(path, defaults);
145
+ return await loadConfig(cwd, path, defaults);
143
146
  }
144
147
  return logger.exit(warnMessage);
145
148
  }
@@ -257,7 +260,7 @@ async function build(cwd, options) {
257
260
  buildStats.size = buildStats.size + stats.size;
258
261
  }
259
262
  if ("types" in entry) {
260
- const { types, plugins } = entry;
263
+ const { types, externals, plugins, banner, footer } = entry;
261
264
  const buildLogs = [];
262
265
  const _output = getOutputPath(outDir, types, true);
263
266
  let _format = "esm";
@@ -267,6 +270,7 @@ async function build(cwd, options) {
267
270
  const format = entry.format || _format;
268
271
  const builder = await rollup({
269
272
  input: resolve(cwd, types),
273
+ external: externals || options.externals,
270
274
  plugins: [dts(plugins?.dts)],
271
275
  onLog: (level, log) => {
272
276
  if (logFilter(log))
@@ -275,7 +279,9 @@ async function build(cwd, options) {
275
279
  });
276
280
  await builder.write({
277
281
  file: resolve(cwd, output),
278
- format
282
+ format,
283
+ banner,
284
+ footer
279
285
  });
280
286
  const stats = await stat(resolve(cwd, output));
281
287
  buildStats.files.push({
@@ -13,23 +13,74 @@ interface BuildPlugins {
13
13
  replace?: RollupReplaceOptions;
14
14
  }
15
15
 
16
- interface Entries {
16
+ interface Entry {
17
+ /**
18
+ * Specifies the path of the transformed module.
19
+ *
20
+ * If not specified, matches the `input` path with the appropriate extension.
21
+ *
22
+ * @default undefined
23
+ */
17
24
  output?: string;
25
+ /**
26
+ * Specifies the format of the generated module.
27
+ *
28
+ * @default 'esm'
29
+ */
18
30
  format?: OutputOptions['format'];
31
+ /**
32
+ * Specifies the module IDs, or regular expressions to match module IDs,
33
+ * that should remain external to the bundle.
34
+ *
35
+ * If not specified, infers the IDs from the global `options.externals` option.
36
+ *
37
+ * @default undefined
38
+ */
19
39
  externals?: (string | RegExp)[];
40
+ /**
41
+ * Specifies the string to be inserted at the beginning of the module.
42
+ *
43
+ * @default undefined
44
+ */
45
+ banner?: OutputOptions['banner'];
46
+ /**
47
+ * Specifies the string to be inserted at the end of the module.
48
+ *
49
+ * @default undefined
50
+ */
51
+ footer?: OutputOptions['footer'];
52
+ /**
53
+ * Specifies custom filters that will display only certain log messages.
54
+ *
55
+ * @default undefined
56
+ */
20
57
  logFilter?: string[];
21
58
  }
22
- interface EntriesInput extends Entries {
59
+ interface EntryInput extends Entry {
60
+ /**
61
+ * Specifies the path of the module's build source.
62
+ */
23
63
  input: string;
24
- banner?: OutputOptions['banner'];
25
- footer?: OutputOptions['footer'];
64
+ /**
65
+ * Specifies plugin options.
66
+ *
67
+ * @default undefined
68
+ */
26
69
  plugins?: BuildPlugins;
27
70
  }
28
- interface EntriesTypes extends Entries {
71
+ interface EntryTypes extends Entry {
72
+ /**
73
+ * Specifies the path of the module's build source that contains only TS definitions.
74
+ */
29
75
  types: string;
76
+ /**
77
+ * Specifies plugin options.
78
+ *
79
+ * @default undefined
80
+ */
30
81
  plugins?: Pick<BuildPlugins, 'dts'>;
31
82
  }
32
- type EntriesOptions = EntriesInput | EntriesTypes;
83
+ type EntriesOptions = EntryInput | EntryTypes;
33
84
 
34
85
  interface BuildHooks {
35
86
  /**
@@ -59,6 +110,10 @@ interface Options {
59
110
  * Specifies the module IDs, or regular expressions to match module IDs,
60
111
  * that should remain external to the bundle.
61
112
  *
113
+ * IDs and regexps from this option are applied globally to all entries.
114
+ *
115
+ * Also, it is possible to define externals individually per entry (`entry.externals`).
116
+ *
62
117
  * @default [/^node:/, /^@types/, /^@rollup/, /^@hypernym/, /^rollup/, ...pkg.dependencies]
63
118
  */
64
119
  externals?: (string | RegExp)[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hypernym/bundler",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "author": "Hypernym Studio",
5
5
  "description": "ESM & TS module bundler.",
6
6
  "license": "MIT",