generated from dellevin/template
1
This commit is contained in:
21
node_modules/module-alias/LICENSE
generated
vendored
Normal file
21
node_modules/module-alias/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2018, Nick Gavrilov
|
||||
|
||||
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.
|
||||
246
node_modules/module-alias/README.md
generated
vendored
Normal file
246
node_modules/module-alias/README.md
generated
vendored
Normal file
@@ -0,0 +1,246 @@
|
||||
# module-alias
|
||||
|
||||
[![NPM Version][npm-image]][npm-url]
|
||||
|
||||
Donations are much appreciated and would help me continue working on this package! [Donate here ❤️](https://tinyurl.com/donate-module-alias).
|
||||
|
||||
---
|
||||
|
||||
Create aliases of directories and register custom module paths in NodeJS like a boss!
|
||||
|
||||
No more long paths in Node, like:
|
||||
|
||||
```js
|
||||
import something from ('../../../../some/very/deep/module');
|
||||
```
|
||||
|
||||
Enough of this madness!
|
||||
|
||||
Just create an alias and do it the right way:
|
||||
|
||||
```js
|
||||
import module from '@deep/module'
|
||||
// Or CommonJS
|
||||
const module = require('@deep/module')
|
||||
```
|
||||
|
||||
It also allows you to register directories that will act just like `node_modules` but with your own private modules, so that you can access them directly:
|
||||
|
||||
```js
|
||||
import module from 'my_private_module'
|
||||
// Or CommonJS
|
||||
const module = require('my_private_module')
|
||||
```
|
||||
|
||||
**WARNING:** If you are going to use this package within another NPM package, please read [Using within another NPM package](#using-within-another-npm-package) first to be aware of potential caveats.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
npm i --save module-alias
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### ES Modules (Node 18.19+)
|
||||
|
||||
Add your custom configuration to your `package.json` (in your application's root):
|
||||
|
||||
```json
|
||||
{
|
||||
"_moduleAliases": {
|
||||
"@root": ".",
|
||||
"@lib": "src/lib",
|
||||
"@utils": "src/utils"
|
||||
},
|
||||
"_moduleDirectories": ["node_modules_custom"]
|
||||
}
|
||||
```
|
||||
|
||||
Run your app with the `--import` flag:
|
||||
|
||||
```bash
|
||||
node --import module-alias/register ./app.mjs # Or use a custom registerer, see below
|
||||
```
|
||||
|
||||
**Why the `--import` flag?**
|
||||
|
||||
Unlike CommonJS, you cannot import `module-alias/register` at runtime. All `import` statements are hoisted and resolved before any code runs. The `--import` flag loads the loader hooks before your application starts.
|
||||
|
||||
### Programmatic ESM Usage (Node 22.15+):
|
||||
|
||||
For programmatic alias registration, create a custom loader file:
|
||||
|
||||
```js
|
||||
// my-aliases.mjs
|
||||
import { addAlias, addAliases } from 'module-alias'
|
||||
|
||||
addAlias('@utils', process.cwd() + '/src/utils')
|
||||
// or
|
||||
addAliases({
|
||||
'@utils': process.cwd() + '/src/utils',
|
||||
'@lib': process.cwd() + '/src/lib'
|
||||
})
|
||||
|
||||
// Custom handler function
|
||||
addAlias('@src', (fromPath, request, alias) => {
|
||||
// fromPath - Full path of the file from which `import` was called
|
||||
// request - The path that was passed into `import` (e.g. '@src/utils.js')
|
||||
// alias - The alias being matched (`@src` in this case)
|
||||
const subpath = request.slice(alias.length) // e.g. '/utils.js'
|
||||
const base = fromPath.includes('/tests/') ? '/mocks' : '/src'
|
||||
return process.cwd() + base + subpath
|
||||
})
|
||||
```
|
||||
|
||||
Then use it with the `--import` flag:
|
||||
|
||||
```bash
|
||||
node --import ./my-aliases.mjs ./app.mjs
|
||||
```
|
||||
|
||||
### CommonJS (older Node 12+ versions)
|
||||
|
||||
Add your custom configuration to your `package.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"_moduleAliases": {
|
||||
"@root": ".",
|
||||
"@deep": "src/some/very/deep/directory/or/file",
|
||||
"@my_module": "lib/some-file.js"
|
||||
},
|
||||
"_moduleDirectories": ["node_modules_custom"]
|
||||
}
|
||||
```
|
||||
|
||||
Then add this line at the very main file of your app, before any code:
|
||||
|
||||
```js
|
||||
require('module-alias/register')
|
||||
```
|
||||
|
||||
Now you can use aliases:
|
||||
|
||||
```js
|
||||
require('something')
|
||||
const module = require('@root/some-module')
|
||||
const veryDeepModule = require('@deep/my-module')
|
||||
```
|
||||
|
||||
## Programmatic CommonJS usage
|
||||
|
||||
If you don't want to modify your `package.json` or you just prefer to set it all up programmatically, then the following methods are available for you:
|
||||
|
||||
* `addAlias('alias', 'target_path')` - register a single alias
|
||||
* `addAliases({ 'alias': 'target_path', ... }) ` - register multiple aliases
|
||||
* `addPath(path)` - Register custom modules directory (like node_modules, but with your own modules)
|
||||
|
||||
_Examples:_
|
||||
```js
|
||||
const moduleAlias = require('module-alias')
|
||||
|
||||
//
|
||||
// Register alias
|
||||
//
|
||||
moduleAlias.addAlias('@client', __dirname + '/src/client')
|
||||
|
||||
// Or multiple aliases
|
||||
moduleAlias.addAliases({
|
||||
'@root' : __dirname,
|
||||
'@client': __dirname + '/src/client',
|
||||
...
|
||||
})
|
||||
|
||||
// Custom handler function (starting from v2.1)
|
||||
moduleAlias.addAlias('@src', (fromPath, request, alias) => {
|
||||
// fromPath - Full path of the file from which `require` was called
|
||||
// request - The path (first argument) that was passed into `require`
|
||||
// alias - The same alias that was passed as first argument to `addAlias` (`@src` in this case)
|
||||
|
||||
// Return any custom target path for the `@src` alias depending on arguments
|
||||
if (fromPath.startsWith(__dirname + '/others')) return __dirname + '/others'
|
||||
return __dirname + '/src'
|
||||
})
|
||||
|
||||
//
|
||||
// Register custom modules directory
|
||||
//
|
||||
moduleAlias.addPath(__dirname + '/node_modules_custom')
|
||||
moduleAlias.addPath(__dirname + '/src')
|
||||
|
||||
//
|
||||
// Import settings from a specific package.json
|
||||
//
|
||||
moduleAlias(__dirname + '/package.json')
|
||||
|
||||
// Or let module-alias to figure where your package.json is
|
||||
// located. By default it will look in the same directory
|
||||
// where you have your node_modules (application's root)
|
||||
moduleAlias()
|
||||
```
|
||||
|
||||
## Usage with Webpack
|
||||
|
||||
Luckily, WebPack has a built in support for aliases and custom modules directories so it's easy to make it work on the client side as well!
|
||||
|
||||
```js
|
||||
// webpack.config.js
|
||||
const npm_package = require('./package.json')
|
||||
|
||||
module.exports = {
|
||||
entry: { ... },
|
||||
resolve: {
|
||||
root: __dirname,
|
||||
alias: npm_package._moduleAliases || {},
|
||||
modules: npm_package._moduleDirectories || [] // eg: ["node_modules", "node_modules_custom", "src"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
More details on the [official documentation](https://webpack.js.org/configuration/resolve).
|
||||
|
||||
## Usage with Jest
|
||||
|
||||
Unfortunately, `module-alias` itself would not work from Jest due to a custom behavior of Jest's `require`. But you can use it's own aliasing mechanism instead. The configuration can be defined either in `package.json` or `jest.config.js`. The example below is for `package.json`:
|
||||
|
||||
```js
|
||||
"jest": {
|
||||
"moduleNameMapper": {
|
||||
"@root/(.*)": "<rootDir>/$1",
|
||||
"@client/(.*)": "<rootDir>/src/client/$1"
|
||||
},
|
||||
}
|
||||
```
|
||||
|
||||
More details on the [official documentation](https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring).
|
||||
|
||||
## Using within another NPM package
|
||||
|
||||
You can use `module-alias` within another NPM package, however there are a few things to take into consideration.
|
||||
|
||||
1. As the aliases are global, you should make sure your aliases are unique, to avoid conflicts with end-user code, or with other libraries using module-alias. For example, you could prefix your aliases with '@my-lib/', and then use require('@my-lib/deep').
|
||||
2. The internal "register" mechanism may not work, you should not rely on `require('module-alias/register')` for automatic detection of `package.json` location (where you defined your aliases), as it tries to find package.json in either the current working directory of your node process, or two levels down from node_modules/module-alias. It is extremely likely that this is end-user code. So, instead, your should either register aliases manually with `moduleAlias.addAlias`, or using something like `require('module-alias')(__dirname)`.
|
||||
|
||||
Here is an [example project](https://github.com/Kehrlann/module-alias-library).
|
||||
|
||||
## Known incompatibilities
|
||||
|
||||
This module does not play well with:
|
||||
|
||||
- Front-end JavaScript code. Module-alias is designed for server side so do not expect it to work with front-end frameworks (React, Vue, ...) as they tend to use Webpack. Use Webpack's [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealias) mechanism instead.
|
||||
- [Jest](https://jestjs.io), which discards node's module system entirely to use it's own module system, bypassing module-alias.
|
||||
- The [NCC compiler](https://github.com/zeit/ncc), as it uses WebPack under the hood without exposing properties, such as resolve.alias. It is not [something they wish to do](https://github.com/zeit/ncc/pull/460).
|
||||
|
||||
## Refactor your code (for already existing projects)
|
||||
|
||||
If you are using this on an existing project, you can use [relative-to-alias](https://github.com/s-yadav/relative-to-alias) to refactor your code to start using aliases.
|
||||
|
||||
## Special Thanks
|
||||
|
||||
Special thanks to [Artur Havrylov](https://github.com/artnikbrothers) and [Daniel Garnier-Moiroux](https://www.npmjs.com/~kehrlann) for valuable contributions.
|
||||
|
||||
[npm-image]: https://img.shields.io/npm/v/module-alias.svg
|
||||
[npm-url]: https://npmjs.org/package/module-alias
|
||||
[travis-image]: https://img.shields.io/travis/ilearnio/module-alias/master.svg
|
||||
[travis-url]: https://travis-ci.org/ilearnio/module-alias
|
||||
152
node_modules/module-alias/esm-loader.mjs
generated
vendored
Normal file
152
node_modules/module-alias/esm-loader.mjs
generated
vendored
Normal file
@@ -0,0 +1,152 @@
|
||||
// ESM loader for module-alias
|
||||
// Provides resolve hooks for ES modules
|
||||
|
||||
import { readFileSync, existsSync, statSync } from 'node:fs'
|
||||
import { join, resolve as pathResolve, dirname } from 'node:path'
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url'
|
||||
|
||||
let aliases = {}
|
||||
let moduleDirectories = []
|
||||
let base = process.cwd()
|
||||
let initialized = false
|
||||
|
||||
export function init (options = {}) {
|
||||
if (initialized) return
|
||||
|
||||
const thisDir = dirname(fileURLToPath(import.meta.url))
|
||||
const candidatePaths = options.base
|
||||
? [pathResolve(options.base)]
|
||||
: [join(thisDir, '../..'), process.cwd()]
|
||||
|
||||
let pkg
|
||||
for (const candidate of candidatePaths) {
|
||||
try {
|
||||
pkg = JSON.parse(readFileSync(join(candidate, 'package.json'), 'utf8'))
|
||||
base = candidate
|
||||
break
|
||||
} catch (e) {
|
||||
// Continue to next candidate
|
||||
}
|
||||
}
|
||||
|
||||
if (!pkg) {
|
||||
console.warn('[module-alias] Unable to find package.json in:', candidatePaths.join(', '))
|
||||
initialized = true
|
||||
return
|
||||
}
|
||||
|
||||
// Load _moduleAliases
|
||||
const pkgAliases = pkg._moduleAliases || {}
|
||||
for (const alias in pkgAliases) {
|
||||
const target = pkgAliases[alias]
|
||||
aliases[alias] = target.startsWith('/') ? target : join(base, target)
|
||||
}
|
||||
|
||||
// Load _moduleDirectories
|
||||
if (Array.isArray(pkg._moduleDirectories)) {
|
||||
moduleDirectories = pkg._moduleDirectories
|
||||
.filter(d => d !== 'node_modules')
|
||||
.map(d => join(base, d))
|
||||
}
|
||||
|
||||
initialized = true
|
||||
}
|
||||
|
||||
export function isPathMatchesAlias (path, alias) {
|
||||
// Matching /^alias(\/|$)/
|
||||
if (path.indexOf(alias) === 0) {
|
||||
if (path.length === alias.length) return true
|
||||
if (path[alias.length] === '/') return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function resolveAlias (specifier, parentURL) {
|
||||
init() // Ensure initialized
|
||||
|
||||
// Sort aliases by length (longest first) for correct matching
|
||||
const sortedAliases = Object.keys(aliases).sort((a, b) => b.length - a.length)
|
||||
|
||||
for (const alias of sortedAliases) {
|
||||
if (isPathMatchesAlias(specifier, alias)) {
|
||||
const target = aliases[alias]
|
||||
|
||||
// Function-based resolver
|
||||
if (typeof target === 'function') {
|
||||
const parentPath = parentURL ? fileURLToPath(parentURL) : process.cwd()
|
||||
const result = target(parentPath, specifier, alias)
|
||||
if (!result || typeof result !== 'string') {
|
||||
throw new Error('[module-alias] Custom handler must return path')
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// String path - join target with remainder of specifier
|
||||
return join(target, specifier.slice(alias.length))
|
||||
}
|
||||
}
|
||||
|
||||
// Check moduleDirectories
|
||||
for (const dir of moduleDirectories) {
|
||||
const modulePath = join(dir, specifier)
|
||||
// Check for directory with index.mjs/index.js
|
||||
if (existsSync(join(modulePath, 'index.mjs'))) {
|
||||
return join(modulePath, 'index.mjs')
|
||||
}
|
||||
if (existsSync(join(modulePath, 'index.js'))) {
|
||||
return join(modulePath, 'index.js')
|
||||
}
|
||||
// Check for file with extension
|
||||
if (existsSync(modulePath + '.mjs')) {
|
||||
return modulePath + '.mjs'
|
||||
}
|
||||
if (existsSync(modulePath + '.js')) {
|
||||
return modulePath + '.js'
|
||||
}
|
||||
// Check for exact file
|
||||
if (existsSync(modulePath) && !statSync(modulePath).isDirectory()) {
|
||||
return modulePath
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
export function addAlias (alias, target) {
|
||||
aliases[alias] = target
|
||||
}
|
||||
|
||||
export function addAliases (aliasMap) {
|
||||
for (const alias in aliasMap) {
|
||||
addAlias(alias, aliasMap[alias])
|
||||
}
|
||||
}
|
||||
|
||||
export function addPath (path) {
|
||||
moduleDirectories.push(path)
|
||||
}
|
||||
|
||||
export function reset () {
|
||||
aliases = {}
|
||||
moduleDirectories = []
|
||||
base = process.cwd()
|
||||
initialized = false
|
||||
}
|
||||
|
||||
// For Node 18-21: async loader hooks
|
||||
export async function resolve (specifier, context, nextResolve) {
|
||||
const resolved = resolveAlias(specifier, context.parentURL)
|
||||
if (resolved) {
|
||||
// If absolute path, convert to file URL
|
||||
if (resolved.startsWith('/')) {
|
||||
return { url: pathToFileURL(resolved).href, shortCircuit: true }
|
||||
}
|
||||
// Otherwise let Node resolve it (could be npm package)
|
||||
return nextResolve(resolved, context)
|
||||
}
|
||||
return nextResolve(specifier, context)
|
||||
}
|
||||
|
||||
export async function initialize (data) {
|
||||
init(data || {})
|
||||
}
|
||||
40
node_modules/module-alias/index.d.ts
generated
vendored
Normal file
40
node_modules/module-alias/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Custom resolver function for dynamic alias resolution
|
||||
* @param fromPath - Full path of the file from which import/require was called
|
||||
* @param request - The full import path (e.g. '@src/utils.js')
|
||||
* @param alias - The alias being matched (e.g. '@src')
|
||||
* @returns The resolved absolute path
|
||||
*/
|
||||
type AliasResolver = (fromPath: string, request: string, alias: string) => string;
|
||||
|
||||
/**
|
||||
* Register a single alias
|
||||
* @param alias - The alias to register (e.g. '@utils')
|
||||
* @param target - The target path or a resolver function
|
||||
*/
|
||||
export function addAlias(alias: string, target: string | AliasResolver): void;
|
||||
|
||||
/**
|
||||
* Register multiple aliases at once
|
||||
* @param aliases - Object mapping aliases to target paths or resolver functions
|
||||
*/
|
||||
export function addAliases(aliases: Record<string, string | AliasResolver>): void;
|
||||
|
||||
/**
|
||||
* Register a custom module directory (like node_modules)
|
||||
* @param path - The directory path to add
|
||||
*/
|
||||
export function addPath(path: string): void;
|
||||
|
||||
/**
|
||||
* Reset all registered aliases and paths
|
||||
*/
|
||||
export function reset(): void;
|
||||
|
||||
/**
|
||||
* Initialize module-alias from a package.json file
|
||||
* @param options - Path to package.json or options object
|
||||
*/
|
||||
declare function moduleAlias(options?: string | { base?: string }): void;
|
||||
|
||||
export default moduleAlias;
|
||||
224
node_modules/module-alias/index.js
generated
vendored
Normal file
224
node_modules/module-alias/index.js
generated
vendored
Normal file
@@ -0,0 +1,224 @@
|
||||
'use strict'
|
||||
|
||||
var BuiltinModule = require('module')
|
||||
|
||||
// Guard against poorly mocked module constructors
|
||||
var Module = module.constructor.length > 1
|
||||
? module.constructor
|
||||
: BuiltinModule
|
||||
|
||||
var nodePath = require('path')
|
||||
|
||||
var modulePaths = []
|
||||
var moduleAliases = {}
|
||||
var moduleAliasNames = []
|
||||
|
||||
var oldNodeModulePaths = Module._nodeModulePaths
|
||||
Module._nodeModulePaths = function (from) {
|
||||
var paths = oldNodeModulePaths.call(this, from)
|
||||
|
||||
// Only include the module path for top-level modules
|
||||
// that were not installed:
|
||||
if (from.indexOf('node_modules') === -1) {
|
||||
paths = modulePaths.concat(paths)
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
var oldResolveFilename = Module._resolveFilename
|
||||
Module._resolveFilename = function (request, parentModule, isMain, options) {
|
||||
for (var i = moduleAliasNames.length; i-- > 0;) {
|
||||
var alias = moduleAliasNames[i]
|
||||
if (isPathMatchesAlias(request, alias)) {
|
||||
var aliasTarget = moduleAliases[alias]
|
||||
// Custom function handler
|
||||
if (typeof moduleAliases[alias] === 'function') {
|
||||
var fromPath = parentModule.filename
|
||||
aliasTarget = moduleAliases[alias](fromPath, request, alias)
|
||||
if (!aliasTarget || typeof aliasTarget !== 'string') {
|
||||
throw new Error('[module-alias] Expecting custom handler function to return path.')
|
||||
}
|
||||
}
|
||||
request = nodePath.join(aliasTarget, request.substr(alias.length))
|
||||
// Only use the first match
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return oldResolveFilename.call(this, request, parentModule, isMain, options)
|
||||
}
|
||||
|
||||
function isPathMatchesAlias (path, alias) {
|
||||
// Matching /^alias(\/|$)/
|
||||
if (path.indexOf(alias) === 0) {
|
||||
if (path.length === alias.length) return true
|
||||
if (path[alias.length] === '/') return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
function addPathHelper (path, targetArray) {
|
||||
path = nodePath.normalize(path)
|
||||
if (targetArray && targetArray.indexOf(path) === -1) {
|
||||
targetArray.unshift(path)
|
||||
}
|
||||
}
|
||||
|
||||
function removePathHelper (path, targetArray) {
|
||||
if (targetArray) {
|
||||
var index = targetArray.indexOf(path)
|
||||
if (index !== -1) {
|
||||
targetArray.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addPath (path) {
|
||||
var parent
|
||||
path = nodePath.normalize(path)
|
||||
|
||||
if (modulePaths.indexOf(path) === -1) {
|
||||
modulePaths.push(path)
|
||||
// Enable the search path for the current top-level module
|
||||
var mainModule = getMainModule()
|
||||
if (mainModule) {
|
||||
addPathHelper(path, mainModule.paths)
|
||||
}
|
||||
parent = module.parent
|
||||
|
||||
// Also modify the paths of the module that was used to load the
|
||||
// app-module-paths module and all of it's parents
|
||||
while (parent && parent !== mainModule) {
|
||||
addPathHelper(path, parent.paths)
|
||||
parent = parent.parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function addAliases (aliases) {
|
||||
for (var alias in aliases) {
|
||||
addAlias(alias, aliases[alias])
|
||||
}
|
||||
}
|
||||
|
||||
function addAlias (alias, target) {
|
||||
moduleAliases[alias] = target
|
||||
// Cost of sorting is lower here than during resolution
|
||||
moduleAliasNames = Object.keys(moduleAliases)
|
||||
moduleAliasNames.sort()
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset any changes maded (resets all registered aliases
|
||||
* and custom module directories)
|
||||
* The function is undocumented and for testing purposes only
|
||||
*/
|
||||
function reset () {
|
||||
var mainModule = getMainModule()
|
||||
|
||||
// Reset all changes in paths caused by addPath function
|
||||
modulePaths.forEach(function (path) {
|
||||
if (mainModule) {
|
||||
removePathHelper(path, mainModule.paths)
|
||||
}
|
||||
|
||||
// Delete from require.cache if the module has been required before.
|
||||
// This is required for node >= 11
|
||||
Object.getOwnPropertyNames(require.cache).forEach(function (name) {
|
||||
if (name.indexOf(path) !== -1) {
|
||||
delete require.cache[name]
|
||||
}
|
||||
})
|
||||
|
||||
var parent = module.parent
|
||||
while (parent && parent !== mainModule) {
|
||||
removePathHelper(path, parent.paths)
|
||||
parent = parent.parent
|
||||
}
|
||||
})
|
||||
|
||||
modulePaths = []
|
||||
moduleAliases = {}
|
||||
moduleAliasNames = []
|
||||
}
|
||||
|
||||
/**
|
||||
* Import aliases from package.json
|
||||
* @param {object} options
|
||||
*/
|
||||
function init (options) {
|
||||
if (typeof options === 'string') {
|
||||
options = { base: options }
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
|
||||
var candidatePackagePaths
|
||||
if (options.base) {
|
||||
candidatePackagePaths = [nodePath.resolve(options.base.replace(/\/package\.json$/, ''))]
|
||||
} else {
|
||||
// There is probably 99% chance that the project root directory in located
|
||||
// above the node_modules directory,
|
||||
// Or that package.json is in the node process' current working directory (when
|
||||
// running a package manager script, e.g. `yarn start` / `npm run start`)
|
||||
candidatePackagePaths = [nodePath.join(__dirname, '../..'), process.cwd()]
|
||||
}
|
||||
|
||||
var npmPackage
|
||||
var base
|
||||
for (var i in candidatePackagePaths) {
|
||||
try {
|
||||
base = candidatePackagePaths[i]
|
||||
|
||||
npmPackage = require(nodePath.join(base, 'package.json'))
|
||||
break
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof npmPackage !== 'object') {
|
||||
var pathString = candidatePackagePaths.join(',\n')
|
||||
throw new Error('Unable to find package.json in any of:\n[' + pathString + ']')
|
||||
}
|
||||
|
||||
//
|
||||
// Import aliases
|
||||
//
|
||||
|
||||
var aliases = npmPackage._moduleAliases || {}
|
||||
|
||||
for (var alias in aliases) {
|
||||
if (aliases[alias][0] !== '/') {
|
||||
aliases[alias] = nodePath.join(base, aliases[alias])
|
||||
}
|
||||
}
|
||||
|
||||
addAliases(aliases)
|
||||
|
||||
//
|
||||
// Register custom module directories (like node_modules)
|
||||
//
|
||||
|
||||
if (npmPackage._moduleDirectories instanceof Array) {
|
||||
npmPackage._moduleDirectories.forEach(function (dir) {
|
||||
if (dir === 'node_modules') return
|
||||
|
||||
var modulePath = nodePath.join(base, dir)
|
||||
addPath(modulePath)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function getMainModule () {
|
||||
return require.main._simulateRepl ? undefined : require.main
|
||||
}
|
||||
|
||||
module.exports = init
|
||||
module.exports.addPath = addPath
|
||||
module.exports.addAlias = addAlias
|
||||
module.exports.addAliases = addAliases
|
||||
module.exports.isPathMatchesAlias = isPathMatchesAlias
|
||||
module.exports.reset = reset
|
||||
66
node_modules/module-alias/index.mjs
generated
vendored
Normal file
66
node_modules/module-alias/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
// ESM entry point for module-alias
|
||||
// Provides programmatic API with auto-registered hooks
|
||||
|
||||
import { registerHooks } from 'node:module'
|
||||
import { addAlias as _addAlias, addAliases as _addAliases, addPath as _addPath, reset as _reset, resolveAlias } from './esm-loader.mjs'
|
||||
|
||||
let hooksRegistered = false
|
||||
|
||||
function ensureHooks() {
|
||||
if (hooksRegistered) return
|
||||
|
||||
const [major, minor] = process.versions.node.split('.').map(Number)
|
||||
const hasRegisterHooks = major > 22 || (major === 22 && minor >= 15)
|
||||
|
||||
if (!hasRegisterHooks) {
|
||||
console.warn('[module-alias] Programmatic ESM usage requires Node 22.15+. For older versions, use module-alias/register with _moduleAliases in package.json.')
|
||||
return
|
||||
}
|
||||
|
||||
registerHooks({
|
||||
resolve(specifier, context, nextResolve) {
|
||||
const resolved = resolveAlias(specifier, context.parentURL)
|
||||
if (resolved) {
|
||||
return nextResolve(resolved, context)
|
||||
}
|
||||
return nextResolve(specifier, context)
|
||||
}
|
||||
})
|
||||
|
||||
hooksRegistered = true
|
||||
}
|
||||
|
||||
export function addAlias(alias, target) {
|
||||
ensureHooks()
|
||||
_addAlias(alias, target)
|
||||
}
|
||||
|
||||
export function addAliases(aliases) {
|
||||
ensureHooks()
|
||||
_addAliases(aliases)
|
||||
}
|
||||
|
||||
export function addPath(path) {
|
||||
ensureHooks()
|
||||
_addPath(path)
|
||||
}
|
||||
|
||||
export function reset() {
|
||||
_reset()
|
||||
hooksRegistered = false
|
||||
}
|
||||
|
||||
// Default export for compatibility with: import moduleAlias from 'module-alias'
|
||||
import { init } from './esm-loader.mjs'
|
||||
|
||||
function moduleAlias(options) {
|
||||
ensureHooks()
|
||||
init(options)
|
||||
}
|
||||
|
||||
moduleAlias.addAlias = addAlias
|
||||
moduleAlias.addAliases = addAliases
|
||||
moduleAlias.addPath = addPath
|
||||
moduleAlias.reset = reset
|
||||
|
||||
export default moduleAlias
|
||||
69
node_modules/module-alias/package.json
generated
vendored
Normal file
69
node_modules/module-alias/package.json
generated
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
{
|
||||
"name": "module-alias",
|
||||
"description": "Create aliases of directories and register custom module paths",
|
||||
"version": "2.3.4",
|
||||
"author": {
|
||||
"name": "Nick Gavrilov",
|
||||
"email": "artnikpro@gmail.com"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run lint && npm run testonly",
|
||||
"testonly": "NODE_ENV=test mocha test/specs.js",
|
||||
"testonly-watch": "NODE_ENV=test mocha -w test/specs.js",
|
||||
"test:esm": "NODE_ENV=test mocha test/esm/unit.mjs test/esm/integration.js",
|
||||
"test:all": "npm run testonly && npm run test:esm",
|
||||
"lint": "standard"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ilearnio/module-alias/issues"
|
||||
},
|
||||
"homepage": "https://github.com/ilearnio/module-alias",
|
||||
"keywords": [
|
||||
"extend",
|
||||
"modules",
|
||||
"node",
|
||||
"path",
|
||||
"resolve"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./index.d.ts",
|
||||
"require": "./index.js",
|
||||
"import": "./index.mjs"
|
||||
},
|
||||
"./register": {
|
||||
"require": "./register.js",
|
||||
"import": "./register.mjs"
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.mjs",
|
||||
"index.d.ts",
|
||||
"register.js",
|
||||
"register.mjs",
|
||||
"esm-loader.mjs",
|
||||
"README.md",
|
||||
"LICENSE"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/ilearnio/module-alias.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"chai": "^3.5.0",
|
||||
"hello-world-classic": "github:ilearnio/hello-world-classic",
|
||||
"husky": "^3.0.2",
|
||||
"mocha": "^2.4.5",
|
||||
"semver": "^6.1.1",
|
||||
"standard": "^12.0.1"
|
||||
},
|
||||
"husky": {
|
||||
"hooks": {
|
||||
"pre-push": "npm run test"
|
||||
}
|
||||
}
|
||||
}
|
||||
1
node_modules/module-alias/register.js
generated
vendored
Normal file
1
node_modules/module-alias/register.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
require('.')()
|
||||
31
node_modules/module-alias/register.mjs
generated
vendored
Normal file
31
node_modules/module-alias/register.mjs
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
// ESM entry point for module-alias
|
||||
// Usage: node --import module-alias/register ./app.mjs
|
||||
|
||||
import { register } from 'node:module'
|
||||
|
||||
// Check Node version for registerHooks support (22.15+)
|
||||
const [major, minor] = process.versions.node.split('.').map(Number)
|
||||
const hasRegisterHooks = major > 22 || (major === 22 && minor >= 15)
|
||||
|
||||
if (hasRegisterHooks) {
|
||||
// Node 22.15+ - use synchronous hooks on main thread
|
||||
const { registerHooks } = await import('node:module')
|
||||
const { resolveAlias, init } = await import('./esm-loader.mjs')
|
||||
|
||||
init()
|
||||
|
||||
registerHooks({
|
||||
resolve (specifier, context, nextResolve) {
|
||||
const resolved = resolveAlias(specifier, context.parentURL)
|
||||
if (resolved) {
|
||||
return nextResolve(resolved, context)
|
||||
}
|
||||
return nextResolve(specifier, context)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// Node 18.19 - 22.14 - use async hooks via worker thread
|
||||
register('./esm-loader.mjs', {
|
||||
parentURL: import.meta.url
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user