Skip to content

Webpack mangle is not parsed #203

@matzeeable

Description

@matzeeable

Bug Report

Describe the current, buggy behavior

I am using webpack (in my WordPress boilerplate https://github.com/devowlio/wp-react-starter) and want to extract strings (make-pot) from my bundled .js file. My file contains this code:

Object(_utils__WEBPACK_IMPORTED_MODULE_7__["__"])("Try again"))

As long as it looks like this, it works as expected. The coding covers that case:

// If the callee is a call expression as created by Webpack resolve it.
// For example: Object(u.__)( "translation" ).
if (
'CallExpression' === $callee->getType() &&
'Identifier' === $callee->getCallee()->getType() &&
'Object' === $callee->getCallee()->getName() &&
[] !== $callee->getArguments() &&
'MemberExpression' === $callee->getArguments()[0]->getType()
) {
$property = $callee->getArguments()[0]->getProperty();
// Matches minified webpack statements: Object(u.__)( "translation" ).
if ( 'Identifier' === $property->getType() ) {
return [
'name' => $property->getName(),
'comments' => $callee->getCallee()->getLeadingComments(),
];
}
// Matches unminified webpack statements:
// Object(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_7__["__"])( "translation" );
if ( 'Literal' === $property->getType() ) {
return [
'name' => $property->getValue(),
'comments' => $callee->getCallee()->getLeadingComments(),
];
}
}

But, due to the fact I need to rely on webpack's splitChunks and some other optimizations (usedExports and sideEffects) it no longer works because the output looks like this:

Object(_utils__WEBPACK_IMPORTED_MODULE_7__[/* __ */ "a"])("Try again"))

You see, it creates an inline comment /* __ */ with the function name (can be also _n or whatever).

Describe how other contributors can replicate this bug

I can not tell you the exact webpack configuration as it is very complicated, but you could do the following:

  • I think you have a webpack example project, navigate to it
  • Open the bundled .js file
  • Search for a working __WEBPACK_IMPORTED_MODULE
  • Replace "__" with /* __ */ "a"

Edit: You can also create a test.js file with the following content:

Object(_utils__WEBPACK_IMPORTED_MODULE_6__[/* __*/ "a"])("Try again");
Object(_utils__WEBPACK_IMPORTED_MODULE_6__["__"])("This works");

Describe what you would expect as the correct outcome

Should parse the string Try again correctly to .pot file.

Let us know what environment you are running this on

OS:     Linux 4.4.0-18362-Microsoft #476-Microsoft Fri Nov 01 16:53:00 PST 2019 x86_64
Shell:  /bin/bash
PHP binary:     /usr/bin/php7.2
PHP version:    7.2.24-0ubuntu0.18.04.2
php.ini used:   /etc/php/7.2/cli/php.ini
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       /d/Programme/VSCode Workspace/devowl-wp/plugins/devowl-site
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.2.0

Provide a possible solution

I do not exactly know how that works with Peast but perhaps we can check for the inline comment (trimmed).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions