UNPKG

3.86 kBJavaScriptView Raw
1/**
2 * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3 * For licensing, see LICENSE.md.
4 */
5
6'use strict';
7
8const path = require( 'path' );
9const webpack = require( 'webpack' );
10const TerserPlugin = require( 'terser-webpack-plugin' );
11const bundler = require( '../bundler' );
12const styles = require( '../styles' );
13const tools = require( '../tools' );
14const CKEditorWebpackPlugin = require( '@ckeditor/ckeditor5-dev-webpack-plugin' );
15
16/**
17 * Returns a webpack configuration that creates a bundle file for the specified package. Thanks to that, plugins exported
18 * by the package can be added to DLL builds.
19 *
20 * @param {Object} options
21 * @param {String} options.themePath An absolute path to the theme package.
22 * @param {String} options.packagePath An absolute path to the root directory of the package.
23 * @param {String} options.manifestPath An absolute path to the CKEditor 5 DLL manifest file.
24 * @param {Boolean} [options.isDevelopmentMode=false] Whether to build a dev mode of the package.
25 * @returns {Object}
26 */
27module.exports = function getDllPluginWebpackConfig( options ) {
28 const packageName = tools.readPackageName( options.packagePath );
29
30 const webpackConfig = {
31 mode: options.isDevelopmentMode ? 'development' : 'production',
32
33 performance: { hints: false },
34
35 entry: path.join( options.packagePath, 'src', 'index.js' ),
36
37 output: {
38 library: [ 'CKEditor5', getGlobalKeyForPackage( packageName ) ],
39
40 path: path.join( options.packagePath, 'build' ),
41 filename: getIndexFileName( packageName ),
42 libraryTarget: 'window'
43 },
44
45 optimization: {
46 minimize: false
47 },
48
49 plugins: [
50 new CKEditorWebpackPlugin( {
51 // UI language. Language codes follow the https://en.wikipedia.org/wiki/ISO_639-1 format.
52 language: 'en',
53 additionalLanguages: 'all',
54 sourceFilesPattern: /^src[/\\].+\.js$/,
55 skipPluralFormFunction: true
56 } ),
57 new webpack.BannerPlugin( {
58 banner: bundler.getLicenseBanner(),
59 raw: true
60 } ),
61 new webpack.DllReferencePlugin( {
62 manifest: require( options.manifestPath ),
63 scope: 'ckeditor5/src',
64 name: 'CKEditor5.dll'
65 } )
66 ],
67
68 resolve: {
69 extensions: [ '.ts', '.js', '.json' ]
70 },
71
72 module: {
73 rules: [
74 {
75 test: /\.svg$/,
76 use: [ 'raw-loader' ]
77 },
78 {
79 test: /\.css$/,
80 use: [
81 {
82 loader: 'style-loader',
83 options: {
84 injectType: 'singletonStyleTag',
85 attributes: {
86 'data-cke': true
87 }
88 }
89 },
90 'css-loader',
91 {
92 loader: 'postcss-loader',
93 options: {
94 postcssOptions: styles.getPostCssConfig( {
95 themeImporter: {
96 themePath: options.themePath
97 },
98 minify: true
99 } )
100 }
101 }
102 ]
103 },
104 {
105 test: /\.ts$/,
106 use: [ 'ts-loader' ]
107 }
108 ]
109 }
110 };
111
112 if ( options.isDevelopmentMode ) {
113 webpackConfig.devtool = 'source-map';
114 } else {
115 webpackConfig.optimization.minimize = true;
116
117 webpackConfig.optimization.minimizer = [
118 new TerserPlugin( {
119 terserOptions: {
120 output: {
121 // Preserve CKEditor 5 license comments.
122 comments: /^!/
123 }
124 },
125 extractComments: false
126 } )
127 ];
128 }
129
130 return webpackConfig;
131};
132
133/**
134 * Transforms the package name (`@ckeditor/ckeditor5-foo-bar`) to the name that will be used while
135 * exporting the library into the global scope.
136 *
137 * @param {String} packageName
138 * @returns {String}
139 */
140function getGlobalKeyForPackage( packageName ) {
141 return packageName
142 .replace( /^@ckeditor\/ckeditor5?-/, '' )
143 .replace( /-([a-z])/g, ( match, p1 ) => p1.toUpperCase() );
144}
145
146/**
147 * Extracts the main file name from the package name.
148 *
149 * @param packageName
150 * @returns {String}
151 */
152function getIndexFileName( packageName ) {
153 return packageName.replace( /^@ckeditor\/ckeditor5?-/, '' ) + '.js';
154}