Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically expose all ES6 imports in core modules #5601

Closed
chillu opened this issue May 26, 2016 · 4 comments
Closed

Automatically expose all ES6 imports in core modules #5601

chillu opened this issue May 26, 2016 · 4 comments

Comments

@chillu
Copy link
Member

chillu commented May 26, 2016

The framework/Gulpfile.js exposes a lot of ES6 modules (both vendor and custom code). This is used to avoid duplicating code in bundle files in other modules. For example, the JS code for the react ES6 module is only included once in framework, and then referenced in asset-admin/Gulpfile.js.

This approach leads to fragile bundles: If a developer forgets to expose an ES6 module in framework , it'll be inaccessible to other SilverStripe modules. If other SilverStripe modules pull in NPM dependencies for those ES6 modules as well, this can lead to the same JavaScript code being bundled twice. This works, due to the closure isolation in individual bundle files. But it makes it very hard to debug and set breakpoints.

framework/Gulpfile.js

.require(`${PATHS.MODULES}/bootstrap/dist/js/umd/collapse.js`,
  { expose: 'bootstrap-collapse' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Form/Form`,
  { expose: 'components/Form/Form' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/Form/FormConstants`,
  { expose: 'components/Form/FormConstants' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/FormAction/FormAction`,
  { expose: 'components/FormAction/FormAction' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/FormBuilder/FormBuilder`,
  { expose: 'components/FormBuilder/FormBuilder' }
)
.require(`${PATHS.ADMIN_JS_SRC}/components/GridField/GridField`,
  { expose: 'components/GridField/GridField' }
)
...

asset-admin/Gulpfile.js

.external('components/TextField/TextField')
.external('components/FormAction/FormAction')
.external('deep-freeze')
.external('lib/Config')
.external('react')
.external('jQuery')
.external('i18n')
.external('lib/SilverStripeComponent')
.external('lib/Backend')
.external('react-dom')
.external('react-addons-test-utils')
...

Options:

  • Create a browserify plugin which automatically exposes all modules it loads through require/import
  • Publish SilverStripe's custom ES6 code in framework as an NPM repo, add it to other SilverStripe modules as a dependency - and live with the bundle code duplication and potential version mismatches. This would still require expose on react and other "heavy" dependencies, we can't duplicate those in each bundle. Would also fix the current NPM include path issues (IDEs don't pick up import components/Form/Form in asset-admin since it's not defined in node_modules).
@sminnee
Copy link
Member

sminnee commented Sep 26, 2016

This has been rendered obsolete by Webpack. Any further comments, @chillu ?

@sminnee sminnee closed this as completed Sep 26, 2016
@chillu
Copy link
Member Author

chillu commented Sep 26, 2016

Hmmm, I believe it's the same issue, just in a different build tool: We still have lots of expose and externals calls - presumably they'll exhibit the same behaviour if you forget to add one (bundling duplicates)

https://github.com/silverstripe/silverstripe-framework/blob/master/admin/client/src/bundles/vendor.js#L14
https://github.com/silverstripe/silverstripe-framework/blob/master/webpack.config.js#L60

@chillu chillu reopened this Sep 26, 2016
@chillu
Copy link
Member Author

chillu commented Sep 26, 2016

The CommonsChunkPlugin will ensure that there's no duplication between framework/admin/client/dist/js/vendor.js and framework/admin/client/dist/js/bundle.js within framework: https://github.com/silverstripe/silverstripe-framework/blob/master/webpack.config.js#L135. But there's nothing warning you about bundling duplicates in asset-admin/client/dist/js/bundle.js

@chillu
Copy link
Member Author

chillu commented May 30, 2017

Replaced by silverstripe/webpack-config#1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants