Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack mangle is not parsed #203

Closed
2 tasks done
matzeeable opened this issue Feb 28, 2020 · 2 comments
Closed
2 tasks done

Webpack mangle is not parsed #203

matzeeable opened this issue Feb 28, 2020 · 2 comments

Comments

@matzeeable
Copy link
Contributor

matzeeable commented Feb 28, 2020

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).

@swissspidy
Copy link
Member

@herregroen Since you've worked on this, perhaps you have some thoughts about that?

@matzeeable
Copy link
Contributor Author

@swissspidy @herregroen I have now taken the initiative myself and opened a pull request: #204 🙂

swissspidy added a commit that referenced this issue Mar 2, 2020
fix(make-pot): parses mangled webpack statements (closes #203)
schlessera pushed a commit that referenced this issue Jan 6, 2022
fix(make-pot): parses mangled webpack statements (closes #203)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants