11import * as d from '../../declarations' ;
2- import { dashToPascalCase , normalizePath } from '@utils' ;
2+ import { buildError , normalizePath } from '@utils' ;
33import { 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 ) ;
0 commit comments