Skip to content

Commit 39b5d71

Browse files
committed
fix(sys): fix node resolving module w/out package.json main
1 parent 789e973 commit 39b5d71

4 files changed

Lines changed: 37 additions & 5 deletions

File tree

src/compiler/style/css-imports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export function resolveCssNodeModule(config: d.Config, diagnostics: d.Diagnostic
136136
try {
137137
const dir = config.sys.path.dirname(filePath);
138138
const moduleId = getModuleId(cssImportData.url);
139-
cssImportData.filePath = config.sys.resolveModule(dir, moduleId);
139+
cssImportData.filePath = config.sys.resolveModule(dir, moduleId, { manuallyResolve: true });
140140
cssImportData.filePath = config.sys.path.dirname(cssImportData.filePath);
141141
cssImportData.filePath += normalizePath(cssImportData.url.substring(moduleId.length + 1));
142142
cssImportData.updatedImport = `@import "${cssImportData.filePath}";`;

src/declarations/system.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface StencilSystem {
3636
optimizeCss?(inputOpts: d.OptimizeCssInput): Promise<d.OptimizeCssOutput>;
3737
path?: Path;
3838
requestLatestCompilerVersion?(): Promise<string>;
39-
resolveModule?(fromDir: string, moduleId: string): string;
39+
resolveModule?(fromDir: string, moduleId: string, opts?: ResolveModuleOptions): string;
4040
rollup?: RollupInterface;
4141
scopeCss?: (cssText: string, scopeId: string, hostScopeId: string, slotScopeId: string) => Promise<string>;
4242
semver?: Semver;
@@ -55,6 +55,11 @@ export interface StencilSystem {
5555
}
5656

5757

58+
export interface ResolveModuleOptions {
59+
manuallyResolve?: boolean;
60+
}
61+
62+
5863
export interface RollupInterface {
5964
rollup: {
6065
(config: any): Promise<any>;

src/sys/node/node-resolve-module.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as d from '../../declarations';
12
import fs from 'graceful-fs';
23
import path from 'path';
34
import { normalizePath } from '../../compiler/util';
@@ -6,14 +7,18 @@ import { normalizePath } from '../../compiler/util';
67
export class NodeResolveModule {
78
private resolveModuleCache = new Map<string, string>();
89

9-
resolveModule(fromDir: string, moduleId: string) {
10+
resolveModule(fromDir: string, moduleId: string, opts?: d.ResolveModuleOptions) {
1011
const cacheKey = `${fromDir}:${moduleId}`;
1112

1213
const cachedPath = this.resolveModuleCache.get(cacheKey);
1314
if (cachedPath) {
1415
return cachedPath;
1516
}
1617

18+
if (opts && opts.manuallyResolve) {
19+
return this.resolveModuleManually(fromDir, moduleId, cacheKey);
20+
}
21+
1722
if (moduleId.startsWith('@types/')) {
1823
return this.resolveTypesModule(fromDir, moduleId, cacheKey);
1924
}
@@ -72,6 +77,28 @@ export class NodeResolveModule {
7277
throw new Error(`error loading "${moduleId}" from "${fromDir}"`);
7378
}
7479

80+
resolveModuleManually(fromDir: string, moduleId: string, cacheKey: string) {
81+
const root = normalizePath(path.parse(fromDir).root);
82+
83+
let dir = normalizePath(path.join(fromDir, 'noop.js'));
84+
let packageJsonFilePath: string;
85+
86+
while (dir !== root) {
87+
dir = normalizePath(path.dirname(dir));
88+
packageJsonFilePath = path.join(dir, 'node_modules', moduleId, 'package.json');
89+
90+
if (!hasAccess(packageJsonFilePath)) {
91+
continue;
92+
}
93+
94+
this.resolveModuleCache.set(cacheKey, packageJsonFilePath);
95+
96+
return packageJsonFilePath;
97+
}
98+
99+
throw new Error(`error loading "${moduleId}" from "${fromDir}"`);
100+
}
101+
75102
}
76103

77104
function hasAccess(filePath: string) {

src/sys/node/node-sys-main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ export class NodeSystem implements d.StencilSystem {
233233
return this.sysWorker.run('requestLatestCompilerVersion');
234234
}
235235

236-
resolveModule(fromDir: string, moduleId: string) {
237-
return this.nodeResolveModule.resolveModule(fromDir, moduleId);
236+
resolveModule(fromDir: string, moduleId: string, opts?: d.ResolveModuleOptions) {
237+
return this.nodeResolveModule.resolveModule(fromDir, moduleId, opts);
238238
}
239239

240240
get rollup() {

0 commit comments

Comments
 (0)