1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | const chalk = require( 'chalk' );
|
9 | const rimraf = require( 'rimraf' );
|
10 | const fs = require( 'fs' );
|
11 | const path = require( 'path' );
|
12 | const semver = require( 'semver' );
|
13 | const { NormalModule } = require( 'webpack' );
|
14 | const { version: webpackVersion } = require( 'webpack/package.json' );
|
15 | const { RawSource, ConcatSource } = require( 'webpack-sources' );
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | module.exports = function serveTranslations( compiler, options, translationService ) {
|
35 | const cwd = process.cwd();
|
36 |
|
37 |
|
38 | const translateSource = ( source, sourceFile ) => translationService.translateSource( source, sourceFile );
|
39 |
|
40 |
|
41 | translationService.on( 'error', emitError );
|
42 | translationService.on( 'warning', emitWarning );
|
43 |
|
44 |
|
45 |
|
46 | const pathToLanguages = path.join( compiler.options.output.path, options.outputDirectory );
|
47 |
|
48 | if ( fs.existsSync( pathToLanguages ) ) {
|
49 | if ( pathToLanguages.includes( cwd ) && cwd !== pathToLanguages ) {
|
50 | rimraf.sync( pathToLanguages );
|
51 | } else {
|
52 | emitError(
|
53 | `Can't remove path to translation files directory (${ pathToLanguages }). Assert whether you specified a correct path.`
|
54 | );
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 | compiler.hooks.normalModuleFactory.tap( 'CKEditor5Plugin', normalModuleFactory => {
|
60 | const resolver = normalModuleFactory.getResolver( 'normal' );
|
61 |
|
62 | resolver.resolve( { cwd }, cwd, options.corePackageSampleResourcePath, {}, ( err, pathToResource ) => {
|
63 | if ( err ) {
|
64 | console.warn( 'Cannot find the CKEditor 5 core translation package (which defaults to `@ckeditor/ckeditor5-core`).' );
|
65 |
|
66 | return;
|
67 | }
|
68 |
|
69 | const corePackage = pathToResource.match( options.corePackagePattern )[ 0 ];
|
70 |
|
71 | translationService.loadPackage( corePackage );
|
72 | } );
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | if ( options.includeCorePackageTranslations ) {
|
78 | resolver.resolve( { cwd }, cwd, options.corePackageContextsResourcePath, {}, ( err, pathToResource ) => {
|
79 | if ( err ) {
|
80 | console.warn( 'Cannot find the CKEditor 5 core translation context (which defaults to `@ckeditor/ckeditor5-core`).' );
|
81 |
|
82 | return;
|
83 | }
|
84 |
|
85 |
|
86 | const contexts = require( pathToResource );
|
87 |
|
88 | for ( const item of Object.keys( contexts ) ) {
|
89 | translationService.addIdMessage( item );
|
90 | }
|
91 | } );
|
92 | }
|
93 | } );
|
94 |
|
95 |
|
96 | compiler.hooks.compilation.tap( 'CKEditor5Plugin', compilation => {
|
97 | getCompilationHooks( compilation ).tap( 'CKEditor5Plugin', ( context, module ) => {
|
98 | const relativePathToResource = path.relative( cwd, module.resource );
|
99 |
|
100 | if ( relativePathToResource.match( options.sourceFilesPattern ) ) {
|
101 | module.loaders.push( {
|
102 | loader: path.join( __dirname, 'translatesourceloader.js' ),
|
103 | options: { translateSource }
|
104 | } );
|
105 |
|
106 | const pathToPackage = getPathToPackage( cwd, module.resource, options.packageNamesPattern );
|
107 |
|
108 | translationService.loadPackage( pathToPackage );
|
109 | }
|
110 | } );
|
111 |
|
112 |
|
113 |
|
114 | getChunkAssets( compilation ).tap( 'CKEditor5Plugin', chunks => {
|
115 | const generatedAssets = translationService.getAssets( {
|
116 | outputDirectory: options.outputDirectory,
|
117 | compilationAssetNames: Object.keys( compilation.assets )
|
118 | .filter( name => name.endsWith( '.js' ) )
|
119 | } );
|
120 |
|
121 | const allFiles = getFilesFromChunks( chunks );
|
122 |
|
123 | for ( const asset of generatedAssets ) {
|
124 | if ( asset.shouldConcat ) {
|
125 |
|
126 | const originalAsset = compilation.assets[ asset.outputPath ];
|
127 |
|
128 | compilation.assets[ asset.outputPath ] = new ConcatSource( asset.outputBody, '\n', originalAsset );
|
129 | } else {
|
130 | const chunkExists = allFiles.includes( asset.outputPath );
|
131 |
|
132 | if ( !chunkExists ) {
|
133 |
|
134 | compilation.assets[ asset.outputPath ] = new RawSource( asset.outputBody );
|
135 | } else {
|
136 |
|
137 |
|
138 | compilation.assets[ asset.outputPath ] = asset.outputBody;
|
139 | }
|
140 | }
|
141 | }
|
142 | } );
|
143 | } );
|
144 |
|
145 |
|
146 | const uniqueMessages = new Set();
|
147 |
|
148 | function emitError( error ) {
|
149 | if ( uniqueMessages.has( error ) ) {
|
150 | return;
|
151 | }
|
152 |
|
153 | uniqueMessages.add( error );
|
154 |
|
155 | if ( options.strict ) {
|
156 | throw new Error( chalk.red( error ) );
|
157 | }
|
158 |
|
159 | console.error( chalk.red( `[CKEditorWebpackPlugin] Error: ${ error }` ) );
|
160 | }
|
161 |
|
162 | function emitWarning( warning ) {
|
163 | if ( uniqueMessages.has( warning ) ) {
|
164 | return;
|
165 | }
|
166 |
|
167 | uniqueMessages.add( warning );
|
168 |
|
169 | if ( options.verbose ) {
|
170 | console.warn( chalk.yellow( `[CKEditorWebpackPlugin] Warning: ${ warning }` ) );
|
171 | }
|
172 | }
|
173 | };
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | function getPathToPackage( cwd, resource, packageNamePattern ) {
|
183 | const relativePathToResource = path.relative( cwd, resource );
|
184 |
|
185 | const match = relativePathToResource.match( packageNamePattern );
|
186 |
|
187 | if ( !match ) {
|
188 | return null;
|
189 | }
|
190 |
|
191 | const index = relativePathToResource.search( packageNamePattern ) + match[ 0 ].length;
|
192 |
|
193 | return relativePathToResource.slice( 0, index );
|
194 | }
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | function getCompilationHooks( compilation ) {
|
203 | if ( semver.major( webpackVersion ) === 4 ) {
|
204 | return compilation.hooks.normalModuleLoader;
|
205 | }
|
206 |
|
207 | return NormalModule.getCompilationHooks( compilation ).loader;
|
208 | }
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | function getChunkAssets( compilation ) {
|
217 |
|
218 | return compilation.hooks.processAssets || compilation.hooks.optimizeChunkAssets;
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | function getFilesFromChunks( chunks ) {
|
228 |
|
229 | if ( Array.isArray( chunks ) ) {
|
230 | return chunks.reduce( ( acc, chunk ) => [ ...acc, ...chunk.files ], [] );
|
231 | }
|
232 |
|
233 |
|
234 | return Object.keys( chunks );
|
235 | }
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|