Skip to content

Commit ba03ccf

Browse files
johnjenkinsJohn Jenkins
andauthored
fix(compiler): stop error from globalScript lack of default export (#6527)
Co-authored-by: John Jenkins <john.jenkins@nanoporetech.com>
1 parent 6482533 commit ba03ccf

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/compiler/bundle/app-data-plugin.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ export const appDataPlugin = (
9797
if (globalScripts.some((s) => s.path === id)) {
9898
const program = this.parse(code, {});
9999
const needsDefault = !(program as any).body.some((s: any) => s.type === 'ExportDefaultDeclaration');
100+
101+
if (needsDefault) {
102+
const diagnostic: d.Diagnostic = {
103+
level: 'warn',
104+
type: 'build',
105+
header: 'Missing default export in globalScript',
106+
messageText: `globalScript should export a default function.\nSee: https://stenciljs.com/docs/config#globalscript`,
107+
relFilePath: id,
108+
lines: [],
109+
};
110+
buildCtx.diagnostics.push(diagnostic);
111+
}
112+
100113
const defaultExport = needsDefault ? '\nexport const globalFn = () => {};\nexport default globalFn;' : '';
101114
code = code + defaultExport;
102115

@@ -176,11 +189,13 @@ export const getGlobalScriptData = (config: d.ValidatedConfig, compilerCtx: d.Co
176189

177190
const appendGlobalScripts = (globalScripts: GlobalScript[], s: MagicString) => {
178191
if (globalScripts.length === 1) {
179-
s.prepend(`import appGlobalScript from '${globalScripts[0].path}';\n`);
192+
s.prepend(`import * as appGlobalScriptNs from '${globalScripts[0].path}';\n`);
193+
s.prepend(`const appGlobalScript = appGlobalScriptNs.default || (() => {});\n`);
180194
s.append(`export const globalScripts = appGlobalScript;\n`);
181195
} else if (globalScripts.length > 1) {
182196
globalScripts.forEach((globalScript) => {
183-
s.prepend(`import ${globalScript.defaultName} from '${globalScript.path}';\n`);
197+
s.prepend(`import * as ${globalScript.defaultName}Ns from '${globalScript.path}';\n`);
198+
s.prepend(`const ${globalScript.defaultName} = ${globalScript.defaultName}Ns.default || (() => {});\n`);
184199
});
185200

186201
s.append(`export const globalScripts = () => {\n`);

0 commit comments

Comments
 (0)