Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolver): use require.resolve.paths(); avoid require for json #1194

Merged
merged 4 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ describe('resolve-src-modules', () => {
const modules = lwcResolver.resolveModulesInDir(simpleStructurePath);
const moduleNames = Object.keys(modules);
expect(moduleNames).toHaveLength(2);
expect(moduleNames).toContain('ns/simpleCmp', 'ns/simple-module');
expect(moduleNames).toEqual(
expect.arrayContaining(['ns/simpleCmp', 'ns/simple-module'])
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,57 @@ describe('resolve-lwc-npm-modules', () => {
const lwcModules = lwcResolver.resolveLwcNpmModules(resolverOptions);
const lwcModuleNames = Object.keys(lwcModules);
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'])
);
});
});
29 changes: 20 additions & 9 deletions packages/@lwc/module-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import fs from 'fs';
import nodeModulePaths from './node-modules-paths';

const DEFAULT_IGNORE = ['**/node_modules/**', '**/__tests__/**'];
const PACKAGE_PATTERN = ['*/*/package.json', '*/package.json'];
const PACKAGE_PATTERN = ['*/*/package.json', '*/package.json', 'package.json'];
const MODULE_ENTRY_PATTERN = `**/*.[jt]s`;
const LWC_CONFIG_FILE = '.lwcrc';

Expand All @@ -26,6 +26,8 @@ export interface RegistryEntry {
export interface ModuleResolverConfig {
moduleDirectories: string[];
rootDir: string;
modulePaths: string[];
ignorePatterns?: string[];
}

interface FlatEntry {
Expand All @@ -41,15 +43,17 @@ function loadLwcConfig(modulePath) {
const lwcConfigPath = path.join(modulePath, LWC_CONFIG_FILE);
let config;
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;
}

Expand Down Expand Up @@ -95,9 +99,16 @@ function hasModuleBeenVisited(module, visited) {
function expandModuleDirectories({
moduleDirectories,
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('.') || [];
}

return nodeModulePaths(rootDir || __dirname, moduleDirectories);
Expand Down Expand Up @@ -135,7 +146,7 @@ export function resolveLwcNpmModules(options: Partial<ModuleResolverConfig> = {}
return glob
.sync<FlatEntry>(PACKAGE_PATTERN, {
cwd: nodeModulesDir,
ignore: DEFAULT_IGNORE,
ignore: options.ignorePatterns || DEFAULT_IGNORE,
transform: entry =>
typeof entry === 'string' ? { path: entry } : { path: entry.path },
})
Expand Down