olum-compiler 0.5.1 → 0.5.3

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.
package/bin/copySrc.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // todo check changed files only to be copied, use this project https://github.com/eissapk/diff
2
2
  const path = require("path");
3
3
  const fs = require("fs");
4
- const { copy, remove } = require("../lib/helpers");
4
+ const { copy, remove, rewriteImports } = require("../lib/helpers");
5
5
 
6
6
  const entryPoint = path.resolve(__dirname, "../../../src");
7
7
  const entryPoint2 = path.resolve(__dirname, "../../../public");
@@ -12,7 +12,15 @@ module.exports = function copySrc() {
12
12
  if (fs.existsSync(path.resolve(__dirname, "../public"))) remove(path.resolve(__dirname, "../public"));
13
13
  if (fs.existsSync(path.resolve(__dirname, "../src"))) remove(path.resolve(__dirname, "../src"));
14
14
  // copy
15
- copy(entryPoint, path.resolve(__dirname, "../src"));
15
+ copy(entryPoint, path.resolve(__dirname, "../src"), () => {
16
+ // modify main.js file import paths
17
+ const mainJsPath = path.resolve(__dirname, "../src/main.js");
18
+ if (fs.existsSync(mainJsPath)) {
19
+ let mainJsContent = fs.readFileSync(mainJsPath).toString();
20
+ mainJsContent = rewriteImports(mainJsContent);
21
+ fs.writeFileSync(mainJsPath, mainJsContent);
22
+ }
23
+ });
16
24
  copy(entryPoint2, path.resolve(__dirname, "../public"));
17
25
  resolve();
18
26
  } catch (err) {
package/bin/server.js CHANGED
@@ -17,13 +17,12 @@ function handleWsFile() {
17
17
  function getPath(entry, url) {
18
18
  entry = entry.endsWith("/") ? entry : entry + "/";
19
19
  url = url.startsWith("/") ? url.slice(1) : url;
20
- let trial_1 = path.resolve(entry + url);
21
- let trial_2 = path.resolve(entry + "../" + url);
22
- // console.log("getPath", { entry, url, trial_1, trial_2 });
23
-
24
- if (fs.existsSync(trial_1)) return trial_1;
25
- else if (fs.existsSync(trial_2)) return trial_2;
26
- else return null;
20
+ const paths = [
21
+ path.resolve(entry, url),
22
+ path.resolve(entry, "../", url),
23
+ path.resolve(process.cwd(), url),
24
+ ];
25
+ return paths.find(fs.existsSync) || null;
27
26
  }
28
27
  // todo handle https
29
28
  /**
package/lib/helpers.js CHANGED
@@ -486,6 +486,64 @@ function minifyJS(src) {
486
486
  return out;
487
487
  }
488
488
 
489
+ function resolvePackage(pkgName) {
490
+ const pkgJsonPath = path.join(
491
+ process.cwd(),
492
+ "node_modules",
493
+ pkgName,
494
+ "package.json"
495
+ );
496
+
497
+ if (!fs.existsSync(pkgJsonPath)) {
498
+ throw new Error(`Package "${pkgName}" not found`);
499
+ }
500
+
501
+ const pkg = JSON.parse(
502
+ fs.readFileSync(pkgJsonPath, "utf8")
503
+ );
504
+
505
+ let entry;
506
+
507
+ if (typeof pkg.exports === "string") {
508
+ entry = pkg.exports;
509
+ } else if (pkg.exports?.["."]?.import) {
510
+ entry = pkg.exports["."].import;
511
+ } else if (pkg.exports?.["."]?.default) {
512
+ entry = pkg.exports["."].default;
513
+ } else if (typeof pkg.exports?.["."] === "string") {
514
+ entry = pkg.exports["."];
515
+ } else {
516
+ entry = pkg.module || pkg.main || "index.js";
517
+ }
518
+
519
+ entry = entry.replace(/^\.?\//, "");
520
+
521
+ return `/node_modules/${pkgName}/${entry}`;
522
+ }
523
+
524
+ function rewriteImports(code) {
525
+ return code.replace(
526
+ /from\s+["']([^"']+)["']/g,
527
+ (match, specifier) => {
528
+ if (
529
+ specifier.startsWith(".") ||
530
+ specifier.startsWith("/") ||
531
+ specifier.startsWith("http:")
532
+ || specifier.startsWith("https:")
533
+ ) {
534
+ return match;
535
+ }
536
+
537
+ try {
538
+ return `from "${resolvePackage(specifier)}"`;
539
+ } catch (err) {
540
+ console.warn(err.message);
541
+ return match;
542
+ }
543
+ }
544
+ );
545
+ }
546
+
489
547
  module.exports.isNum = isNum;
490
548
  module.exports.id = id;
491
549
  module.exports.isUpper = isUpper;
@@ -507,3 +565,4 @@ module.exports.createScopeHandler = createScopeHandler;
507
565
  module.exports.getGlobalMethods = getGlobalMethods;
508
566
  module.exports.getGlobalProps = getGlobalProps;
509
567
  module.exports.minifyJS = minifyJS;
568
+ module.exports.rewriteImports = rewriteImports;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {