Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Progressively Adopting Your Favorite Framework

Progressively Adopting Your Favorite Framework

When working on a product you can’t simply stop development to rewrite your old web application using the latest tools and frameworks. This is a talk about how to progressively modernize and improve your old web application without disrupting daily product goals, while improving the overall codebase with tests and using that shiny new framework you always wanted. We will talk about Angularjs, Vuejs, webpack and unit testing with Jest.

Avatar for João Sardinha

João Sardinha

November 15, 2017
Tweet

Other Decks in Programming

Transcript

  1. Backstory • Outdated AngularJS version • A confusing mix of

    AngularJS and jQuery code • Part of a monolithic architecture • Untestable code and therefore 0 tests
  2. Adopting Vue.js for the UI • Can be used by

    including script directly in page • Component based with reactive view layer • Easy to adopt progressively as the project grows
  3. Overall Goals 1. Link existing application with new components 2.

    Preparing the future 3. Adding Unit Testing 4. Removing AngularJS entirely
  4. Talking with AngularJS • No fancy methods, just jQuery events

    • Main application triggers jQuery event with data • Sidebar catches the event and updates the data • Sidebar can emit events back to application
  5. Talking with AngularJS • On sidebar component mount jQuery(document)
 .on(‘updateData’,

    this.updateDataHandler) jQuery(document)
 .trigger(‘updateData’, [data]) • On main application
  6. Vue Components inside Angular •You can create new Vue components

    inside AngularJS •Create the mounting point for the component const MountingPoint = document.createElement('div'); •Place the mounting point in the desired place target.appendChild(MountingPoint);
  7. Vue Components inside Angular •Create the component const ComponentVM =

    new Component({ propsData: { message: 'Hello world’, }, }); •Mount to element ComponentVM.$mount(MountingPoint); •Destroy when not in use ComponentVM.$destroy();
  8. Overall Goals 1. Link existing application with new components ✅

    2. Preparing the future 3. Adding Unit Testing 4. Removing AngularJS entirely
  9. Preparing the future • The application becomes an independent project

    • Old application remains untouched • Separate old app from new app • New features will be added in the new Vue.js architecture, fully testable and modular.
  10. Preparing the future src/
 assets/
 scss/
 components/
 common/
 main.js tests/


    config/
 utils/
 data/
 unit/
 • Folder structure for new features
  11. Preparing the future • Add Webpack with support for Vue

    Single File Components (.vue files) • Migrate existing components to .vue files • Enable Live Reload and Hot Module Reloading (HMR) • Check github.com/vuejs-templates/webpack
  12. Managing common dependencies • Dependencies common to both applications will

    be managed by the new application and exposed to the old application through window • On main.js we expose jQuery import jQuery from 'jquery'; window.jQuery = jQuery;
  13. Overall Goals 1. Link existing application with new components ✅

    2. Preparing the future ✅ 3. Adding Unit Testing 4. Removing AngularJS entirely
  14. Adding Unit Testing • To make sure our application is

    healthy, we need tests. • We’re using Jest github.com/facebook/jest by Facebook • Plays well with our Webpack configuration • Reports code coverage
  15. Mocking dependencies • Mocking dependencies is super easy • In

    jest.config.js add setupFiles: ['<rootDir>/tests/mocks.js',], • In mocks.js add jest.mock('@/Api', () => require('@/ApiMock'));
  16. Adding Unit Testing • Any file that imports import Api

    from '@/Api'; import Api from '@/ApiMock'; • Will get
  17. Overall Goals 1. Link existing application with new components ✅

    2. Preparing the future ✅ 3. Adding Unit Testing ✅ 4. Removing AngularJS entirely
  18. Overall Goals 1. Link existing application with new components ✅

    2. Preparing the future ✅ 3. Adding Unit Testing ✅ 4. Removing AngularJS entirely
  19. To wrap-up 1. Adopted Vue.js for new components 2. Prepared

    the tools to help developing and deploying 3. Added unit tests to make sure the project remains healthy over time