-
-
Notifications
You must be signed in to change notification settings - Fork 544
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
ESM build does not work in the browser ("debug") #912
Comments
Hey, @LarsDenBakker. Thanks for reporting this. This issue may be related to #851, as MSW doesn't rely on the |
It's not really the same issue. The One solution is for Another is to bundle debug into the esm build, rollup can make it browser compatible. |
That sounds great. I've never heard of compilers transforming non-ES modules into ES.
Not to use |
I think the problem is that debug is externalized in the esm build and therefore is not bundled into the rolled-up version of the library. Since we don't declare it as a dependency, it's not guaranteed to be in a consumers node_modules. Therefore, we need to have one or the other. Either 1.) Stop externalizing it in the esm build, OR 2.) Declare it as a dependency or peerDependency. (IMO dependency is more appropriate in this case) See also #851 |
Let’s discuss the debug issue in the respected GitHub ticket. Thanks for the feedback, I think you’re right in saying we need to specify debug as a dependency. |
@patrickmcdougle-okta @kettanaito unfortunately that's not the case. Even if debug is there in the node_modules, the module itself doesn't work in the browser because it uses commonjs syntax not es module. So adding it as a dependency won't solve it. |
To clarify, when speaking "doesn't work in the browser" you're referring to using ES in a browser (via |
Yep trying to use it using the module loader of the browser. |
Edit: I think I'm wrong...however debug doesn't even bundle an esm version in the package (and probably never will), so thats probably not going to work. |
Since debug is only a small module, I recommend bundling it into the esm build of MSW. Or to create a separate output with all dependencies bundled. |
Currently running into this issue too. Running the following code: <html>
<body>
<script type="module">
import {rest} from 'msw';
console.log(rest);
</script>
</body>
</html> And run it with:
Results in:
Because indeed "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCleanUrl = void 0;
/**
* Removes query parameters and hashes from a given URL.
*/
function getCleanUrl(url, isAbsolute) {
if (isAbsolute === void 0) { isAbsolute = true; }
return [isAbsolute && url.origin, url.pathname].filter(Boolean).join('');
}
exports.getCleanUrl = getCleanUrl;
//# sourceMappingURL=getCleanUrl.js.map It would be great to find a solution for this 🙂 I'd love to start using msw. Given some discussion/pointers, id probably be able to spend some time to contribute towards a solution for this issue. For example, would it be possible to ship both esm/cjs for the |
In case anybody else wants to use <html>
<body>
<script type="module">
import {rest} from '@bundled-es-modules/msw';
console.log(rest);
</script>
</body>
</html> |
I'm improving the build modules in #1247, which may affect this in a good way. It's true that |
The work around is to substitute CC @thepassle |
I have the same issue with an NX monorepo with Angular. I followed this tutorial to setup msw. But I get warnings for a lot more packages than listed here:
The last warning is a bit strange:
Versions
|
I can confirm that MSW 2.0 works correctly in the browser. Please test this and let me know. |
@kettanaito what about MSW 1.2? Since there are a lot of breaking changes for many developers/projects it might not be possible to upgrade soon. |
@mfrieling, I understand that. That's why I spent a lot of time writing the Migration guidelines, which also include common problems and how to address them at the bottom. I strongly encourage everybody to upgrade, as many people have already done. There will be no new features and even bug fixes to 1.x. Exception being critical vulnerabilities but I highly doubt those will happen (1.x is the direct continuation of 0.x and we haven't got any critical issues in years). |
Describe the bug
The es module build ships bare module imports, which can be resolved using a dev server or other tool. But when using the es module build in the browser it fails because it's importing the "debug" module which is actually a commonjs module.
Environment
msw: 0.35.0
nodejs: 16.4.0
npm: 7.18.0
To Reproduce
Steps to reproduce the behavior:
@web/dev-server
:npx web-dev-server --node-resolve --open
Uncaught SyntaxError: The requested module './../../../debug/src/index.js' does not provide an export named 'default'
Expected behavior
MSW imports a browser compatible module.
The text was updated successfully, but these errors were encountered: