forked from TYPO3/typo3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathckeditor5.rollup.config.js
120 lines (116 loc) · 5.16 KB
/
ckeditor5.rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import postcss from 'rollup-plugin-postcss';
import svg from 'rollup-plugin-svg';
import { terser } from 'rollup-plugin-terser';
import * as path from 'path';
import { buildConfigForTranslations } from './ckeditor5.rollup.helpers';
import { readdirSync, statSync, existsSync } from 'fs';
import { createRequire } from 'node:module';
const { styles } = require( '@ckeditor/ckeditor5-dev-utils' );
const postCssConfig = styles.getPostCssConfig({
themeImporter: {
themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
},
minify: true
});
const packages = readdirSync('node_modules/@ckeditor')
.filter(dir =>
statSync(`node_modules/@ckeditor/${dir}`).isDirectory() &&
existsSync(`node_modules/@ckeditor/${dir}/package.json`) &&
!['ckeditor5-dev-translations', 'ckeditor5-dev-utils'].includes(dir)
);
export default [
...packages.map(pkg => {
const packageName = `@ckeditor/${pkg}`;
const packageJson = `./node_modules/${packageName}/package.json`;
const require = createRequire(import.meta.url);
let input = `./node_modules/${packageName}/${require(packageJson).main}`;
if (packageName === '@ckeditor/ckeditor5-link') {
input = 'Sources/JavaScript/rte_ckeditor/contrib/ckeditor5-link.js';
}
return {
input: [
input,
],
output: {
compact: true,
file: `../typo3/sysext/rte_ckeditor/Resources/Public/Contrib/${packageName}.js`,
format: 'es',
plugins: [terser()],
},
plugins: [
{
name: 'externals',
resolveId: (source, from) => {
if (source === '@ckeditor/ckeditor5-utils/src/version.js' && from.includes('@ckeditor/ckeditor5-engine')) {
return { id: '@ckeditor/ckeditor5-utils', external: true }
}
if (source.startsWith('@ckeditor/') && !source.startsWith(packageName) && !source.endsWith('.svg') && !source.endsWith('.css')) {
if (source.split('/').length > 2) {
throw new Error(`Non package-entry point was imported: ${source}`);
}
return { id: source.replace(/.js$/, ''), external: true }
}
if (source.startsWith('ckeditor5/src/')) {
return { id: '@ckeditor/ckeditor5-' + source.substring(14).replace(/.js$/, ''), external: true };
}
if (source === 'lodash-es') {
return { id: 'lodash-es', external: true }
}
if (source.startsWith('@ckeditor/') && source.endsWith('.js') && source.split('/').length === 2) {
throw new Error(`JS File with suffix: ${source} import from ${from}`);
}
if (
!source.startsWith('@ckeditor/') &&
!source.startsWith('.') &&
!source.startsWith('/') &&
!source.startsWith('Sources/JavaScript/rte_ckeditor/contrib') &&
source !== 'vanilla-colorful/hex-color-picker.js' &&
source !== 'vanilla-colorful/lib/entrypoints/hex' &&
source !== 'color-convert' &&
source !== 'color-name' &&
source !== 'color-parse'
) {
throw new Error(`HEADS UP: New CKEditor5 import "${source}" (import from ${from}). Please decide whether to bundle or package separately and adapt Build/ckeditor5.rollup.config.js accordingly.`);
}
return null
}
},
{
name: 'patchLinkEditing',
transform(code, id) {
if (id.endsWith('@ckeditor/ckeditor5-link/src/linkediting.js')) {
// Workaround a CKEditor5 bug where a link without an `href` attribute is created
// when the cursor is placed at the end of a link containing a class attribute.
// @todo: Fix this upstream: htmlA should theoretically be removed automatically
// when linkHref is removed as it is defined to be a coupledAttribute with linkHref.
// (see @ckeditor/ckeditor5-html-support/src/schemadefinitions.js)
const source = "return textAttributes.filter(attribute => attribute.startsWith('link'));";
const target = "return textAttributes.filter(attribute => attribute.startsWith('link') || attribute === 'htmlA');";
if (!code.includes(source)) {
throw new Error(`Expected to find "${search}" in "${id}". Please adapt the rollup plugin "patchLinkEditing".`);
}
return code.replace(source, target);
}
return code;
}
},
postcss({
...postCssConfig,
inject: function (cssVariableName, fileId) {
// overrides functionality of native `style-inject` package, now applies `window.litNonce` to `<style>`
const importPath = path.resolve('./ckeditor5.rollup.functions.js');
return `import styleInject from '${importPath}';\n` + `styleInject(${cssVariableName});`;
},
}),
resolve({
extensions: ['.js']
}),
commonjs(),
svg(),
]
}
}),
...buildConfigForTranslations()
];