Skip to content

Commit bdae144

Browse files
committed
fix(global): require global scripts to be wrapped w/ default export fn
1 parent 9d9ee20 commit bdae144

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

src/compiler/app-core/bundle-app-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const bundleApp = async (config: d.Config, compilerCtx: d.CompilerCtx, bu
3838
}),
3939
stencilClientPlugin(config),
4040
stencilBuildConditionalsPlugin(build, config.fsNamespace),
41-
globalScriptsPlugin(config, compilerCtx),
41+
globalScriptsPlugin(config, compilerCtx, buildCtx),
4242
componentEntryPlugin(config, compilerCtx, buildCtx, build, buildCtx.entryModules),
4343
config.sys.rollup.plugins.commonjs({
4444
include: /node_modules/,

src/compiler/component-hydrate/bundle-hydrate-app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const bundleHydrateApp = async (config: d.Config, compilerCtx: d.Compiler
3030
}),
3131
stencilHydratePlugin(config),
3232
stencilBuildConditionalsPlugin(build, config.fsNamespace),
33-
globalScriptsPlugin(config, compilerCtx),
33+
globalScriptsPlugin(config, compilerCtx, buildCtx),
3434
componentEntryPlugin(config, compilerCtx, buildCtx, build, buildCtx.entryModules),
3535
config.sys.rollup.plugins.commonjs({
3636
include: /node_modules/,

src/compiler/rollup-plugins/global-scripts.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as d from '../../declarations';
2-
import { dashToPascalCase, normalizePath } from '@utils';
2+
import { buildError, normalizePath } from '@utils';
33
import { Plugin } from 'rollup';
44

55

@@ -17,7 +17,7 @@ export function hasGlobalScriptPaths(config: d.Config, compilerCtx: d.CompilerCt
1717
}
1818

1919

20-
export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx): Plugin {
20+
export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx): Plugin {
2121
const globalPaths: string[] = [];
2222

2323
if (typeof config.globalScript === 'string') {
@@ -60,7 +60,8 @@ export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx
6060
return null;
6161
},
6262
transform(code, id) {
63-
if (globalPaths.includes(normalizePath(id))) {
63+
id = normalizePath(id);
64+
if (globalPaths.includes(id)) {
6465
const output = [
6566
INJECT_CONTEXT
6667
];
@@ -69,13 +70,13 @@ export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx
6970
const needsDefault = !program.body.some(s => s.type === 'ExportDefaultDeclaration');
7071

7172
if (needsDefault) {
72-
const fileName = config.sys.path.basename(id).toLowerCase();
73-
let varName = dashToPascalCase(fileName.replace(/[|&;$%@"<>()+,.{}_]/g, '-')).trim();
74-
varName = varName.charAt(0).toLowerCase() + varName.substr(1);
75-
output.push(`export const ${varName} = () => {`);
76-
output.push(code);
77-
output.push(`};`);
78-
output.push(`export default ${varName};`);
73+
const diagnostic = buildError(buildCtx.diagnostics);
74+
diagnostic.header = `Global Script`;
75+
diagnostic.absFilePath = id;
76+
diagnostic.messageText = `The code to be executed should be placed within a default function that is exported by the global script. Ensure all of the code in the global script is wrapped in the function() that is exported.`;
77+
78+
output.push(`export const fallbackGlobalFn = () => {}`);
79+
output.push(`export default fallbackGlobalFn;`);
7980

8081
} else {
8182
output.push(code);

test/karma/test-app/global.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ import { setMode } from '@stencil/core';
22

33
declare const Context: any;
44

5-
Context['myService'] = 12;
5+
const globalScript = () => {
6+
Context['myService'] = 12;
67

7-
setMode(elm => {
8-
return (elm as any).colormode || elm.getAttribute('colormode') || (window as any).KarmaMode
9-
});
8+
setMode(elm => {
9+
return (elm as any).colormode || elm.getAttribute('colormode') || (window as any).KarmaMode
10+
});
11+
};
12+
13+
export default globalScript;

0 commit comments

Comments
 (0)