Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/wp-build/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Breaking Changes

- `@wordpress/build` no longer parses JSX syntax in `.js` source files. Rename these files to `.jsx` or `.tsx` before building ([#82189](https://github.com/WordPress/gutenberg/pull/82189)).

### Enhancements

- Target `@wordpress/browserslist-config` for JavaScript and CSS when the project has no Browserslist config, instead of Browserslist's implicit defaults ([#82179](https://github.com/WordPress/gutenberg/pull/82179)).
Expand Down
3 changes: 3 additions & 0 deletions packages/wp-build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Build tool for WordPress plugins.
- **PHP Generation**: Automatically generates PHP registration files for scripts, modules, and styles
- **Watch Mode**: Incremental rebuilds during development

JSX syntax must use a `.jsx` or `.tsx` file extension. Files ending in `.js`
use esbuild's standard JavaScript loader and cannot contain JSX.

## Installation

```bash
Expand Down
2 changes: 0 additions & 2 deletions packages/wp-build/lib/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,6 @@ async function transpilePackage( packageName ) {
target,
jsx: 'automatic',
jsxImportSource: 'react',
loader: { '.js': 'jsx' },
plugins,
} )
);
Expand Down Expand Up @@ -1523,7 +1522,6 @@ async function transpilePackage( packageName ) {
target,
jsx: 'automatic',
jsxImportSource: 'react',
loader: { '.js': 'jsx' },
plugins,
} )
);
Expand Down
85 changes: 85 additions & 0 deletions packages/wp-build/lib/test/jsx-extensions.js

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we don't need tests? Testing a behavior we don't support always feels a bit weird to me. Same with mentioning in README.md.

If we do keep it, can the file be .ts? #80123 gave us .tsx coverage, but we still need something similar for .js ➡️ .ts 😅

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
existsSync,
mkdirSync,
mkdtempSync,
rmSync,
writeFileSync,
} from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';

describe( 'JSX source extensions', () => {
let temporaryDirectory;

beforeEach( () => {
temporaryDirectory = mkdtempSync(
path.join( os.tmpdir(), 'wordpress-build-jsx-extensions-' )
);
} );

afterEach( () => {
rmSync( temporaryDirectory, { force: true, recursive: true } );
} );

function buildProject( sourceFilename ) {
const sourceDirectory = path.join(
temporaryDirectory,
'packages/example/src'
);
mkdirSync( sourceDirectory, { recursive: true } );

writeFileSync(
path.join( temporaryDirectory, 'package.json' ),
JSON.stringify( {
name: 'wp-build-jsx-extensions-test',
version: '1.0.0',
wpPlugin: { name: 'wp_build_jsx_extensions_test' },
} )
);
writeFileSync(
path.join( temporaryDirectory, 'packages/example/package.json' ),
JSON.stringify( {
name: '@test/example',
version: '1.0.0',
module: './build-module/index.mjs',
} )
);
writeFileSync(
path.join( sourceDirectory, sourceFilename ),
'export default <div />;\n'
);

return spawnSync(
process.execPath,
[ path.resolve( __dirname, '../build.mjs' ) ],
{
cwd: temporaryDirectory,
encoding: 'utf8',
}
);
}

it( 'builds JSX from a .jsx source file', () => {
const result = buildProject( 'index.jsx' );

expect( result.status ).toBe( 0 );
expect(
existsSync(
path.join(
temporaryDirectory,
'packages/example/build-module/index.mjs'
)
)
).toBe( true );
} );

it( 'rejects JSX syntax in a .js source file', () => {
const result = buildProject( 'index.js' );

expect( result.status ).not.toBe( 0 );
expect( `${ result.stdout }${ result.stderr }` ).toContain(
'The JSX syntax extension is not currently enabled'
);
} );
} );
2 changes: 0 additions & 2 deletions packages/wp-build/lib/worker-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ export const workerCode = ${ JSON.stringify( workerContent ) };
target,
jsx: 'automatic',
jsxImportSource: 'react',
loader: { '.js': 'jsx' },
plugins,
} )
);
Expand All @@ -356,7 +355,6 @@ export const workerCode = ${ JSON.stringify( workerContent ) };
target,
jsx: 'automatic',
jsxImportSource: 'react',
loader: { '.js': 'jsx' },
plugins,
} )
);
Expand Down
Loading