This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+15
View File
@@ -0,0 +1,15 @@
Copyright (c) 2021 David Mark Clements
Permission to
use, copy, modify, and /or distribute this software for any purpose with or
without fee is hereby granted, provided that the above copyright notice and this
permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND
David Mark Clements DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
SHALL David Mark Clements BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.
+13
View File
@@ -0,0 +1,13 @@
'use strict'
const parentModule = require('parent-module')
const { createRequire } = require('module')
module.exports = async (req) => {
const { resolve } = createRequire(parentModule())
try {
const mod = await import(resolve(req))
return mod
} catch (err) {
console.error(err)
process.exit(1)
}
}
+29
View File
@@ -0,0 +1,29 @@
/**
Get the path of the parent module.
@param filePath - File path of the module of which to get the parent path.
Useful if you want it to work [multiple module levels down](https://github.com/sindresorhus/parent-module/tree/master/fixtures/filepath).
Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename)
@example
```
// bar.ts
const parentModule = require('parent-module');
export default () => {
console.log(parentModule());
//=> '/Users/sindresorhus/dev/unicorn/foo.ts'
};
// foo.ts
import bar from './bar';
bar();
```
*/
declare function parentModule(filePath?: string): string | undefined;
export = parentModule;
+37
View File
@@ -0,0 +1,37 @@
'use strict';
const callsites = require('callsites');
module.exports = filePath => {
const stacks = callsites();
if (!filePath) {
return stacks[2].getFileName();
}
let hasSeenValue = false;
// Skip the first stack as it's this function
stacks.shift();
for (const stack of stacks) {
const parentFilePath = stack.getFileName();
if (typeof parentFilePath !== 'string') {
continue;
}
if (parentFilePath === filePath) {
hasSeenValue = true;
continue;
}
// Skip native modules
if (parentFilePath === 'module.js') {
continue;
}
if (hasSeenValue && parentFilePath !== filePath) {
return parentFilePath;
}
}
};
+9
View File
@@ -0,0 +1,9 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+47
View File
@@ -0,0 +1,47 @@
{
"name": "parent-module",
"version": "2.0.0",
"description": "Get the path of the parent module",
"license": "MIT",
"repository": "sindresorhus/parent-module",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"parent",
"module",
"package",
"caller",
"calling",
"module",
"path",
"callsites",
"callsite",
"stacktrace",
"stack",
"trace",
"function",
"file"
],
"dependencies": {
"callsites": "^3.1.0"
},
"devDependencies": {
"ava": "^1.4.1",
"execa": "^1.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
+67
View File
@@ -0,0 +1,67 @@
# parent-module [![Build Status](https://travis-ci.org/sindresorhus/parent-module.svg?branch=master)](https://travis-ci.org/sindresorhus/parent-module)
> Get the path of the parent module
Node.js exposes `module.parent`, but it only gives you the first cached parent, which is not necessarily the actual parent.
## Install
```
$ npm install parent-module
```
## Usage
```js
// bar.js
const parentModule = require('parent-module');
module.exports = () => {
console.log(parentModule());
//=> '/Users/sindresorhus/dev/unicorn/foo.js'
};
```
```js
// foo.js
const bar = require('./bar');
bar();
```
## API
### parentModule([filePath])
By default, it will return the path of the immediate parent.
#### filePath
Type: `string`<br>
Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename)
File path of the module of which to get the parent path.
Useful if you want it to work [multiple module levels down](https://github.com/sindresorhus/parent-module/tree/master/fixtures/filepath).
## Tip
Combine it with [`read-pkg-up`](https://github.com/sindresorhus/read-pkg-up) to read the package.json of the parent module.
```js
const path = require('path');
const readPkgUp = require('read-pkg-up');
const parentModule = require('parent-module');
console.log(readPkgUp.sync({cwd: path.dirname(parentModule())}).pkg);
//=> {name: 'chalk', version: '1.0.0', …}
```
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
+34
View File
@@ -0,0 +1,34 @@
{
"name": "inclusion",
"version": "1.0.1",
"description": "Dynamic imports for all",
"main": "index.js",
"scripts": {
"test": "tap --no-esm"
},
"keywords": [
"import",
"require",
"dynamic",
"typescript",
"babel",
"esm",
"cjs"
],
"author": "@davidmarkclem",
"license": "ISC",
"dependencies": {
"parent-module": "^2.0.0"
},
"devDependencies": {
"tap": "^14.11.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/davidmarkclements/inclusion.git"
},
"bugs": {
"url": "https://github.com/davidmarkclements/inclusion/issues"
},
"homepage": "https://github.com/davidmarkclements/inclusion#readme"
}
+50
View File
@@ -0,0 +1,50 @@
# inclusion
> Dynamic imports for all
Using dynamic `import` in legacy compiled-to-JS environments can be problematic. It's often transpiled to `require` by Babel, TypeScript etc. Configurations can be changed to not do this, but that can have other unintended side effects on resulting compiled code.
`inclusion` wraps `import` so that it can be used in these scenarios. It's also fine to use it for other scenarios.
## API
### `inclusion(modulePath) => Promise => exports`
Given an ESM module (`someEsmModule`) like the following:
```js
export default function () { return 'hi' }
export const ABC = 123
```
We can import and interact with it in a CJS environment (which is what most compile-to-JS libraries still currently do, as used in the wild), like so:
```js
'use strict'
async function doSomething () {
const { default: someEsmModule, ABC } = await inclusion('some-esm-module')
console.log(someEsmModule(), ABC) // hi 123
}
```
## Test
```sh
npm test
```
```
Suites: 1 passed, 1 of 1 completed
Asserts: 3 passed, of 3
Time: 456.13ms
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
```
## LICENSE
ISC
+25
View File
@@ -0,0 +1,25 @@
'use strict'
const { test } = require('tap')
const parentModule = require('parent-module')
const include = require('.')
test('dynamically imports module', async ({ same }) => {
const mod = await include('parent-module')
same(mod.default, parentModule)
})
test('dynamically outputs error and exits with 1 on error', async ({ plan, is, match, teardown }) => {
const { error } = console
const { exit } = process
teardown(async () => {
console.error = error
process.exit = exit
})
plan(2)
console.error = (err) => {
match(err.message, 'Cannot find module \'not-a-module\'')
}
process.exit = (code) => {
is(code, 1)
}
await include('not-a-module')
})