Import/Require with namespace. 基于命名空间的 import 和 require。
主要代码和灵感参考自:https://github.com/shigma/ns-require
在此表示感谢。
https://github.com/CaoMeiYouRen/ns-import#readme
- node >=16
npm install ns-import
**注意:ES modules 和 CommonJS 两种使用方法不可混用! **
本项目仅适用于 NodeJS 端!无法在浏览器中运行!
// Basic Usage
import { ns } from 'ns-import'
const scope = ns({
namespace: 'awesome',
prefix: 'plugin',
})
// 注意动态 import 需要配合 await 使用
await scope.import('foo') // will resolve to `awesome-plugin-foo`
await scope.import('@foo/bar') // will resolve to `@foo/awesome-plugin-bar`
// With Official Scope
import { ns } from 'ns-import'
const scope = ns({
namespace: 'awesome',
prefix: 'plugin',
official: 'scope',
})
// 注意动态 import 需要配合 await 使用
await scope.import('foo') // will resolve to `@scope/plugin-foo`
// and then `awesome-plugin-foo`
await scope.import('@foo/bar') // will resolve to `@foo/awesome-plugin-bar`
// Basic Usage
const { ns } = require('ns-import')
const scope = ns({
namespace: 'awesome',
prefix: 'plugin',
})
scope.require('foo') // will resolve to `awesome-plugin-foo`
scope.require('@foo/bar') // will resolve to `@foo/awesome-plugin-bar`
// With Official Scope
const { ns } = require('ns-import')
const scope = ns({
namespace: 'awesome',
prefix: 'plugin',
official: 'scope',
})
scope.require('foo') // will resolve to `@scope/plugin-foo`
// and then `awesome-plugin-foo`
scope.require('@foo/bar') // will resolve to `@foo/awesome-plugin-bar`
npm run dev
npm run build
npm run lint
npm run commit
CaoMeiYouRen
-
Website: https://blog.cmyr.ltd/
-
GitHub: @CaoMeiYouRen
欢迎 贡献、提问或提出新功能!
如有问题请查看 issues page.
贡献或提出新功能可以查看contributing guide.
如果觉得这个项目有用的话请给一颗⭐️,非常感谢
Copyright © 2023 CaoMeiYouRen.
This project is MIT licensed.
This README was generated with ❤️ by cmyr-template-cli