Skip to content

Commit de960a6

Browse files
committed
Add support for array in "main" field and make logic error proof
Fixes codesandbox#1739
1 parent 43e36d1 commit de960a6

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/common/src/templates/template.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { absolute } from '../utils/path';
32
import {
43
ConfigurationFile,
@@ -118,15 +117,31 @@ export default class Template {
118117
this.showCube = options.showCube != null ? options.showCube : true;
119118
}
120119

120+
private getMainFromPackage(pkg: {
121+
main?: string[] | string;
122+
}): string | undefined {
123+
try {
124+
if (!pkg.main) {
125+
return undefined;
126+
}
127+
128+
if (Array.isArray(pkg.main)) {
129+
return absolute(pkg.main[0]);
130+
}
131+
132+
if (typeof pkg.main === 'string') {
133+
return absolute(pkg.main);
134+
}
135+
} catch (e) {}
136+
}
137+
121138
/**
122139
* Get possible entry files to evaluate, differs per template
123140
*/
124141
getEntries(configurationFiles: ParsedConfigurationFiles): Array<string> {
125142
return [
126143
configurationFiles.package &&
127-
configurationFiles.package.parsed &&
128-
configurationFiles.package.parsed.main &&
129-
absolute(configurationFiles.package.parsed.main),
144+
this.getMainFromPackage(configurationFiles.package.parsed),
130145
'/index.' + (this.isTypescript ? 'ts' : 'js'),
131146
'/src/index.' + (this.isTypescript ? 'ts' : 'js'),
132147
'/src/index.ts',

0 commit comments

Comments
 (0)