1- import { dispatch , clearErrorTransformers } from 'codesandbox-api' ;
1+ import { dispatch , reattach , clearErrorTransformers } from 'codesandbox-api' ;
22import { absolute } from 'common/utils/path' ;
33import _debug from 'app/utils/debug' ;
44import parseConfigurations from 'common/templates/configuration/parse' ;
@@ -54,28 +54,143 @@ const WHITELISTED_DEV_DEPENDENCIES = [
5454
5555// Dependencies that we actually don't need, we will replace this by a dynamic
5656// system in the future
57- const BLACKLISTED_DEPENDENCIES = [ 'react-scripts' , 'react-scripts-ts' ] ;
57+ const PREINSTALLED_DEPENDENCIES = [
58+ 'node-lib-browser' ,
59+ 'babel-runtime' ,
60+ 'react-scripts' ,
61+ 'react-scripts-ts' ,
62+ 'parcel-bundler' ,
63+ 'babel-preset-env' ,
64+ 'babel-preset-latest' ,
65+ 'babel-preset-es2015' ,
66+ 'babel-preset-es2015-loose' ,
67+ 'babel-preset-es2016' ,
68+ 'babel-preset-es2017' ,
69+ 'babel-preset-react' ,
70+ 'babel-preset-stage-0' ,
71+ 'babel-preset-stage-1' ,
72+ 'babel-preset-stage-2' ,
73+ 'babel-preset-stage-3' ,
74+ 'babel-plugin-check-es2015-constants' ,
75+ 'babel-plugin-external-helpers' ,
76+ 'babel-plugin-inline-replace-variables' ,
77+ 'babel-plugin-syntax-async-functions' ,
78+ 'babel-plugin-syntax-async-generators' ,
79+ 'babel-plugin-syntax-class-constructor-call' ,
80+ 'babel-plugin-syntax-class-properties' ,
81+ 'babel-plugin-syntax-decorators' ,
82+ 'babel-plugin-syntax-do-expressions' ,
83+ 'babel-plugin-syntax-exponentiation-operator' ,
84+ 'babel-plugin-syntax-export-extensions' ,
85+ 'babel-plugin-syntax-flow' ,
86+ 'babel-plugin-syntax-function-bind' ,
87+ 'babel-plugin-syntax-function-sent' ,
88+ 'babel-plugin-syntax-jsx' ,
89+ 'babel-plugin-syntax-object-rest-spread' ,
90+ 'babel-plugin-syntax-trailing-function-commas' ,
91+ 'babel-plugin-transform-async-functions' ,
92+ 'babel-plugin-transform-async-to-generator' ,
93+ 'babel-plugin-transform-async-to-module-method' ,
94+ 'babel-plugin-transform-class-constructor-call' ,
95+ 'babel-plugin-transform-class-properties' ,
96+ 'babel-plugin-transform-decorators' ,
97+ 'babel-plugin-transform-decorators-legacy' ,
98+ 'babel-plugin-transform-do-expressions' ,
99+ 'babel-plugin-transform-es2015-arrow-functions' ,
100+ 'babel-plugin-transform-es2015-block-scoped-functions' ,
101+ 'babel-plugin-transform-es2015-block-scoping' ,
102+ 'babel-plugin-transform-es2015-classes' ,
103+ 'babel-plugin-transform-es2015-computed-properties' ,
104+ 'babel-plugin-transform-es2015-destructuring' ,
105+ 'babel-plugin-transform-es2015-duplicate-keys' ,
106+ 'babel-plugin-transform-es2015-for-of' ,
107+ 'babel-plugin-transform-es2015-function-name' ,
108+ 'babel-plugin-transform-es2015-instanceof' ,
109+ 'babel-plugin-transform-es2015-literals' ,
110+ 'babel-plugin-transform-es2015-modules-amd' ,
111+ 'babel-plugin-transform-es2015-modules-commonjs' ,
112+ 'babel-plugin-transform-es2015-modules-systemjs' ,
113+ 'babel-plugin-transform-es2015-modules-umd' ,
114+ 'babel-plugin-transform-es2015-object-super' ,
115+ 'babel-plugin-transform-es2015-parameters' ,
116+ 'babel-plugin-transform-es2015-shorthand-properties' ,
117+ 'babel-plugin-transform-es2015-spread' ,
118+ 'babel-plugin-transform-es2015-sticky-regex' ,
119+ 'babel-plugin-transform-es2015-template-literals' ,
120+ 'babel-plugin-transform-es2015-typeof-symbol' ,
121+ 'babel-plugin-transform-es2015-unicode-regex' ,
122+ 'babel-plugin-transform-es3-member-expression-literals' ,
123+ 'babel-plugin-transform-es3-property-literals' ,
124+ 'babel-plugin-transform-es5-property-mutators' ,
125+ 'babel-plugin-transform-eval' ,
126+ 'babel-plugin-transform-exponentiation-operator' ,
127+ 'babel-plugin-transform-export-extensions' ,
128+ 'babel-plugin-transform-flow-comments' ,
129+ 'babel-plugin-transform-flow-strip-types' ,
130+ 'babel-plugin-transform-function-bind' ,
131+ 'babel-plugin-transform-jscript' ,
132+ 'babel-plugin-transform-object-assign' ,
133+ 'babel-plugin-transform-object-rest-spread' ,
134+ 'babel-plugin-transform-object-set-prototype-of-to-assign' ,
135+ 'babel-plugin-transform-proto-to-assign' ,
136+ 'babel-plugin-transform-react-constant-elements' ,
137+ 'babel-plugin-transform-react-display-name' ,
138+ 'babel-plugin-transform-react-inline-elements' ,
139+ 'babel-plugin-transform-react-jsx' ,
140+ 'babel-plugin-transform-react-jsx-compat' ,
141+ 'babel-plugin-transform-react-jsx-self' ,
142+ 'babel-plugin-transform-react-jsx-source' ,
143+ 'babel-plugin-transform-regenerator' ,
144+ 'babel-plugin-transform-runtime' ,
145+ 'babel-plugin-transform-strict-mode' ,
146+ 'babel-plugin-undeclared-variables-check' ,
147+ 'babel-plugin-dynamic-import-node' ,
148+ 'babel-plugin-detective' ,
149+ 'babel-plugin-transform-prevent-infinite-loops' ,
150+ 'babel-plugin-transform-vue-jsx' ,
151+ 'babel-plugin-jsx-pragmatic' ,
152+ ] ;
58153
59- function getDependencies ( parsedPackage ) {
154+ function getDependencies ( parsedPackage , configurations ) {
60155 const {
61156 dependencies : d = { } ,
62157 peerDependencies = { } ,
63158 devDependencies = { } ,
64159 } = parsedPackage ;
65160
66- const returnedDependencies = { ...peerDependencies } ;
161+ const returnedDependencies = { ...peerDependencies , ...d } ;
162+
163+ const foundWhitelistedDevDependencies = [ ...WHITELISTED_DEV_DEPENDENCIES ] ;
164+
165+ // Add all babel plugins/presets to whitelisted dependencies
166+ if ( configurations && configurations . babel && configurations . babel . parsed ) {
167+ ( configurations . babel . parsed . presets || [ ] )
168+ . filter ( p => typeof p === 'string' )
169+ . forEach ( p => {
170+ foundWhitelistedDevDependencies . push ( p ) ;
171+ foundWhitelistedDevDependencies . push ( `babel-preset-${ p } ` ) ;
172+ } ) ;
173+
174+ ( configurations . babel . parsed . plugins || [ ] )
175+ . filter ( p => typeof p === 'string' )
176+ . forEach ( p => {
177+ foundWhitelistedDevDependencies . push ( p ) ;
178+ foundWhitelistedDevDependencies . push ( `babel-plugin-${ p } ` ) ;
179+ } ) ;
180+ }
67181
68- Object . keys ( d ) . forEach ( dep => {
69- if ( BLACKLISTED_DEPENDENCIES . indexOf ( dep ) === - 1 ) {
70- returnedDependencies [ dep ] = d [ dep ] ;
71- }
72- } ) ;
73182 Object . keys ( devDependencies ) . forEach ( dep => {
74- if ( WHITELISTED_DEV_DEPENDENCIES . indexOf ( dep ) > - 1 ) {
183+ if ( foundWhitelistedDevDependencies . indexOf ( dep ) > - 1 ) {
75184 returnedDependencies [ dep ] = devDependencies [ dep ] ;
76185 }
77186 } ) ;
78187
188+ PREINSTALLED_DEPENDENCIES . forEach ( dep => {
189+ if ( returnedDependencies [ dep ] ) {
190+ delete returnedDependencies [ dep ] ;
191+ }
192+ } ) ;
193+
79194 return returnedDependencies ;
80195}
81196
@@ -103,8 +218,7 @@ async function updateManager(
103218 manager . setManifest ( manifest ) ;
104219 }
105220 manager . updateConfigurations ( configurations ) ;
106- await manager . updateData ( managerModules ) ;
107- return manager ;
221+ return await manager . updateData ( managerModules ) ;
108222}
109223
110224function initializeResizeListener ( ) {
@@ -119,6 +233,24 @@ function initializeResizeListener() {
119233 } ) ;
120234 initializedResizeListener = true ;
121235}
236+
237+ function overrideDocumentClose ( ) {
238+ const oldClose = window . document . close ;
239+
240+ window . document . close = function close ( ...args ) {
241+ try {
242+ oldClose . call ( document , args ) ;
243+ } catch ( e ) {
244+ throw e ;
245+ } finally {
246+ inject ( ) ;
247+ reattach ( ) ;
248+ }
249+ } ;
250+ }
251+
252+ overrideDocumentClose ( ) ;
253+
122254inject ( ) ;
123255
124256async function compile ( {
@@ -142,6 +274,7 @@ async function compile({
142274 } catch ( e ) {
143275 console . error ( e ) ;
144276 }
277+
145278 hadError = false ;
146279
147280 actionsEnabled = hasActions ;
@@ -175,7 +308,7 @@ async function compile({
175308
176309 const parsedPackageJSON = configurations . package . parsed ;
177310
178- const dependencies = getDependencies ( parsedPackageJSON ) ;
311+ const dependencies = getDependencies ( parsedPackageJSON , configurations ) ;
179312 const { manifest, isNewCombination } = await loadDependencies ( dependencies ) ;
180313
181314 if ( isNewCombination && ! firstLoad ) {
@@ -237,7 +370,7 @@ async function compile({
237370 /* don't do anything with this error */
238371 }
239372 }
240- if ( ! manager . webpackHMR || firstLoad ) {
373+ if ( ( ! manager . webpackHMR || firstLoad ) && ! manager . preset . htmlDisabled ) {
241374 const htmlModule =
242375 modules [
243376 templateDefinition
@@ -257,7 +390,8 @@ async function compile({
257390 const oldHTML = document . body . innerHTML ;
258391 const evalled = manager . evaluateModule ( managerModuleToTranspile ) ;
259392 debug ( `Evaluation time: ${ Date . now ( ) - tt } ms` ) ;
260- const domChanged = oldHTML !== document . body . innerHTML ;
393+ const domChanged =
394+ ! manager . preset . htmlDisabled && oldHTML !== document . body . innerHTML ;
261395
262396 if (
263397 isModuleView &&
@@ -291,7 +425,7 @@ async function compile({
291425
292426 await manager . preset . teardown ( manager ) ;
293427
294- if ( ! initializedResizeListener ) {
428+ if ( ! initializedResizeListener && ! manager . preset . htmlDisabled ) {
295429 initializeResizeListener ( ) ;
296430 }
297431
@@ -325,8 +459,9 @@ async function compile({
325459 manager . clearCache ( ) ;
326460 }
327461
328- e . module = e . module ;
329- e . fileName = e . fileName ;
462+ if ( firstLoad ) {
463+ inject ( ) ;
464+ }
330465
331466 const event = new Event ( 'error' ) ;
332467 event . error = e ;
0 commit comments