Skip to content

Commit a0b88eb

Browse files
committed
In webpack_demo, add example showing that you can only use either ESM export syntax or CommonJS export syntax, not both
1 parent 7b82529 commit a0b88eb

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

webpack_demo/dist/main.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webpack_demo/src/commonjs_module.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
console.log("commonjs_module");
2+
13
exports.myCommonJsModuleExportedFunction = function () {
24
return "antlers";
35
};

webpack_demo/src/es_module.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
console.log("es_module");
2+
13
export function myEsModuleExportedFunction() {
24
return "elephant";
35
}

webpack_demo/src/export_both.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
console.log("export_both");
2+
3+
exports.exportBothCommonJs = function () {
4+
return "lion";
5+
};
6+
7+
// If you want to use exports, this needs to be commented out.
8+
// export function exportBothEs() {
9+
// return "tiger";
10+
// }

webpack_demo/src/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
// All of these work!
1+
console.log("index");
22

3-
// import { myEsModuleExportedFunction } from "./es_module.js";
3+
// ===== All of these work! =====
4+
import { myEsModuleExportedFunction } from "./es_module.js";
45
// const { myEsModuleExportedFunction } = require("./es_module.js");
56

67
// const { myCommonJsModuleExportedFunction } = require("./commonjs_module.js");
78
// import { myCommonJsModuleExportedFunction } from "./commonjs_module.js";
9+
// ==============================
10+
11+
// ===== Can only use one of these =====
12+
// import { exportBothEs } from "./export_both";
13+
import { exportBothCommonJs } from "./export_both";
14+
// =====================================
815

916
function component() {
1017
const element = document.createElement("div");
1118

12-
// element.innerHTML = myEsModuleExportedFunction();
13-
// element.innerHTML = myCommonJsModuleExportedFunction();
19+
element.innerHTML = "";
20+
// element.innerHTML += myEsModuleExportedFunction();
21+
// element.innerHTML += myCommonJsModuleExportedFunction();
22+
23+
// element.innerHTML += exportBothEs();
24+
element.innerHTML += exportBothCommonJs();
1425

1526
return element;
1627
}

0 commit comments

Comments
 (0)