@yaonyan/jsr2npm 0.1.7 → 0.1.8

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.
Files changed (2) hide show
  1. package/bin/jsr2npm.mjs +24 -17
  2. package/package.json +1 -1
package/bin/jsr2npm.mjs CHANGED
@@ -26,7 +26,7 @@ import { mkdirSync } from "node:fs";
26
26
  import { copyFile, mkdir, readdir, stat } from "node:fs/promises";
27
27
  import { join } from "node:path";
28
28
  import { build } from "esbuild";
29
- async function bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages = []) {
29
+ async function bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages = [], useBrowserPlatform = false) {
30
30
  const entryPath = join(process.cwd(), packageDir, inputFile);
31
31
  const outputPath = join(process.cwd(), packageDir, "dist", outputFile);
32
32
  const outputDir = outputPath.split("/").slice(0, -1).join("/");
@@ -35,21 +35,24 @@ async function bundleWithEsbuild(packageDir, inputFile, outputFile, externalPack
35
35
  });
36
36
  const externalList = externalPackages.length > 0 ? externalPackages.join(", ") : "none";
37
37
  console.log(` \u{1F4E6} External packages: ${externalList}`);
38
+ const platform = useBrowserPlatform ? "neutral" : "node";
39
+ const banner = useBrowserPlatform ? {} : {
40
+ js: `#!/usr/bin/env node
41
+ import { createRequire } from 'node:module';
42
+ const require = createRequire(import.meta.url);`
43
+ };
44
+ console.log(` \u{1F527} Platform: ${platform}${useBrowserPlatform ? " (browser)" : ""}`);
38
45
  await build({
39
46
  entryPoints: [
40
47
  entryPath
41
48
  ],
42
49
  bundle: true,
43
- platform: "node",
50
+ platform,
44
51
  format: "esm",
45
52
  outfile: outputPath,
46
53
  external: externalPackages,
47
54
  packages: "bundle",
48
- banner: {
49
- js: `#!/usr/bin/env node
50
- import { createRequire } from 'node:module';
51
- const require = createRequire(import.meta.url);`
52
- },
55
+ banner,
53
56
  write: true
54
57
  });
55
58
  }
@@ -287,13 +290,16 @@ async function copyExtraFiles(sourceDir, targetDir) {
287
290
  }
288
291
 
289
292
  // __yao__jsr2npm_latest/node_modules/@yao/jsr2npm/converter.js
290
- async function convertPackage(packageName, version, bin, overrides) {
293
+ async function convertPackage(packageName, version, bin, overrides, browser) {
291
294
  console.log(`
292
295
  \u{1F4E6} Package: ${packageName}`);
293
296
  console.log(`\u{1F3F7}\uFE0F Version: ${version}`);
294
297
  if (bin) {
295
298
  console.log(`\u{1F527} CLI Commands: ${Object.keys(bin).join(", ")}`);
296
299
  }
300
+ if (browser) {
301
+ console.log(`\u{1F310} Browser mode: enabled`);
302
+ }
297
303
  const workspaceDir = createWorkspace(packageName, version);
298
304
  const originalCwd = process.cwd();
299
305
  try {
@@ -304,7 +310,7 @@ async function convertPackage(packageName, version, bin, overrides) {
304
310
  recursive: true
305
311
  });
306
312
  const { externals, allDependencies } = await getExternalPackages(packageDir);
307
- await bundlePackage(packageDir, bin, externals);
313
+ await bundlePackage(packageDir, bin, externals, browser);
308
314
  await copyTypeDeclarations(packageDir);
309
315
  await copyExtraFiles(packageDir, `${packageDir}/dist`);
310
316
  await generatePackageJson(packageDir, bin, overrides, allDependencies);
@@ -388,32 +394,32 @@ async function findConflictingPackages(packageDir, jsrPackages, topLevelDeps) {
388
394
  }
389
395
  return conflicts;
390
396
  }
391
- async function bundlePackage(packageDir, bin, externalPackages) {
397
+ async function bundlePackage(packageDir, bin, externalPackages, browser) {
392
398
  if (bin) {
393
- await bundleBinCommands(packageDir, bin, externalPackages);
399
+ await bundleBinCommands(packageDir, bin, externalPackages, browser);
394
400
  } else {
395
- await bundleLibraryExports(packageDir, externalPackages);
401
+ await bundleLibraryExports(packageDir, externalPackages, browser);
396
402
  }
397
403
  }
398
- async function bundleBinCommands(packageDir, bin, externalPackages) {
404
+ async function bundleBinCommands(packageDir, bin, externalPackages, browser) {
399
405
  console.log("\n\u{1F528} Bundling CLI tools...");
400
406
  for (const [cmdName, inputFile] of Object.entries(bin)) {
401
407
  await verifyEntrypoint(packageDir, inputFile);
402
408
  const outputFile = `bin/${cmdName}.mjs`;
403
- await bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages);
409
+ await bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages, browser || false);
404
410
  const outputPath = join3(packageDir, "dist", outputFile);
405
411
  await chmod(outputPath, 493);
406
412
  console.log(` \u2705 Created ${cmdName}: ${outputFile}`);
407
413
  }
408
414
  }
409
- async function bundleLibraryExports(packageDir, externalPackages) {
415
+ async function bundleLibraryExports(packageDir, externalPackages, browser) {
410
416
  const exports = await readDenoJsonExports(packageDir);
411
417
  if (!exports) return;
412
418
  console.log("\n\u{1F528} Bundling library exports...");
413
419
  for (const [exportKey, inputFile] of Object.entries(exports)) {
414
420
  await verifyEntrypoint(packageDir, inputFile);
415
421
  const outputFile = exportKey === "." ? "index.mjs" : `${exportKey.replace(/^\.\//, "")}.mjs`;
416
- await bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages);
422
+ await bundleWithEsbuild(packageDir, inputFile, outputFile, externalPackages, browser || false);
417
423
  console.log(` \u2705 Bundled ${exportKey}: ${outputFile}`);
418
424
  }
419
425
  }
@@ -506,7 +512,8 @@ ${"=".repeat(60)}`);
506
512
  pkg.name,
507
513
  pkg.version,
508
514
  pkg.bin,
509
- pkg.packageJson
515
+ pkg.packageJson,
516
+ pkg.browser
510
517
  );
511
518
  } catch (error) {
512
519
  console.error(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yaonyan/jsr2npm",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "homepage": "https://jsr.io/@yao/jsr2npm",
5
5
  "type": "module",
6
6
  "dependencies": {