ckeditor / ckeditor5

Powerful rich text editor framework with a modular architecture, modern integrations, and features like collaborative editing.
https://ckeditor.com/ckeditor-5
Other
9.49k stars 3.7k forks source link

Create ESM out of source files on NPM pre-publish #6075

Closed ma2ciek closed 11 months ago

ma2ciek commented 4 years ago

πŸ“ Provide a description of the new feature

First, thanks to @phaux for the proposal. Firstly I thought it's impossible, but after a talk, I'd give it a try as the benefits side of this feature is so huge.

The problem

Right now it's very hard to add something to the build without rebuilding it. It makes also the CKEditor 5 project very dependent on the webpack bundler (and on its configuration as well) and creates other issues when it comes to bundling CKEditor 5 sources inside frameworks.

The solution

What if we could just allow users to use ESM and just type the following in the JS file that won't need any bundling / transpilation and will just work in the browser:

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/esm/classiceditor';
import Essentials from '@ckeditor/ckeditor5-essentials/esm/essentials';
import Paragraph from '@ckeditor/ckeditor5-paragraph/esm/paragraph';

// or:
import { ClassicEditor } from '@ckeditor/ckeditor5-editor-classic';
import { Essentials } from '@ckeditor/ckeditor5-essentials';
import { Paragraph } from '@ckeditor/ckeditor5-paragraph';

ClassicEditor.create( div, {
   plugins: [ Essentials, Paragraph ]
} );

Benefits:

Cons:

Some more technical details:

There're at least three potential problems I'm aware of:

Since all our JS files are documented and can be imported, all our JS files should be entry points during the ESM generation process. SVG files and CSS files that are shared across multiple files should be moved to some shared code chunks.

But... I'm not sure if it gonna work at all πŸ™ƒThe ESM building would need to be very precise to not duplicate CSS styles, which I think is the critical issue here.

Waiting for your critical opinions ;)


If you'd like to see this feature implemented, add a πŸ‘ reaction to this post.

phaux commented 4 years ago

Things that cause problems in the code right now:

The first thing we need to decide is which tool we will use ourselves to prepare the ESM versions of packages before publishing.

Things we should care about are:

Possible tool options are:

So after a bit of thought, the first sensible options that come to my mind are:

In any case, the remaining blockers are:

Reinmar commented 4 years ago

This is a really interesting idea and would definitely solve many issues.

However, I can't stop thinking about the blockers:

Things that I think should be fairly easy:

My other thought is that ESM has some common problems to the "adding plugins to builds" problem. Those blockers would concern me in both cases. So I've got a feeling that we'd have to tackle both things at the same time to not end up with a solution for ESM which later won't work for adding plugins to builds.

ma2ciek commented 4 years ago

Each package and even each module in a package can require a CSS file now. If we build this into pure ES modules, we need to bundle that CSS into those files somehow... but how? 100 modules, each adding its own stylesheet to a page. What about common dependencies? What about order of those stylesheets? It's not about code size here – it's about this not blowing up completely.

The first issue is probably solvable. The order of stylesheets could be a much bigger problem.

I could imagine that from such three endpoints with their dependencies:

- A.js
   - A.css
   - B.js
- B.js
   - B.css
- C.js
   - B.css

We could generate the following ESM:

 - A.js
    - A.css.js 
    - B.js
 - B.js
    - B.css.js
 - C.js
    - B.css.js
 - A.css.js 
 - B.css.js

So the CSS output won't be duplicated and will be imported once. However, that might won't work with PostCSS @import tags. (Though, I think that it might duplicate the CSS code even now)

My other thought is that ESM has some common problems to the "adding plugins to builds" problem. Those blockers would concern me in both cases. So I've got a feeling that we'd have to tackle both things at the same time to not end up with a solution for ESM which later won't work for adding plugins to builds.

The only way to solve the "adding plugins to builds" problem is to kill builds at some point IMHO.

ma2ciek commented 4 years ago

Similar thing for translations. No idea how this could work :(.

Maybe even something like this would do the trick if we collect all available translations:

import { polishTranslations } from '@ckeditor/ckeditor5-translations';
import translationService from '@ckeditor/ckeditor5-utils/src/translation-service';

translationService.add( 'pl', polishTranslations );

ClassicEditor.create( ... )

This way it could be super easy to fill the missing translations by hand if we skip the CKEditorWebpackPlugin:

import { polishTranslations: officialPolishTranslations } from '@ckeditor/ckeditor5-translations';
import translationService from '@ckeditor/ckeditor5-utils/src/translation-service';

translationService.add( 'pl', officialPolishTranslations );
translationService.add( 'pl', { Add: 'Dodaj' } );
ma2ciek commented 4 years ago

Actually this approach is used in other packages as well - https://date-fns.org (i18n example).

Reinmar commented 4 years ago

The only way to solve the "adding plugins to builds" problem is to kill builds at some point IMHO.

❀️

ma2ciek commented 4 years ago

It seems that the libraryTarget: 'module' is planned for the webpack 5 release, but it's still not implemented or implemented as an experimental feature - webpack/webpack#9802, https://webpack.js.org/configuration/experiments/#root. It will be interesting to see if it can bring us some value.

CKEditorBot commented 1 year ago

There has been no activity on this issue for the past year. We've marked it as stale and will close it in 30 days. We understand it may be relevant, so if you're interested in the solution, leave a comment or reaction under this issue.

CKEditorBot commented 11 months ago

We've closed your issue due to inactivity over the last year. We understand that the issue may still be relevant. If so, feel free to open a new one (and link this issue to it).