TopLevelThisWarning.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Alexander Akait @alexander-akait
  4. */
  5. "use strict";
  6. const WebpackError = require("./WebpackError");
  7. /**
  8. * @typedef {object} TopLevelThisDetails
  9. * @property {string} name the module, by request
  10. * @property {number} count how many times it reads `this` at the top level
  11. */
  12. class TopLevelThisWarning extends WebpackError {
  13. /**
  14. * Creates an instance of TopLevelThisWarning.
  15. * @param {TopLevelThisDetails[]} modules the modules that do it
  16. * @param {number} total how many such reads there are in all
  17. */
  18. constructor(modules, total) {
  19. const list = modules.map((it) => `\n ${it.name} (${it.count})`).join("");
  20. super(
  21. `top-level this: ${total} ${total === 1 ? "read" : "reads"} of 'this' at the top level of an ES module:${list}\nwebpack is treating these as ES modules, where 'this' is 'undefined' at the top level rather than 'module.exports' — so a file that worked as CommonJS reads nothing once it is bundled this way. A single 'import' or 'export' is enough to make 'javascript/auto' decide a file is an ES module. Use 'globalThis' where the global object was meant, 'import.meta' for anything about the module, or give the file a '.cjs' extension to keep it CommonJS.\nFor more info visit https://webpack.js.org/api/module-variables/#importmeta`
  22. );
  23. /** @type {string} */
  24. this.name = "TopLevelThisWarning";
  25. }
  26. }
  27. module.exports = TopLevelThisWarning;