Browse Source

Add plugin for esbuild to use relative paths from neovan files.

Lee Morgan 14 hours ago
parent
commit
7d639966e8
2 changed files with 21 additions and 0 deletions
  1. 5 0
      parseComponent.js
  2. 16 0
      relativePathPlugin.js

+ 5 - 0
parseComponent.js

@@ -4,6 +4,7 @@ import os from "os";
 import path from "path";
 import htmlMinifier from "html-minifier-terser";
 import esbuild from "esbuild";
+import forceRelativePath from "./relativePathPlugin.js";
 
 const parseComponent = async (file)=>{
     const dir = path.dirname(file);
@@ -82,11 +83,15 @@ const createBundle = async (data)=>{
 
     data.html = await addComponents(data.html, data.dir);
 
+    const plugins = [];
+    if(data.tmpDir) plugins.push(forceRelativePath(data.dir));
+
     const esbuildProm = esbuild.build({
         entryPoints: entryPoints,
         bundle: true,
         minify: true,
         write: false,
+        plugins: plugins,
         outdir: "/"
     });
     const htmlProm = htmlMinifier.minify(data.html, {

+ 16 - 0
relativePathPlugin.js

@@ -0,0 +1,16 @@
+import path from "path";
+
+export default (root)=>({
+    name: "force-relative-root",
+
+    setup(build){
+        build.onResolve({filter: /^\./}, (args)=>{
+            if(!args.path.startsWith(".")) return null;
+            const resolvedPath = path.resolve(root, args.path);
+            return {
+                path: resolvedPath,
+                external: false
+            };
+        })
+    }
+});