@lwc/module-resolver
Advanced tools
Comparing version 0.37.3-alpha9 to 0.38.0-alpha1
@@ -13,3 +13,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const glob_1 = __importDefault(require("glob")); | ||
const fast_glob_1 = __importDefault(require("fast-glob")); | ||
const path_1 = __importDefault(require("path")); | ||
@@ -19,3 +19,3 @@ const fs_1 = __importDefault(require("fs")); | ||
const DEFAULT_IGNORE = ['**/node_modules/**', '**/__tests__/**']; | ||
const PACKAGE_PATTERN = `**/*/package.json`; | ||
const PACKAGE_PATTERN = ['*/*/package.json', '*/package.json', 'package.json']; | ||
const MODULE_ENTRY_PATTERN = `**/*.[jt]s`; | ||
@@ -31,17 +31,24 @@ const LWC_CONFIG_FILE = '.lwcrc'; | ||
try { | ||
const jsonPkg = require(packageJsonPath); | ||
config = JSON.parse(fs_1.default.readFileSync(lwcConfigPath, 'utf8')); | ||
} | ||
catch (ignore) { | ||
// ignore | ||
} | ||
if (!config) { | ||
try { | ||
config = fs_1.default.readFileSync(lwcConfigPath, 'utf8'); | ||
config = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf8')).lwc; | ||
} | ||
catch (e) { | ||
config = jsonPkg.lwc; | ||
catch (ignore) { | ||
// ignore | ||
} | ||
} | ||
catch (ignore) { | ||
// ignore | ||
} | ||
return config; | ||
} | ||
function resolveModulesInDir(absPath) { | ||
return glob_1.default.sync(MODULE_ENTRY_PATTERN, { cwd: absPath }).reduce((mappings, file) => { | ||
return fast_glob_1.default | ||
.sync(MODULE_ENTRY_PATTERN, { | ||
cwd: absPath, | ||
transform: entry => typeof entry === 'string' ? { path: entry } : { path: entry.path }, | ||
}) | ||
.reduce((mappings, { path: file }) => { | ||
const ext = path_1.default.extname(file); | ||
@@ -70,5 +77,11 @@ const fileName = path_1.default.basename(file, ext); | ||
} | ||
function expandModuleDirectories({ moduleDirectories, rootDir, } = {}) { | ||
function expandModuleDirectories({ moduleDirectories, rootDir, modulePaths, } = {}) { | ||
if (modulePaths) { | ||
return modulePaths; | ||
} | ||
if (!moduleDirectories && !rootDir) { | ||
return module.paths; | ||
// paths() is spec'd to return null only for built-in node | ||
// modules like 'http'. To be safe, return empty array in | ||
// instead of null in this case. | ||
return require.resolve.paths('.') || []; | ||
} | ||
@@ -107,5 +120,9 @@ return node_modules_paths_1.default(rootDir || __dirname, moduleDirectories); | ||
return modulePaths.reduce((m, nodeModulesDir) => { | ||
return glob_1.default | ||
.sync(PACKAGE_PATTERN, { cwd: nodeModulesDir, ignore: DEFAULT_IGNORE }) | ||
.reduce((mappings, file) => { | ||
return fast_glob_1.default | ||
.sync(PACKAGE_PATTERN, { | ||
cwd: nodeModulesDir, | ||
ignore: options.ignorePatterns || DEFAULT_IGNORE, | ||
transform: entry => typeof entry === 'string' ? { path: entry } : { path: entry.path }, | ||
}) | ||
.reduce((mappings, { path: file }) => { | ||
const moduleRoot = path_1.default.dirname(path_1.default.join(nodeModulesDir, file)); | ||
@@ -112,0 +129,0 @@ const lwcConfig = loadLwcConfig(moduleRoot); |
@@ -10,2 +10,4 @@ export interface RegistryEntry { | ||
rootDir: string; | ||
modulePaths: string[]; | ||
ignorePatterns?: string[]; | ||
} | ||
@@ -12,0 +14,0 @@ export declare function resolveModulesInDir(absPath: string): { |
{ | ||
"name": "@lwc/module-resolver", | ||
"description": "Resolves paths for LWC components", | ||
"version": "0.37.3-alpha9", | ||
"version": "0.38.0-alpha1", | ||
"main": "./dist/commonjs/index.js", | ||
@@ -11,3 +11,3 @@ "scripts": { | ||
"dependencies": { | ||
"glob": "^7.1.2" | ||
"fast-glob": "~2.2.6" | ||
}, | ||
@@ -17,3 +17,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "47b5590bcc9f1b43fc4e5524a30c1fae042b20d7" | ||
"gitHead": "1477d56275cab6ba33ae45bc18a726434fa83410" | ||
} |
@@ -20,5 +20,7 @@ /* | ||
expect(moduleNames).toHaveLength(2); | ||
expect(moduleNames).toContain('ns/simpleCmp', 'ns/simple-module'); | ||
expect(moduleNames).toEqual( | ||
expect.arrayContaining(['ns/simpleCmp', 'ns/simple-module']) | ||
); | ||
}); | ||
}); | ||
}); |
@@ -20,9 +20,55 @@ /* | ||
expect(lwcModuleNames).toHaveLength(4); | ||
expect(lwcModuleNames).toContain( | ||
'alias-fake-package', | ||
'fake-module1', | ||
'fake-module2', | ||
'other-resource' | ||
expect(lwcModuleNames).toEqual( | ||
expect.arrayContaining([ | ||
'alias-fake-package', | ||
'fake/module1', | ||
'fake/module2', | ||
'other-resource', | ||
]) | ||
); | ||
}); | ||
it('resolve from npm: modulePaths options', () => { | ||
const resolverOptions = { | ||
modulePaths: [path.join(__dirname, 'fixtures', 'fake_node_modules')], | ||
}; | ||
const lwcModules = lwcResolver.resolveLwcNpmModules(resolverOptions); | ||
const lwcModuleNames = Object.keys(lwcModules); | ||
expect(lwcModuleNames).toHaveLength(4); | ||
expect(lwcModuleNames).toEqual( | ||
expect.arrayContaining([ | ||
'alias-fake-package', | ||
'fake/module1', | ||
'fake/module2', | ||
'other-resource', | ||
]) | ||
); | ||
}); | ||
it('resolve from npm: ignorePattern', () => { | ||
const resolverOptions = { | ||
modulePaths: [path.join(__dirname, 'fixtures', 'fake_node_modules')], | ||
ignorePatterns: ['**/fake-module-package/**'], | ||
}; | ||
const lwcModules = lwcResolver.resolveLwcNpmModules(resolverOptions); | ||
const lwcModuleNames = Object.keys(lwcModules); | ||
expect(lwcModuleNames).toHaveLength(3); | ||
expect(lwcModuleNames).toEqual( | ||
expect.arrayContaining(['fake/module1', 'fake/module2', 'other-resource']) | ||
); | ||
}); | ||
it('resolve from npm: modulePaths has direct package.json folder reference', () => { | ||
const resolverOptions = { | ||
modulePaths: [ | ||
path.join(__dirname, 'fixtures', 'fake_node_modules', 'fake-multi-component'), | ||
], | ||
}; | ||
const lwcModules = lwcResolver.resolveLwcNpmModules(resolverOptions); | ||
const lwcModuleNames = Object.keys(lwcModules); | ||
expect(lwcModuleNames).toHaveLength(3); | ||
expect(lwcModuleNames).toEqual( | ||
expect.arrayContaining(['fake/module1', 'fake/module2', 'other-resource']) | ||
); | ||
}); | ||
}); |
@@ -9,3 +9,3 @@ /* | ||
import glob from 'glob'; | ||
import glob from 'fast-glob'; | ||
import path from 'path'; | ||
@@ -16,3 +16,3 @@ import fs from 'fs'; | ||
const DEFAULT_IGNORE = ['**/node_modules/**', '**/__tests__/**']; | ||
const PACKAGE_PATTERN = `**/*/package.json`; | ||
const PACKAGE_PATTERN = ['*/*/package.json', '*/package.json', 'package.json']; | ||
const MODULE_ENTRY_PATTERN = `**/*.[jt]s`; | ||
@@ -31,4 +31,10 @@ const LWC_CONFIG_FILE = '.lwcrc'; | ||
rootDir: string; | ||
modulePaths: string[]; | ||
ignorePatterns?: string[]; | ||
} | ||
interface FlatEntry { | ||
path: string; | ||
} | ||
function createRegistryEntry(entry, moduleSpecifier, moduleName, moduleNamespace): RegistryEntry { | ||
@@ -43,11 +49,13 @@ return { entry, moduleSpecifier, moduleName, moduleNamespace }; | ||
try { | ||
const jsonPkg = require(packageJsonPath); | ||
try { | ||
config = fs.readFileSync(lwcConfigPath, 'utf8'); | ||
} catch (e) { | ||
config = jsonPkg.lwc; | ||
} | ||
config = JSON.parse(fs.readFileSync(lwcConfigPath, 'utf8')); | ||
} catch (ignore) { | ||
// ignore | ||
} | ||
if (!config) { | ||
try { | ||
config = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).lwc; | ||
} catch (ignore) { | ||
// ignore | ||
} | ||
} | ||
return config; | ||
@@ -57,23 +65,29 @@ } | ||
export function resolveModulesInDir(absPath: string): { [name: string]: RegistryEntry } { | ||
return glob.sync(MODULE_ENTRY_PATTERN, { cwd: absPath }).reduce((mappings, file) => { | ||
const ext = path.extname(file); | ||
const fileName = path.basename(file, ext); | ||
const rootDir = path.dirname(file); | ||
const rootParts = rootDir.split('/'); // the glob library normalizes paths to forward slashes only - https://github.com/isaacs/node-glob#windows | ||
const entry = path.join(absPath, file); | ||
return glob | ||
.sync<FlatEntry>(MODULE_ENTRY_PATTERN, { | ||
cwd: absPath, | ||
transform: entry => | ||
typeof entry === 'string' ? { path: entry } : { path: entry.path }, | ||
}) | ||
.reduce((mappings, { path: file }) => { | ||
const ext = path.extname(file); | ||
const fileName = path.basename(file, ext); | ||
const rootDir = path.dirname(file); | ||
const rootParts = rootDir.split('/'); // the glob library normalizes paths to forward slashes only - https://github.com/isaacs/node-glob#windows | ||
const entry = path.join(absPath, file); | ||
const dirModuleName = rootParts.pop(); | ||
const dirModuleNamespace = rootParts.pop(); | ||
if (dirModuleNamespace && dirModuleName === fileName) { | ||
const registryNode = createRegistryEntry( | ||
entry, | ||
`${dirModuleNamespace}/${fileName}`, | ||
fileName, | ||
dirModuleNamespace.toLowerCase() | ||
); | ||
mappings[registryNode.moduleSpecifier] = registryNode; | ||
} | ||
const dirModuleName = rootParts.pop(); | ||
const dirModuleNamespace = rootParts.pop(); | ||
if (dirModuleNamespace && dirModuleName === fileName) { | ||
const registryNode = createRegistryEntry( | ||
entry, | ||
`${dirModuleNamespace}/${fileName}`, | ||
fileName, | ||
dirModuleNamespace.toLowerCase() | ||
); | ||
mappings[registryNode.moduleSpecifier] = registryNode; | ||
} | ||
return mappings; | ||
}, {}); | ||
return mappings; | ||
}, {}); | ||
} | ||
@@ -93,5 +107,12 @@ | ||
rootDir, | ||
modulePaths, | ||
}: Partial<ModuleResolverConfig> = {}) { | ||
if (modulePaths) { | ||
return modulePaths; | ||
} | ||
if (!moduleDirectories && !rootDir) { | ||
return module.paths; | ||
// paths() is spec'd to return null only for built-in node | ||
// modules like 'http'. To be safe, return empty array in | ||
// instead of null in this case. | ||
return require.resolve.paths('.') || []; | ||
} | ||
@@ -132,4 +153,9 @@ | ||
return glob | ||
.sync(PACKAGE_PATTERN, { cwd: nodeModulesDir, ignore: DEFAULT_IGNORE }) | ||
.reduce((mappings, file) => { | ||
.sync<FlatEntry>(PACKAGE_PATTERN, { | ||
cwd: nodeModulesDir, | ||
ignore: options.ignorePatterns || DEFAULT_IGNORE, | ||
transform: entry => | ||
typeof entry === 'string' ? { path: entry } : { path: entry.path }, | ||
}) | ||
.reduce((mappings, { path: file }) => { | ||
const moduleRoot = path.dirname(path.join(nodeModulesDir, file)); | ||
@@ -136,0 +162,0 @@ const lwcConfig = loadLwcConfig(moduleRoot); |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
26398
494
2
+ Addedfast-glob@~2.2.6
+ Added@mrmlnc/readdir-enhanced@2.2.1(transitive)
+ Added@nodelib/fs.stat@1.1.3(transitive)
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedarray-unique@0.3.2(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedatob@2.1.2(transitive)
+ Addedbase@0.11.2(transitive)
+ Addedbraces@2.3.2(transitive)
+ Addedcache-base@1.0.1(transitive)
+ Addedcall-me-maybe@1.0.2(transitive)
+ Addedclass-utils@0.3.6(transitive)
+ Addedcollection-visit@1.0.0(transitive)
+ Addedcomponent-emitter@1.3.1(transitive)
+ Addedcopy-descriptor@0.1.1(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addeddefine-property@0.2.51.0.02.0.2(transitive)
+ Addedexpand-brackets@2.1.4(transitive)
+ Addedextend-shallow@2.0.13.0.2(transitive)
+ Addedextglob@2.0.4(transitive)
+ Addedfast-glob@2.2.7(transitive)
+ Addedfill-range@4.0.0(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfragment-cache@0.2.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-value@2.0.6(transitive)
+ Addedglob-parent@3.1.0(transitive)
+ Addedglob-to-regexp@0.3.0(transitive)
+ Addedhas-value@0.3.11.0.0(transitive)
+ Addedhas-values@0.1.41.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-accessor-descriptor@1.0.1(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-data-descriptor@1.0.1(transitive)
+ Addedis-descriptor@0.1.71.0.3(transitive)
+ Addedis-extendable@0.1.11.0.1(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@3.1.04.0.3(transitive)
+ Addedis-number@3.0.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedis-windows@1.0.2(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.03.0.1(transitive)
+ Addedkind-of@3.2.24.0.06.0.3(transitive)
+ Addedmap-cache@0.2.2(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@3.1.10(transitive)
+ Addedmixin-deep@1.3.2(transitive)
+ Addedms@2.0.0(transitive)
+ Addednanomatch@1.2.13(transitive)
+ Addedobject-copy@0.1.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedobject.pick@1.3.0(transitive)
+ Addedpascalcase@0.1.1(transitive)
+ Addedpath-dirname@1.0.2(transitive)
+ Addedposix-character-classes@0.1.1(transitive)
+ Addedregex-not@1.0.2(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedresolve-url@0.2.1(transitive)
+ Addedret@0.1.15(transitive)
+ Addedsafe-regex@1.1.0(transitive)
+ Addedset-value@2.0.1(transitive)
+ Addedsnapdragon@0.8.2(transitive)
+ Addedsnapdragon-node@2.1.1(transitive)
+ Addedsnapdragon-util@3.0.1(transitive)
+ Addedsource-map@0.5.7(transitive)
+ Addedsource-map-resolve@0.5.3(transitive)
+ Addedsource-map-url@0.4.1(transitive)
+ Addedsplit-string@3.1.0(transitive)
+ Addedstatic-extend@0.1.2(transitive)
+ Addedto-object-path@0.3.0(transitive)
+ Addedto-regex@3.0.2(transitive)
+ Addedto-regex-range@2.1.1(transitive)
+ Addedunion-value@1.0.1(transitive)
+ Addedunset-value@1.0.0(transitive)
+ Addedurix@0.1.0(transitive)
+ Addeduse@3.1.1(transitive)
- Removedglob@^7.1.2
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedwrappy@1.0.2(transitive)