Skip to content

Commit 0712e88

Browse files
committed
fix(): ensure only components are exported
1 parent e3e8ce3 commit 0712e88

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/compiler/transformers/decorators-to-static/component-decorator.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { DEFAULT_STYLE_MODE, augmentDiagnosticWithNode, buildError, validateComp
55
import { CLASS_DECORATORS_TO_REMOVE } from '../remove-stencil-import';
66

77

8-
export function componentDecoratorToStatic(config: d.Config, diagnostics: d.Diagnostic[], cmpNode: ts.ClassDeclaration, newMembers: ts.ClassElement[], componentDecorator: ts.Decorator) {
8+
export function componentDecoratorToStatic(config: d.Config, typeChecker: ts.TypeChecker, diagnostics: d.Diagnostic[], cmpNode: ts.ClassDeclaration, newMembers: ts.ClassElement[], componentDecorator: ts.Decorator) {
99
removeDecorators(cmpNode, CLASS_DECORATORS_TO_REMOVE);
1010

1111
const [ componentOptions ] = getDeclarationParameters<d.ComponentOptions>(componentDecorator);
1212
if (!componentOptions) {
1313
return;
1414
}
1515

16-
if (!validateComponent(config, diagnostics, componentOptions, cmpNode, componentDecorator)) {
16+
if (!validateComponent(config, diagnostics, typeChecker, componentOptions, cmpNode, componentDecorator)) {
1717
return;
1818
}
1919

@@ -67,9 +67,10 @@ export function componentDecoratorToStatic(config: d.Config, diagnostics: d.Diag
6767
newMembers.push(createStaticGetter('styles', convertValueToLiteral(styles)));
6868
}
6969
}
70+
7071
}
7172

72-
function validateComponent(config: d.Config, diagnostics: d.Diagnostic[], componentOptions: d.ComponentOptions, cmpNode: ts.ClassDeclaration, componentDecorator: ts.Node) {
73+
function validateComponent(config: d.Config, diagnostics: d.Diagnostic[], typeChecker: ts.TypeChecker, componentOptions: d.ComponentOptions, cmpNode: ts.ClassDeclaration, componentDecorator: ts.Node) {
7374
const extendNode = cmpNode.heritageClauses && cmpNode.heritageClauses.find(c => c.token === ts.SyntaxKind.ExtendsKeyword);
7475
if (extendNode) {
7576
const err = buildError(diagnostics);
@@ -111,6 +112,23 @@ function validateComponent(config: d.Config, diagnostics: d.Diagnostic[], compon
111112
augmentDiagnosticWithNode(config, err, findTagNode('tag', componentDecorator));
112113
return false;
113114
}
115+
116+
if (!config._isTesting) {
117+
const nonTypeExports = typeChecker.getExportsOfModule(typeChecker.getSymbolAtLocation(cmpNode.getSourceFile()))
118+
.filter(symbol => (symbol.flags & (ts.SymbolFlags.Interface | ts.SymbolFlags.TypeAlias)) === 0)
119+
.filter(symbol => symbol.name !== cmpNode.name.text);
120+
121+
nonTypeExports.forEach(symbol => {
122+
const err = buildError(diagnostics);
123+
err.messageText = `To allow efficient bundling with rollup, modules using @Component() can only have a single export which is the component class itself.
124+
Any other exports should be moved to a separate file.
125+
For further information check out: https://stenciljs.com/docs/module-bundling`;
126+
augmentDiagnosticWithNode(config, err, symbol.valueDeclaration.modifiers[0]);
127+
});
128+
if (nonTypeExports.length > 0) {
129+
return false;
130+
}
131+
}
114132
return true;
115133
}
116134

src/compiler/transformers/decorators-to-static/convert-decorators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function visitClass(config: d.Config, diagnostics: d.Diagnostic[], typeChecker:
4646
const newMembers: ts.ClassElement[] = [...cmpNode.members];
4747

4848
// parser component decorator (Component)
49-
componentDecoratorToStatic(config, diagnostics, cmpNode, newMembers, componentDecorator);
49+
componentDecoratorToStatic(config, typeChecker, diagnostics, cmpNode, newMembers, componentDecorator);
5050

5151
// parse member decorators (Prop, State, Listen, Event, Method, Element and Watch)
5252
const decoratedMembers = newMembers.filter(member => Array.isArray(member.decorators) && member.decorators.length > 0);

0 commit comments

Comments
 (0)