Skip to content

Commit

Permalink
make wepback 4 and webpack 3 compatible
Browse files Browse the repository at this point in the history
- this will use `tapable.hooks` if its present, otherwise `tapable.plugin`
- fix linting errors
- update translation-map.json (includes missing sprintf strings).
  • Loading branch information
nerrad committed Apr 12, 2018
1 parent c33a646 commit 25c83f9
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions bin/i18n-map-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class wpi18nExtractor {
reduce(
Array.from( modules ),
function( mapped, module ) {
if ( ! module instanceof NormalModule ||
if ( ! ( module instanceof NormalModule ) ||
! isFunction( module.originalSource )
) {
return mapped;
Expand All @@ -87,26 +87,42 @@ class wpi18nExtractor {
}

apply( compiler ) {
const {
options,
translationMap,
parseSourcesToMap,
} = this;
const { processChunks } = this;
const extractor = this;

compiler.plugin( 'this-compilation', ( compilation ) => {
compilation.plugin( [ 'optimize-chunks', 'optimize-extracted-chunks' ], ( chunks ) => {
forEach( chunks, function( chunk ) {
if ( chunk.name ) {
parseSourcesToMap( chunk._modules, chunk.name, extractor );
}
/**
* webpack 4 registration
*/
if ( has( compiler, 'hooks' ) ) {
compiler.hooks.thisCompilation.tap( 'webpack-i18n-map-extractor', compilation => {
compilation.hooks.optimizeChunks.tap( 'webpack-i18n-map-extractor', chunks => {
processChunks( chunks, extractor );
} );
} );
} else {
compiler.plugin( 'this-compilation', ( compilation ) => {
compilation.plugin( [ 'optimize-chunks', 'optimize-extracted-chunks' ], ( chunks ) => {
processChunks( chunks, extractor );
} );
writeFileSync( './' + options.filename,
JSON.stringify( translationMap, null, 2 ),
'utf-8'
);
} );
}
}

processChunks( chunks, extractor ) {
const {
options,
translationMap,
parseSourcesToMap,
} = extractor;
forEach( chunks, function( chunk ) {
if ( chunk.name ) {
parseSourcesToMap( chunk._modules, chunk.name, extractor );
}
} );
writeFileSync( './' + options.filename,
JSON.stringify( translationMap, null, 2 ),
'utf-8'
);
}
}

Expand Down

0 comments on commit 25c83f9

Please sign in to comment.