-
-
Notifications
You must be signed in to change notification settings - Fork 62
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
assert
is not browser friendly
#87
Comments
|
import type { ComponentType, FC, ReactElement } from 'react'
import { Fragment, useState } from 'react'
import rehypeDomParse from 'rehype-dom-parse'
import rehypeReact from 'rehype-react'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { unified } from 'unified'
import { Loading } from './Loading'
import { useConstant, useEnhancedEffect } from 'hooks'
export const Rehype: FC<{ children?: string; markdown?: boolean }> = ({
children,
markdown,
}) => {
const processor = useConstant(() => {
let processor = unified()
processor = markdown
? processor.use(remarkParse).use(remarkRehype)
: processor.use(rehypeDomParse)
return processor
.use(rehypeReact, {
createElement<T>(
Element: ComponentType<T>,
props: T,
...children: unknown[]
) {
return <Element {...props}>{children}</Element>
},
Fragment,
})
.freeze()
}, [markdown])
const [result, setResult] = useState<ReactElement | null>()
useEnhancedEffect(async () => {
if (children == null) {
setResult(null)
} else {
const { result } = await processor.process(children)
setResult(result)
}
}, [children, setResult])
return result === undefined ? <Loading /> : result
} |
See also: remarkjs/react-markdown#632 (comment)
|
@wooorm For browser/bundler friendly, there are two ways.
// assume.js
export const assertEqual = assert.equal
export const assertDeepEqual = assert.deepEqual
export const debug = _debug('micromark')
// assume-browser.js
export const assertEqual = noop
export const assertDeepEqual = noop
export const debug = noop // package.json
{
"exports": {
"./assume.js": {
"browser": "./assume-browser.js"
}
}
} The first approach is a bit verbose but much more minify friendly for frontend project. |
I like the idea of being more browser compatible. 👍
I'm not sure if this would get us further towards being browser compatible?
This could work 🤔 |
For native browser loading via esm, |
The rule when creating a lib, is to not make it dependent to any tools (bundler in this case), unless you are coding for yourself. |
Re @JounQin:
Please look at how micromark works, here’s a small example: micromark/packages/micromark-util-normalize-identifier/package.json Lines 34 to 42 in b866e34
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.
You can decide if you want that. You implicitly / your bundler explicitly ask micromark to load development code which with assertions.
Re: @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:
|
Btw, I honestly think this is something that Vite should fix: Node does not use I think Vite should behave the same |
Can we have "exports": {
"development": "./dev/index.js",
"default": "./index.js",
"browser": {
"development": "./index.js"
}
} Will this work for all cases? |
Also, were you able to discuss this with Vite maintainers? I remain of the opinion that they should not default to an explicit |
OK, I just open vitejs/vite#4815 for tracking, it would be great if you could get involved. |
Besides, I'm not quite sure to understand your quote. I tried the following and it's working: "exports": {
"browser": "./index.js", // browser will always point to `./index.js` as entry
"development": "./dev/index.js",
"default": "./index.js"
} Please point me if I'm wrong. |
My last comment had to do with the order. Your order earlier was different. Your order is okay now. Your new example is similar to my last suggestion before: #87 (comment). But I think browsers should also be able to load development code. |
I don't get your point why browsers should load codes which are node specific module: |
Browsers or bundlers? Most bundlers handle this fine because they don’t load code marked explicitly as development code. All bundlers can handle |
Cross-referencing same problem in webpack land: remarkjs/remark#831 I'm not familiar enough with packaging to provide any real useful input, but I'm not excited at the prospect of more bundle config. Current best solution for me I think is manually installing |
If micromark is supposed to be environment agnostic, it should not rely on environment specific builtins, in this case, the node built-in assert. Browser's don't have assert built-in, so any code using it will break without a polyfill, regardless of the bundler. If you expect users to polyfill this themselves, you should document that it is a required polyfill so folks expect this and properly configure their bundlers to handle node specific code.
This isn't true, webpack loads code explicitly marked as development code when doing a non-production build/watch. I don't think its reasonable to expect all webpack consumers of this package to do a production build/watch whenever locally deving. |
@maclockard This thread needs a solution rather than convincing. I laid out three solutions at the end of this comment above, but they all have some problems. If you have ideas, or can spend the time working on overcoming those problems, that’d be appreciated.
It did that based on a |
@wooorm re:
If definitely typed were updated to include type assertions in the |
Indeed, I think that’s the blocker. |
Upstream PR with fixes for |
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 The advantages here would be fewer dependencies with more control over behavior. Disadvantages being its more code to maintain for this package. |
[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. |
Fixes published in version 1.5.5 of the types https://www.npmjs.com/package/@types/power-assert/v/1.5.5 |
This comment has been minimized.
This comment has been minimized.
Thanks @ChristianMurphy for updating the types. I’ve released all micromark extensions to not include |
Related-to: <https://twitter.com/lukeed05/status/1446626816328802305>. Related-to: unassert-js/unassert#22. Related-to: GH-87. Closes GH-95.
Initial checklist
Affected packages and versions
latest
Link to runnable example
No response
Steps to reproduce
browserify/node-util#62 (comment)
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
The text was updated successfully, but these errors were encountered: