Skip to content

Commit

Permalink
Improved asset concatenation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Mar 20, 2018
1 parent 3f8d463 commit 5893351
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ module.exports = class MultipleLanguageTranslationService extends EventEmitter {
}

const mainAssetName = compilationAssetNames[ 0 ];
const mainCompilationAsset = compilationAssets[ mainAssetName ];

const mainTranslationAsset = this._getTranslationAssets( outputDirectory, [ this._mainLanguage ] )[ 0 ];

const mergedCompilationAsset = {
outputBody: mainTranslationAsset.outputBody + '\n' + mainCompilationAsset.source(),
outputPath: mainAssetName
outputBody: mainTranslationAsset.outputBody,
outputPath: mainAssetName,
shouldConcat: true
};

const otherLanguages = Array.from( this._languages )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ describe( 'translations', () => {
{
outputPath: 'ckeditor.js',
outputBody: '(d=>d[\'pl\']=Object.assign(d[\'pl\']||{},{a:"Anuluj",b:"Zapisz"}))' +
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));\nsource'
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));',
shouldConcat: true
},
{
outputPath: path.join( 'lang', 'en.js' ),
Expand Down Expand Up @@ -286,7 +287,8 @@ describe( 'translations', () => {
{
outputPath: 'ckeditor.js',
outputBody: '(d=>d[\'pl\']=Object.assign(d[\'pl\']||{},{a:"Anuluj",b:"Zapisz"}))' +
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));\nsource'
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));',
shouldConcat: true
},
{
outputPath: path.join( 'lang', 'xxx.js' ),
Expand Down Expand Up @@ -386,7 +388,8 @@ describe( 'translations', () => {
{
outputPath: 'ckeditor.js',
outputBody: '(d=>d[\'pl\']=Object.assign(d[\'pl\']||{},{a:"Anuluj",b:"Zapisz"}))' +
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));\nsource'
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));',
shouldConcat: true
}
] );
} );
Expand Down Expand Up @@ -464,7 +467,8 @@ describe( 'translations', () => {
{
outputPath: 'ckeditor.js',
outputBody: '(d=>d[\'pl\']=Object.assign(d[\'pl\']||{},{a:"Anuluj"}))' +
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));\nsource'
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));',
shouldConcat: true
},
{
outputPath: path.join( 'custom-lang-path', 'en.js' ),
Expand Down Expand Up @@ -547,7 +551,8 @@ describe( 'translations', () => {
{
outputPath: 'ckeditor.js',
outputBody: '(d=>d[\'pl\']=Object.assign(d[\'pl\']||{},{a:"Zapisz"}))' +
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));\nsource'
'(window.CKEDITOR_TRANSLATIONS||(window.CKEDITOR_TRANSLATIONS={}));',
shouldConcat: true
},
{
outputPath: path.join( 'lang', 'de.js' ),
Expand Down
21 changes: 19 additions & 2 deletions packages/ckeditor5-dev-webpack-plugin/lib/servetranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const chalk = require( 'chalk' );
const rimraf = require( 'rimraf' );
const fs = require( 'fs' );
const path = require( 'path' );
const { RawSource } = require( 'webpack-sources' );
const { RawSource, ConcatSource } = require( 'webpack-sources' );

/**
* Serve translations depending on the used translation service and passed options.
Expand Down Expand Up @@ -86,8 +86,25 @@ module.exports = function serveTranslations( compiler, options, translationServi
compilationAssets: compilation.assets
} );

const allFiles = chunks.reduce( ( acc, chunk ) => [ ...acc, ...chunk.files ], [] );

for ( const asset of generatedAssets ) {
compilation.assets[ asset.outputPath ] = new RawSource( asset.outputBody );
if ( asset.shouldConcat ) {
// We need to concat sources here to support source maps for CKE5 code.
const originalAsset = compilation.assets[ asset.outputPath ];
compilation.assets[ asset.outputPath ] = new ConcatSource( asset.outputBody, '\n', originalAsset );
} else {
const chunkExists = allFiles.includes( asset.outputPath );

if ( !chunkExists ) {
// RawSource is used when corresponding chunk does not exist.
compilation.assets[ asset.outputPath ] = new RawSource( asset.outputBody );
} else {
// String is used when corresponding chunk exists and maintain proper sourcemaps.
// Changing to RawSource would drop source maps.
compilation.assets[ asset.outputPath ] = asset.outputBody;
}
}
}

done();
Expand Down

0 comments on commit 5893351

Please sign in to comment.