Specify namespace with url in ESM
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 35/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- javascript, nodejs
- 领域
- backend
调研方向
Start by reviewing the proposed native ESM URL syntax and the package.json exports idea described in the issue. Determine whether namespace and optional color parameters belong in the library, then define the supported import forms and tests needed to show the behavior is complete.
由索引模型根据 Issue 内容生成。
描述
When using native esm in Node, it's not possible anymore to use the pattern
const debug = require('debug')('namespace');
and we have to do
import createDebug from 'debug';
const debug = createDebug('namespace');
instead. In one of my projects, I've found myself to use an alternative approach though, where I do something like
import createDebug from 'debug';
const url = new URL(import.meta.url);
const [[namespace]] = url.searchParams;
export default createDebug(namespace);
so that it becomes possible to do stuff like
// In server
import debug from './debug.js?server';
// In worker
import debug from './debug.js?worker';
I was wondering whether it would be useful to have this functionality in the library itself. I'd be happy to clean my code up a bit and file a PR for this. An alternative approach would be to create my own npm module on top of the debug module if it's not desirable to have this in the library itself.
For the syntax, I was thinking about something like
import debug from 'debug/url?namespace'
where debug/url can become an exports in the package.json.
The url approach can be used to add more functionality as well, for example to force a specific color or something:
import createDebug from 'debug';
const url = new URL(import.meta.url);
const [namespace] = [...url.searchParams].find(([key, value]) => value === '');
const debug = createDebug(namespace);
const color = url.searchParams.get('color');
if (color !== null) {
debug.color = color;
}
export default debug;
I'd love to hear some thoughts about this from the maintainers.
- 主要语言
- JavaScript
- 星标
- 11.5k
- 派生
- 993
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
debug-js/debug 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 82/100
-
难度 4/5 3-5 天 新手友好度 25/100
-
Debug as JSON未关闭
难度 5/5 一周以上 新手友好度 25/100
-
难度 3/5 1-2 天 新手友好度 35/100
-
难度 3/5 1-2 天 新手友好度 28/100