RequireJS environment in browser will break the UMD module conversion
See original GitHub issueDescribe the bug
Say I have an umd module
;(function(root, factory) {
// istanbul ignore next
if (typeof define === "function" && define.amd) {
// istanbul ignore next
define([], factory)
} else if (typeof module === "object" && module.exports) {
module.exports = factory()
} else {
// istanbul ignore next
root.regexpToAst = factory()
}
})(
typeof self !== "undefined"
? // istanbul ignore next
self
: this,
function() {}
)
With the commonjs conversion, it returns code like
var require_regexp_to_ast = __commonJS({
"../../node_modules/regexp-to-ast/lib/regexp-to-ast.js"(exports, module) {
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.regexpToAst = factory();
}
})(typeof self !== "undefined" ? self : exports, function() {
function RegExpParser2() {
}
But this will break in runtime because there is global define
in the browser since I have a requireJS environment. This module will be registered in the global AMD module instead of vite ESM module.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
vite-plugin-treat-umd-as-commonjs - npm
This plugin is for a paticular edge case. check RequireJS environment in browser will break the UMD module conversion.
Read more >vite-plugin-treat-umd-as-commonjs - npm package - Snyk
This plugin is for a paticular edge case. check RequireJS environment in browser will break the UMD module conversion.
Read more >Why AMD? - RequireJS
This page talks about the design forces and use of the Asynchronous Module Definition (AMD) API for JavaScript modules, the module API supported...
Read more >How JavaScript works: the module pattern + comparing ...
How JavaScript works: the module pattern + comparing CommonJS, AMD, UMD, and ES6 Modules. This is post # 50 of the series, dedicated...
Read more >JavaScript Reference Guide: JS module ecosystem
JavaScript modules are individual parts of a JavaScript program that ... environment could be Node.js // RequireJS (AMD) or a Web browser.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Published a plugin to handle this case vite-plugin-treat-umd-as-commonjs.
this would be very useful for migrating legacy projects like apps using durandaljs and so… or other apps using requirejs amd modules