๐ Bug report
CKEditor5 with Customized Build according to this tutorial: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html#adding-a-plugin-to-a-build
Follow instructions on this tutorial step-by-step. I do not add anything nor skip anything.
Create a new create-react-app (or a new react app in codesandbox).
Create a folder called ckeditor on the same level as App.js.
Copy the contents of the build folder into the ckeditor folder. When done, the ckeditor folder contains the following:
npm install @ckeditor/ckeditor5-react
Import both CKEditor and ClassicEditor into App.js as follows:
import CKEditor from "@ckeditor/ckeditor5-react";
import ClassicEditor from "./ckeditor/ckeditor";
<CKEditor> component to App.js as follows:function App() {
return (
<div className="App">
<h2>Editor</h2>
<CKEditor
editor={ClassicEditor}
data="<p>Hello from CKEditor 5!</p>"
onInit={editor => {
// You can store the "editor" and use when it is needed.
console.log("Editor is ready to use!", editor);
}}
onChange={(event, editor) => {
const data = editor.getData();
console.log({ event, editor, data });
}}
onBlur={(event, editor) => {
console.log("Blur.", editor);
}}
onFocus={(event, editor) => {
console.log("Focus.", editor);
}}
/>
</div>
);
}
NOTE
Here is the final code of App.js:
import React from "react";
import logo from "./logo.svg";
import "./App.css";
import CKEditor from "@ckeditor/ckeditor5-react";
import ClassicEditor from "./ckeditor/ckeditor";
function App() {
return (
<div className="App">
<h2>Editor</h2>
<CKEditor
editor={ClassicEditor}
data="<p>Hello from CKEditor 5!</p>"
onInit={editor => {
// You can store the "editor" and use when it is needed.
console.log("Editor is ready to use!", editor);
}}
onChange={(event, editor) => {
const data = editor.getData();
console.log({ event, editor, data });
}}
onBlur={(event, editor) => {
console.log("Blur.", editor);
}}
onFocus={(event, editor) => {
console.log("Focus.", editor);
}}
/>
</div>
);
}
export default App;
And here is the folder structure of my app:
npm start to start up create-react-app.That I would see a working version of my customized build (like I do when I run sample/index.html
I get the following error message:
Failed to compile.
./src/App.js
Attempted import error: './ckeditor/ckeditor' does not contain a default export (imported as 'ClassicEditor').
If I install @ckeditor/ckeditor5-build-classic and import it:
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";
Then everything works fine.
Also, here is a gist of the unminified version of the customized build ckeditor.js file (i.e., the file that I am trying to import, but that is giving me the error): https://gist.github.com/moshemo/25f5b84e92ceabab32343e45282294c1
Related issue described here โ https://github.com/ckeditor/ckeditor5-build-classic/issues/81#issuecomment-526563499.
TL;DR: do not copy dependencies to the src/ directory which is parsed by Webpack/Babel and other scripts that can break a minified build.
I recommend cloning the entire build repository, commit your changes and install the package (build) using npm/yarn and Github URL.
@pomek First of all, thanks for the quick response.
Secondly, let me see if I understand your response. I'll refer for now to your TL;DR (although I did try and read your longer response that you quoted above).
First -- you wrote: 'do not copy dependencies to the src/ directory'.
These are the steps that I took: https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installing-plugins.html#adding-a-plugin-to-a-build
Can you let me know which of those steps is considered copying a dependency to the src directory? Is it adding the alignment package from npm?
Secondly, you state that you 'recommend cloning the entire build repository'.
That's what I thought I did -- I started off as follows:
git clone -b stable https://github.com/ckeditor/ckeditor5-build-classic.git
Do you mean 'forking' the repository? If so, I think I now understand what you are recommending -- that I fork the repository, make my changes and then publish those changes to npm. I then install the npm package and import it that way.
Is that correct?
If so, I'm just not clear where the Github URL fits into all of this.
Either way, can you verify that I understood your recommendation correctly?
Thanks.
@moshemo, everything that you've done is correct. By typing "cloning" I meant forking the entire repository of course.
What I would like to say that we should avoid adding any libraries (especially minified ones) to a directory where our sources are placed. As we know, most of the applications that we are creating now, need to be parsed by Webpack or Babel or other scripts that modify source code. During this transpile process, source code may be understood incorrectly by those parsers and they will produce an invalid output code that leads to unknown problems.
Let me answer your question:
Can you let me know which of those steps is considered copying a dependency to the src directory? Is it adding the
alignmentpackage from npm?
Our guide assumes that you will have your own repository for the build. It can be forked based on ours or you can create a fresh one that will provide a build.
After modifying source files and building the destination file (the editor), you should commit and push those changes.
It allows installing the build from Github (as a dependency). It will land to the node_modules/ directory which is ignored during transpile process.
Instead of, you did:
- Copy the contents of the
buildfolder into theckeditorfolder. When done, theckeditorfolder contains the following:
And the entire minified code was parsed once again. Something went wrong and we finished with the error.
What could you do in order to get those things to work:
npm install /path-to-the-build but it won't work for other guys you work with.node_modules/[package-name]/In src/App.js you can import the editor by typing:
import ClassicEditor from '[package-name]';
Because dependencies (everything inside the node_modules/ directory) aren't touched during transpile process, everything should work.
Let me know did I helped you.
Well, it took me a while (and a number of tries) to create the npm package. But, that did it -- and it now WORKS!
Thanks -- much appreciated.
Now that I got it to work, I would like to update the docs so that it's a bit clearer.
Before I do so, though, I'd like to clarify a point that you made. You wrote:
It allows installing the build from Github (as a dependency). It will land to the node_modules/ directory
I'm not 100% following. I understand how I can install an npm package into the node_modules directory. But how can I install a Github repository into the node_modules directory (unless I've published that repository to npmjs and that is what you are referring to -- i.e., to a Github repository that has been published on npm).
Take a look at the npm documentation โ https://docs.npmjs.com/cli/install
npm install <git-host>:<git-user>/<repo-name>
It should work.
Got it, thanks for all your help.
Thanks. This helped me too.
First I tried to clone the repo, adjust it, make a build, then copy into my project and import(), but it failed.
Then did it this way:
stable branch of my forknpm install <gituser>/ckeditor5#stableimport('ckeditor5/packages/ckeditor5-build-inline/build/ckeditor')And finally it worked!
But I have a strong feeling that this is not how it should be done. Forking whole repo, committing compiled files, then downloading whole forked repo just to use one file from it. Looks very excessive. But I couldn't find any better way unfortunately.
Most helpful comment
@moshemo, everything that you've done is correct. By typing "cloning" I meant forking the entire repository of course.
What I would like to say that we should avoid adding any libraries (especially minified ones) to a directory where our sources are placed. As we know, most of the applications that we are creating now, need to be parsed by Webpack or Babel or other scripts that modify source code. During this transpile process, source code may be understood incorrectly by those parsers and they will produce an invalid output code that leads to unknown problems.
Let me answer your question:
Our guide assumes that you will have your own repository for the build. It can be forked based on ours or you can create a fresh one that will provide a build.
After modifying source files and building the destination file (the editor), you should commit and push those changes.
It allows installing the build from Github (as a dependency). It will land to the
node_modules/directory which is ignored during transpile process.Instead of, you did:
And the entire minified code was parsed once again. Something went wrong and we finished with the error.
What could you do in order to get those things to work:
npm install /path-to-the-buildbut it won't work for other guys you work with.node_modules/[package-name]/In
src/App.jsyou can import the editor by typing:Because dependencies (everything inside the
node_modules/directory) aren't touched during transpile process, everything should work.Let me know did I helped you.