Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on mixing ESM and commonJS #3807

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion js/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,15 @@ func (c *Compiler) compileImpl(
return nil, code, err
}
// the compatibility mode "decreases" here as we shouldn't transform twice
return c.compileImpl(code, filename, wrap, lib.CompatibilityModeBase, state.srcMap)
var prg *sobek.Program
prg, code, err = c.compileImpl(code, filename, wrap, lib.CompatibilityModeBase, state.srcMap)
if err == nil && strings.Contains(src, "module.exports") {
c.logger.Warningf(
"While compiling %q it was noticed that it mixes import/export syntax (ESM) and commonJS module.exports. "+
"This isn't standard behaviour and will soon not work. Please use one or the other.",
mstoykov marked this conversation as resolved.
Show resolved Hide resolved
filename)
}
return prg, code, err
}

if compatibilityMode == lib.CompatibilityModeExperimentalEnhanced {
Expand Down
23 changes: 23 additions & 0 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,26 @@ func TestMinimalSourceMap(t *testing.T) {
require.NoError(t, err)
require.Empty(t, hook.Drain())
}

func TestMixingImportExport(t *testing.T) {
t.Parallel()
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = io.Discard
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

compiler := New(logger)
compiler.Options = Options{
CompatibilityMode: lib.CompatibilityModeExtended,
Strict: true,
}
_, _, err := compiler.Compile("export let s = 5;\nmodule.exports = 'something';", "somefile", false)
require.NoError(t, err)
entries := hook.Drain()
require.Len(t, entries, 1)
msg, err := entries[0].String() // we need this in order to get the field error
require.NoError(t, err)

require.Contains(t, msg, `it was noticed that it mixes`)
}
Loading