Advanced setup

The Installation guide describes the easiest ways to run CKEditor builds in your project and the Custom builds guide explains how to add or remove features from the build or change webpack configuration.

In this guide, we would like to show you ways to closer integrate CKEditor 5 with your application. Thanks to that, you will be able to optimize the bundling process of your project and customize the builds in a more convenient way.

Requirements

In order to start developing CKEditor 5 you will require:

  • Node.js 6.9.0+
  • npm 4+ (note: some npm 5+ versions were known to cause problems, especially with deduplicating packages; upgrade npm when in doubt)
  • Git

Bundler

CKEditor 5 is currently built using webpack@4. All builds, examples and demos are generated using this bundler. It should also be possible to build CKEditor using other bundlers (if they are configured properly), such as Rollup or Browserify, but these setups are not officially supported yet. Also, the @ckeditor/ckeditor5-dev-webpack-plugin that allows to localize the editor is only available for webpack. More work on this subject will be done in the future.

Therefore, a prerequisite to this guide is that you are using webpack as your build tool.

Scenario 1: Integrating existing builds

This is the simplest scenario. It assumes that you want to use one of the existing builds “as-is” (you can, of course, still configure the rich text editor). It also gives the fastest build times.

First, install the build of your choice from npm:

  1. npm install --save @ckeditor/ckeditor5-build-classic

Now, import the editor build into your code:

  1. // Using ES6 imports:
  2. import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
  3. // Or CJS imports:
  4. const ClassicEditor = require( '@ckeditor/ckeditor5-build-classic' );

And use it:

  1. ClassicEditor
  2. .create( document.querySelector( '#editor' ) )
  3. .then( editor => {
  4. console.log( editor );
  5. } )
  6. .catch( error => {
  7. console.error( error );
  8. } );

Since you are using an already built editor (so a result of passing CKEditor 5 source through webpack), you do not need any additional webpack configuration. In this case CKEditor works as a ready-to-use library.

Scenario 2: Building from source

This scenario allows you to fully control the building process of CKEditor. This means that you will not actually use the builds anymore, but instead build CKEditor from source directly into your project. This integration method gives you full control over which features will be included and how webpack will be configured.

Similar results to what this method allows can be achieved by customizing an existing build and integrating your custom build like in scenario 1. This will give faster build times (since CKEditor will be built once and committed), however, it requires maintaining a separate repository and installing the code from that repository into your project (e.g. by publishing a new npm package or using tools like Lerna). This makes it less convenient than the method described in this scenario.

First of all, you need to install source packages that you will use. If you base your integration on one of the existing builds, you can take them from that build’s package.json file (see e.g. classic build’s package.json). At this moment you can choose the editor creator and the features you want.

Copy these dependencies to your package.json and call npm install to install them. The dependencies (or devDependencies) section of package.json should look more or less like this:

  1. "dependencies": {
  2. // ...
  3. "@ckeditor/ckeditor5-adapter-ckfinder": "^x.y.z",
  4. "@ckeditor/ckeditor5-autoformat": "^x.y.z",
  5. "@ckeditor/ckeditor5-basic-styles": "^x.y.z",
  6. "@ckeditor/ckeditor5-block-quote": "^x.y.z",
  7. "@ckeditor/ckeditor5-easy-image": "^x.y.z",
  8. "@ckeditor/ckeditor5-editor-classic": "^x.y.z",
  9. "@ckeditor/ckeditor5-essentials": "^x.y.z",
  10. "@ckeditor/ckeditor5-heading": "^x.y.z",
  11. "@ckeditor/ckeditor5-image": "^x.y.z",
  12. "@ckeditor/ckeditor5-link": "^x.y.z",
  13. "@ckeditor/ckeditor5-list": "^x.y.z",
  14. "@ckeditor/ckeditor5-paragraph": "^x.y.z",
  15. "@ckeditor/ckeditor5-theme-lark": "^x.y.z",
  16. "@ckeditor/ckeditor5-upload": "^x.y.z"
  17. // ...
  18. }

The second step is to install dependencies needed to build the editor. The list may differ if you want to customize the webpack configuration, but this is a typical setup:

  1. npm install --save \
  2. @ckeditor/ckeditor5-dev-webpack-plugin \
  3. @ckeditor/ckeditor5-dev-utils \
  4. postcss-loader \
  5. raw-loader \
  6. style-loader \
  7. webpack@4 \
  8. webpack-cli@3 \

Webpack configuration

You can now configure webpack. There are a couple of things that you need to take care of when building CKEditor 5:

  • Handling CSS files of the CKEditor theme. They are included in the CKEditor 5 sources using import 'path/to/styles.css' statements, so you need proper loaders.
  • Similarly, you need to handle bundling SVG icons, which are also imported directly into the source. For that you need the raw-loader.
  • Finally, to localize the editor you need to use the @ckeditor/ckeditor5-dev-webpack-plugin webpack plugin. The minimal configuration, assuming that you use the same methods of handling assets as CKEditor 5 builds, will look like this:
  1. const CKEditorWebpackPlugin = require( '@ckeditor/ckeditor5-dev-webpack-plugin' );
  2. const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );
  3. module.exports = {
  4. plugins: [
  5. // ...
  6. new CKEditorWebpackPlugin( {
  7. // See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.html
  8. language: 'pl'
  9. } )
  10. ],
  11. module: {
  12. rules: [
  13. {
  14. // Or /ckeditor5-[^/]+\/theme\/icons\/[^/]+\.svg$/ if you want to limit this loader
  15. // to CKEditor 5 icons only.
  16. test: /\.svg$/,
  17. use: [ 'raw-loader' ]
  18. },
  19. {
  20. // Or /ckeditor5-[^/]+\/theme\/[\w-/]+\.css$/ if you want to limit this loader
  21. // to CKEditor 5 theme only.
  22. test: /\.css$/,
  23. use: [
  24. {
  25. loader: 'style-loader',
  26. options: {
  27. singleton: true
  28. }
  29. },
  30. {
  31. loader: 'postcss-loader',
  32. options: styles.getPostCssConfig( {
  33. themeImporter: {
  34. themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
  35. },
  36. minify: true
  37. } )
  38. },
  39. ]
  40. }
  41. ]
  42. }
  43. };

Running the editor – method 1

You can now import all the needed plugins and the creator directly into your code and use it there. The easiest way to do so is to copy it from the src/ckeditor.js file available in every build repository.

  1. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  2. import EssentialsPlugin from '@ckeditor/ckeditor5-essentials/src/essentials';
  3. import UploadAdapterPlugin from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
  4. import AutoformatPlugin from '@ckeditor/ckeditor5-autoformat/src/autoformat';
  5. import BoldPlugin from '@ckeditor/ckeditor5-basic-styles/src/bold';
  6. import ItalicPlugin from '@ckeditor/ckeditor5-basic-styles/src/italic';
  7. import BlockQuotePlugin from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  8. import EasyImagePlugin from '@ckeditor/ckeditor5-easy-image/src/easyimage';
  9. import HeadingPlugin from '@ckeditor/ckeditor5-heading/src/heading';
  10. import ImagePlugin from '@ckeditor/ckeditor5-image/src/image';
  11. import ImageCaptionPlugin from '@ckeditor/ckeditor5-image/src/imagecaption';
  12. import ImageStylePlugin from '@ckeditor/ckeditor5-image/src/imagestyle';
  13. import ImageToolbarPlugin from '@ckeditor/ckeditor5-image/src/imagetoolbar';
  14. import ImageUploadPlugin from '@ckeditor/ckeditor5-image/src/imageupload';
  15. import LinkPlugin from '@ckeditor/ckeditor5-link/src/link';
  16. import ListPlugin from '@ckeditor/ckeditor5-list/src/list';
  17. import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  18. export default class ClassicEditor extends ClassicEditorBase {}
  19. ClassicEditor.builtinPlugins = [
  20. EssentialsPlugin,
  21. UploadAdapterPlugin,
  22. AutoformatPlugin,
  23. BoldPlugin,
  24. ItalicPlugin,
  25. BlockQuotePlugin,
  26. EasyImagePlugin,
  27. HeadingPlugin,
  28. ImagePlugin,
  29. ImageCaptionPlugin,
  30. ImageStylePlugin,
  31. ImageToolbarPlugin,
  32. ImageUploadPlugin,
  33. LinkPlugin,
  34. ListPlugin,
  35. ParagraphPlugin
  36. ];
  37. ClassicEditor.defaultConfig = {
  38. toolbar: {
  39. items: [
  40. 'heading',
  41. '|',
  42. 'bold',
  43. 'italic',
  44. 'link',
  45. 'bulletedList',
  46. 'numberedList',
  47. 'imageUpload',
  48. 'blockQuote',
  49. 'undo',
  50. 'redo'
  51. ]
  52. },
  53. image: {
  54. toolbar: [
  55. 'imageStyle:full',
  56. 'imageStyle:side',
  57. '|',
  58. 'imageTextAlternative'
  59. ]
  60. },
  61. language: 'en'
  62. };

This module will export an editor creator class which has all the plugins and configuration that you need already built-in. To use such editor, simply import that class and call the static .create() method like in all examples.

  1. import ClassicEditor from './ckeditor';
  2. ClassicEditor
  3. // Note that you do not have to specify the plugin and toolbar configuration — using defaults from the build.
  4. .create( document.querySelector( '#editor' ) )
  5. .then( editor => {
  6. console.log( 'Editor was initialized', editor );
  7. } )
  8. .catch( error => {
  9. console.error( error.stack );
  10. } );

Running the editor – method 2

The second variant how to run the editor is to use the creator class directly, without creating an intermediary subclass. The above code would translate to:

  1. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  2. import EssentialsPlugin from '@ckeditor/ckeditor5-essentials/src/essentials';
  3. import UploadAdapterPlugin from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
  4. import AutoformatPlugin from '@ckeditor/ckeditor5-autoformat/src/autoformat';
  5. import BoldPlugin from '@ckeditor/ckeditor5-basic-styles/src/bold';
  6. import ItalicPlugin from '@ckeditor/ckeditor5-basic-styles/src/italic';
  7. import BlockQuotePlugin from '@ckeditor/ckeditor5-block-quote/src/blockquote';
  8. import EasyImagePlugin from '@ckeditor/ckeditor5-easy-image/src/easyimage';
  9. import HeadingPlugin from '@ckeditor/ckeditor5-heading/src/heading';
  10. import ImagePlugin from '@ckeditor/ckeditor5-image/src/image';
  11. import ImageCaptionPlugin from '@ckeditor/ckeditor5-image/src/imagecaption';
  12. import ImageStylePlugin from '@ckeditor/ckeditor5-image/src/imagestyle';
  13. import ImageToolbarPlugin from '@ckeditor/ckeditor5-image/src/imagetoolbar';
  14. import ImageUploadPlugin from '@ckeditor/ckeditor5-image/src/imageupload';
  15. import LinkPlugin from '@ckeditor/ckeditor5-link/src/link';
  16. import ListPlugin from '@ckeditor/ckeditor5-list/src/list';
  17. import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  18. ClassicEditor
  19. .create( document.querySelector( '#editor'), {
  20. // The plugins are now passed directly to .create().
  21. plugins: [
  22. EssentialsPlugin,
  23. AutoformatPlugin,
  24. BoldPlugin,
  25. ItalicPlugin,
  26. BlockQuotePlugin,
  27. HeadingPlugin,
  28. ImagePlugin,
  29. ImageCaptionPlugin,
  30. ImageStylePlugin,
  31. ImageToolbarPlugin,
  32. EasyImagePlugin,
  33. ImageUploadPlugin,
  34. LinkPlugin,
  35. ListPlugin,
  36. ParagraphPlugin,
  37. UploadAdapterPlugin
  38. ],
  39. // So is the rest of the default configuration.
  40. toolbar: [
  41. 'heading',
  42. 'bold',
  43. 'italic',
  44. 'link',
  45. 'bulletedList',
  46. 'numberedList',
  47. 'imageUpload',
  48. 'blockQuote',
  49. 'undo',
  50. 'redo'
  51. ],
  52. image: {
  53. toolbar: [
  54. 'imageStyle:full',
  55. 'imageStyle:side',
  56. '|',
  57. 'imageTextAlternative'
  58. ]
  59. }
  60. } )
  61. .then( editor => {
  62. console.log( editor );
  63. } )
  64. .catch( error => {
  65. console.error( error );
  66. } );

Building

Finally, you can build your application. Run webpack on your project and the rich text editor will be a part of it.

Option: Minifying JavaScript

Webpack 4 introduced the concept of modes. It comes with two predefined modes: development and production. The latter automatically enables uglifyjs-webpack-plugin which takes care of JavaScript minification. Therefore, it is enough to execute webpack with the —mode production option or set mode: 'production' in your webpack.config.js to optimize the build.

Prior to version 1.2.7 uglifyjs-webpack-plugin had a bug which caused webpack to crash with the following error: TypeError: Assignment to constant variable.. If you experienced this error, make sure that your node_modules contains an up-to-date version of this package (and that webpack uses this version).

Option: Extracting CSS

One of the most common requirements is to extract CKEditor 5 CSS to a separate file (by default it is included in the output JavaScript file). To do that, you can use mini-css-extract-plugin:

  1. npm install --save \
  2. mini-css-extract-plugin \
  3. css-loader

And add it to your webpack configuration:

  1. const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
  2. module.exports = {
  3. // ...
  4. plugins: [
  5. // ...
  6. new MiniCssExtractPlugin( {
  7. filename: 'styles.css'
  8. } )
  9. ],
  10. module: {
  11. rules: [
  12. {
  13. test: /\.svg$/,
  14. use: [ 'raw-loader' ]
  15. },
  16. {
  17. test: /\.css$/,
  18. use: [
  19. MiniCssExtractPlugin.loader,
  20. 'css-loader',
  21. {
  22. loader: 'postcss-loader',
  23. options: styles.getPostCssConfig( {
  24. themeImporter: {
  25. themePath: require.resolve( '@ckeditor/ckeditor5-theme-lark' )
  26. },
  27. minify: true
  28. } )
  29. }
  30. ]
  31. }
  32. ]
  33. }
  34. };

Webpack will now create a separate file called styles.css which you will need to load manually into your HTML (using the <link rel="stylesheet"> tag).

Option: Building to ES5 target

CKEditor 5 is written in ECMAScript 2015 (also called ES6). All browsers in which CKEditor 5 is currently supported have sufficient ES6 support to run CKEditor 5. Thanks to that, CKEditor 5 Builds are also published in the original ES6 format.

However, it may happen that your environment requires ES5. For instance, if you use tools like the original UglifyJS which do not support ES6+ yet, you may need to transpile CKEditor 5 source to ES5. This will create ~80% bigger builds but will ensure that your environment can process CKEditor 5 code.

In the production mode webpack uses uglifyjs-webpack-plugin which supports ES6+ code. This is because it does not use the original UglifyJS plugin (which does not support ES6+), but instead it uses the uglify-es package.

We recommend upgrading your setup to webpack@4 and its built-in modes which allows you to avoid transpiling the source to ES5.

In order to create an ES5 build of CKEditor 5 you can use Babel:

  1. npm install --save babel-loader @babel/core @babel/preset-env regenerator-runtime

Then, add this item to webpack module.rules section:

  1. module: {
  2. rules: [
  3. {
  4. test: /ckeditor5-[^\/\\]+[\/\\].*\.js$/,
  5. use: [
  6. {
  7. loader: 'babel-loader',
  8. options: {
  9. presets: [ require( '@babel/preset-env' ) ]
  10. }
  11. }
  12. ]
  13. },
  14. ...
  15. ]
  16. }

And load regenerator-runtime (needed to make ES6 generators work after transpilation) by adding it as the first entry point:

  1. entry: [
  2. require.resolve( 'regenerator-runtime/runtime.js' ),
  3. // Your entries...
  4. ]

This setup ensures that the source code is transpiled to ES5. However, it does not ensure that all ES6 polyfills are loaded. Therefore, if you would like to, for example, give bringing IE11 compatibility a try, make sure to also load babel-polyfill.

The babel-preset-env package lets you choose the environment that you want to support and transpiles ES6+ features to match that environment’s capabilities. Without configuration it will produce ES5 builds.

Scenario 3: Using two different editors

The ability to use two or more types of rich text editors on one page is a common requirement. For instance, you may want to use the classic editor next to a couple of inline editors.

Do not load two builds on one page. This is a mistake which leads to:

  • Code duplication. Both builds share up to 99% of the code, including CSS and SVGs. By loading them twice you make your page unnecessarily heavy.
  • Duplicated CSS may lead to conflicts and, thus, broken UI of the editors.
  • Translation repository gets duplicated entries which may cause loading incorrect strings with translations.

Solutions

If you want to load two different editors on one page you need to make sure that they are built together (once). This can be achieved in at least two ways:

Creating “super builds”

There is no limit for how many editor classes a single build can export. By default, the official builds export a single editor class only. However, they can easily import more.

You can start from forking (or copying) an existing build like in the “Creating custom builds” guide. Let’s say you forked and cloned the ckeditor5-build-classic repository and want to add InlineEditor to it:

  1. git clone -b stable git@github.com:<your-username>/ckeditor5-build-classic.git
  2. cd ckeditor5-build-classic
  3. npm install

Now it is time to add the missing editor package and install it:

  1. npm install --save-dev @ckeditor/ckeditor5-editor-inline

Once all the dependencies are installed, modify the webpack’s entry point which is the src/ckeditor.js file. For now it was exporting just a single class:

  1. // The editor creator to use.
  2. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  3. // ...
  4. export default class ClassicEditor extends ClassicEditorBase {}
  5. // Plugins to include in the build.
  6. ClassicEditor.builtinPlugins = [
  7. // ...
  8. ];
  9. // Editor configuration.
  10. ClassicEditor.defaultConfig = {
  11. // ...
  12. };

Let’s make it export an object with two classes: ClassicEditor and InlineEditor. To make both constructors work in the same way (load the same plugins and default configuration) you also need to assign builtinPlugins and defaultConfig static properties to both of them:

  1. // The editor creators to use.
  2. import ClassicEditorBase from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  3. import InlineEditorBase from '@ckeditor/ckeditor5-editor-inline/src/inlineeditor';
  4. // ...
  5. class ClassicEditor extends ClassicEditorBase {}
  6. class InlineEditor extends InlineEditorBase {}
  7. // Plugins to include in the build.
  8. const plugins = [
  9. // ...
  10. ];
  11. ClassicEditor.builtinPlugins = plugins;
  12. InlineEditor.builtinPlugins = plugins;
  13. // Editor configuration.
  14. const config = {
  15. // ...
  16. };
  17. ClassicEditor.defaultConfig = config;
  18. InlineEditor.defaultConfig = config;
  19. export default {
  20. ClassicEditor, InlineEditor
  21. };

Since you now export an object with two properties (ClassicEditor and InlineEditor), it is also reasonable to rename the global variable to which webpack will assign this object. So far it was called ClassicEditor. A more adequate name now would be for example CKEDITOR. This variable is defined in webpack.config.js in the output.library setting:

  1. diff --git a/webpack.config.js b/webpack.config.js
  2. index c57e371..04fc9fe 100644
  3. --- a/webpack.config.js
  4. +++ b/webpack.config.js
  5. @@ -21,7 +21,7 @@ module.exports = {
  6. output: {
  7. // The name under which the editor will be exported.
  8. - library: 'ClassicEditor',
  9. + library: 'CKEDITOR',
  10. path: path.resolve( __dirname, 'build' ),
  11. filename: 'ckeditor.js',

Once you changed the src/ckeditor.js and webpack.config.js files it is time to rebuild the build:

  1. yarn run build

Finally, when webpack finishes compiling your super build, you can change the samples/index.html file to test both editors:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CKEditor 5 – super build</title>
  6. <style>
  7. body {
  8. max-width: 800px;
  9. margin: 20px auto;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>CKEditor 5 – super build</h1>
  15. <div id="classic-editor">
  16. <h2>Sample</h2>
  17. <p>This is an instance of the <a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/overview.html#classic-editor">classic editor build</a>.</p>
  18. </div>
  19. <div id="inline-editor">
  20. <h2>Sample</h2>
  21. <p>This is an instance of the <a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/overview.html#inline-editor">inline editor build</a>.</p>
  22. </div>
  23. <script src="../build/ckeditor.js"></script>
  24. <script>
  25. CKEDITOR.ClassicEditor
  26. .create( document.querySelector( '#classic-editor' ) )
  27. .catch( err => {
  28. console.error( err.stack );
  29. } );
  30. CKEDITOR.InlineEditor
  31. .create( document.querySelector( '#inline-editor' ) )
  32. .catch( err => {
  33. console.error( err.stack );
  34. } );
  35. </script>
  36. </body>
  37. </html>