@wordpress/build 0.21.0 → 0.23.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/.cache/tsconfig.tsbuildinfo +1 -1
- package/CHANGELOG.md +38 -0
- package/README.md +97 -80
- package/lib/browserslist.mjs +15 -0
- package/lib/build.mjs +59 -99
- package/lib/compile-inline-style.mjs +99 -0
- package/lib/source-files.mjs +25 -0
- package/lib/test/browserslist.js +34 -0
- package/lib/test/compile-inline-style.test.js +69 -0
- package/lib/test/package-utils.js +10 -5
- package/lib/test/php-generator.js +40 -0
- package/lib/test/source-files.js +58 -0
- package/lib/test/style-injection.browser.test.js +34 -0
- package/lib/test/style-injection.jsdom.test.js +11 -0
- package/lib/widget-utils.mjs +7 -7
- package/lib/worker-build.mjs +0 -2
- package/package.json +8 -5
- package/templates/page-wp-admin.php.template +11 -3
- package/templates/page.php.template +21 -1
- package/tsconfig.json +1 -1
package/lib/build.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFile, writeFile, copyFile, mkdir, unlink } from 'fs/promises';
|
|
3
3
|
import path from 'path';
|
|
4
|
-
import { createHash } from 'node:crypto';
|
|
5
4
|
import { createRequire as createNodeRequire } from 'node:module';
|
|
6
5
|
import { parseArgs } from 'node:util';
|
|
7
6
|
import esbuild from 'esbuild';
|
|
@@ -10,7 +9,6 @@ import chokidar from 'chokidar';
|
|
|
10
9
|
import browserslistToEsbuild from 'browserslist-to-esbuild';
|
|
11
10
|
import { sassPlugin } from 'esbuild-sass-plugin';
|
|
12
11
|
import postcss from 'postcss';
|
|
13
|
-
import postcssModules from 'postcss-modules';
|
|
14
12
|
import autoprefixer from 'autoprefixer';
|
|
15
13
|
import rtlcss from 'rtlcss';
|
|
16
14
|
import cssnano from 'cssnano';
|
|
@@ -28,6 +26,8 @@ import {
|
|
|
28
26
|
renderTemplateToString,
|
|
29
27
|
} from './php-generator.mjs';
|
|
30
28
|
import { getPackageInfo, getPackageInfoFromFile } from './package-utils.mjs';
|
|
29
|
+
import { getBrowserslistQueries } from './browserslist.mjs';
|
|
30
|
+
import { getSourceFileGlob, isTestSourceFile } from './source-files.mjs';
|
|
31
31
|
import { createWordpressExternalsPlugin } from './wordpress-externals-plugin.mjs';
|
|
32
32
|
import {
|
|
33
33
|
getAllRoutes,
|
|
@@ -45,22 +45,27 @@ import {
|
|
|
45
45
|
buildWorkers,
|
|
46
46
|
generateWorkerCode,
|
|
47
47
|
} from './worker-build.mjs';
|
|
48
|
+
import {
|
|
49
|
+
compileInlineStyle,
|
|
50
|
+
dsTokenFallbacks,
|
|
51
|
+
} from './compile-inline-style.mjs';
|
|
48
52
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Resolve the ESBuild target from the project's Browserslist config.
|
|
55
|
+
*
|
|
56
|
+
* @return {string[]} ESBuild target strings.
|
|
57
|
+
*/
|
|
58
|
+
function getEsbuildTarget() {
|
|
59
|
+
return browserslistToEsbuild( getBrowserslistQueries() );
|
|
60
|
+
}
|
|
61
|
+
// Optional dependency: @wordpress/theme provides a plugin that injects fallback
|
|
62
|
+
// values for design system tokens in JavaScript. Fails gracefully when the
|
|
63
|
+
// package is not installed (it is an optional peerDependency).
|
|
53
64
|
let dsTokenFallbacksJs;
|
|
54
65
|
try {
|
|
55
|
-
const { default: postcssPlugin } = await import(
|
|
56
|
-
// eslint-disable-next-line import/no-unresolved
|
|
57
|
-
'@wordpress/theme/postcss-plugins/postcss-ds-token-fallbacks'
|
|
58
|
-
);
|
|
59
66
|
const { default: esbuildPlugin } = await import(
|
|
60
|
-
// eslint-disable-next-line import/no-unresolved
|
|
61
67
|
'@wordpress/theme/esbuild-plugins/esbuild-ds-token-fallbacks'
|
|
62
68
|
);
|
|
63
|
-
dsTokenFallbacks = postcssPlugin;
|
|
64
69
|
dsTokenFallbacksJs = esbuildPlugin;
|
|
65
70
|
} catch {
|
|
66
71
|
// @wordpress/theme is optional; skip token fallbacks if not available.
|
|
@@ -70,7 +75,6 @@ const ROOT_DIR = process.cwd();
|
|
|
70
75
|
const PACKAGES_DIR = path.join( ROOT_DIR, 'packages' );
|
|
71
76
|
const BUILD_DIR = path.join( ROOT_DIR, 'build' );
|
|
72
77
|
|
|
73
|
-
const SOURCE_EXTENSIONS = '{js,mjs,ts,tsx}';
|
|
74
78
|
const ASSET_EXTENSIONS = 'json';
|
|
75
79
|
const IGNORE_PATTERNS = [
|
|
76
80
|
'**/benchmark/**',
|
|
@@ -78,10 +82,6 @@ const IGNORE_PATTERNS = [
|
|
|
78
82
|
'**/{storybook,stories}/**',
|
|
79
83
|
'**/*.{spec,test}.*',
|
|
80
84
|
];
|
|
81
|
-
const TEST_FILE_PATTERNS = [
|
|
82
|
-
/\/(benchmark|__mocks__|__tests__|test|storybook|stories)\/.+/,
|
|
83
|
-
/\.(spec|test)\.(js|ts|tsx)$/,
|
|
84
|
-
];
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
87
|
* Get all package names from the packages directory.
|
|
@@ -107,6 +107,11 @@ const HANDLE_PREFIX = WP_PLUGIN_CONFIG.handlePrefix || PACKAGE_NAMESPACE;
|
|
|
107
107
|
const EXTERNAL_NAMESPACES = WP_PLUGIN_CONFIG.externalNamespaces || {};
|
|
108
108
|
const PAGES = WP_PLUGIN_CONFIG.pages || [];
|
|
109
109
|
|
|
110
|
+
// Capability required to view a generated page when the page config does not
|
|
111
|
+
// declare one. Pages rendering outside the menu page callback flow must
|
|
112
|
+
// enforce this themselves.
|
|
113
|
+
const DEFAULT_PAGE_CAPABILITY = 'manage_options';
|
|
114
|
+
|
|
110
115
|
/**
|
|
111
116
|
* Interprets a configuration value as a boolean, where `"true"` and `"1"`
|
|
112
117
|
* are considered true while all other values are false.
|
|
@@ -201,66 +206,6 @@ function getSassOptions( workingDir ) {
|
|
|
201
206
|
};
|
|
202
207
|
}
|
|
203
208
|
|
|
204
|
-
function compileInlineStyle( { cssModules = false, minify = true } = {} ) {
|
|
205
|
-
return async function styleType( cssText, _dirname, filePath ) {
|
|
206
|
-
let moduleExports = null;
|
|
207
|
-
|
|
208
|
-
// Transform the code: token fallbacks, CSS modules and minification.
|
|
209
|
-
const plugins = [
|
|
210
|
-
dsTokenFallbacks,
|
|
211
|
-
cssModules &&
|
|
212
|
-
postcssModules( {
|
|
213
|
-
generateScopedName: '[contenthash]__[local]',
|
|
214
|
-
getJSON: ( _, json ) => {
|
|
215
|
-
moduleExports = json;
|
|
216
|
-
},
|
|
217
|
-
} ),
|
|
218
|
-
minify &&
|
|
219
|
-
cssnano( {
|
|
220
|
-
preset: [
|
|
221
|
-
'default',
|
|
222
|
-
{ discardComments: { removeAll: true } },
|
|
223
|
-
],
|
|
224
|
-
} ),
|
|
225
|
-
].filter( Boolean );
|
|
226
|
-
|
|
227
|
-
const { css } = await postcss( plugins ).process( cssText, {
|
|
228
|
-
from: filePath,
|
|
229
|
-
map: false,
|
|
230
|
-
} );
|
|
231
|
-
|
|
232
|
-
// Hash the transformed CSS so that the dedup key reflects the actual
|
|
233
|
-
// injected content, including mangled CSS module class names.
|
|
234
|
-
const hash = createHash( 'sha1' )
|
|
235
|
-
.update( css )
|
|
236
|
-
.digest( 'hex' )
|
|
237
|
-
.slice( 0, 10 );
|
|
238
|
-
|
|
239
|
-
// Skip automatic style injection in Node-based test environments, where DOM
|
|
240
|
-
// implementations do not reliably support modern CSS features like @layer.
|
|
241
|
-
let cssModule = cssModules
|
|
242
|
-
? `import { registerStyle } from '@wordpress/style-runtime';
|
|
243
|
-
if (typeof process === 'undefined' || process.env.NODE_ENV !== 'test') {
|
|
244
|
-
registerStyle("${ hash }", ${ JSON.stringify( css ) });
|
|
245
|
-
}
|
|
246
|
-
`
|
|
247
|
-
: `if (typeof document !== 'undefined' && process.env.NODE_ENV !== 'test' && !document.head.querySelector("style[data-wp-hash='${ hash }']")) {
|
|
248
|
-
const style = document.createElement("style");
|
|
249
|
-
style.setAttribute("data-wp-hash", "${ hash }");
|
|
250
|
-
style.appendChild(document.createTextNode(${ JSON.stringify( css ) }));
|
|
251
|
-
document.head.appendChild(style);
|
|
252
|
-
}
|
|
253
|
-
`;
|
|
254
|
-
|
|
255
|
-
// The CSS modules transform produces an `exports` object with class name mappings.
|
|
256
|
-
if ( moduleExports ) {
|
|
257
|
-
const exportsString = JSON.stringify( moduleExports );
|
|
258
|
-
cssModule += `export default ${ exportsString };\n`;
|
|
259
|
-
}
|
|
260
|
-
return cssModule;
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
|
|
264
209
|
function inlineStyle( contents ) {
|
|
265
210
|
// The `contents` is already a JS code created by `compileInlineStyle`.
|
|
266
211
|
return contents;
|
|
@@ -593,7 +538,7 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
593
538
|
if ( packageJson.wpScript ) {
|
|
594
539
|
const entryPoint = resolveEntryPoint( packageDir, packageJson );
|
|
595
540
|
const outputDir = path.join( BUILD_DIR, 'scripts', packageName );
|
|
596
|
-
const target =
|
|
541
|
+
const target = getEsbuildTarget();
|
|
597
542
|
|
|
598
543
|
// Check if package matches the namespace and should expose a global
|
|
599
544
|
const packageFullName = packageJson.name;
|
|
@@ -690,7 +635,7 @@ async function bundlePackage( packageName, options = {} ) {
|
|
|
690
635
|
}
|
|
691
636
|
|
|
692
637
|
if ( packageJson.wpScriptModuleExports ) {
|
|
693
|
-
const target =
|
|
638
|
+
const target = getEsbuildTarget();
|
|
694
639
|
const rootBuildModuleDir = path.join(
|
|
695
640
|
BUILD_DIR,
|
|
696
641
|
'modules',
|
|
@@ -1293,6 +1238,7 @@ async function generatePagesPhp( pageData, replacements ) {
|
|
|
1293
1238
|
'{{PREFIX}}': prefixUnderscore,
|
|
1294
1239
|
'{{INIT_MODULES_PHP_ARRAY}}': initModulesPhp,
|
|
1295
1240
|
'{{INIT_MODULES_JSON}}': JSON.stringify( page.initModules ),
|
|
1241
|
+
'{{CAPABILITY}}': page.capability || DEFAULT_PAGE_CAPABILITY,
|
|
1296
1242
|
};
|
|
1297
1243
|
|
|
1298
1244
|
// Generate both page.php and page-wp-admin.php
|
|
@@ -1359,7 +1305,7 @@ async function transpilePackage( packageName ) {
|
|
|
1359
1305
|
);
|
|
1360
1306
|
}
|
|
1361
1307
|
|
|
1362
|
-
const srcFiles = await glob(
|
|
1308
|
+
const srcFiles = await glob( getSourceFileGlob( 'src/**/*' ), {
|
|
1363
1309
|
cwd: packageDir,
|
|
1364
1310
|
ignore: IGNORE_PATTERNS,
|
|
1365
1311
|
absolute: true,
|
|
@@ -1374,7 +1320,7 @@ async function transpilePackage( packageName ) {
|
|
|
1374
1320
|
const buildDir = path.join( packageDir, 'build' );
|
|
1375
1321
|
const buildModuleDir = path.join( packageDir, 'build-module' );
|
|
1376
1322
|
const srcDir = path.join( packageDir, 'src' );
|
|
1377
|
-
const target =
|
|
1323
|
+
const target = getEsbuildTarget();
|
|
1378
1324
|
|
|
1379
1325
|
const builds = [];
|
|
1380
1326
|
|
|
@@ -1486,7 +1432,6 @@ async function transpilePackage( packageName ) {
|
|
|
1486
1432
|
target,
|
|
1487
1433
|
jsx: 'automatic',
|
|
1488
1434
|
jsxImportSource: 'react',
|
|
1489
|
-
loader: { '.js': 'jsx' },
|
|
1490
1435
|
plugins,
|
|
1491
1436
|
} )
|
|
1492
1437
|
);
|
|
@@ -1520,7 +1465,6 @@ async function transpilePackage( packageName ) {
|
|
|
1520
1465
|
target,
|
|
1521
1466
|
jsx: 'automatic',
|
|
1522
1467
|
jsxImportSource: 'react',
|
|
1523
|
-
loader: { '.js': 'jsx' },
|
|
1524
1468
|
plugins,
|
|
1525
1469
|
} )
|
|
1526
1470
|
);
|
|
@@ -1629,7 +1573,11 @@ async function compileStyles( packageName ) {
|
|
|
1629
1573
|
const ltrResult = await postcss(
|
|
1630
1574
|
[
|
|
1631
1575
|
dsTokenFallbacks,
|
|
1632
|
-
autoprefixer( {
|
|
1576
|
+
autoprefixer( {
|
|
1577
|
+
grid: true,
|
|
1578
|
+
overrideBrowserslist:
|
|
1579
|
+
getBrowserslistQueries(),
|
|
1580
|
+
} ),
|
|
1633
1581
|
].filter( Boolean )
|
|
1634
1582
|
).process( source, { from: undefined } );
|
|
1635
1583
|
|
|
@@ -1680,7 +1628,7 @@ function isPackageSourceFile( filename ) {
|
|
|
1680
1628
|
return false;
|
|
1681
1629
|
}
|
|
1682
1630
|
|
|
1683
|
-
if (
|
|
1631
|
+
if ( isTestSourceFile( relativePath ) ) {
|
|
1684
1632
|
return false;
|
|
1685
1633
|
}
|
|
1686
1634
|
|
|
@@ -1737,7 +1685,7 @@ async function buildRoute( routeName ) {
|
|
|
1737
1685
|
|
|
1738
1686
|
// Build route.js if it exists
|
|
1739
1687
|
if ( files.hasRoute ) {
|
|
1740
|
-
const routeEntryPoints = await glob(
|
|
1688
|
+
const routeEntryPoints = await glob( getSourceFileGlob( 'route' ), {
|
|
1741
1689
|
cwd: routeDir,
|
|
1742
1690
|
absolute: true,
|
|
1743
1691
|
} );
|
|
@@ -1750,7 +1698,7 @@ async function buildRoute( routeName ) {
|
|
|
1750
1698
|
outfile: path.join( outputDir, 'route.min.js' ),
|
|
1751
1699
|
bundle: true,
|
|
1752
1700
|
format: 'esm',
|
|
1753
|
-
target:
|
|
1701
|
+
target: getEsbuildTarget(),
|
|
1754
1702
|
minify: true,
|
|
1755
1703
|
define: getDefine( false ),
|
|
1756
1704
|
plugins: [
|
|
@@ -1768,7 +1716,7 @@ async function buildRoute( routeName ) {
|
|
|
1768
1716
|
outfile: path.join( outputDir, 'route.js' ),
|
|
1769
1717
|
bundle: true,
|
|
1770
1718
|
format: 'esm',
|
|
1771
|
-
target:
|
|
1719
|
+
target: getEsbuildTarget(),
|
|
1772
1720
|
minify: false,
|
|
1773
1721
|
define: getDefine( true ),
|
|
1774
1722
|
plugins: [
|
|
@@ -1801,7 +1749,7 @@ async function buildRoute( routeName ) {
|
|
|
1801
1749
|
outfile: path.join( outputDir, 'content.min.js' ),
|
|
1802
1750
|
bundle: true,
|
|
1803
1751
|
format: 'esm',
|
|
1804
|
-
target:
|
|
1752
|
+
target: getEsbuildTarget(),
|
|
1805
1753
|
minify: true,
|
|
1806
1754
|
define: getDefine( false ),
|
|
1807
1755
|
plugins: [
|
|
@@ -1819,7 +1767,7 @@ async function buildRoute( routeName ) {
|
|
|
1819
1767
|
outfile: path.join( outputDir, 'content.js' ),
|
|
1820
1768
|
bundle: true,
|
|
1821
1769
|
format: 'esm',
|
|
1822
|
-
target:
|
|
1770
|
+
target: getEsbuildTarget(),
|
|
1823
1771
|
minify: false,
|
|
1824
1772
|
define: getDefine( true ),
|
|
1825
1773
|
plugins: [
|
|
@@ -1900,7 +1848,7 @@ async function buildWidget( widgetName ) {
|
|
|
1900
1848
|
|
|
1901
1849
|
// Build render.js if it exists
|
|
1902
1850
|
if ( files.hasRender ) {
|
|
1903
|
-
const renderEntryPoints = await glob(
|
|
1851
|
+
const renderEntryPoints = await glob( getSourceFileGlob( 'render' ), {
|
|
1904
1852
|
cwd: widgetDir,
|
|
1905
1853
|
absolute: true,
|
|
1906
1854
|
} );
|
|
@@ -1913,7 +1861,7 @@ async function buildWidget( widgetName ) {
|
|
|
1913
1861
|
outfile: path.join( outputDir, 'render.min.js' ),
|
|
1914
1862
|
bundle: true,
|
|
1915
1863
|
format: 'esm',
|
|
1916
|
-
target:
|
|
1864
|
+
target: getEsbuildTarget(),
|
|
1917
1865
|
minify: true,
|
|
1918
1866
|
define: getDefine( false ),
|
|
1919
1867
|
plugins: [
|
|
@@ -1931,7 +1879,7 @@ async function buildWidget( widgetName ) {
|
|
|
1931
1879
|
outfile: path.join( outputDir, 'render.js' ),
|
|
1932
1880
|
bundle: true,
|
|
1933
1881
|
format: 'esm',
|
|
1934
|
-
target:
|
|
1882
|
+
target: getEsbuildTarget(),
|
|
1935
1883
|
minify: false,
|
|
1936
1884
|
define: getDefine( true ),
|
|
1937
1885
|
plugins: [
|
|
@@ -1950,7 +1898,7 @@ async function buildWidget( widgetName ) {
|
|
|
1950
1898
|
|
|
1951
1899
|
// Build widget.js if it exists
|
|
1952
1900
|
if ( files.hasWidget ) {
|
|
1953
|
-
const widgetEntryPoints = await glob(
|
|
1901
|
+
const widgetEntryPoints = await glob( getSourceFileGlob( 'widget' ), {
|
|
1954
1902
|
cwd: widgetDir,
|
|
1955
1903
|
absolute: true,
|
|
1956
1904
|
} );
|
|
@@ -1963,7 +1911,7 @@ async function buildWidget( widgetName ) {
|
|
|
1963
1911
|
outfile: path.join( outputDir, 'widget.min.js' ),
|
|
1964
1912
|
bundle: true,
|
|
1965
1913
|
format: 'esm',
|
|
1966
|
-
target:
|
|
1914
|
+
target: getEsbuildTarget(),
|
|
1967
1915
|
minify: true,
|
|
1968
1916
|
define: getDefine( false ),
|
|
1969
1917
|
plugins: [
|
|
@@ -1981,7 +1929,7 @@ async function buildWidget( widgetName ) {
|
|
|
1981
1929
|
outfile: path.join( outputDir, 'widget.js' ),
|
|
1982
1930
|
bundle: true,
|
|
1983
1931
|
format: 'esm',
|
|
1984
|
-
target:
|
|
1932
|
+
target: getEsbuildTarget(),
|
|
1985
1933
|
minify: false,
|
|
1986
1934
|
define: getDefine( true ),
|
|
1987
1935
|
plugins: [
|
|
@@ -2347,13 +2295,19 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2347
2295
|
// Normalize PAGES config to support both string and object formats
|
|
2348
2296
|
const normalizedPages = PAGES.map( ( page ) => {
|
|
2349
2297
|
if ( typeof page === 'string' ) {
|
|
2350
|
-
return {
|
|
2298
|
+
return {
|
|
2299
|
+
id: page,
|
|
2300
|
+
init: [],
|
|
2301
|
+
title: undefined,
|
|
2302
|
+
capability: DEFAULT_PAGE_CAPABILITY,
|
|
2303
|
+
};
|
|
2351
2304
|
}
|
|
2352
2305
|
return {
|
|
2353
2306
|
id: page.id,
|
|
2354
2307
|
init: page.init || [],
|
|
2355
2308
|
title: page.title || undefined,
|
|
2356
2309
|
experimental: page.experimental || false,
|
|
2310
|
+
capability: page.capability || DEFAULT_PAGE_CAPABILITY,
|
|
2357
2311
|
};
|
|
2358
2312
|
} );
|
|
2359
2313
|
|
|
@@ -2401,7 +2355,12 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2401
2355
|
path: metadata.path,
|
|
2402
2356
|
page,
|
|
2403
2357
|
hasRoute: routeFiles.hasRoute,
|
|
2404
|
-
|
|
2358
|
+
// Must match the condition that builds `content.js`, which
|
|
2359
|
+
// bundles the canvas alongside the stage and inspector.
|
|
2360
|
+
hasContent:
|
|
2361
|
+
routeFiles.hasStage ||
|
|
2362
|
+
routeFiles.hasInspector ||
|
|
2363
|
+
routeFiles.hasCanvas,
|
|
2405
2364
|
};
|
|
2406
2365
|
} );
|
|
2407
2366
|
} );
|
|
@@ -2416,6 +2375,7 @@ async function buildAll( baseUrlExpression ) {
|
|
|
2416
2375
|
routes: pageRoutes,
|
|
2417
2376
|
initModules: page.init,
|
|
2418
2377
|
title: page.title,
|
|
2378
|
+
capability: page.capability,
|
|
2419
2379
|
};
|
|
2420
2380
|
} );
|
|
2421
2381
|
|
|
@@ -2624,7 +2584,7 @@ async function watchMode() {
|
|
|
2624
2584
|
const watcher = chokidar.watch( watchPaths, {
|
|
2625
2585
|
ignored: [
|
|
2626
2586
|
'**/{__mocks__,__tests__,test,storybook,stories}/**',
|
|
2627
|
-
'**/*.{spec,test}
|
|
2587
|
+
getSourceFileGlob( '**/*.{spec,test}' ),
|
|
2628
2588
|
// Avoid rebuild loops: worker packages write bundled WASM/JS back
|
|
2629
2589
|
// into src/worker-code.ts during each build (e.g. @wordpress/vips).
|
|
2630
2590
|
'**/worker-code.ts',
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import cssnano from 'cssnano';
|
|
3
|
+
import postcss from 'postcss';
|
|
4
|
+
import postcssModules from 'postcss-modules';
|
|
5
|
+
|
|
6
|
+
/** @type {import('postcss').AcceptedPlugin | undefined} */
|
|
7
|
+
let dsTokenFallbacks;
|
|
8
|
+
try {
|
|
9
|
+
const { default: postcssPlugin } = await import(
|
|
10
|
+
'@wordpress/theme/postcss-plugins/postcss-ds-token-fallbacks'
|
|
11
|
+
);
|
|
12
|
+
dsTokenFallbacks = postcssPlugin;
|
|
13
|
+
} catch {
|
|
14
|
+
// @wordpress/theme is optional; skip token fallbacks if not available.
|
|
15
|
+
}
|
|
16
|
+
export { dsTokenFallbacks };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Compile CSS into the JavaScript module emitted by wp-build.
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} options
|
|
22
|
+
* @param {boolean} [options.cssModules=false] Whether to emit CSS Module exports.
|
|
23
|
+
* @param {boolean} [options.minify=true] Whether to minify the CSS.
|
|
24
|
+
* @return {Function} esbuild-sass-plugin transform callback.
|
|
25
|
+
*/
|
|
26
|
+
export function compileInlineStyle( {
|
|
27
|
+
cssModules = false,
|
|
28
|
+
minify = true,
|
|
29
|
+
} = {} ) {
|
|
30
|
+
/**
|
|
31
|
+
* @param {string} cssText CSS source to compile.
|
|
32
|
+
* @param {string} _dirname Directory containing the source file.
|
|
33
|
+
* @param {string} filePath Source file path.
|
|
34
|
+
* @return {Promise<string>} Compiled JavaScript module.
|
|
35
|
+
*/
|
|
36
|
+
return async function styleType( cssText, _dirname, filePath ) {
|
|
37
|
+
let moduleExports = null;
|
|
38
|
+
|
|
39
|
+
const plugins = dsTokenFallbacks ? [ dsTokenFallbacks ] : [];
|
|
40
|
+
if ( cssModules ) {
|
|
41
|
+
plugins.push(
|
|
42
|
+
postcssModules( {
|
|
43
|
+
generateScopedName: '[contenthash]__[local]',
|
|
44
|
+
getJSON: ( _, json ) => {
|
|
45
|
+
moduleExports = json;
|
|
46
|
+
},
|
|
47
|
+
} )
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if ( minify ) {
|
|
51
|
+
plugins.push(
|
|
52
|
+
cssnano( {
|
|
53
|
+
preset: [
|
|
54
|
+
'default',
|
|
55
|
+
{ discardComments: { removeAll: true } },
|
|
56
|
+
],
|
|
57
|
+
} )
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const { css } = await postcss( plugins ).process( cssText, {
|
|
62
|
+
from: filePath,
|
|
63
|
+
map: false,
|
|
64
|
+
} );
|
|
65
|
+
|
|
66
|
+
// Hash the transformed CSS so that the dedup key reflects the actual
|
|
67
|
+
// injected content, including mangled CSS module class names.
|
|
68
|
+
const hash = createHash( 'sha1' )
|
|
69
|
+
.update( css )
|
|
70
|
+
.digest( 'hex' )
|
|
71
|
+
.slice( 0, 10 );
|
|
72
|
+
const shouldInjectStyle =
|
|
73
|
+
`typeof process === 'undefined' || ` +
|
|
74
|
+
`process.env.NODE_ENV !== 'test'`;
|
|
75
|
+
|
|
76
|
+
// Skip automatic style injection in Node-based test environments, where DOM
|
|
77
|
+
// implementations do not reliably support modern CSS features like @layer.
|
|
78
|
+
let cssModule = cssModules
|
|
79
|
+
? `import { registerStyle } from '@wordpress/style-runtime';
|
|
80
|
+
if (${ shouldInjectStyle }) {
|
|
81
|
+
registerStyle("${ hash }", ${ JSON.stringify( css ) });
|
|
82
|
+
}
|
|
83
|
+
`
|
|
84
|
+
: `if (typeof document !== 'undefined' && (${ shouldInjectStyle }) && !document.head.querySelector("style[data-wp-hash='${ hash }']")) {
|
|
85
|
+
const style = document.createElement("style");
|
|
86
|
+
style.setAttribute("data-wp-hash", "${ hash }");
|
|
87
|
+
style.appendChild(document.createTextNode(${ JSON.stringify( css ) }));
|
|
88
|
+
document.head.appendChild(style);
|
|
89
|
+
}
|
|
90
|
+
`;
|
|
91
|
+
|
|
92
|
+
// The CSS modules transform produces an `exports` object with class name mappings.
|
|
93
|
+
if ( moduleExports ) {
|
|
94
|
+
const exportsString = JSON.stringify( moduleExports );
|
|
95
|
+
cssModule += `export default ${ exportsString };\n`;
|
|
96
|
+
}
|
|
97
|
+
return cssModule;
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
const SOURCE_EXTENSIONS = [ 'js', 'jsx', 'mjs', 'ts', 'tsx' ];
|
|
2
|
+
const TEST_FILE_PATTERNS = [
|
|
3
|
+
/\/(benchmark|__mocks__|__tests__|test|storybook|stories)\/.+/,
|
|
4
|
+
new RegExp( `\\.(spec|test)\\.(${ SOURCE_EXTENSIONS.join( '|' ) })$` ),
|
|
5
|
+
];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Add every supported source extension to a file glob.
|
|
9
|
+
*
|
|
10
|
+
* @param {string} basePattern File glob without an extension.
|
|
11
|
+
* @return {string} File glob with all supported source extensions.
|
|
12
|
+
*/
|
|
13
|
+
export function getSourceFileGlob( basePattern ) {
|
|
14
|
+
return `${ basePattern }.{${ SOURCE_EXTENSIONS.join( ',' ) }}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Determine whether a source path belongs to tests or development fixtures.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} relativePath Repository-relative source path.
|
|
21
|
+
* @return {boolean} True when the path should be excluded from production source.
|
|
22
|
+
*/
|
|
23
|
+
export function isTestSourceFile( relativePath ) {
|
|
24
|
+
return TEST_FILE_PATTERNS.some( ( regex ) => regex.test( relativePath ) );
|
|
25
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import browserslist from 'browserslist';
|
|
3
|
+
import wordpressBrowserslistConfig from '@wordpress/browserslist-config';
|
|
4
|
+
import { getBrowserslistQueries } from '../browserslist.mjs';
|
|
5
|
+
|
|
6
|
+
describe( 'browserslist targeting', () => {
|
|
7
|
+
let findConfigSpy;
|
|
8
|
+
|
|
9
|
+
afterEach( () => {
|
|
10
|
+
findConfigSpy.mockRestore();
|
|
11
|
+
} );
|
|
12
|
+
|
|
13
|
+
it( 'falls back to @wordpress/browserslist-config when the project has no config', () => {
|
|
14
|
+
findConfigSpy = vi
|
|
15
|
+
.spyOn( browserslist, 'findConfig' )
|
|
16
|
+
.mockReturnValue( undefined );
|
|
17
|
+
|
|
18
|
+
expect( getBrowserslistQueries() ).toEqual(
|
|
19
|
+
wordpressBrowserslistConfig
|
|
20
|
+
);
|
|
21
|
+
} );
|
|
22
|
+
|
|
23
|
+
it( 'uses the project browserslist config when one is present', () => {
|
|
24
|
+
findConfigSpy = vi
|
|
25
|
+
.spyOn( browserslist, 'findConfig' )
|
|
26
|
+
.mockReturnValue( {
|
|
27
|
+
defaults: [ 'last 1 chrome version' ],
|
|
28
|
+
} );
|
|
29
|
+
|
|
30
|
+
expect( getBrowserslistQueries() ).toEqual( [
|
|
31
|
+
'last 1 chrome version',
|
|
32
|
+
] );
|
|
33
|
+
} );
|
|
34
|
+
} );
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { compileInlineStyle } from '../compile-inline-style.mjs';
|
|
3
|
+
|
|
4
|
+
async function compileStyleModule() {
|
|
5
|
+
return compileInlineStyle( { minify: false } )(
|
|
6
|
+
'.fixture { color: rgb(1, 2, 3); }',
|
|
7
|
+
import.meta.dirname,
|
|
8
|
+
`${ import.meta.dirname }/fixture.css`
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function executeStyleModule( source, processValue ) {
|
|
13
|
+
let injectedStyleCount = 0;
|
|
14
|
+
const document = {
|
|
15
|
+
createElement: () => ( {
|
|
16
|
+
appendChild() {},
|
|
17
|
+
setAttribute() {},
|
|
18
|
+
} ),
|
|
19
|
+
createTextNode: () => ( {} ),
|
|
20
|
+
head: {
|
|
21
|
+
appendChild: () => {
|
|
22
|
+
injectedStyleCount++;
|
|
23
|
+
},
|
|
24
|
+
querySelector: () => null,
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
new Function( 'document', 'process', source )( document, processValue );
|
|
29
|
+
|
|
30
|
+
return injectedStyleCount;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe( 'compileInlineStyle', () => {
|
|
34
|
+
test( 'applies design token fallbacks by default', async () => {
|
|
35
|
+
const source = await compileInlineStyle( { minify: false } )(
|
|
36
|
+
'.fixture { gap: var(--wpds-dimension-gap-sm); }',
|
|
37
|
+
import.meta.dirname,
|
|
38
|
+
`${ import.meta.dirname }/fixture.css`
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
expect( source ).toContain( 'var(--wpds-dimension-gap-sm, 8px)' );
|
|
42
|
+
} );
|
|
43
|
+
|
|
44
|
+
test( 'skips generated style injection in test mode', async () => {
|
|
45
|
+
const source = await compileStyleModule();
|
|
46
|
+
|
|
47
|
+
expect(
|
|
48
|
+
executeStyleModule( source, {
|
|
49
|
+
env: { NODE_ENV: 'test' },
|
|
50
|
+
} )
|
|
51
|
+
).toBe( 0 );
|
|
52
|
+
} );
|
|
53
|
+
|
|
54
|
+
test( 'injects generated styles outside test mode', async () => {
|
|
55
|
+
const source = await compileStyleModule();
|
|
56
|
+
|
|
57
|
+
expect(
|
|
58
|
+
executeStyleModule( source, {
|
|
59
|
+
env: { NODE_ENV: 'development' },
|
|
60
|
+
} )
|
|
61
|
+
).toBe( 1 );
|
|
62
|
+
} );
|
|
63
|
+
|
|
64
|
+
test( 'injects generated styles when process is absent', async () => {
|
|
65
|
+
const source = await compileStyleModule();
|
|
66
|
+
|
|
67
|
+
expect( executeStyleModule( source, undefined ) ).toBe( 1 );
|
|
68
|
+
} );
|
|
69
|
+
} );
|
|
@@ -1,26 +1,31 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
1
4
|
import { getPackageInfo } from '../package-utils.mjs';
|
|
2
5
|
|
|
6
|
+
const testDirectory = path.dirname( fileURLToPath( import.meta.url ) );
|
|
7
|
+
|
|
3
8
|
describe( 'getPackageInfo() resolve-miss contract', () => {
|
|
4
9
|
it( 'returns null (does not throw) for an unresolvable scoped package', () => {
|
|
5
10
|
expect( () =>
|
|
6
|
-
getPackageInfo( '@does-not-exist/nope',
|
|
11
|
+
getPackageInfo( '@does-not-exist/nope', testDirectory )
|
|
7
12
|
).not.toThrow();
|
|
8
13
|
|
|
9
14
|
expect(
|
|
10
|
-
getPackageInfo( '@does-not-exist/nope',
|
|
15
|
+
getPackageInfo( '@does-not-exist/nope', testDirectory )
|
|
11
16
|
).toBeNull();
|
|
12
17
|
} );
|
|
13
18
|
|
|
14
19
|
it( 'returns the cached null on a repeated miss', () => {
|
|
15
|
-
const first = getPackageInfo( '@still-missing/pkg',
|
|
16
|
-
const second = getPackageInfo( '@still-missing/pkg',
|
|
20
|
+
const first = getPackageInfo( '@still-missing/pkg', testDirectory );
|
|
21
|
+
const second = getPackageInfo( '@still-missing/pkg', testDirectory );
|
|
17
22
|
|
|
18
23
|
expect( first ).toBeNull();
|
|
19
24
|
expect( second ).toBeNull();
|
|
20
25
|
} );
|
|
21
26
|
|
|
22
27
|
it( 'still resolves an installed package that exposes its package.json', () => {
|
|
23
|
-
const info = getPackageInfo( '@wordpress/build',
|
|
28
|
+
const info = getPackageInfo( '@wordpress/build', testDirectory );
|
|
24
29
|
|
|
25
30
|
expect( info ).toMatchObject( { name: '@wordpress/build' } );
|
|
26
31
|
} );
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { renderTemplateToString as generateTemplate } from '../php-generator.mjs';
|
|
3
|
+
|
|
4
|
+
describe( 'page-wp-admin.php template', () => {
|
|
5
|
+
it( 'retains the Core Boot compatibility class on the mount element', async () => {
|
|
6
|
+
const generatedPage = await generateTemplate(
|
|
7
|
+
'page-wp-admin.php.template',
|
|
8
|
+
{
|
|
9
|
+
'{{PAGE_SLUG}}': 'example',
|
|
10
|
+
}
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
expect( generatedPage ).toContain(
|
|
14
|
+
'<div id="example-wp-admin-app" class="boot-layout-container"></div>'
|
|
15
|
+
);
|
|
16
|
+
} );
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
describe( 'page.php template', () => {
|
|
20
|
+
it( 'guards the admin_init interceptor with authentication and the configured capability', async () => {
|
|
21
|
+
const generatedPage = await generateTemplate( 'page.php.template', {
|
|
22
|
+
'{{PAGE_SLUG}}': 'example',
|
|
23
|
+
'{{PAGE_SLUG_UNDERSCORE}}': 'example',
|
|
24
|
+
'{{PREFIX}}': 'wp',
|
|
25
|
+
'{{CAPABILITY}}': 'edit_theme_options',
|
|
26
|
+
} );
|
|
27
|
+
|
|
28
|
+
const interceptor = generatedPage.slice(
|
|
29
|
+
generatedPage.indexOf( 'function wp_example_intercept_render()' )
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
expect( interceptor ).toContain( 'is_user_logged_in()' );
|
|
33
|
+
expect( interceptor ).toContain(
|
|
34
|
+
"current_user_can( 'edit_theme_options' )"
|
|
35
|
+
);
|
|
36
|
+
expect( interceptor.indexOf( 'current_user_can' ) ).toBeLessThan(
|
|
37
|
+
interceptor.indexOf( 'wp_example_render_page()' )
|
|
38
|
+
);
|
|
39
|
+
} );
|
|
40
|
+
} );
|