@wordpress/build 0.28.0 → 0.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/README.md +16 -1
- package/lib/build.mjs +237 -231
- package/lib/style-dependencies.mjs +69 -0
- package/lib/translator-comments-plugin.mjs +347 -0
- package/package.json +8 -9
- package/templates/constants.php.template +3 -2
- package/templates/module-registration.php.template +1 -2
- package/templates/routes-registration.php.template +8 -2
- package/templates/script-registration.php.template +5 -1
- package/templates/style-registration.php.template +5 -1
- package/templates/widget-registration.php.template +4 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.29.0 (2026-12-02)
|
|
6
|
+
|
|
7
|
+
### New Features
|
|
8
|
+
|
|
9
|
+
- Add a `--no-script-debug` flag that skips generating the unminified debug assets, for plugins that would rather not ship them. The generated registration code loads the minified files even when `SCRIPT_DEBUG` is enabled if the build has no unminified files ([#75396](https://github.com/WordPress/gutenberg/pull/75396)).
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
- Keep `translators:` comments next to their `__()`, `_x()`, `_n()` and `_nx()` calls in the transpiled and unminified output, so `wp i18n make-pot` extracts them from the built files ([#87189](https://github.com/WordPress/gutenberg/pull/87189)).
|
|
14
|
+
|
|
15
|
+
### Internal
|
|
16
|
+
|
|
17
|
+
- Remove the unused `moment-timezone` dependency. The `moment-timezone` alias resolves the pre-built bundle from the package that imports it, never from `wp-build` ([#87538](https://github.com/WordPress/gutenberg/pull/87538)).
|
|
18
|
+
- The generated registration PHP now picks the `SCRIPT_DEBUG` file extension with an `if` that also checks the new `has_debug_assets` key of `build/constants.php`, in every build. Tools that post-process the generated PHP may need updating; read `has_debug_assets` instead of matching the template code ([#75396](https://github.com/WordPress/gutenberg/pull/75396)).
|
|
19
|
+
- Update `@types/node` development dependency to 24.19.2 ([#87251](https://github.com/WordPress/gutenberg/pull/87251)).
|
|
20
|
+
- Update `vitest` development dependency to 5.1.1 ([#87254](https://github.com/WordPress/gutenberg/pull/87254)).
|
|
21
|
+
|
|
5
22
|
## 0.28.0 (2026-11-18)
|
|
6
23
|
|
|
7
24
|
### Enhancements
|
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ Build tool for WordPress plugins.
|
|
|
9
9
|
- **Transpilation**: Converts TypeScript/JSX source code to both CommonJS (`build/`) and ESM (`build-module/`) formats using Rolldown
|
|
10
10
|
- **Style Compilation**: Processes SCSS files and CSS modules, generating LTR and RTL versions
|
|
11
11
|
- **Bundling**: Creates browser-ready bundles for WordPress scripts and modules
|
|
12
|
+
- **Translator Comments**: Keeps `translators:` comments next to the `__()`, `_x()`, `_n()` and `_nx()` calls they describe in the transpiled and unminified output, so `wp i18n make-pot` can extract them from the built files
|
|
12
13
|
- **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
|
|
13
14
|
- **Watch Mode**: Incremental rebuilds during development
|
|
14
15
|
|
|
@@ -52,6 +53,20 @@ or via npm script:
|
|
|
52
53
|
}
|
|
53
54
|
```
|
|
54
55
|
|
|
56
|
+
### Skipping Debug Assets
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
wp-build --no-script-debug
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Builds only the minified `.min.js` and `.min.css` files, leaving out the non-minified copies used when `SCRIPT_DEBUG` is enabled. The generated registration code then loads the minified files even with `SCRIPT_DEBUG` on. Code that registers files from the build directory by hand should check the `has_debug_assets` key of the array that `build/constants.php` returns, the same way. The flag is ignored in watch mode.
|
|
63
|
+
|
|
64
|
+
`wp-build` does not empty the build directory, so remove it first to drop non-minified files left by an earlier build.
|
|
65
|
+
|
|
66
|
+
Without the non-minified files, `wp i18n make-pot` (which WordPress.org also runs to import plugin strings) skips `*.min.js` and reads JavaScript strings only from the `*.min.js.map` source maps. Ship those maps with the plugin, and do not exclude `*.map` in `.distignore`, or the plugin's JavaScript strings cannot be translated. This works because the maps embed the JavaScript that `wp-build` compiles each package to; `make-pot` cannot parse TypeScript, so it finds no strings in a source map that embeds TypeScript sources. Routes and widgets are built without source maps, so their strings are not extracted from a `--no-script-debug` build.
|
|
67
|
+
|
|
68
|
+
Gutenberg's own build does not support the flag yet. Its `lib/client-assets.php` registers some stylesheets by hand without checking `has_debug_assets`, so a Gutenberg build made with `--no-script-debug` requests missing non-minified files when `SCRIPT_DEBUG` is enabled.
|
|
69
|
+
|
|
55
70
|
### Build Concurrency
|
|
56
71
|
|
|
57
72
|
By default, `wp-build` limits concurrent Rolldown build jobs to half of the available CPU parallelism, rounded up. This prevents large projects from starting every package, route, and widget bundle at once.
|
|
@@ -748,7 +763,7 @@ The generated `widgets.php` registers each widget's entries as script modules vi
|
|
|
748
763
|
{handlePrefix}/widgets/{widget-dir-name}/widget
|
|
749
764
|
```
|
|
750
765
|
|
|
751
|
-
Registration is hooked into the `init` action. The `SCRIPT_DEBUG` constant controls whether minified (`.min.js`) or non-minified (`.js`) files are loaded.
|
|
766
|
+
Registration is hooked into the `init` action. The `SCRIPT_DEBUG` constant controls whether minified (`.min.js`) or non-minified (`.js`) files are loaded. Builds made with `--no-script-debug` do not include the non-minified files, so they always load the minified ones.
|
|
752
767
|
|
|
753
768
|
### Watch Mode
|
|
754
769
|
|
package/lib/build.mjs
CHANGED
|
@@ -23,15 +23,13 @@ import {
|
|
|
23
23
|
getPhpReplacements,
|
|
24
24
|
renderTemplateToString,
|
|
25
25
|
} from './php-generator.mjs';
|
|
26
|
-
import {
|
|
27
|
-
boolConfigVal,
|
|
28
|
-
getPackageInfo,
|
|
29
|
-
getPackageInfoFromFile,
|
|
30
|
-
} from './package-utils.mjs';
|
|
26
|
+
import { boolConfigVal, getPackageInfoFromFile } from './package-utils.mjs';
|
|
31
27
|
import { copyPackageFile, getCopyFiles } from './copy-files.mjs';
|
|
28
|
+
import { inferStyleDependencies } from './style-dependencies.mjs';
|
|
32
29
|
import { getBrowserslistQueries } from './browserslist.mjs';
|
|
33
30
|
import { getSourceFileGlob, isTestSourceFile } from './source-files.mjs';
|
|
34
31
|
import { createWordpressExternalsPlugin } from './wordpress-externals-plugin.mjs';
|
|
32
|
+
import { translatorCommentsPlugin } from './translator-comments-plugin.mjs';
|
|
35
33
|
import {
|
|
36
34
|
getAllRoutes,
|
|
37
35
|
getRouteFiles,
|
|
@@ -542,6 +540,7 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
542
540
|
handlePrefix = HANDLE_PREFIX,
|
|
543
541
|
scriptGlobal = SCRIPT_GLOBAL,
|
|
544
542
|
packageNamespace = PACKAGE_NAMESPACE,
|
|
543
|
+
noScriptDebug = false,
|
|
545
544
|
} = options;
|
|
546
545
|
|
|
547
546
|
const builtModules = [];
|
|
@@ -631,32 +630,37 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
631
630
|
file: path.join( outputDir, 'index.min.js' ),
|
|
632
631
|
minify: true,
|
|
633
632
|
},
|
|
634
|
-
} ),
|
|
635
|
-
buildWithConcurrency( {
|
|
636
|
-
...baseConfig,
|
|
637
|
-
transform: {
|
|
638
|
-
target,
|
|
639
|
-
define: getDefine( true ),
|
|
640
|
-
jsx: JSX_OPTIONS,
|
|
641
|
-
},
|
|
642
|
-
resolve: { conditionNames: developmentConditions },
|
|
643
|
-
plugins: [
|
|
644
|
-
...baseBundlePlugins,
|
|
645
|
-
wordpressExternalsPlugin(
|
|
646
|
-
'index.min',
|
|
647
|
-
'iife',
|
|
648
|
-
packageJson.wpScriptExtraDependencies || [],
|
|
649
|
-
false // Skip asset file for non-minified build
|
|
650
|
-
),
|
|
651
|
-
],
|
|
652
|
-
output: {
|
|
653
|
-
...baseOutput,
|
|
654
|
-
file: path.join( outputDir, 'index.js' ),
|
|
655
|
-
minify: false,
|
|
656
|
-
},
|
|
657
633
|
} )
|
|
658
634
|
);
|
|
659
635
|
|
|
636
|
+
if ( ! noScriptDebug ) {
|
|
637
|
+
builds.push(
|
|
638
|
+
buildWithConcurrency( {
|
|
639
|
+
...baseConfig,
|
|
640
|
+
transform: {
|
|
641
|
+
target,
|
|
642
|
+
define: getDefine( true ),
|
|
643
|
+
jsx: JSX_OPTIONS,
|
|
644
|
+
},
|
|
645
|
+
resolve: { conditionNames: developmentConditions },
|
|
646
|
+
plugins: [
|
|
647
|
+
...baseBundlePlugins,
|
|
648
|
+
wordpressExternalsPlugin(
|
|
649
|
+
'index.min',
|
|
650
|
+
'iife',
|
|
651
|
+
packageJson.wpScriptExtraDependencies || [],
|
|
652
|
+
false // Skip asset file for non-minified build
|
|
653
|
+
),
|
|
654
|
+
],
|
|
655
|
+
output: {
|
|
656
|
+
...baseOutput,
|
|
657
|
+
file: path.join( outputDir, 'index.js' ),
|
|
658
|
+
minify: false,
|
|
659
|
+
},
|
|
660
|
+
} )
|
|
661
|
+
);
|
|
662
|
+
}
|
|
663
|
+
|
|
660
664
|
builtScripts.push( {
|
|
661
665
|
handle: `${ handlePrefix }-${ packageName }`,
|
|
662
666
|
path: `${ packageName }/index`,
|
|
@@ -728,7 +732,7 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
728
732
|
} )
|
|
729
733
|
);
|
|
730
734
|
|
|
731
|
-
if ( ! isWasmWorker ) {
|
|
735
|
+
if ( ! noScriptDebug && ! isWasmWorker ) {
|
|
732
736
|
builds.push(
|
|
733
737
|
buildWithConcurrency( {
|
|
734
738
|
moduleTypes: LOADER_CONFIG,
|
|
@@ -800,18 +804,17 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
800
804
|
// Generate minified path: style.css -> style.min.css, style-rtl.css -> style-rtl.min.css
|
|
801
805
|
const minifiedPath = destPath.replace( /\.css$/, '.min.css' );
|
|
802
806
|
|
|
803
|
-
// Always produce both versions (like JavaScript does):
|
|
804
|
-
// 1. Non-minified version (for SCRIPT_DEBUG=true)
|
|
805
|
-
// 2. Minified version (for SCRIPT_DEBUG=false)
|
|
806
807
|
builds.push(
|
|
807
808
|
( async () => {
|
|
808
809
|
await mkdir( destDir, { recursive: true } );
|
|
809
810
|
const content = await readFile( cssFile, 'utf8' );
|
|
810
811
|
|
|
811
|
-
// Write non-minified version
|
|
812
|
-
|
|
812
|
+
// Write non-minified version (for SCRIPT_DEBUG=true)
|
|
813
|
+
if ( ! noScriptDebug ) {
|
|
814
|
+
await writeFile( destPath, content );
|
|
815
|
+
}
|
|
813
816
|
|
|
814
|
-
// Write minified version
|
|
817
|
+
// Write minified version (for SCRIPT_DEBUG=false)
|
|
815
818
|
const result = await postcss( [
|
|
816
819
|
cssnano( {
|
|
817
820
|
preset: [
|
|
@@ -875,10 +878,10 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
875
878
|
?.map( ( d ) => d.replace( /'/g, '' ) ) || [];
|
|
876
879
|
}
|
|
877
880
|
|
|
878
|
-
const styleDeps = await inferStyleDependencies(
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
);
|
|
881
|
+
const styleDeps = await inferStyleDependencies( scriptDependencies, {
|
|
882
|
+
resolveDir: path.join( PACKAGES_DIR, packageName ),
|
|
883
|
+
buildDir: BUILD_DIR,
|
|
884
|
+
} );
|
|
882
885
|
|
|
883
886
|
builtStyles.push( {
|
|
884
887
|
handle: `${ handlePrefix }-${ packageName }`,
|
|
@@ -894,69 +897,6 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
894
897
|
};
|
|
895
898
|
}
|
|
896
899
|
|
|
897
|
-
/**
|
|
898
|
-
* Infer style dependencies from script dependencies.
|
|
899
|
-
* Only includes dependencies that:
|
|
900
|
-
* 1. Are `@wordpress` packages (start with 'wp-')
|
|
901
|
-
* 2. Have wpScript: true in their package.json
|
|
902
|
-
* 3. Actually have a built style.css file
|
|
903
|
-
*
|
|
904
|
-
* @param {string[]} scriptDependencies Array of script handles from asset file.
|
|
905
|
-
* @param {string} packageName Package name (short name) being bundled, for context-aware resolution.
|
|
906
|
-
* @return {Promise<string[]>} Array of style handles to depend on.
|
|
907
|
-
*/
|
|
908
|
-
async function inferStyleDependencies( scriptDependencies, packageName ) {
|
|
909
|
-
if ( ! scriptDependencies || scriptDependencies.length === 0 ) {
|
|
910
|
-
return [];
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
const styleDeps = [];
|
|
914
|
-
// Get the resolve directory for context-aware package resolution
|
|
915
|
-
const resolveDir = path.join( PACKAGES_DIR, packageName );
|
|
916
|
-
|
|
917
|
-
for ( const scriptHandle of scriptDependencies ) {
|
|
918
|
-
// Skip non-package dependencies (like 'react', 'lodash', etc.)
|
|
919
|
-
if ( ! scriptHandle.startsWith( 'wp-' ) ) {
|
|
920
|
-
continue;
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
// Convert handle to package name: 'wp-components' → '@wordpress/components'
|
|
924
|
-
const shortName = scriptHandle.replace( 'wp-', '' );
|
|
925
|
-
const depPackageName = `@wordpress/${ shortName }`;
|
|
926
|
-
|
|
927
|
-
// Read the dependency's package.json with context-aware resolution
|
|
928
|
-
try {
|
|
929
|
-
const depPackageJson = getPackageInfo( depPackageName, resolveDir );
|
|
930
|
-
|
|
931
|
-
if ( ! depPackageJson ) {
|
|
932
|
-
continue;
|
|
933
|
-
}
|
|
934
|
-
|
|
935
|
-
// ONLY include if it has wpScript: true (which means it builds styles)
|
|
936
|
-
if ( depPackageJson.wpScript === true ) {
|
|
937
|
-
// Double-check the style file actually exists
|
|
938
|
-
const styleFile = path.join(
|
|
939
|
-
BUILD_DIR,
|
|
940
|
-
'styles',
|
|
941
|
-
shortName,
|
|
942
|
-
'style.css'
|
|
943
|
-
);
|
|
944
|
-
try {
|
|
945
|
-
await readFile( styleFile );
|
|
946
|
-
styleDeps.push( scriptHandle );
|
|
947
|
-
} catch {
|
|
948
|
-
// Style file doesn't exist, skip it
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
} catch {
|
|
952
|
-
// Package not found or can't read package.json - skip it
|
|
953
|
-
continue;
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
return styleDeps;
|
|
958
|
-
}
|
|
959
|
-
|
|
960
900
|
/**
|
|
961
901
|
* Generate PHP files for script module registration.
|
|
962
902
|
*
|
|
@@ -1363,6 +1303,9 @@ async function transpilePackage( packageName ) {
|
|
|
1363
1303
|
},
|
|
1364
1304
|
};
|
|
1365
1305
|
const plugins = [
|
|
1306
|
+
// Keeps translator comments in `build` and `build-module`, which the
|
|
1307
|
+
// script and module bundles are built from.
|
|
1308
|
+
translatorCommentsPlugin(),
|
|
1366
1309
|
dsTokenFallbacksJs,
|
|
1367
1310
|
wasmInlinePlugin,
|
|
1368
1311
|
// CSS modules import @wordpress/style-runtime in generated JS. Resolve
|
|
@@ -1632,10 +1575,12 @@ function getPackageName( filename ) {
|
|
|
1632
1575
|
/**
|
|
1633
1576
|
* Build a single route's files.
|
|
1634
1577
|
*
|
|
1635
|
-
* @param {string}
|
|
1578
|
+
* @param {string} routeName Route name.
|
|
1579
|
+
* @param {Object} root0 Options object.
|
|
1580
|
+
* @param {boolean} root0.noScriptDebug Whether to skip unminified builds.
|
|
1636
1581
|
* @return {Promise<number>} Build time in milliseconds.
|
|
1637
1582
|
*/
|
|
1638
|
-
async function buildRoute( routeName ) {
|
|
1583
|
+
async function buildRoute( routeName, { noScriptDebug = false } = {} ) {
|
|
1639
1584
|
const startTime = Date.now();
|
|
1640
1585
|
const routeDir = path.join( ROOT_DIR, 'routes', routeName );
|
|
1641
1586
|
const outputDir = path.join( BUILD_DIR, 'routes', routeName );
|
|
@@ -1653,8 +1598,7 @@ async function buildRoute( routeName ) {
|
|
|
1653
1598
|
} );
|
|
1654
1599
|
|
|
1655
1600
|
if ( routeEntryPoints.length > 0 ) {
|
|
1656
|
-
|
|
1657
|
-
await Promise.all( [
|
|
1601
|
+
const routeBuilds = [
|
|
1658
1602
|
buildWithConcurrency( {
|
|
1659
1603
|
moduleTypes: LOADER_CONFIG,
|
|
1660
1604
|
checks: BASE_CHECKS,
|
|
@@ -1681,33 +1625,41 @@ async function buildRoute( routeName ) {
|
|
|
1681
1625
|
codeSplitting: false,
|
|
1682
1626
|
},
|
|
1683
1627
|
} ),
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1628
|
+
];
|
|
1629
|
+
|
|
1630
|
+
if ( ! noScriptDebug ) {
|
|
1631
|
+
routeBuilds.push(
|
|
1632
|
+
buildWithConcurrency( {
|
|
1633
|
+
moduleTypes: LOADER_CONFIG,
|
|
1634
|
+
checks: BASE_CHECKS,
|
|
1635
|
+
input: routeEntryPoints,
|
|
1636
|
+
transform: {
|
|
1637
|
+
target: getEsbuildTarget(),
|
|
1638
|
+
define: getDefine( true ),
|
|
1639
|
+
jsx: JSX_OPTIONS,
|
|
1640
|
+
},
|
|
1641
|
+
resolve: { conditionNames: developmentConditions },
|
|
1642
|
+
plugins: [
|
|
1643
|
+
translatorCommentsPlugin(),
|
|
1644
|
+
styleRuntimeAliasPlugin(),
|
|
1645
|
+
wordpressExternalsPlugin(
|
|
1646
|
+
'route.min',
|
|
1647
|
+
'esm',
|
|
1648
|
+
[],
|
|
1649
|
+
false // Skip asset file for non-minified build
|
|
1650
|
+
),
|
|
1651
|
+
],
|
|
1652
|
+
output: {
|
|
1653
|
+
file: path.join( outputDir, 'route.js' ),
|
|
1654
|
+
format: 'esm',
|
|
1655
|
+
minify: false,
|
|
1656
|
+
codeSplitting: false,
|
|
1657
|
+
},
|
|
1658
|
+
} )
|
|
1659
|
+
);
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
await Promise.all( routeBuilds );
|
|
1711
1663
|
}
|
|
1712
1664
|
}
|
|
1713
1665
|
|
|
@@ -1720,8 +1672,7 @@ async function buildRoute( routeName ) {
|
|
|
1720
1672
|
// Write temporary entry file
|
|
1721
1673
|
await writeFile( tempEntryPath, syntheticEntry );
|
|
1722
1674
|
|
|
1723
|
-
|
|
1724
|
-
await Promise.all( [
|
|
1675
|
+
const contentBuilds = [
|
|
1725
1676
|
buildWithConcurrency( {
|
|
1726
1677
|
moduleTypes: LOADER_CONFIG,
|
|
1727
1678
|
checks: BASE_CHECKS,
|
|
@@ -1748,33 +1699,41 @@ async function buildRoute( routeName ) {
|
|
|
1748
1699
|
codeSplitting: false,
|
|
1749
1700
|
},
|
|
1750
1701
|
} ),
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1702
|
+
];
|
|
1703
|
+
|
|
1704
|
+
if ( ! noScriptDebug ) {
|
|
1705
|
+
contentBuilds.push(
|
|
1706
|
+
buildWithConcurrency( {
|
|
1707
|
+
moduleTypes: LOADER_CONFIG,
|
|
1708
|
+
checks: BASE_CHECKS,
|
|
1709
|
+
input: [ tempEntryPath ],
|
|
1710
|
+
transform: {
|
|
1711
|
+
target: getEsbuildTarget(),
|
|
1712
|
+
define: getDefine( true ),
|
|
1713
|
+
jsx: JSX_OPTIONS,
|
|
1714
|
+
},
|
|
1715
|
+
resolve: { conditionNames: developmentConditions },
|
|
1716
|
+
plugins: [
|
|
1717
|
+
translatorCommentsPlugin(),
|
|
1718
|
+
...createStyleBundlingPlugins( routeDir ),
|
|
1719
|
+
wordpressExternalsPlugin(
|
|
1720
|
+
'content.min',
|
|
1721
|
+
'esm',
|
|
1722
|
+
[],
|
|
1723
|
+
false // Skip asset file for non-minified build
|
|
1724
|
+
),
|
|
1725
|
+
],
|
|
1726
|
+
output: {
|
|
1727
|
+
file: path.join( outputDir, 'content.js' ),
|
|
1728
|
+
format: 'esm',
|
|
1729
|
+
minify: false,
|
|
1730
|
+
codeSplitting: false,
|
|
1731
|
+
},
|
|
1732
|
+
} )
|
|
1733
|
+
);
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
await Promise.all( contentBuilds );
|
|
1778
1737
|
|
|
1779
1738
|
await unlink( tempEntryPath );
|
|
1780
1739
|
}
|
|
@@ -1789,9 +1748,14 @@ async function buildRoute( routeName ) {
|
|
|
1789
1748
|
* empty set (the default) to build every route.
|
|
1790
1749
|
*
|
|
1791
1750
|
* @param {Set<string>} experimentalPageIds Page ids marked experimental.
|
|
1751
|
+
* @param {Object} root1 Options object.
|
|
1752
|
+
* @param {boolean} root1.noScriptDebug Whether to skip unminified builds.
|
|
1792
1753
|
* @return {Promise<void>}
|
|
1793
1754
|
*/
|
|
1794
|
-
async function buildRoutes(
|
|
1755
|
+
async function buildRoutes(
|
|
1756
|
+
experimentalPageIds = new Set(),
|
|
1757
|
+
{ noScriptDebug = false } = {}
|
|
1758
|
+
) {
|
|
1795
1759
|
console.log( '\n🚦 Phase 3: Building routes...\n' );
|
|
1796
1760
|
|
|
1797
1761
|
const allRoutes = getAllRoutes( ROOT_DIR );
|
|
@@ -1816,7 +1780,7 @@ async function buildRoutes( experimentalPageIds = new Set() ) {
|
|
|
1816
1780
|
|
|
1817
1781
|
await Promise.all(
|
|
1818
1782
|
routes.map( async ( routeName ) => {
|
|
1819
|
-
const buildTime = await buildRoute( routeName );
|
|
1783
|
+
const buildTime = await buildRoute( routeName, { noScriptDebug } );
|
|
1820
1784
|
console.log( ` ✔ Built route ${ routeName } (${ buildTime }ms)` );
|
|
1821
1785
|
} )
|
|
1822
1786
|
);
|
|
@@ -1825,10 +1789,12 @@ async function buildRoutes( experimentalPageIds = new Set() ) {
|
|
|
1825
1789
|
/**
|
|
1826
1790
|
* Build a single widget's files.
|
|
1827
1791
|
*
|
|
1828
|
-
* @param {string}
|
|
1792
|
+
* @param {string} widgetName Widget name.
|
|
1793
|
+
* @param {Object} root0 Options object.
|
|
1794
|
+
* @param {boolean} root0.noScriptDebug Whether to skip unminified builds.
|
|
1829
1795
|
* @return {Promise<number>} Build time in milliseconds.
|
|
1830
1796
|
*/
|
|
1831
|
-
async function buildWidget( widgetName ) {
|
|
1797
|
+
async function buildWidget( widgetName, { noScriptDebug = false } = {} ) {
|
|
1832
1798
|
const startTime = Date.now();
|
|
1833
1799
|
const widgetDir = path.join( ROOT_DIR, 'widgets', widgetName );
|
|
1834
1800
|
const outputDir = path.join( BUILD_DIR, 'widgets', widgetName );
|
|
@@ -1846,8 +1812,8 @@ async function buildWidget( widgetName ) {
|
|
|
1846
1812
|
} );
|
|
1847
1813
|
|
|
1848
1814
|
if ( renderEntryPoints.length > 0 ) {
|
|
1849
|
-
// Build
|
|
1850
|
-
|
|
1815
|
+
// Build minified and (unless skipped) non-minified versions in parallel
|
|
1816
|
+
const renderBuilds = [
|
|
1851
1817
|
buildWithConcurrency( {
|
|
1852
1818
|
moduleTypes: LOADER_CONFIG,
|
|
1853
1819
|
checks: BASE_CHECKS,
|
|
@@ -1874,33 +1840,41 @@ async function buildWidget( widgetName ) {
|
|
|
1874
1840
|
codeSplitting: false,
|
|
1875
1841
|
},
|
|
1876
1842
|
} ),
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1843
|
+
];
|
|
1844
|
+
|
|
1845
|
+
if ( ! noScriptDebug ) {
|
|
1846
|
+
renderBuilds.push(
|
|
1847
|
+
buildWithConcurrency( {
|
|
1848
|
+
moduleTypes: LOADER_CONFIG,
|
|
1849
|
+
checks: BASE_CHECKS,
|
|
1850
|
+
input: renderEntryPoints,
|
|
1851
|
+
transform: {
|
|
1852
|
+
target: getEsbuildTarget(),
|
|
1853
|
+
define: getDefine( true ),
|
|
1854
|
+
jsx: JSX_OPTIONS,
|
|
1855
|
+
},
|
|
1856
|
+
resolve: { conditionNames: developmentConditions },
|
|
1857
|
+
plugins: [
|
|
1858
|
+
translatorCommentsPlugin(),
|
|
1859
|
+
...createStyleBundlingPlugins( widgetDir ),
|
|
1860
|
+
wordpressExternalsPlugin(
|
|
1861
|
+
'render.min',
|
|
1862
|
+
'esm',
|
|
1863
|
+
[],
|
|
1864
|
+
false // Skip asset file for non-minified build
|
|
1865
|
+
),
|
|
1866
|
+
],
|
|
1867
|
+
output: {
|
|
1868
|
+
file: path.join( outputDir, 'render.js' ),
|
|
1869
|
+
format: 'esm',
|
|
1870
|
+
minify: false,
|
|
1871
|
+
codeSplitting: false,
|
|
1872
|
+
},
|
|
1873
|
+
} )
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
await Promise.all( renderBuilds );
|
|
1904
1878
|
}
|
|
1905
1879
|
}
|
|
1906
1880
|
|
|
@@ -1912,8 +1886,8 @@ async function buildWidget( widgetName ) {
|
|
|
1912
1886
|
} );
|
|
1913
1887
|
|
|
1914
1888
|
if ( widgetEntryPoints.length > 0 ) {
|
|
1915
|
-
// Build
|
|
1916
|
-
|
|
1889
|
+
// Build minified and (unless skipped) non-minified versions in parallel
|
|
1890
|
+
const widgetBuilds = [
|
|
1917
1891
|
buildWithConcurrency( {
|
|
1918
1892
|
moduleTypes: LOADER_CONFIG,
|
|
1919
1893
|
checks: BASE_CHECKS,
|
|
@@ -1940,33 +1914,41 @@ async function buildWidget( widgetName ) {
|
|
|
1940
1914
|
codeSplitting: false,
|
|
1941
1915
|
},
|
|
1942
1916
|
} ),
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1917
|
+
];
|
|
1918
|
+
|
|
1919
|
+
if ( ! noScriptDebug ) {
|
|
1920
|
+
widgetBuilds.push(
|
|
1921
|
+
buildWithConcurrency( {
|
|
1922
|
+
moduleTypes: LOADER_CONFIG,
|
|
1923
|
+
checks: BASE_CHECKS,
|
|
1924
|
+
input: widgetEntryPoints,
|
|
1925
|
+
transform: {
|
|
1926
|
+
target: getEsbuildTarget(),
|
|
1927
|
+
define: getDefine( true ),
|
|
1928
|
+
jsx: JSX_OPTIONS,
|
|
1929
|
+
},
|
|
1930
|
+
resolve: { conditionNames: developmentConditions },
|
|
1931
|
+
plugins: [
|
|
1932
|
+
translatorCommentsPlugin(),
|
|
1933
|
+
styleRuntimeAliasPlugin(),
|
|
1934
|
+
wordpressExternalsPlugin(
|
|
1935
|
+
'widget.min',
|
|
1936
|
+
'esm',
|
|
1937
|
+
[],
|
|
1938
|
+
false // Skip asset file for non-minified build
|
|
1939
|
+
),
|
|
1940
|
+
],
|
|
1941
|
+
output: {
|
|
1942
|
+
file: path.join( outputDir, 'widget.js' ),
|
|
1943
|
+
format: 'esm',
|
|
1944
|
+
minify: false,
|
|
1945
|
+
codeSplitting: false,
|
|
1946
|
+
},
|
|
1947
|
+
} )
|
|
1948
|
+
);
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
await Promise.all( widgetBuilds );
|
|
1970
1952
|
}
|
|
1971
1953
|
}
|
|
1972
1954
|
|
|
@@ -1976,9 +1958,11 @@ async function buildWidget( widgetName ) {
|
|
|
1976
1958
|
/**
|
|
1977
1959
|
* Build all discovered widgets.
|
|
1978
1960
|
*
|
|
1961
|
+
* @param {Object} root0 Options object.
|
|
1962
|
+
* @param {boolean} root0.noScriptDebug Whether to skip unminified builds.
|
|
1979
1963
|
* @return {Promise<void>}
|
|
1980
1964
|
*/
|
|
1981
|
-
async function buildAllWidgets() {
|
|
1965
|
+
async function buildAllWidgets( { noScriptDebug = false } = {} ) {
|
|
1982
1966
|
console.log( '\n🧩 Phase 4: Building widgets...\n' );
|
|
1983
1967
|
|
|
1984
1968
|
const widgets = getAllWidgets( ROOT_DIR );
|
|
@@ -1994,7 +1978,9 @@ async function buildAllWidgets() {
|
|
|
1994
1978
|
|
|
1995
1979
|
await Promise.all(
|
|
1996
1980
|
widgets.map( async ( widgetName ) => {
|
|
1997
|
-
const buildTime = await buildWidget( widgetName
|
|
1981
|
+
const buildTime = await buildWidget( widgetName, {
|
|
1982
|
+
noScriptDebug,
|
|
1983
|
+
} );
|
|
1998
1984
|
console.log(
|
|
1999
1985
|
` ✔ Built widget ${ widgetName } (${ buildTime }ms)`
|
|
2000
1986
|
);
|
|
@@ -2351,8 +2337,10 @@ async function generateWidgetsPhp( widgets, replacements ) {
|
|
|
2351
2337
|
* Main build function.
|
|
2352
2338
|
*
|
|
2353
2339
|
* @param {string?} baseUrlExpression
|
|
2340
|
+
* @param {Object} root0 Options object.
|
|
2341
|
+
* @param {boolean} root0.noScriptDebug Whether to skip unminified builds.
|
|
2354
2342
|
*/
|
|
2355
|
-
async function buildAll( baseUrlExpression ) {
|
|
2343
|
+
async function buildAll( baseUrlExpression, { noScriptDebug = false } = {} ) {
|
|
2356
2344
|
console.log( '🔨 Building packages...\n' );
|
|
2357
2345
|
|
|
2358
2346
|
const startTime = Date.now();
|
|
@@ -2393,7 +2381,9 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2393
2381
|
await Promise.all(
|
|
2394
2382
|
PACKAGES.map( async ( packageName ) => {
|
|
2395
2383
|
const startBundleTime = Date.now();
|
|
2396
|
-
const ret = await bundlePackage( packageName
|
|
2384
|
+
const ret = await bundlePackage( packageName, {
|
|
2385
|
+
noScriptDebug,
|
|
2386
|
+
} );
|
|
2397
2387
|
const buildTime = Date.now() - startBundleTime;
|
|
2398
2388
|
if ( ret ) {
|
|
2399
2389
|
console.log(
|
|
@@ -2448,10 +2438,10 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2448
2438
|
.map( ( page ) => page.id )
|
|
2449
2439
|
)
|
|
2450
2440
|
: new Set();
|
|
2451
|
-
await buildRoutes( experimentalPageIds );
|
|
2441
|
+
await buildRoutes( experimentalPageIds, { noScriptDebug } );
|
|
2452
2442
|
|
|
2453
2443
|
// Build widgets
|
|
2454
|
-
await buildAllWidgets();
|
|
2444
|
+
await buildAllWidgets( { noScriptDebug } );
|
|
2455
2445
|
|
|
2456
2446
|
// Collect widget data for PHP generation
|
|
2457
2447
|
const widgets = collectWidgets();
|
|
@@ -2505,6 +2495,9 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2505
2495
|
ROOT_DIR,
|
|
2506
2496
|
baseUrlExpression
|
|
2507
2497
|
);
|
|
2498
|
+
phpReplacements[ '{{HAS_DEBUG_ASSETS}}' ] = noScriptDebug
|
|
2499
|
+
? 'false'
|
|
2500
|
+
: 'true';
|
|
2508
2501
|
await Promise.all( [
|
|
2509
2502
|
generateMainBuildPhp( phpReplacements ),
|
|
2510
2503
|
generateModuleRegistrationPhp( modules, phpReplacements ),
|
|
@@ -2976,6 +2969,10 @@ async function main() {
|
|
|
2976
2969
|
short: 'w',
|
|
2977
2970
|
default: false,
|
|
2978
2971
|
},
|
|
2972
|
+
'no-script-debug': {
|
|
2973
|
+
type: 'boolean',
|
|
2974
|
+
default: false,
|
|
2975
|
+
},
|
|
2979
2976
|
'base-url': {
|
|
2980
2977
|
type: 'string',
|
|
2981
2978
|
default:
|
|
@@ -2995,7 +2992,16 @@ async function main() {
|
|
|
2995
2992
|
setBuildConcurrency( buildConcurrency );
|
|
2996
2993
|
console.log( `⚙️ Using build concurrency: ${ buildConcurrency }\n` );
|
|
2997
2994
|
|
|
2998
|
-
|
|
2995
|
+
let noScriptDebug = values[ 'no-script-debug' ];
|
|
2996
|
+
|
|
2997
|
+
if ( noScriptDebug && values.watch ) {
|
|
2998
|
+
console.warn(
|
|
2999
|
+
'⚠️ --no-script-debug is ignored in watch mode (watch is for development).'
|
|
3000
|
+
);
|
|
3001
|
+
noScriptDebug = false;
|
|
3002
|
+
}
|
|
3003
|
+
|
|
3004
|
+
await buildAll( baseUrlExpression, { noScriptDebug } );
|
|
2999
3005
|
|
|
3000
3006
|
if ( values.watch ) {
|
|
3001
3007
|
console.log( '\n👀 Watching for changes...\n' );
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getPackageInfo } from './package-utils.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Infer style dependencies from script dependencies.
|
|
7
|
+
* Only includes dependencies that:
|
|
8
|
+
* 1. Are `@wordpress` packages (start with 'wp-')
|
|
9
|
+
* 2. Have wpScript: true in their package.json
|
|
10
|
+
* 3. Actually have a built style.min.css file
|
|
11
|
+
*
|
|
12
|
+
* @param {string[]} scriptDependencies Array of script handles from asset file.
|
|
13
|
+
* @param {Object} options Options object.
|
|
14
|
+
* @param {string} options.resolveDir Directory to resolve dependency packages from.
|
|
15
|
+
* @param {string} options.buildDir Build output directory.
|
|
16
|
+
* @return {Promise<string[]>} Array of style handles to depend on.
|
|
17
|
+
*/
|
|
18
|
+
export async function inferStyleDependencies(
|
|
19
|
+
scriptDependencies,
|
|
20
|
+
{ resolveDir, buildDir }
|
|
21
|
+
) {
|
|
22
|
+
if ( ! scriptDependencies || scriptDependencies.length === 0 ) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const styleDeps = [];
|
|
27
|
+
|
|
28
|
+
for ( const scriptHandle of scriptDependencies ) {
|
|
29
|
+
// Skip non-package dependencies (like 'react', 'lodash', etc.)
|
|
30
|
+
if ( ! scriptHandle.startsWith( 'wp-' ) ) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Convert handle to package name: 'wp-components' → '@wordpress/components'
|
|
35
|
+
const shortName = scriptHandle.replace( 'wp-', '' );
|
|
36
|
+
const depPackageName = `@wordpress/${ shortName }`;
|
|
37
|
+
|
|
38
|
+
// Read the dependency's package.json with context-aware resolution
|
|
39
|
+
try {
|
|
40
|
+
const depPackageJson = getPackageInfo( depPackageName, resolveDir );
|
|
41
|
+
|
|
42
|
+
if ( ! depPackageJson ) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ONLY include if it has wpScript: true (which means it builds styles)
|
|
47
|
+
if ( depPackageJson.wpScript ) {
|
|
48
|
+
// Check the minified file, which is built even with --no-script-debug.
|
|
49
|
+
const styleFile = path.join(
|
|
50
|
+
buildDir,
|
|
51
|
+
'styles',
|
|
52
|
+
shortName,
|
|
53
|
+
'style.min.css'
|
|
54
|
+
);
|
|
55
|
+
try {
|
|
56
|
+
await readFile( styleFile );
|
|
57
|
+
styleDeps.push( scriptHandle );
|
|
58
|
+
} catch {
|
|
59
|
+
// Style file doesn't exist, skip it
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// Package not found or can't read package.json - skip it
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return styleDeps;
|
|
69
|
+
}
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import { RolldownMagicString } from 'rolldown';
|
|
2
|
+
import { parseSync } from 'rolldown/utils';
|
|
3
|
+
|
|
4
|
+
/** @typedef {import('rolldown/utils').ParseResult['comments'][number]} Comment */
|
|
5
|
+
|
|
6
|
+
/** @typedef {import('rolldown/utils').ESTree.Node} Node */
|
|
7
|
+
/** @typedef {import('rolldown/utils').ESTree.CallExpression} CallExpression */
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A node and where it is in the tree.
|
|
11
|
+
*
|
|
12
|
+
* @typedef {Object} NodeEntry
|
|
13
|
+
* @property {Node} node Node.
|
|
14
|
+
* @property {Node} [parent] Parent node.
|
|
15
|
+
* @property {string} [key] Key of the node in its parent.
|
|
16
|
+
* @property {Node} [grandparent] Parent of the parent node.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Names of the gettext functions that `wp i18n make-pot` extracts strings from.
|
|
21
|
+
*/
|
|
22
|
+
const GETTEXT_FUNCTIONS = new Set( [ '__', '_x', '_n', '_nx' ] );
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Module types the plugin parses.
|
|
26
|
+
*/
|
|
27
|
+
const SCRIPT_MODULE_TYPES = [ 'js', 'jsx', 'ts', 'tsx' ];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Line terminators, as JavaScript counts them.
|
|
31
|
+
*/
|
|
32
|
+
const LINE_BREAK = /\r\n?|[\n\u2028\u2029]/g;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Check whether a comment is a translator comment, using the same test as
|
|
36
|
+
* `wp i18n make-pot`: after the comment markers and any leading `*`, the text
|
|
37
|
+
* starts with `translators`.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} value Comment text without the `//` or `/*` and `*\/` markers.
|
|
40
|
+
* @return {boolean} Whether the comment is a translator comment.
|
|
41
|
+
*/
|
|
42
|
+
export function isTranslatorComment( value ) {
|
|
43
|
+
return /^[\s*]*translators/i.test( value );
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Get the name of the function a call expression calls, for the callee shapes
|
|
48
|
+
* `make-pot` understands: `__()`, `i18n.__()`, `wp.i18n.__()` and
|
|
49
|
+
* `( 0, i18n.__ )()`.
|
|
50
|
+
*
|
|
51
|
+
* @param {CallExpression} node Call expression node.
|
|
52
|
+
* @return {string|undefined} Function name, if the callee has one.
|
|
53
|
+
*/
|
|
54
|
+
function getCalleeName( node ) {
|
|
55
|
+
let callee = node.callee;
|
|
56
|
+
if (
|
|
57
|
+
callee.type === 'SequenceExpression' &&
|
|
58
|
+
callee.expressions.length === 2
|
|
59
|
+
) {
|
|
60
|
+
callee = callee.expressions[ 1 ];
|
|
61
|
+
}
|
|
62
|
+
if ( callee.type === 'Identifier' ) {
|
|
63
|
+
return callee.name;
|
|
64
|
+
}
|
|
65
|
+
if (
|
|
66
|
+
callee.type === 'MemberExpression' &&
|
|
67
|
+
! callee.computed &&
|
|
68
|
+
callee.property.type === 'Identifier'
|
|
69
|
+
) {
|
|
70
|
+
return callee.property.name;
|
|
71
|
+
}
|
|
72
|
+
return undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const STATEMENT_KEYS = [ 'body', 'consequent', 'alternate', 'declaration' ];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Whether a node sits in a position where Rolldown's printer keeps a JSDoc
|
|
79
|
+
* comment written just before it: a statement, a call or `new` argument, an
|
|
80
|
+
* object property value, a JSX attribute value or a template literal
|
|
81
|
+
* expression. In other positions, such as a conditional branch, an array
|
|
82
|
+
* element, a logical operand, a variable initializer or a `return` argument,
|
|
83
|
+
* the printer drops the comment.
|
|
84
|
+
*
|
|
85
|
+
* @param {NodeEntry} entry Node and where it is.
|
|
86
|
+
* @return {boolean} Whether a leading comment is printed.
|
|
87
|
+
*/
|
|
88
|
+
function isCommentPreservingPosition( { node, parent, key, grandparent } ) {
|
|
89
|
+
if ( ! parent ) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if ( isStatement( node ) && STATEMENT_KEYS.includes( key ?? '' ) ) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
switch ( parent.type ) {
|
|
96
|
+
case 'CallExpression':
|
|
97
|
+
case 'NewExpression':
|
|
98
|
+
return key === 'arguments';
|
|
99
|
+
case 'Property':
|
|
100
|
+
return key === 'value' && ! parent.shorthand;
|
|
101
|
+
case 'TemplateLiteral':
|
|
102
|
+
return key === 'expressions';
|
|
103
|
+
case 'JSXExpressionContainer':
|
|
104
|
+
return grandparent?.type === 'JSXAttribute';
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Whether a node is a statement or a declaration.
|
|
111
|
+
*
|
|
112
|
+
* @param {Node} node Node.
|
|
113
|
+
* @return {boolean} Whether the node is a statement.
|
|
114
|
+
*/
|
|
115
|
+
function isStatement( node ) {
|
|
116
|
+
return /(Statement|Declaration)$/.test( node.type );
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Walk an ESTree AST depth-first, in source order.
|
|
121
|
+
*
|
|
122
|
+
* @param {Node} node Node to start from.
|
|
123
|
+
* @param {( entry: NodeEntry ) => void} visit Called with each node and where it is.
|
|
124
|
+
* @param {Node} [parent] Parent node.
|
|
125
|
+
* @param {string} [key] Key of `node` in `parent`.
|
|
126
|
+
* @param {Node} [grandparent] Parent of `parent`.
|
|
127
|
+
*/
|
|
128
|
+
function walk( node, visit, parent, key, grandparent ) {
|
|
129
|
+
visit( { node, parent, key, grandparent } );
|
|
130
|
+
for ( const [ childKey, child ] of Object.entries( node ) ) {
|
|
131
|
+
const children = Array.isArray( child ) ? child : [ child ];
|
|
132
|
+
for ( const item of children ) {
|
|
133
|
+
if ( item && typeof item.type === 'string' ) {
|
|
134
|
+
walk( item, visit, node, childKey, parent );
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Turn a comment into the text of a JSDoc comment, which Rolldown's printer
|
|
142
|
+
* keeps. A `*\/` inside a line comment is broken up so it can't end the new
|
|
143
|
+
* block comment early.
|
|
144
|
+
*
|
|
145
|
+
* @param {Comment} comment Comment from the parser.
|
|
146
|
+
* @param {string} text Comment, with its markers.
|
|
147
|
+
* @return {string} JSDoc comment.
|
|
148
|
+
*/
|
|
149
|
+
function toJsDocComment( comment, text ) {
|
|
150
|
+
if ( comment.type === 'Line' ) {
|
|
151
|
+
return `/** ${ comment.value.trim().replace( /\*\//g, '* /' ) } */`;
|
|
152
|
+
}
|
|
153
|
+
return text.startsWith( '/**' ) ? text : `/**${ text.slice( 2 ) }`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Keep the translator comments of a module in the printed output.
|
|
158
|
+
*
|
|
159
|
+
* Rolldown's printer keeps JSDoc comments, and only in some positions. For
|
|
160
|
+
* each translator comment that `make-pot` would attach to a gettext call (the
|
|
161
|
+
* comment ends on the call's line or the line before), this:
|
|
162
|
+
*
|
|
163
|
+
* - turns the comment into a JSDoc comment where it is, when it directly
|
|
164
|
+
* precedes the call, or the statement that starts with the call, in a
|
|
165
|
+
* position the printer keeps;
|
|
166
|
+
* - otherwise moves it into the call, before the call's first argument, a
|
|
167
|
+
* position the printer always keeps and that `make-pot` reads.
|
|
168
|
+
*
|
|
169
|
+
* Only comments are added, changed or moved, so the code, and so the minified
|
|
170
|
+
* output, stays the same. A module that doesn't parse is left to Rolldown to
|
|
171
|
+
* report.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} code Module source.
|
|
174
|
+
* @param {string} id Module id.
|
|
175
|
+
* @param {'js'|'jsx'|'ts'|'tsx'} moduleType Module type.
|
|
176
|
+
* @return {RolldownMagicString|null} Edited source, or null if nothing changed.
|
|
177
|
+
*/
|
|
178
|
+
export function keepTranslatorComments( code, id, moduleType ) {
|
|
179
|
+
const { program, comments, errors } = parseSync( id, code, {
|
|
180
|
+
lang: moduleType,
|
|
181
|
+
sourceType: 'module',
|
|
182
|
+
} );
|
|
183
|
+
if ( errors.length > 0 ) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const translatorComments = comments.filter( ( comment ) =>
|
|
188
|
+
isTranslatorComment( comment.value )
|
|
189
|
+
);
|
|
190
|
+
if ( translatorComments.length === 0 ) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Line start offsets, to turn offsets into line numbers.
|
|
195
|
+
const lineStarts = [ 0 ];
|
|
196
|
+
for ( const match of code.matchAll( LINE_BREAK ) ) {
|
|
197
|
+
lineStarts.push( match.index + match[ 0 ].length );
|
|
198
|
+
}
|
|
199
|
+
/** @type {( offset: number ) => number} */
|
|
200
|
+
const getLine = ( offset ) => {
|
|
201
|
+
let low = 0;
|
|
202
|
+
let high = lineStarts.length - 1;
|
|
203
|
+
while ( low < high ) {
|
|
204
|
+
const middle = Math.ceil( ( low + high ) / 2 );
|
|
205
|
+
if ( lineStarts[ middle ] <= offset ) {
|
|
206
|
+
low = middle;
|
|
207
|
+
} else {
|
|
208
|
+
high = middle - 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return low;
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// The gettext calls, and for each offset the nodes that start there,
|
|
215
|
+
// outermost first.
|
|
216
|
+
/** @type {CallExpression[]} */
|
|
217
|
+
const gettextCalls = [];
|
|
218
|
+
/** @type {Map<number, NodeEntry[]>} */
|
|
219
|
+
const nodesByStart = new Map();
|
|
220
|
+
walk( program, ( entry ) => {
|
|
221
|
+
const { node } = entry;
|
|
222
|
+
const entries = nodesByStart.get( node.start ) ?? [];
|
|
223
|
+
entries.push( entry );
|
|
224
|
+
nodesByStart.set( node.start, entries );
|
|
225
|
+
if (
|
|
226
|
+
node.type === 'CallExpression' &&
|
|
227
|
+
node.arguments.length > 0 &&
|
|
228
|
+
GETTEXT_FUNCTIONS.has( getCalleeName( node ) ?? '' )
|
|
229
|
+
) {
|
|
230
|
+
gettextCalls.push( node );
|
|
231
|
+
}
|
|
232
|
+
} );
|
|
233
|
+
gettextCalls.sort( ( a, b ) => a.start - b.start );
|
|
234
|
+
|
|
235
|
+
const commentsByStart = new Map(
|
|
236
|
+
comments.map( ( comment ) => [ comment.start, comment ] )
|
|
237
|
+
);
|
|
238
|
+
/** @type {( offset: number ) => number} */
|
|
239
|
+
const getNextTokenStart = ( offset ) => {
|
|
240
|
+
let position = offset;
|
|
241
|
+
for (;;) {
|
|
242
|
+
while ( /\s/.test( code[ position ] ?? '' ) ) {
|
|
243
|
+
position++;
|
|
244
|
+
}
|
|
245
|
+
const comment = commentsByStart.get( position );
|
|
246
|
+
if ( ! comment ) {
|
|
247
|
+
return position;
|
|
248
|
+
}
|
|
249
|
+
position = comment.end;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const magicString = new RolldownMagicString( code );
|
|
254
|
+
|
|
255
|
+
for ( const comment of translatorComments ) {
|
|
256
|
+
const commentEndLine = getLine( comment.end );
|
|
257
|
+
|
|
258
|
+
// The call `make-pot` gives the comment to: the first gettext call that
|
|
259
|
+
// starts after the comment, on the same line or the next, or the call
|
|
260
|
+
// the comment is in, when it comes before the first argument.
|
|
261
|
+
const call = gettextCalls.find(
|
|
262
|
+
( node ) =>
|
|
263
|
+
( node.start >= comment.end &&
|
|
264
|
+
getLine( node.start ) - commentEndLine <= 1 ) ||
|
|
265
|
+
( node.start < comment.start &&
|
|
266
|
+
comment.end <= node.arguments[ 0 ].start )
|
|
267
|
+
);
|
|
268
|
+
if ( ! call ) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// The comment stays where it is if the printer keeps it there, and it
|
|
273
|
+
// then precedes the call, or the statement that starts on its line.
|
|
274
|
+
const startingNodes = nodesByStart.get(
|
|
275
|
+
getNextTokenStart( comment.end )
|
|
276
|
+
);
|
|
277
|
+
const precedesCall = startingNodes?.some(
|
|
278
|
+
( { node } ) => node === call || node === call.arguments[ 0 ]
|
|
279
|
+
);
|
|
280
|
+
const keptInPlace = startingNodes?.some(
|
|
281
|
+
( entry ) =>
|
|
282
|
+
isCommentPreservingPosition( entry ) &&
|
|
283
|
+
( precedesCall ||
|
|
284
|
+
( isStatement( entry.node ) &&
|
|
285
|
+
getLine( entry.node.start ) ===
|
|
286
|
+
getLine( call.start ) ) )
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
const commentText = code.slice( comment.start, comment.end );
|
|
290
|
+
const jsDocComment = toJsDocComment( comment, commentText );
|
|
291
|
+
if ( keptInPlace ) {
|
|
292
|
+
if ( jsDocComment !== commentText ) {
|
|
293
|
+
magicString.overwrite(
|
|
294
|
+
comment.start,
|
|
295
|
+
comment.end,
|
|
296
|
+
jsDocComment
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Otherwise it moves into the call. What replaces it keeps any line
|
|
303
|
+
// break the comment had, as a multi-line comment counts as one for
|
|
304
|
+
// automatic semicolon insertion.
|
|
305
|
+
magicString.overwrite(
|
|
306
|
+
comment.start,
|
|
307
|
+
comment.end,
|
|
308
|
+
/[\r\n\u2028\u2029]/.test( commentText ) ? '\n' : ' '
|
|
309
|
+
);
|
|
310
|
+
magicString.appendLeft(
|
|
311
|
+
call.arguments[ 0 ].start,
|
|
312
|
+
`${ jsDocComment } `
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return magicString.hasChanged() ? magicString : null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Rolldown plugin that keeps translator comments (`/* translators: … *\/`
|
|
321
|
+
* and `// translators: …`) in the printed output, next to the gettext call
|
|
322
|
+
* they describe, so `wp i18n make-pot` can extract them from the built
|
|
323
|
+
* files.
|
|
324
|
+
*
|
|
325
|
+
* @return {import('rolldown').Plugin} Rolldown plugin.
|
|
326
|
+
*/
|
|
327
|
+
export function translatorCommentsPlugin() {
|
|
328
|
+
return {
|
|
329
|
+
name: 'translator-comments',
|
|
330
|
+
transform: {
|
|
331
|
+
filter: {
|
|
332
|
+
code: /translators/i,
|
|
333
|
+
moduleType: SCRIPT_MODULE_TYPES,
|
|
334
|
+
},
|
|
335
|
+
handler( source, id, { moduleType } ) {
|
|
336
|
+
// Rolldown builds the source map from the magic string's edits.
|
|
337
|
+
const code = keepTranslatorComments(
|
|
338
|
+
source,
|
|
339
|
+
id,
|
|
340
|
+
// The filter only lets these module types through.
|
|
341
|
+
/** @type {'js'|'jsx'|'ts'|'tsx'} */ ( moduleType )
|
|
342
|
+
);
|
|
343
|
+
return code && { code };
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
};
|
|
347
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/build",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.29.0",
|
|
4
4
|
"description": "Build tool for WordPress plugins.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
"wp-build": "./lib/build.mjs"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@wordpress/browserslist-config": "^7.
|
|
45
|
-
"@wordpress/style-runtime": "^0.
|
|
46
|
-
"@wordpress/theme-build-tools": "^1.
|
|
44
|
+
"@wordpress/browserslist-config": "^7.3.0",
|
|
45
|
+
"@wordpress/style-runtime": "^0.17.0",
|
|
46
|
+
"@wordpress/theme-build-tools": "^1.2.0",
|
|
47
47
|
"autoprefixer": "^10.4.21",
|
|
48
48
|
"browserslist": "^4.28.7",
|
|
49
49
|
"browserslist-to-esbuild": "^2.1.1",
|
|
@@ -51,7 +51,6 @@
|
|
|
51
51
|
"chokidar": "^4.0.0",
|
|
52
52
|
"cssnano": "^6.0.1",
|
|
53
53
|
"fast-glob": "^3.2.7",
|
|
54
|
-
"moment-timezone": "^0.5.40",
|
|
55
54
|
"postcss": "^8.5.33",
|
|
56
55
|
"postcss-modules": "^6.0.1",
|
|
57
56
|
"rolldown": "^1.2.15",
|
|
@@ -59,9 +58,9 @@
|
|
|
59
58
|
"sass-embedded": "^1.97.2"
|
|
60
59
|
},
|
|
61
60
|
"devDependencies": {
|
|
62
|
-
"@types/node": "^
|
|
63
|
-
"@wordpress/theme": "^5.
|
|
64
|
-
"vitest": "^5.1.
|
|
61
|
+
"@types/node": "^22.20.5",
|
|
62
|
+
"@wordpress/theme": "^5.1.0",
|
|
63
|
+
"vitest": "^5.1.1"
|
|
65
64
|
},
|
|
66
65
|
"peerDependencies": {
|
|
67
66
|
"@wordpress/theme": "^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
@@ -74,5 +73,5 @@
|
|
|
74
73
|
"publishConfig": {
|
|
75
74
|
"access": "public"
|
|
76
75
|
},
|
|
77
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "7f19202f61aeba8f4bdce892a1a0df485ad76e2b"
|
|
78
77
|
}
|
|
@@ -39,10 +39,9 @@ function {{PREFIX}}_register_script_modules() {
|
|
|
39
39
|
|
|
40
40
|
$modules = require $modules_file;
|
|
41
41
|
$base_url = $build_constants['build_url'] . 'modules/';
|
|
42
|
-
|
|
43
42
|
foreach ( $modules as $module ) {
|
|
44
43
|
// WASM-only modules (e.g., vips worker) only have .min.js; use it even when SCRIPT_DEBUG is `true`.
|
|
45
|
-
$extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && empty( $module['min_only'] ) )
|
|
44
|
+
$extension = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] && empty( $module['min_only'] ) )
|
|
46
45
|
? '.js'
|
|
47
46
|
: '.min.js';
|
|
48
47
|
|
|
@@ -56,7 +56,10 @@ function {{PREFIX}}_register_page_routes( $page_routes, $register_function_name
|
|
|
56
56
|
if ( file_exists( $content_asset_path ) ) {
|
|
57
57
|
$content_asset = require $content_asset_path;
|
|
58
58
|
$content_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/content';
|
|
59
|
-
$extension =
|
|
59
|
+
$extension = '.min.js';
|
|
60
|
+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] ) {
|
|
61
|
+
$extension = '.js';
|
|
62
|
+
}
|
|
60
63
|
// Deregister first to override any previously registered version
|
|
61
64
|
// (e.g., Core's default modules when running as a plugin).
|
|
62
65
|
wp_deregister_script_module( $content_handle );
|
|
@@ -75,7 +78,10 @@ function {{PREFIX}}_register_page_routes( $page_routes, $register_function_name
|
|
|
75
78
|
if ( file_exists( $route_asset_path ) ) {
|
|
76
79
|
$route_asset = require $route_asset_path;
|
|
77
80
|
$route_handle = '{{HANDLE_PREFIX}}/routes/' . $route['name'] . '/route';
|
|
78
|
-
$extension =
|
|
81
|
+
$extension = '.min.js';
|
|
82
|
+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] ) {
|
|
83
|
+
$extension = '.js';
|
|
84
|
+
}
|
|
79
85
|
// Deregister first to override any previously registered version
|
|
80
86
|
// (e.g., Core's default modules when running as a plugin).
|
|
81
87
|
wp_deregister_script_module( $route_handle );
|
|
@@ -57,7 +57,6 @@ function {{PREFIX}}_register_package_scripts( $scripts ) {
|
|
|
57
57
|
// Load build constants
|
|
58
58
|
$build_constants = require __DIR__ . '/constants.php';
|
|
59
59
|
$default_version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : $build_constants['version'];
|
|
60
|
-
$extension = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.js' : '.min.js';
|
|
61
60
|
|
|
62
61
|
$scripts_dir = __DIR__ . '/scripts';
|
|
63
62
|
$scripts_file = $scripts_dir . '/registry.php';
|
|
@@ -67,6 +66,11 @@ function {{PREFIX}}_register_package_scripts( $scripts ) {
|
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
$scripts_data = require $scripts_file;
|
|
69
|
+
|
|
70
|
+
$extension = '.min.js';
|
|
71
|
+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] ) {
|
|
72
|
+
$extension = '.js';
|
|
73
|
+
}
|
|
70
74
|
$plugin_dir = dirname( __FILE__ );
|
|
71
75
|
|
|
72
76
|
foreach ( $scripts_data as $script_data ) {
|
|
@@ -47,7 +47,11 @@ function {{PREFIX}}_register_package_styles( $styles ) {
|
|
|
47
47
|
// Load build constants
|
|
48
48
|
$build_constants = require __DIR__ . '/constants.php';
|
|
49
49
|
$default_version = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? time() : $build_constants['version'];
|
|
50
|
-
|
|
50
|
+
|
|
51
|
+
$suffix = '.min';
|
|
52
|
+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] ) {
|
|
53
|
+
$suffix = '';
|
|
54
|
+
}
|
|
51
55
|
|
|
52
56
|
$styles_dir = __DIR__ . '/styles';
|
|
53
57
|
$styles_file = $styles_dir . '/registry.php';
|
|
@@ -28,7 +28,10 @@ function {{PREFIX}}_register_widget_modules() {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
$build_constants = require __DIR__ . '/constants.php';
|
|
31
|
-
$extension =
|
|
31
|
+
$extension = '.min.js';
|
|
32
|
+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && $build_constants['has_debug_assets'] ) {
|
|
33
|
+
$extension = '.js';
|
|
34
|
+
}
|
|
32
35
|
|
|
33
36
|
foreach ( $widgets as $widget ) {
|
|
34
37
|
$widget_name = $widget['dir_name'];
|