11import * as d from '../../declarations' ;
2- import { buildError , normalizePath } from '@utils' ;
2+ import { 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 , buildCtx : d . BuildCtx ) : Plugin {
20+ export function globalScriptsPlugin ( config : d . Config , compilerCtx : d . CompilerCtx ) : Plugin {
2121 const globalPaths : string [ ] = [ ] ;
2222
2323 if ( typeof config . globalScript === 'string' ) {
@@ -39,7 +39,6 @@ export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx
3939 if ( id === GLOBAL_ID ) {
4040 return {
4141 id,
42- moduleSideEffects : true
4342 } ;
4443 }
4544 return null ;
@@ -62,27 +61,12 @@ export function globalScriptsPlugin(config: d.Config, compilerCtx: d.CompilerCtx
6261 transform ( code , id ) {
6362 id = normalizePath ( id ) ;
6463 if ( globalPaths . includes ( id ) ) {
65- const output = [
66- INJECT_CONTEXT
67- ] ;
68-
6964 const program = this . parse ( code , { } ) ;
7065 const needsDefault = ! program . body . some ( s => s . type === 'ExportDefaultDeclaration' ) ;
71-
72- if ( needsDefault ) {
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;` ) ;
80-
81- } else {
82- output . push ( code ) ;
83- }
84-
85- return output . join ( '\n' ) ;
66+ const defaultExport = needsDefault
67+ ? '\nexport const globalFn = () => {};\nexport default globalFn;'
68+ : '' ;
69+ return INJECT_CONTEXT + code + defaultExport ;
8670 }
8771 return null ;
8872 }
0 commit comments