Skip to content

Commit

Permalink
Use json2php to generate PHP version of assets file
Browse files Browse the repository at this point in the history
Props to @aristath for sharing this npm package.
  • Loading branch information
gziolo committed Sep 3, 2019
1 parent 5e3cf74 commit d0a983f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/dependency-extraction-webpack-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
const { createHash } = require( 'crypto' );
const { serialize: phpSerialize } = require( 'php-serialize' );
const json2php = require( 'json2php' );
const { ExternalsPlugin } = require( 'webpack' );
const { RawSource } = require( 'webpack-sources' );

Expand Down Expand Up @@ -78,7 +78,7 @@ class DependencyExtractionWebpackPlugin {

serialize( asset ) {
if ( this.options.outputFormat === 'php' ) {
return `<?php return unserialize('${ phpSerialize( asset ) }');`;
return `<?php return ${ json2php( JSON.parse( JSON.stringify( asset ) ) ) };`;
}

return JSON.stringify( asset );
Expand Down
2 changes: 1 addition & 1 deletion packages/dependency-extraction-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
],
"main": "index.js",
"dependencies": {
"php-serialize": "^3.0.0",
"json2php": "^0.0.4",
"webpack": "^4.8.3",
"webpack-sources": "^1.3.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ exports[`Webpack \`no-deps\` should produce expected output: Asset file should m

exports[`Webpack \`no-deps\` should produce expected output: External modules should match snapshot 1`] = `Array []`;

exports[`Webpack \`output-format-php\` should produce expected output: Asset file should match snapshot 1`] = `"<?php return unserialize('a:2:{s:12:\\"dependencies\\";a:1:{i:0;s:6:\\"lodash\\";}s:7:\\"version\\";s:32:\\"24a5cf4a92b121c1709114ecf54db6b6\\";}');"`;
exports[`Webpack \`output-format-php\` should produce expected output: Asset file should match snapshot 1`] = `"<?php return array('dependencies' => array('lodash'), 'version' => '24a5cf4a92b121c1709114ecf54db6b6');"`;

exports[`Webpack \`output-format-php\` should produce expected output: External modules should match snapshot 1`] = `
Array [
Expand Down

0 comments on commit d0a983f

Please sign in to comment.