Skip to content

Commit 8e05f95

Browse files
committed
fix: predefine tree shaking in own coding correctly (#3wkvfe) with vendor chunks (#3wnntb)
1 parent b7966d3 commit 8e05f95

File tree

8 files changed

+231
-48
lines changed

8 files changed

+231
-48
lines changed

common/Gruntfile.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44

55
import { execSync } from "child_process";
6+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
7+
import { basename, dirname } from "path";
68

79
/**
810
* Create "pre:" and "post:" hooks for an array of tasks.
@@ -127,6 +129,23 @@ function applyDefaultRunnerConfiguration(grunt: IGrunt) {
127129
grunt.log.oklns("All production licenses passed!");
128130
}
129131
});
132+
133+
/**
134+
* This is a workaround to this solved issue: https://github.com/wp-cli/i18n-command/issues/203
135+
*
136+
* Unfortunately the fix / PR is merged but not yet released. Due to the fact that the below also works for us, it's ok.
137+
*/
138+
grunt.registerTask("i18n:prepare:wp", () =>
139+
grunt.file.expand([`${pkg.slug ? "src/public/dev" : "dev"}/*.js`, "!**/*vendor~*"]).forEach((file) => {
140+
const content = readFileSync(file, { encoding: "UTF-8" });
141+
const regex = /(Object\([A-Za-z0-9_]+__WEBPACK_IMPORTED_MODULE[A-Za-z0-9_]+\[)\/\* ?(__|_n|_x|_nx) \*\/ "[A-Za-z0-9_]"/gm;
142+
const target = `${dirname(file)}/i18n-dir/`;
143+
!existsSync(target) && mkdirSync(target);
144+
writeFileSync(`${target}${basename(file)}`, content.replace(regex, `$1"$2"`), {
145+
encoding: "UTF-8"
146+
});
147+
})
148+
);
130149
}
131150

132151
export { hookable, applyDefaultRunnerConfiguration };

common/webpack.factory.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { resolve, join } from "path";
1010
import fs from "fs";
11-
import { Configuration, DefinePlugin, Compiler } from "webpack";
11+
import { Configuration, DefinePlugin, Compiler, Options } from "webpack";
1212
import { spawn, execSync } from "child_process";
1313
import WebpackBar from "webpackbar";
1414
import MiniCssExtractPlugin from "mini-css-extract-plugin";
@@ -174,6 +174,35 @@ function createDefaultSettings(
174174
const pluginSettings: Configuration = {
175175
context: pwd,
176176
mode: NODE_ENV,
177+
optimization: {
178+
usedExports: true,
179+
sideEffects: true,
180+
splitChunks: {
181+
cacheGroups:
182+
type === "plugin"
183+
? {
184+
// Dynamically get the entries from first-level files so every entrypoint get's an own vendor file (https://git.io/Jv2XY)
185+
...plugins
186+
.filter(({ location }) => location === pwd)
187+
.reduce((map: { [key: string]: Options.CacheGroupsOptions }, obj) => {
188+
map[`vendor~${obj.entrypointName}`] = {
189+
test: /node_modules/,
190+
chunks: (chunk) => chunk.name === obj.entrypointName,
191+
name: `vendor~${obj.entrypointName}`,
192+
enforce: true
193+
};
194+
return map;
195+
}, {})
196+
}
197+
: {
198+
vendor: {
199+
test: /node_modules/,
200+
chunks: "initial",
201+
enforce: true
202+
}
203+
}
204+
}
205+
},
177206
output: {
178207
path: outputPath,
179208
filename: "[name].js",

packages/utils/languages/frontend/utils.pot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ msgstr ""
77
"MIME-Version: 1.0\n"
88
"Content-Type: text/plain; charset=UTF-8\n"
99
"Content-Transfer-Encoding: 8bit\n"
10-
"POT-Creation-Date: 2020-02-05T09:05:29+01:00\n"
10+
"POT-Creation-Date: 2020-03-03T08:06:27+01:00\n"
1111
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
12-
"X-Generator: WP-CLI 2.2.0\n"
12+
"X-Generator: WP-CLI 2.4.0\n"

packages/utils/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
"node": ">=10.15.3",
1111
"yarn": ">=1.19.1"
1212
},
13+
"sideEffects": [
14+
"**/*.{css,scss,less}",
15+
"./src/public/ts/*.tsx"
16+
],
1317
"main": "lib/index.tsx",
1418
"files": [
1519
"dist",
@@ -21,7 +25,7 @@
2125
"bootstrap": "composer install",
2226
"dev": "concurrently --raw \"test $IS_DOCKER_START_COMMAND && exit 0 || yarn webpack --watch\" \"yarn --silent chokidar 'src/**/*.php' -c 'yarn i18n:generate:backend' --silent\"",
2327
"i18n:generate:backend": "yarn --silent wp:weak i18n make-pot src/ languages/backend/$(basename \"$(pwd)\").pot --ignore-domain",
24-
"i18n:generate:frontend": "yarn --silent wp:weak i18n make-pot dev languages/frontend/$(basename \"$(pwd)\").pot --ignore-domain && rm -rf languages/frontend/json && wp i18n make-json languages/frontend languages/frontend/json --no-purge",
28+
"i18n:generate:frontend": "yarn --silent grunt i18n:prepare:wp && yarn --silent wp:weak i18n make-pot dev/i18n-dir languages/frontend/$(basename \"$(pwd)\").pot --ignore-domain && rm -rf languages/frontend/json dev/i18n-dir && wp i18n make-json languages/frontend languages/frontend/json --no-purge",
2529
"build": "yarn --silent build:js:production && yarn --silent build:js:development",
2630
"build:js:production": "NODE_ENV=production yarn webpack",
2731
"build:js:development": "yarn webpack",
@@ -39,7 +43,7 @@
3943
"webpack": "webpack --config scripts/webpack.config.ts",
4044
"phpunit": "./vendor/bin/phpunit --prepend test/phpunit.xdebug.php --config test/phpunit.xml",
4145
"jest": "jest --config test/jest.config.js --rootDir .",
42-
"wp:weak": "php -n -dextension=phar.so -dextension=json.so -dextension=mbstring.so -dextension=tokenizer.so -dmemory_limit=512M $(which wp)"
46+
"wp:weak": "php -n -dextension=phar.so -dextension=json.so -dextension=mbstring.so -dextension=tokenizer.so -dmemory_limit=-1 $(which wp)"
4347
},
4448
"phpunit-coverage-threshold": 80,
4549
"browserslist": [

packages/utils/src/Assets.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,25 @@ public function enqueueMobx() {
132132
]);
133133
}
134134

135+
/**
136+
* Checks if a `vendor~` file is created for a given script and enqueue it.
137+
*
138+
* @param string $handle
139+
* @param boolean $isLib
140+
* @param string $src
141+
* @param string[] $deps
142+
* @param boolean $in_footer
143+
* @param string $media
144+
*/
145+
protected function probablyEnqueueChunk($handle, $isLib, $src, &$deps, $in_footer, $media) {
146+
if (!$isLib) {
147+
$handle = $this->enqueue('vendor~' . $handle, 'vendor~' . $src, $deps, false, 'script', $in_footer, $media);
148+
if ($handle !== false) {
149+
array_push($deps, $handle);
150+
}
151+
}
152+
}
153+
135154
/**
136155
* Enqueue helper for entry points and libraries. See dependents for more documentation.
137156
*
@@ -165,13 +184,15 @@ protected function enqueue(
165184
continue;
166185
}
167186

168-
$publicSrc = $publicFolder . (is_array($s) ? $s[1] : $s);
187+
$useSrc = (is_array($s) ? $s[1] : $s);
188+
$publicSrc = $publicFolder . $useSrc;
169189
$path = path_join($this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_PATH), $publicSrc);
170190
if (file_exists($path)) {
171191
$url = plugins_url($publicSrc, $this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_FILE));
172192
$cachebuster = $this->getCachebusterVersion($publicSrc, $isLib);
173193

174194
if ($type === 'script') {
195+
$this->probablyEnqueueChunk($useHandle, $isLib, $useSrc, $deps, $in_footer, $media);
175196
wp_enqueue_script($useHandle, $url, $deps, $cachebuster, $in_footer);
176197

177198
// Only set translations for our own entry points, libraries handle localization usually in another way
@@ -269,15 +290,33 @@ public function enqueueLibraryStyle($handle, $src, $deps = [], $media = 'all') {
269290
return $this->enqueueStyle($handle, $src, $deps, $media, true);
270291
}
271292

293+
/**
294+
* Checks if a `vendor~` file is created for a given script in a composer package and enqueue it.
295+
*
296+
* @param string $handle
297+
* @param string $src
298+
* @param string[] $deps
299+
* @param boolean $in_footer
300+
* @param string $media
301+
*/
302+
protected function probablyEnqueueComposerChunk($handle, $src, &$deps, $in_footer, $media) {
303+
$rootSlug = $this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_ROOT_SLUG);
304+
$handle = $this->enqueueComposer($handle, 'vendor~' . $src, $deps, 'script', $in_footer, $media, 'vendor~' . $rootSlug . '-' . $handle);
305+
if ($handle !== false) {
306+
array_push($deps, $handle);
307+
}
308+
}
309+
272310
/**
273311
* Enqueue helper for monorepo packages. See dependents for more documentation.
274312
*
275313
* @param string $handle
276-
* @param mixed $src
314+
* @param string $src
277315
* @param string[] $deps
278316
* @param string $type Can be 'script' or 'style'
279317
* @param boolean $in_footer
280318
* @param string $media
319+
* @param string $vendorHandle
281320
* @return string|boolean The used handle
282321
*/
283322
protected function enqueueComposer(
@@ -286,10 +325,11 @@ protected function enqueueComposer(
286325
$deps = [],
287326
$type = 'script',
288327
$in_footer = true,
289-
$media = 'all'
328+
$media = 'all',
329+
$vendorHandle = null
290330
) {
291331
$rootSlug = $this->getPluginConstant(PluginReceiver::$PLUGIN_CONST_ROOT_SLUG);
292-
$useHandle = $rootSlug . '-' . $handle;
332+
$useHandle = $vendorHandle !== null ? $vendorHandle : $rootSlug . '-' . $handle;
293333
$useNonMinifiedSources = $this->useNonMinifiedSources();
294334
$packageDir = 'vendor/' . $rootSlug . '/' . $handle . '/';
295335
$packageSrc = $packageDir . ($useNonMinifiedSources ? 'dev' : 'dist') . '/' . $src;
@@ -310,6 +350,7 @@ protected function enqueueComposer(
310350
}
311351

312352
if ($type === 'script') {
353+
$this->probablyEnqueueComposerChunk($handle, $src, $deps, $in_footer, $media);
313354
wp_enqueue_script($useHandle, $url, $deps, $cachebuster, $in_footer);
314355
$this->setLazyScriptTranslations(
315356
$useHandle,

0 commit comments

Comments
 (0)