Skip to content

Commit

Permalink
Improve cjs resolving(close #256)
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Feb 5, 2022
1 parent 5d29f33 commit cf8c5eb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
15 changes: 11 additions & 4 deletions server/build-npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ esbuild:
MinifySyntax: !task.DevMode,
Plugins: []api.Plugin{esmResolverPlugin},
Loader: map[string]api.Loader{
".wasm": api.LoaderBinary,
".wasm": api.LoaderDataURL,
".svg": api.LoaderDataURL,
".png": api.LoaderDataURL,
".webp": api.LoaderDataURL,
Expand Down Expand Up @@ -567,7 +567,7 @@ esbuild:
err = yarnAdd(task.wd, fmt.Sprintf("%s@%s", pkg.Name, pkg.Version))
}
if err == nil {
meta, err := initESM(task.wd, *pkg, task.Target, task.DevMode)
dep, err := initESM(task.wd, *pkg, task.Target, task.DevMode)
if err == nil {
if bytes.HasPrefix(p, []byte{'.'}) {
// right shift to strip the object `key`
Expand All @@ -582,7 +582,7 @@ esbuild:
}
// support edge case like `require('htmlparser').Parser`
importName := string(p[1 : shift+1])
for _, v := range meta.Exports {
for _, v := range dep.Exports {
if v == importName {
cjsImports.Add(importName)
marked = true
Expand All @@ -592,10 +592,15 @@ esbuild:
}
}
// if the dependency is an es module without `default` export, then use star import
if !marked && meta.Module != "" && !meta.ExportDefault {
if !marked && dep.Module != "" && !dep.ExportDefault {
cjsImports.Add("*")
marked = true
}
// if the dependency is an cjs module with `default` export, then use star import
if !marked && dep.Module == "" && dep.ExportDefault {
cjsImports.Add("__esModule")
marked = true
}
}
}
}
Expand Down Expand Up @@ -638,6 +643,8 @@ esbuild:
fmt.Fprintf(buf, `import __%s$ from "%s";%s`, identifier, importPath, eol)
case "*":
fmt.Fprintf(buf, `import * as __%s$ from "%s";%s`, identifier, importPath, eol)
case "__esModule":
fmt.Fprintf(buf, `import * as __%s$$ from "%s";const __%s$=Object.assign({__esModule:true},__%s$$);%s`, identifier, importPath, identifier, identifier, eol)
default:
fmt.Fprintf(buf, `import { %s as __%s$%s } from "%s";%s`, name, identifier, name, importPath, eol)
}
Expand Down
5 changes: 3 additions & 2 deletions server/cjs_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type cjsExportsResult struct {
Exports []string `json:"exports"`
Error string `json:"error"`
ExportDefault bool `json:"exportDefault"`
Exports []string `json:"exports"`
Error string `json:"error"`
}

func parseCJSModuleExports(buildDir string, importPath string, nodeEnv string) (ret cjsExportsResult, err error) {
Expand Down
2 changes: 1 addition & 1 deletion server/esm.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ func initESM(wd string, pkg Pkg, target string, isDev bool) (esm *ESM, err error
if ret.Error != "" {
return nil, fmt.Errorf("parseCJSModuleExports: %s", ret.Error)
}
esm.ExportDefault = ret.ExportDefault
esm.Exports = ret.Exports
esm.ExportDefault = true
// if ret.Error != "" && strings.Contains(ret.Error, "Unexpected export statement in CJS module") {
// if pkg.Submodule != "" {
// esm.Module = pkg.Submodule
Expand Down
2 changes: 1 addition & 1 deletion server/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func query(devMode bool) rex.Handle {
fmt.Fprintf(buf, `export default function WorkerWrapper() {%s return new Worker('%s%s', { type: 'module' })%s}`, "\n", origin, taskID, "\n")
} else {
fmt.Fprintf(buf, `export * from "%s%s";%s`, origin, taskID, "\n")
if esm.ExportDefault {
if esm.Module == "" || esm.ExportDefault {
fmt.Fprintf(
buf,
`export { default } from "%s%s";%s`,
Expand Down

0 comments on commit cf8c5eb

Please sign in to comment.