@wordpress/build 0.27.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 +35 -0
- package/README.md +46 -9
- package/lib/build.mjs +350 -378
- package/lib/copy-files.mjs +165 -0
- package/lib/package-utils.mjs +14 -0
- package/lib/style-dependencies.mjs +69 -0
- package/lib/translator-comments-plugin.mjs +347 -0
- package/package.json +10 -11
- package/templates/build.php.template +4 -0
- package/templates/constants.php.template +7 -2
- package/templates/module-registration.php.template +5 -2
- package/templates/module-registry.php.template +4 -0
- package/templates/page-wp-admin.php.template +65 -52
- package/templates/page.php.template +25 -19
- package/templates/pages.php.template +4 -0
- package/templates/route-registry.php.template +4 -0
- package/templates/routes-registration.php.template +12 -2
- package/templates/script-registration.php.template +9 -1
- package/templates/script-registry.php.template +4 -0
- package/templates/style-registration.php.template +9 -1
- package/templates/style-registry.php.template +4 -0
- package/templates/widget-registration.php.template +8 -1
- package/templates/widget-registry.php.template +4 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { readFile, writeFile, copyFile, mkdir } from 'fs/promises';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import glob from 'fast-glob';
|
|
4
|
+
import { boolConfigVal } from './package-utils.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {Object} PhpTransforms
|
|
8
|
+
*
|
|
9
|
+
* @property {string} [functionPrefix] Prefix for the functions a PHP file defines, replacing a `wp_` prefix.
|
|
10
|
+
* @property {string} [classSuffix] Suffix for the classes in `suffixClasses`.
|
|
11
|
+
* @property {string[]} [prefixFunctions] Other functions to prefix with `functionPrefix`.
|
|
12
|
+
* @property {string[]} [suffixClasses] Classes to suffix with `classSuffix`.
|
|
13
|
+
* @property {number} [addActionPriority] Priority to add to the `init` action registering a block.
|
|
14
|
+
* @property {string} [filenameSuffix] Suffix for the copied file's name.
|
|
15
|
+
* @property {boolean} [flattenIndexFiles] Whether to copy `{name}/index.php` to `{name}.php`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @typedef {Object} CopyFiles
|
|
20
|
+
*
|
|
21
|
+
* @property {string[]} files Glob patterns of the files to copy, relative to the package.
|
|
22
|
+
* @property {{ php?: PhpTransforms }} [transforms] Transforms to apply to the copied files.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @typedef {{ wpCopyFiles?: CopyFiles }} CopyFilesPackageJson
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Apply a package's `wpCopyFiles` PHP transforms to a PHP file's content.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} content PHP file content.
|
|
33
|
+
* @param {PhpTransforms} transforms The `wpCopyFiles.transforms.php` configuration.
|
|
34
|
+
* @return {string} Transformed content.
|
|
35
|
+
*/
|
|
36
|
+
export function transformPhpContent( content, transforms ) {
|
|
37
|
+
const {
|
|
38
|
+
functionPrefix = '',
|
|
39
|
+
classSuffix = '',
|
|
40
|
+
prefixFunctions = [],
|
|
41
|
+
suffixClasses = [],
|
|
42
|
+
addActionPriority,
|
|
43
|
+
} = transforms;
|
|
44
|
+
|
|
45
|
+
content = content.toString();
|
|
46
|
+
|
|
47
|
+
/*
|
|
48
|
+
* Transforms are used to modify PHP files that are committed to version
|
|
49
|
+
* control in their wordpress-develop state (`wp_` function prefixes, `WP_`
|
|
50
|
+
* class prefixes, etc.). When building for WordPress Core, it's not
|
|
51
|
+
* necessary to perform these steps.
|
|
52
|
+
*/
|
|
53
|
+
if (
|
|
54
|
+
boolConfigVal( process.env.IS_WORDPRESS_CORE ) ??
|
|
55
|
+
boolConfigVal( process.env.npm_package_config_IS_WORDPRESS_CORE )
|
|
56
|
+
) {
|
|
57
|
+
return content;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if ( prefixFunctions.length ) {
|
|
61
|
+
content = content.replace(
|
|
62
|
+
new RegExp( prefixFunctions.join( '|' ), 'g' ),
|
|
63
|
+
( match ) => `${ functionPrefix }${ match.replace( /^wp_/, '' ) }`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if ( suffixClasses.length ) {
|
|
68
|
+
content = content.replace(
|
|
69
|
+
new RegExp( suffixClasses.join( '|' ), 'g' ),
|
|
70
|
+
( match ) => `${ match }${ classSuffix }`
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if ( functionPrefix ) {
|
|
75
|
+
content = Array.from(
|
|
76
|
+
content.matchAll( /^\s*function ([^\(]+)/gm )
|
|
77
|
+
).reduce( ( result, [ , functionName ] ) => {
|
|
78
|
+
return result.replace(
|
|
79
|
+
new RegExp( functionName + '(?![a-zA-Z0-9_])', 'g' ),
|
|
80
|
+
( match ) => functionPrefix + match.replace( /^wp_/, '' )
|
|
81
|
+
);
|
|
82
|
+
}, content );
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if ( addActionPriority ) {
|
|
86
|
+
content = content.replace(
|
|
87
|
+
/(add_action\(\s*'init',\s*'gutenberg_register_block_[^']+'(?!,))/,
|
|
88
|
+
`$1, ${ addActionPriority }`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return content;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Find the source files a package copies to the build with `wpCopyFiles`.
|
|
97
|
+
*
|
|
98
|
+
* @param {string} packageDir Package directory.
|
|
99
|
+
* @param {CopyFilesPackageJson} packageJson Package's package.json.
|
|
100
|
+
* @return {Promise<string[]>} Absolute paths of the files to copy.
|
|
101
|
+
*/
|
|
102
|
+
export async function getCopyFiles( packageDir, packageJson ) {
|
|
103
|
+
if ( ! packageJson.wpCopyFiles ) {
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
// Glob relative to the package, so that its path is not read as a pattern.
|
|
107
|
+
return glob( packageJson.wpCopyFiles.files, {
|
|
108
|
+
cwd: packageDir,
|
|
109
|
+
absolute: true,
|
|
110
|
+
} );
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Copy one of a package's `wpCopyFiles` to the build, transforming PHP files.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} sourceFile Absolute path of the file to copy.
|
|
117
|
+
* @param {string} packageDir Package directory.
|
|
118
|
+
* @param {string} outputDir Directory the package's files are copied to.
|
|
119
|
+
* @param {CopyFilesPackageJson} packageJson Package's package.json.
|
|
120
|
+
*/
|
|
121
|
+
export async function copyPackageFile(
|
|
122
|
+
sourceFile,
|
|
123
|
+
packageDir,
|
|
124
|
+
outputDir,
|
|
125
|
+
packageJson
|
|
126
|
+
) {
|
|
127
|
+
const transforms = packageJson.wpCopyFiles?.transforms ?? {};
|
|
128
|
+
const relativePath = path.relative(
|
|
129
|
+
path.join( packageDir, 'src' ),
|
|
130
|
+
sourceFile
|
|
131
|
+
);
|
|
132
|
+
let destPath = path.join( outputDir, relativePath );
|
|
133
|
+
|
|
134
|
+
if ( ! sourceFile.endsWith( '.php' ) || ! transforms.php ) {
|
|
135
|
+
await mkdir( path.dirname( destPath ), { recursive: true } );
|
|
136
|
+
await copyFile( sourceFile, destPath );
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const content = await readFile( sourceFile, 'utf8' );
|
|
141
|
+
const transformed = transformPhpContent( content, transforms.php );
|
|
142
|
+
|
|
143
|
+
if ( transforms.php.filenameSuffix ) {
|
|
144
|
+
const ext = path.extname( destPath );
|
|
145
|
+
const base = path.basename( destPath, ext );
|
|
146
|
+
destPath = path.join(
|
|
147
|
+
path.dirname( destPath ),
|
|
148
|
+
`${ base }${ transforms.php.filenameSuffix }${ ext }`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Check if we should flatten index.php files
|
|
153
|
+
if (
|
|
154
|
+
transforms.php.flattenIndexFiles &&
|
|
155
|
+
path.basename( sourceFile ) === 'index.php'
|
|
156
|
+
) {
|
|
157
|
+
// Flatten: button/index.php → button.php
|
|
158
|
+
const parentDir = path.dirname( destPath );
|
|
159
|
+
const blockName = path.basename( parentDir );
|
|
160
|
+
destPath = path.join( path.dirname( parentDir ), `${ blockName }.php` );
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
await mkdir( path.dirname( destPath ), { recursive: true } );
|
|
164
|
+
await writeFile( destPath, transformed );
|
|
165
|
+
}
|
package/lib/package-utils.mjs
CHANGED
|
@@ -124,3 +124,17 @@ export function getPackageInfoFromFile( packageJsonPath ) {
|
|
|
124
124
|
packagePathCache.set( packageJsonPath, packageJson );
|
|
125
125
|
return packageJson;
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Interprets a configuration value as a boolean, where `"true"` and `"1"`
|
|
130
|
+
* are considered true while all other values are false.
|
|
131
|
+
*
|
|
132
|
+
* @param {string|undefined} value The configuration value to interpret.
|
|
133
|
+
* @return {boolean|undefined} Boolean interpretation of the given configuration value, or undefined if not set.
|
|
134
|
+
*/
|
|
135
|
+
export const boolConfigVal = ( value ) => {
|
|
136
|
+
if ( value === undefined ) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return [ 'true', '1' ].includes( value.toLowerCase() );
|
|
140
|
+
};
|
|
@@ -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,20 +51,19 @@
|
|
|
51
51
|
"chokidar": "^4.0.0",
|
|
52
52
|
"cssnano": "^6.0.1",
|
|
53
53
|
"fast-glob": "^3.2.7",
|
|
54
|
-
"
|
|
55
|
-
"postcss": "^8.4.38",
|
|
54
|
+
"postcss": "^8.5.33",
|
|
56
55
|
"postcss-modules": "^6.0.1",
|
|
57
56
|
"rolldown": "^1.2.15",
|
|
58
57
|
"rtlcss": "^4.3.0",
|
|
59
58
|
"sass-embedded": "^1.97.2"
|
|
60
59
|
},
|
|
61
60
|
"devDependencies": {
|
|
62
|
-
"@types/node": "^
|
|
63
|
-
"@wordpress/theme": "^
|
|
64
|
-
"vitest": "^5.
|
|
61
|
+
"@types/node": "^22.20.5",
|
|
62
|
+
"@wordpress/theme": "^5.1.0",
|
|
63
|
+
"vitest": "^5.1.1"
|
|
65
64
|
},
|
|
66
65
|
"peerDependencies": {
|
|
67
|
-
"@wordpress/theme": "^3.0.0 || ^4.0.0"
|
|
66
|
+
"@wordpress/theme": "^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
68
67
|
},
|
|
69
68
|
"peerDependenciesMeta": {
|
|
70
69
|
"@wordpress/theme": {
|
|
@@ -74,5 +73,5 @@
|
|
|
74
73
|
"publishConfig": {
|
|
75
74
|
"access": "public"
|
|
76
75
|
},
|
|
77
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "7f19202f61aeba8f4bdce892a1a0df485ad76e2b"
|
|
78
77
|
}
|
|
@@ -8,7 +8,12 @@
|
|
|
8
8
|
* @package {{PREFIX}}
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
if ( ! defined( 'ABSPATH' ) ) {
|
|
12
|
+
exit;
|
|
13
|
+
}
|
|
14
|
+
|
|
11
15
|
return array(
|
|
12
|
-
'version'
|
|
13
|
-
'build_url'
|
|
16
|
+
'version' => '{{VERSION}}',
|
|
17
|
+
'build_url' => {{BASE_URL}},
|
|
18
|
+
'has_debug_assets' => {{HAS_DEBUG_ASSETS}},
|
|
14
19
|
);
|