Skip to content

Commit

Permalink
Fix exported names from a dep (close #750)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Nov 11, 2023
1 parent 103e54a commit 393bed1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
32 changes: 26 additions & 6 deletions server/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,16 +534,12 @@ rebuild:
}

if isLocalSpecifier(specifier) {
// is sub-module of current package and non-dynamic import
// sub-module of current package and non-dynamic import
if strings.HasPrefix(fullFilepath, task.realWd) && args.Kind != api.ResolveJSDynamicImport {
// splits modules based on the `exports` defines in package.json,
// see https://nodejs.org/api/packages.html
if om, ok := npm.PkgExports.(*orderedMap); ok {
modName := "." + strings.TrimPrefix(fullFilepath, path.Join(task.installDir, "node_modules", npm.Name))
// bundles _internal_ modules
if strings.Contains(modName, "/internal/") {
return api.OnResolveResult{}, nil
}
if strings.HasSuffix(modName, ".mjs") {
modName = strings.TrimSuffix(specifier, ".mjs")
} else {
Expand Down Expand Up @@ -586,7 +582,31 @@ rebuild:
}
}
}
return api.OnResolveResult{}, nil

// externalize the module that is an alias of a dependency
// means this file just include a single line(js): `export * from "dep"`
isAlias := false
fi, ioErr := os.Lstat(fullFilepath)
if ioErr == nil && fi.Size() < 256 {
data, ioErr := os.ReadFile(fullFilepath)
if ioErr == nil {
out, esbErr := minify(string(data), api.ESNext, api.LoaderJS)
if esbErr == nil {
p := bytes.Split(out, []byte("\""))
if len(p) == 3 && string(p[0]) == "export*from" && string(p[2]) == ";\n" {
url := string(p[1])
if !isLocalSpecifier(url) {
isAlias = true
specifier = url
}
}
}
}
}

if !isAlias {
return api.OnResolveResult{}, nil
}
}
specifier = strings.TrimPrefix(fullFilepath, filepath.Join(task.installDir, "node_modules")+"/")
}
Expand Down
19 changes: 19 additions & 0 deletions test/issue-750/issue-750.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";

import * as element from "http://localhost:8080/@lume/[email protected]?target=esnext";
import * as variable from "http://localhost:8080/@lume/[email protected]";

Deno.test("issue #750", () => {
assert(checkKeys(element, variable));
});

// check keys of an object in another object
function checkKeys(a: object, b: object) {
for (const key in b) {
if (!(key in a)) {
console.error(`key ${key} is missing in b`);
return false;
}
}
return true;
}

0 comments on commit 393bed1

Please sign in to comment.