Comparing version 4.2.7 to 4.3.0
@@ -0,1 +1,5 @@ | ||
# v4.3.0 | ||
- Add support for [Native Node ES modules](https://nodejs.org/api/esm.html) on Node v14+ ([#211](https://github.com/jeffijoe/awilix/pull/211), [Richard Simko](https://github.com/richardsimko)) | ||
# v4.2.7 | ||
@@ -2,0 +6,0 @@ |
@@ -5,2 +5,3 @@ import { sync } from 'glob'; | ||
import { camelCase } from 'camel-case'; | ||
import { importModule } from './load-module-native.js'; | ||
@@ -533,2 +534,27 @@ /** | ||
/*! ***************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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. | ||
***************************************************************************** */ | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
/** | ||
@@ -1026,2 +1052,5 @@ * Lifetime types. | ||
* | ||
* @param {boolean} opts.esModules | ||
* Set to `true` to use Node's native ECMAScriptModules modules | ||
* | ||
* @return {Object} | ||
@@ -1031,50 +1060,96 @@ * Returns an object describing the result. | ||
function loadModules(dependencies, globPatterns, opts) { | ||
opts !== null && opts !== void 0 ? opts : (opts = {}); | ||
const container = dependencies.container; | ||
opts = optsWithDefaults(opts); | ||
const modules = dependencies.listModules(globPatterns, opts); | ||
const result = modules.map((m) => { | ||
const items = []; | ||
const loaded = dependencies.require(m.path); | ||
// Meh, it happens. | ||
if (!loaded) { | ||
return items; | ||
if (opts.esModules) { | ||
return loadEsModules(dependencies, container, modules, opts); | ||
} | ||
else { | ||
const result = modules.map((m) => { | ||
const loaded = dependencies.require(m.path); | ||
return parseLoadedModule(loaded, m); | ||
}); | ||
return registerModules(result, container, modules, opts); | ||
} | ||
} | ||
/** | ||
* Loads the modules using native ES6 modules and the async import() | ||
* @param {AwilixContainer} container | ||
* @param {ModuleDescriptor[]} modules | ||
* @param {LoadModulesOptions} opts | ||
*/ | ||
function loadEsModules(dependencies, container, modules, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const importPromises = []; | ||
for (const m of modules) { | ||
importPromises.push(dependencies.require(m.path)); | ||
} | ||
if (isFunction(loaded)) { | ||
// for module.exports = ... | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded, | ||
opts: m.opts, | ||
}); | ||
return items; | ||
const imports = yield Promise.all(importPromises); | ||
const result = []; | ||
for (let i = 0; i < modules.length; i++) { | ||
result.push(parseLoadedModule(imports[i], modules[i])); | ||
} | ||
if (loaded.default && isFunction(loaded.default)) { | ||
// ES6 default export | ||
return registerModules(result, container, modules, opts); | ||
}); | ||
} | ||
/** | ||
* Parses the module which has been required | ||
* | ||
* @param {any} loaded | ||
* @param {ModuleDescriptor} m | ||
*/ | ||
function parseLoadedModule(loaded, m) { | ||
const items = []; | ||
// Meh, it happens. | ||
if (!loaded) { | ||
return items; | ||
} | ||
if (isFunction(loaded)) { | ||
// for module.exports = ... | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded, | ||
opts: m.opts, | ||
}); | ||
return items; | ||
} | ||
if (loaded.default && isFunction(loaded.default)) { | ||
// ES6 default export | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded.default, | ||
opts: m.opts, | ||
}); | ||
} | ||
// loop through non-default exports, but require the RESOLVER property set for | ||
// it to be a valid service module export. | ||
for (const key of Object.keys(loaded)) { | ||
if (key === 'default') { | ||
// default case handled separately due to its different name (file name) | ||
continue; | ||
} | ||
if (isFunction(loaded[key]) && RESOLVER in loaded[key]) { | ||
items.push({ | ||
name: m.name, | ||
name: key, | ||
path: m.path, | ||
value: loaded.default, | ||
value: loaded[key], | ||
opts: m.opts, | ||
}); | ||
} | ||
// loop through non-default exports, but require the RESOLVER property set for | ||
// it to be a valid service module export. | ||
for (const key of Object.keys(loaded)) { | ||
if (key === 'default') { | ||
// default case handled separately due to its different name (file name) | ||
continue; | ||
} | ||
if (isFunction(loaded[key]) && RESOLVER in loaded[key]) { | ||
items.push({ | ||
name: key, | ||
path: m.path, | ||
value: loaded[key], | ||
opts: m.opts, | ||
}); | ||
} | ||
} | ||
return items; | ||
}); | ||
result | ||
} | ||
return items; | ||
} | ||
/** | ||
* Registers the modules | ||
* | ||
* @param {ModuleDescriptorVal[][]} modulesToRegister | ||
* @param {AwilixContainer} container | ||
* @param {ModuleDescriptor[]} modules | ||
* @param {LoadModulesOptions} opts | ||
*/ | ||
function registerModules(modulesToRegister, container, modules, opts) { | ||
modulesToRegister | ||
.reduce((acc, cur) => acc.concat(cur), []) | ||
@@ -1459,4 +1534,10 @@ .filter((x) => x) | ||
}; | ||
loadModules(_loadModulesDeps, globPatterns, opts); | ||
return container; | ||
if (opts === null || opts === void 0 ? void 0 : opts.esModules) { | ||
_loadModulesDeps.require = importModule; | ||
return loadModules(_loadModulesDeps, globPatterns, opts).then(() => container); | ||
} | ||
else { | ||
loadModules(_loadModulesDeps, globPatterns, opts); | ||
return container; | ||
} | ||
} | ||
@@ -1463,0 +1544,0 @@ /** |
@@ -44,3 +44,3 @@ import { GlobWithOptions } from './list-modules'; | ||
*/ | ||
loadModules(globPatterns: Array<string | GlobWithOptions>, options?: LoadModulesOptions): this; | ||
loadModules<ESM extends boolean = false>(globPatterns: Array<string | GlobWithOptions>, options?: LoadModulesOptions<ESM>): ESM extends false ? this : Promise<this>; | ||
/** | ||
@@ -47,0 +47,0 @@ * Adds a single registration that using a pre-constructed resolver. |
@@ -12,2 +12,3 @@ "use strict"; | ||
const errors_1 = require("./errors"); | ||
const load_module_native_js_1 = require("./load-module-native.js"); | ||
/** | ||
@@ -342,4 +343,10 @@ * Family tree symbol. | ||
}; | ||
load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts); | ||
return container; | ||
if (opts === null || opts === void 0 ? void 0 : opts.esModules) { | ||
_loadModulesDeps.require = load_module_native_js_1.importModule; | ||
return load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts).then(() => container); | ||
} | ||
else { | ||
load_modules_1.loadModules(_loadModulesDeps, globPatterns, opts); | ||
return container; | ||
} | ||
} | ||
@@ -346,0 +353,0 @@ /** |
@@ -23,2 +23,8 @@ import * as glob from 'glob'; | ||
/** | ||
* Metadata of the module as well as the loaded module itself. | ||
*/ | ||
export interface LoadedModuleDescriptor extends ModuleDescriptor { | ||
value: unknown; | ||
} | ||
/** | ||
* A glob pattern with associated registration options. | ||
@@ -25,0 +31,0 @@ */ |
@@ -8,6 +8,7 @@ import { ModuleDescriptor, GlobWithOptions, listModules } from './list-modules'; | ||
*/ | ||
export interface LoadModulesOptions { | ||
export interface LoadModulesOptions<ESM extends boolean = false> { | ||
cwd?: string; | ||
formatName?: NameFormatter | BuiltInNameFormatters; | ||
resolverOptions?: BuildResolverOptions<any>; | ||
esModules?: ESM; | ||
} | ||
@@ -34,32 +35,10 @@ /** | ||
container: AwilixContainer; | ||
require(path: string): any; | ||
require(path: string): any | Promise<any>; | ||
} | ||
/** | ||
* Given an array of glob strings, will call `require` | ||
* on them, and call their default exported function with the | ||
* container as the first parameter. | ||
* | ||
* @param {AwilixContainer} dependencies.container | ||
* The container to install loaded modules in. | ||
* | ||
* @param {Function} dependencies.listModules | ||
* The listModules function to use for listing modules. | ||
* | ||
* @param {Function} dependencies.require | ||
* The require function - it's a dependency because it makes testing easier. | ||
* | ||
* @param {String[]} globPatterns | ||
* The array of globs to use when loading modules. | ||
* | ||
* @param {Object} opts | ||
* Passed to `listModules`, e.g. `{ cwd: '...' }`. | ||
* | ||
* @param {(string, ModuleDescriptor) => string} opts.formatName | ||
* Used to format the name the module is registered with in the container. | ||
* | ||
* @return {Object} | ||
* Returns an object describing the result. | ||
* The list of loaded modules | ||
*/ | ||
export declare function loadModules(dependencies: LoadModulesDeps, globPatterns: string | Array<string | GlobWithOptions>, opts?: LoadModulesOptions): { | ||
loadedModules: ModuleDescriptor[]; | ||
}; | ||
export interface LoadModulesResult { | ||
loadedModules: Array<ModuleDescriptor>; | ||
} | ||
export declare function loadModules<ESM extends boolean = false>(dependencies: LoadModulesDeps, globPatterns: string | Array<string | GlobWithOptions>, opts?: LoadModulesOptions<ESM>): ESM extends true ? Promise<LoadModulesResult> : LoadModulesResult; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -34,2 +43,5 @@ exports.loadModules = void 0; | ||
* | ||
* @param {boolean} opts.esModules | ||
* Set to `true` to use Node's native ECMAScriptModules modules | ||
* | ||
* @return {Object} | ||
@@ -39,50 +51,97 @@ * Returns an object describing the result. | ||
function loadModules(dependencies, globPatterns, opts) { | ||
opts !== null && opts !== void 0 ? opts : (opts = {}); | ||
const container = dependencies.container; | ||
opts = optsWithDefaults(opts, container); | ||
const modules = dependencies.listModules(globPatterns, opts); | ||
const result = modules.map((m) => { | ||
const items = []; | ||
const loaded = dependencies.require(m.path); | ||
// Meh, it happens. | ||
if (!loaded) { | ||
return items; | ||
if (opts.esModules) { | ||
return loadEsModules(dependencies, container, modules, opts); | ||
} | ||
else { | ||
const result = modules.map((m) => { | ||
const loaded = dependencies.require(m.path); | ||
return parseLoadedModule(loaded, m); | ||
}); | ||
return registerModules(result, container, modules, opts); | ||
} | ||
} | ||
exports.loadModules = loadModules; | ||
/** | ||
* Loads the modules using native ES6 modules and the async import() | ||
* @param {AwilixContainer} container | ||
* @param {ModuleDescriptor[]} modules | ||
* @param {LoadModulesOptions} opts | ||
*/ | ||
function loadEsModules(dependencies, container, modules, opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const importPromises = []; | ||
for (const m of modules) { | ||
importPromises.push(dependencies.require(m.path)); | ||
} | ||
if (utils_1.isFunction(loaded)) { | ||
// for module.exports = ... | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded, | ||
opts: m.opts, | ||
}); | ||
return items; | ||
const imports = yield Promise.all(importPromises); | ||
const result = []; | ||
for (let i = 0; i < modules.length; i++) { | ||
result.push(parseLoadedModule(imports[i], modules[i])); | ||
} | ||
if (loaded.default && utils_1.isFunction(loaded.default)) { | ||
// ES6 default export | ||
return registerModules(result, container, modules, opts); | ||
}); | ||
} | ||
/** | ||
* Parses the module which has been required | ||
* | ||
* @param {any} loaded | ||
* @param {ModuleDescriptor} m | ||
*/ | ||
function parseLoadedModule(loaded, m) { | ||
const items = []; | ||
// Meh, it happens. | ||
if (!loaded) { | ||
return items; | ||
} | ||
if (utils_1.isFunction(loaded)) { | ||
// for module.exports = ... | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded, | ||
opts: m.opts, | ||
}); | ||
return items; | ||
} | ||
if (loaded.default && utils_1.isFunction(loaded.default)) { | ||
// ES6 default export | ||
items.push({ | ||
name: m.name, | ||
path: m.path, | ||
value: loaded.default, | ||
opts: m.opts, | ||
}); | ||
} | ||
// loop through non-default exports, but require the RESOLVER property set for | ||
// it to be a valid service module export. | ||
for (const key of Object.keys(loaded)) { | ||
if (key === 'default') { | ||
// default case handled separately due to its different name (file name) | ||
continue; | ||
} | ||
if (utils_1.isFunction(loaded[key]) && resolvers_1.RESOLVER in loaded[key]) { | ||
items.push({ | ||
name: m.name, | ||
name: key, | ||
path: m.path, | ||
value: loaded.default, | ||
value: loaded[key], | ||
opts: m.opts, | ||
}); | ||
} | ||
// loop through non-default exports, but require the RESOLVER property set for | ||
// it to be a valid service module export. | ||
for (const key of Object.keys(loaded)) { | ||
if (key === 'default') { | ||
// default case handled separately due to its different name (file name) | ||
continue; | ||
} | ||
if (utils_1.isFunction(loaded[key]) && resolvers_1.RESOLVER in loaded[key]) { | ||
items.push({ | ||
name: key, | ||
path: m.path, | ||
value: loaded[key], | ||
opts: m.opts, | ||
}); | ||
} | ||
} | ||
return items; | ||
}); | ||
result | ||
} | ||
return items; | ||
} | ||
/** | ||
* Registers the modules | ||
* | ||
* @param {ModuleDescriptorVal[][]} modulesToRegister | ||
* @param {AwilixContainer} container | ||
* @param {ModuleDescriptor[]} modules | ||
* @param {LoadModulesOptions} opts | ||
*/ | ||
function registerModules(modulesToRegister, container, modules, opts) { | ||
modulesToRegister | ||
.reduce((acc, cur) => acc.concat(cur), []) | ||
@@ -95,3 +154,2 @@ .filter((x) => x) | ||
} | ||
exports.loadModules = loadModules; | ||
/** | ||
@@ -98,0 +156,0 @@ * Returns a new options object with defaults applied. |
{ | ||
"name": "awilix", | ||
"version": "4.2.7", | ||
"version": "4.3.0", | ||
"description": "Extremely powerful dependency injection container.", | ||
@@ -53,25 +53,25 @@ "main": "lib/awilix.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.12.3", | ||
"@babel/core": "^7.12.8", | ||
"@babel/plugin-transform-runtime": "^7.12.1", | ||
"@babel/preset-env": "^7.12.1", | ||
"@babel/runtime": "^7.12.1", | ||
"@babel/preset-env": "^7.12.7", | ||
"@babel/runtime": "^7.12.5", | ||
"@types/glob": "^7.1.3", | ||
"@types/jest": "^26.0.15", | ||
"@types/node": "^14.14.2", | ||
"@types/node": "^14.14.9", | ||
"@types/prettier": "^2.1.5", | ||
"babel-jest": "^26.6.1", | ||
"babel-jest": "^26.6.3", | ||
"coveralls": "^3.1.0", | ||
"husky": "^4.3.0", | ||
"istanbul": "^0.4.5", | ||
"jest": "^26.6.1", | ||
"lint-staged": "^10.4.2", | ||
"prettier": "^2.1.2", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.2", | ||
"prettier": "^2.2.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.32.1", | ||
"rollup": "^2.33.3", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-replace": "^2.2.0", | ||
"rollup-plugin-typescript2": "^0.28.0", | ||
"rollup-plugin-typescript2": "^0.29.0", | ||
"smid": "^0.1.1", | ||
"ts-jest": "^26.4.1", | ||
"ts-jest": "^26.4.4", | ||
"tslib": "^2.0.3", | ||
@@ -81,7 +81,8 @@ "tslint": "^6.1.3", | ||
"tslint-config-standard": "^9.0.0", | ||
"typescript": "^4.0.3" | ||
"typescript": "^4.1.2" | ||
}, | ||
"dependencies": { | ||
"camel-case": "^4.1.1", | ||
"glob": "^7.1.6" | ||
"glob": "^7.1.6", | ||
"rollup-plugin-copy": "^3.3.0" | ||
}, | ||
@@ -121,3 +122,4 @@ "lint-staged": { | ||
"__tests__", | ||
"lib" | ||
"lib", | ||
"src/load-module-native.js" | ||
], | ||
@@ -124,0 +126,0 @@ "moduleFileExtensions": [ |
@@ -554,3 +554,3 @@ # Awilix | ||
**Important**: Auto-loading relies on `glob` and therefore does not with | ||
**Important**: Auto-loading relies on `glob` and therefore does not work with | ||
bundlers like Webpack, Rollup and Browserify. | ||
@@ -1068,2 +1068,3 @@ | ||
the lifetime, injection mode and more of the loaded modules. | ||
- `opts.esModules`: Loads modules using Node's native ES modules. This is only supported on Node 14.0+ and should only be used if you're using the [Native Node ES modules](https://nodejs.org/api/esm.html) | ||
@@ -1070,0 +1071,0 @@ Example: |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
315097
41
6579
1359
3
+ Addedrollup-plugin-copy@^3.3.0
+ Added@nodelib/fs.scandir@2.1.5(transitive)
+ Added@nodelib/fs.stat@2.0.5(transitive)
+ Added@nodelib/fs.walk@1.2.8(transitive)
+ Added@types/fs-extra@8.1.5(transitive)
+ Added@types/glob@7.2.0(transitive)
+ Added@types/minimatch@5.1.2(transitive)
+ Added@types/node@22.5.5(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedbraces@3.0.3(transitive)
+ Addedcolorette@1.4.0(transitive)
+ Addeddir-glob@3.0.1(transitive)
+ Addedfast-glob@3.3.2(transitive)
+ Addedfastq@1.17.1(transitive)
+ Addedfill-range@7.1.1(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedglob-parent@5.1.2(transitive)
+ Addedglobby@10.0.1(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedignore@5.3.2(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@4.0.3(transitive)
+ Addedis-number@7.0.0(transitive)
+ Addedis-plain-object@3.0.1(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@4.0.8(transitive)
+ Addedpath-type@4.0.0(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedreusify@1.0.4(transitive)
+ Addedrollup-plugin-copy@3.5.0(transitive)
+ Addedrun-parallel@1.2.0(transitive)
+ Addedslash@3.0.0(transitive)
+ Addedto-regex-range@5.0.1(transitive)
+ Addedundici-types@6.19.8(transitive)
+ Addeduniversalify@0.1.2(transitive)