micromark: `assert` is not browser friendly
Initial checklist
- I read the support docs
- I read the contributing guide
- I agree to follow the code of conduct
- I searched issues and couldn’t find anything (or linked relevant results below)
Affected packages and versions
latest
Link to runnable example
No response
Steps to reproduce
https://github.com/browserify/node-util/pull/62#issuecomment-908343918
Expected behavior
Use console.assert
instead.
Actual behavior
assert
is a node built-in module.
Runtime
Node v14
Package manager
yarn v1
OS
macOS
Build and bundle tools
Vite
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 28 (23 by maintainers)
Thanks @ChristianMurphy for updating the types. I’ve released all micromark extensions to not include
node:assert
in their development builds, but to instead usepower-assert
.Sorry, misread some of the back-and-forth.
An alternative solution that I would be more than happy to contribute is just adding a utility function to this package with the same behavior as
assert
.The advantages here would be fewer dependencies with more control over behavior. Disadvantages being its more code to maintain for this package.
Re @JounQin:
Please look at how micromark works, here’s a small example: https://github.com/micromark/micromark/blob/b866e34551cf3ad34250c58e92f322714ba9d8da/packages/micromark-util-normalize-identifier/package.json#L34-L42 It’s using a very new, Node.js-specific, compile time, replacement of that. Defaulting to production code for everyone, and only when asked to load development code with this new feature, loads code with assertions/debug statements.
assert
anddebug
are not available in the default production build. They only exist in development.You can decide if you want that. You implicitly / your bundler explicitly ask micromark to load development code which with assertions. If you don’t want the assertions, you can:
--conditions development
by defaultassert
(and maybe other builtins) in developmentassert
to a local package in developmentassert
to an empty packcage in developmentRe: @jalik:
Both.
Node builtins should not be added to packages. They are not used. Some bundlers replace things with those dependencies though, but that’s left up to the final user (you).
Three ideas for micromark:
console.assert
— unfortunatelyconsole.assert
misses features, such as:It prints a warning instead of throwing an error
It’s not typed in TypeScript (because it doesn’t throw an error):
unassert
@types/power-assert
but without assertions:[edit: hit send too early]:
Thanks!
micromark has a quite modern set up, which was Node specific, to load some extra code when explicitly debugging. To summarize, the extra code is compiled away, you won’t see it in production, so using
assert
orpowerassert
won’t impact performance or bundle size. But this problem arose because several tools now started defaulting to this Node-specific debugging code by default.Upstream PR with fixes for
power-assert
typings at https://github.com/DefinitelyTyped/DefinitelyTyped/pull/56062Indeed, I think that’s the blocker. I’ve never used it myself, so perhaps it doesn’t work well, but it says it has the same api as
assert
, so I think it would work!I don’t get your point why browsers should load codes which are node specific module:
assert
.