@@ -147,7 +147,7 @@ export type IBabel = {
147147
148148declare var Babel: IBabel ;
149149
150- Babel . registerPreset ( 'env' , Babel . availablePresets . latest ) ;
150+ let loadedTranspilerURL = null ;
151151
152152Babel . registerPlugin ( 'dynamic-import-node' , dynamicImportPlugin ) ;
153153Babel . registerPlugin ( 'babel-plugin-detective' , detective ) ;
@@ -180,24 +180,52 @@ self.addEventListener('message', async event => {
180180
181181 resetCache ( ) ;
182182
183- const { code, path, sandboxOptions, config, loaderOptions } = event . data ;
183+ const {
184+ code,
185+ path,
186+ babelTranspilerOptions,
187+ sandboxOptions,
188+ config,
189+ loaderOptions,
190+ } = event . data ;
191+
192+ const { disableCodeSandboxPlugins } = loaderOptions ;
184193
185194 const flattenedPresets = flatten ( config . presets ) ;
186195 const flattenedPlugins = flatten ( config . plugins ) ;
196+
187197 if (
188- flattenedPlugins . indexOf ( 'transform-vue-jsx' ) > - 1 &&
189- Object . keys ( Babel . availablePlugins ) . indexOf ( 'transform-vue-jsx' ) === - 1
198+ babelTranspilerOptions &&
199+ babelTranspilerOptions . babelURL &&
200+ babelTranspilerOptions . babelURL !== loadedTranspilerURL
190201 ) {
191- const vuePlugin = await import ( /* webpackChunkName: 'babel-plugin-transform-vue-jsx' */ 'babel-plugin-transform-vue-jsx' ) ;
192- Babel . registerPlugin ( 'transform-vue-jsx' , vuePlugin ) ;
202+ self . importScripts ( [ babelTranspilerOptions . babelURL ] ) ;
203+ loadedTranspilerURL = babelTranspilerOptions . babelURL ;
193204 }
194205
195- if (
196- flattenedPlugins . indexOf ( 'jsx-pragmatic' ) > - 1 &&
197- Object . keys ( Babel . availablePlugins ) . indexOf ( 'jsx-pragmatic' ) === - 1
198- ) {
199- const pragmaticPlugin = await import ( /* webpackChunkName: 'babel-plugin-jsx-pragmatic' */ 'babel-plugin-jsx-pragmatic' ) ;
200- Babel . registerPlugin ( 'jsx-pragmatic' , pragmaticPlugin ) ;
206+ if ( ! disableCodeSandboxPlugins ) {
207+ if (
208+ flattenedPresets . indexOf ( 'env' ) > - 1 &&
209+ Object . keys ( Babel . availablePresets ) . indexOf ( 'env' ) === - 1
210+ ) {
211+ Babel . registerPreset ( 'env' , Babel . availablePresets . latest ) ;
212+ }
213+
214+ if (
215+ flattenedPlugins . indexOf ( 'transform-vue-jsx' ) > - 1 &&
216+ Object . keys ( Babel . availablePlugins ) . indexOf ( 'transform-vue-jsx' ) === - 1
217+ ) {
218+ const vuePlugin = await import ( /* webpackChunkName: 'babel-plugin-transform-vue-jsx' */ 'babel-plugin-transform-vue-jsx' ) ;
219+ Babel . registerPlugin ( 'transform-vue-jsx' , vuePlugin ) ;
220+ }
221+
222+ if (
223+ flattenedPlugins . indexOf ( 'jsx-pragmatic' ) > - 1 &&
224+ Object . keys ( Babel . availablePlugins ) . indexOf ( 'jsx-pragmatic' ) === - 1
225+ ) {
226+ const pragmaticPlugin = await import ( /* webpackChunkName: 'babel-plugin-jsx-pragmatic' */ 'babel-plugin-jsx-pragmatic' ) ;
227+ Babel . registerPlugin ( 'jsx-pragmatic' , pragmaticPlugin ) ;
228+ }
201229 }
202230
203231 try {
@@ -235,18 +263,22 @@ self.addEventListener('message', async event => {
235263 } )
236264 ) ;
237265
238- const plugins = [ ...config . plugins , 'dynamic-import-node' ] ;
266+ const plugins = [ ...config . plugins ] ;
239267
240- if ( loaderOptions . dynamicCSSModules ) {
241- plugins . push ( 'dynamic-css-modules' ) ;
242- }
268+ if ( ! disableCodeSandboxPlugins ) {
269+ plugins . push ( 'dynamic-import-node' ) ;
243270
244- plugins . push ( [ 'babel-plugin-detective' , { source : true , nodes : true } ] ) ;
271+ if ( loaderOptions . dynamicCSSModules ) {
272+ plugins . push ( 'dynamic-css-modules' ) ;
273+ }
245274
246- if ( ! sandboxOptions || sandboxOptions . infiniteLoopProtection ) {
247- plugins . push ( 'babel-plugin-transform-prevent-infinite-loops' ) ;
275+ if ( ! sandboxOptions || sandboxOptions . infiniteLoopProtection ) {
276+ plugins . push ( 'babel-plugin-transform-prevent-infinite-loops' ) ;
277+ }
248278 }
249279
280+ plugins . push ( [ 'babel-plugin-detective' , { source : true , nodes : true } ] ) ;
281+
250282 const customConfig = {
251283 ...config ,
252284 plugins,
0 commit comments