storytouch / ep_webpack

Etherpad plugin to bundle files using webpack
Apache License 2.0
3 stars 0 forks source link

ep_webpack

Etherpad plugin to bundle files using webpack, thus improving the performance of file loading on client-side.

Why should you use this plugin?

Sometimes the browser takes FOREVER to load Etherpad editor, especially on instances with a lot of plugins, and with each plugin using a lot of files. On an instance with 30+ plugins and 400+ files to load, it can take up to a minute for the pad to be ready to be edited.

By installing this plugin you can speed up this process a lot:

Features:

Development mode

This plugin has a watch mode, so you don't need to restart Etherpad every time you change something on the plugin files. To use it, just go the the folder where ep_webpack is installed, and run the watch command with the target webpack.config.*.js file for your settings:

cd path/to/etherpad-lite/node_modules/ep_webpack && npm run watch -- --config './webpack.config-withMinifyAndCss.js'

If Docker is your thing, you can add this to your docker-compose.yml:

  webpack:
    # ideally, use the same image used by Etherpad containers
    image: node:8.9.0
    environment:
      NODE_ENV: "development"
    command: bash -c "cd /etherpad-lite/node_modules/ep_webpack && npm run watch -- --config './webpack.config-withMinifyAndCss.js'"
    volumes:
      - path/to/etherpad-lite/:/etherpad-lite/

How does this plugin work?

Here's a short explanation of what ep_webpack does to bundle all plugins and serve a single file to the client:

  1. on the first server-side hook after plugins are loaded (loadSettings), we get the original list of plugins & their hooks on plugins.parts (defined on ep_etherpad-lite/static/js/pluginfw/plugins);
  2. for each file containing a client-side hook, we import it on a single file (ep_webpack/static/js/index.js). This allows us to use a single entry point for all dependencies;
  3. then, we generate the bundle using webpack. A new file is created: ep_webpack/static/dist/js/index;
  4. after that, for each client-side hook, we override the path for its definition with the generated file, making Etherpad call a single JS file for all the plugins it loads on the client.

If CSS files should be bundled, some extra steps are executed on the flow mentioned above:

  1. we try to execute all aceEditorCSS hooks in order to get the list of CSS files, and we import them together with the JS single entry point (ep_webpack/static/js/index.js);
  2. when we run webpack, another file is created: ep_webpack/static/dist/css/all.css;
  3. finally, all aceEditorCSS hooks are removed from the plugins, and the only hook of that type will point to the bundled CSS file.

Available customizations

Ignore a plugin

Sometimes an installed plugin is not ready to be used with webpack, so it is possible to ignore it when building the bundle: just include the part(s) of the plugin on your settings.json (notice that you need to exclude the parts, not the plugins):

  "ep_webpack": {
    /* Ignores parts (not plugins) not ready to be bundled */
    "ignoredParts": ["autocomp", "comments_page"]
  }

Bundle CSS files too

Warning: this is an advanced customization and might not work with all the plugins

If you also want to bundle CSS files, you need to turn this feature on on the settings.json:

  "ep_webpack": {
    /* Bundle CSS files too */
    "bundleCSS": true
  }

If you enable this feature and start to see error messages on the server console, take a look on the Troubleshooting session.

Use your own set of webpack configs

ep_webpack comes with a set of default configs to bundle the files, but you can override them by defining a JS object that will be used when bundling files:

  "ep_webpack": {
    /*
     * Path to the custom webpack config file. Must be an absolute path,
     * or relative to etherpad-lite/node_modules/ep_webpack/.
     */
    "customWebpackConfigFile": "path/to/your/webpack.config.js"
  }

Troubleshooting

The most common type of error is when trying to bundle CSS files. If you see an error message like Could not load the list of CSS files from aceEditorCSS hook, so its files won't be bundled., you'll need to make some changes in order to be able to bundle the CSS files:

Next improvements

Want to help? Here's a list of future improvements this plugins might have: