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

Allow ES Modules to export __esModule #1622

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1835,16 +1835,16 @@ TestMinifiedExportsAndModuleFormatCommonJS
// foo/test.js
var t = {};
f(t, {
foo: () => l
foo: () => m
});
var l = 123;
var m = 123;

// bar/test.js
var r = {};
f(r, {
bar: () => m
bar: () => b
});
var m = 123;
var b = 123;

// entry.js
console.log(exports, module.exports, t, r);
Expand Down
12 changes: 6 additions & 6 deletions internal/bundler/snapshots/snapshots_splitting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ TestSplittingHybridESMAndCJSIssue617
import {
foo,
init_a
} from "./chunk-ZIIA2BWE.js";
} from "./chunk-IFICMH5R.js";
init_a();
export {
foo
Expand All @@ -338,15 +338,15 @@ export {
import {
a_exports,
init_a
} from "./chunk-ZIIA2BWE.js";
} from "./chunk-IFICMH5R.js";

// b.js
var bar = (init_a(), a_exports);
export {
bar
};

---------- /out/chunk-ZIIA2BWE.js ----------
---------- /out/chunk-IFICMH5R.js ----------
// a.js
var a_exports = {};
__export(a_exports, {
Expand Down Expand Up @@ -399,20 +399,20 @@ TestSplittingMissingLazyExport
---------- /out/a.js ----------
import {
foo
} from "./chunk-XIEF2OJ5.js";
} from "./chunk-MN2S3FNS.js";

// a.js
console.log(foo());

---------- /out/b.js ----------
import {
bar
} from "./chunk-XIEF2OJ5.js";
} from "./chunk-MN2S3FNS.js";

// b.js
console.log(bar());

---------- /out/chunk-XIEF2OJ5.js ----------
---------- /out/chunk-MN2S3FNS.js ----------
// empty.js
var empty_exports = {};
__markAsModule(empty_exports);
Expand Down
3 changes: 0 additions & 3 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12639,9 +12639,6 @@ func (p *parser) recordExport(loc logger.Loc, alias string, ref js_ast.Ref) {
fmt.Sprintf("Multiple exports with the same name %q", alias),
[]logger.MsgData{logger.RangeData(&p.tracker, js_lexer.RangeOfIdentifier(p.source, name.AliasLoc),
fmt.Sprintf("%q was originally exported here", alias))})
} else if alias == "__esModule" {
p.log.AddRangeError(&p.tracker, js_lexer.RangeOfIdentifier(p.source, loc),
"The export name \"__esModule\" is reserved and cannot be used (it's needed as an export marker when converting ES module syntax to CommonJS)")
} else {
p.namedExports[alias] = js_ast.NamedExport{AliasLoc: loc, Ref: ref}
}
Expand Down
7 changes: 0 additions & 7 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2502,13 +2502,6 @@ func TestExport(t *testing.T) {
"<stdin>: error: This export alias is invalid because it contains the unpaired Unicode surrogate U+DC00\n")
expectParseErrorTarget(t, 2020, "export * as '' from 'foo'",
"<stdin>: error: Using a string as a module namespace identifier name is not supported in the configured target environment\n")

// Exports with the name "__esModule" are forbidden
esModuleError := "<stdin>: error: The export name \"__esModule\" is reserved and cannot be used " +
"(it's needed as an export marker when converting ES module syntax to CommonJS)\n"
expectParseError(t, "export var __esModule", esModuleError)
expectParseError(t, "export {__esModule}; var __esModule", esModuleError)
expectParseError(t, "export {__esModule} from 'foo'", esModuleError)
}

func TestExportDuplicates(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func code(isES6 bool) string {
export var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b))

// Tells importing modules that this can be considered an ES6 module
var __markAsModule = target => __defProp(target, '__esModule', { value: true })
var __markAsModule = target => !__hasOwnProp.call(target, '__esModule') && __defProp(target, '__esModule', { value: true })

// Tells importing modules that this can be considered an ES6 module
export var __name = (target, value) => __defProp(target, 'name', { value, configurable: true })
Expand Down Expand Up @@ -168,9 +168,9 @@ func code(isES6 bool) string {

// Used to implement ES6 exports to CommonJS
export var __export = (target, all) => {
__markAsModule(target)
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true })
__markAsModule(target)
}
export var __reExport = (target, module, desc) => {
if (module && typeof module === 'object' || typeof module === 'function')
Expand Down
4 changes: 4 additions & 0 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@
'in.js': `const out = require('./foo'); if (!out.__esModule || out.default !== null) throw 'fail'`,
'foo.js': `export default class x {} x = null`,
}),
test(['--bundle', 'in.js', '--outfile=node.js'], {
'in.js': `const out = require('./foo'); if (!out.__esModule) throw 'fail'`,
'foo.js': `export const __esModule = true`,
}),
test(['--bundle', 'in.js', '--outfile=node.js'], {
'in.js': `
// This is the JavaScript generated by "tsc" for the following TypeScript:
Expand Down