Skip to content

[repo-assist] fix(purgecss-from-jsx): parse files with import and export statements by default - #1601

Merged
Ffloriel merged 2 commits into
mainfrom
repo-assist/fix-jsx-default-module-syntax-107f97ec5c15d7a1
Nov 16, 2026
Merged

Ffloriel merged 2 commits into
mainfrom
repo-assist/fix-jsx-default-module-syntax-107f97ec5c15d7a1

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Caution

Protected files were modified in this change.
This pull request is in request-review mode and requires explicit human scrutiny before merge.

Protected files: .changeset/jsx-extractor-module-syntax.md

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

With its default options, @fullhuman/purgecss-from-jsx throws on almost every real JSX file. purgeFromJsx() parsed the code with { ecmaVersion: "latest" }, and acorn defaults that to sourceType: "script". So any file with import or export failed with:

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)

The extractor error aborts the whole purge(). The README's main example (content: ["**/*.jsx"], extractor: purgeFromJsx()) hits this as soon as a component has import React from "react". The existing tests didn't catch it because they always pass sourceType: "module" explicitly.

purgeFromTsx() mostly avoids it because TypeScript transpiles to CommonJS by default. With tsOptions: { module: ts.ModuleKind.ESNext } it fails the same way.

Fix

The default acorn options now parse the code as a script that can also use ES module syntax:

const DEFAULT_ACORN_OPTIONS: acorn.Options = {
  ecmaVersion: "latest",
  sourceType: "script",
  allowImportExportEverywhere: true,
  allowAwaitOutsideFunction: true,
};
  • ES modules parse: import, export, export * from, import.meta, dynamic import() and top-level await.
  • Scripts still parse, including require/module.exports, a hashbang and sloppy-mode code such as with or legacy octal literals.
  • I chose this over sourceType: "module" because module mode is strict mode, and sloppy-mode scripts that parse today would start failing. With this approach every file that parses today still parses, and module files now parse too.
  • Options passed to the extractor still replace the defaults, so user configurations behave as before.

Trade-offs

  • allowAwaitOutsideFunction makes await a keyword at the top level. A script that uses await as a variable name at the top level would no longer parse. I think that's very unlikely in JSX code, and it's the same restriction ES modules have.

Changes

  • packages/purgecss-from-jsx/src/index.ts: new DEFAULT_ACORN_OPTIONS, used when no options are given.
  • Tests: default-options tests for an ES module and a script in purgecss-from-jsx, and a purgeFromTsx({ tsOptions: { module: ESNext } }) test in purgecss-from-tsx. Without the fix, both ES module tests fail with the sourceType: module error.
  • READMEs of both extractors: documented the new defaults.
  • Changeset: patch for @fullhuman/purgecss-from-jsx and @fullhuman/purgecss-from-tsx.

Test Status

  • ✅ pnpm run build: passes (only the usual API Extractor warnings).
  • ✅ pnpm run lint: 0 errors. The 10 warnings were already there (tsdoc @jest-config-loader in the jest configs), and lint made no changes.
  • ✅ pnpm run test --coverage: all packages pass. purgecss 306/306, purgecss-from-jsx 14/14 (100% statements), purgecss-from-tsx 11/11, purgecss-from-html 17/17, purgecss-from-pug 69/69, postcss-purgecss 85/85, gulp 8/8, grunt 6/6, webpack 80/80, rollup 44/44.
  • ✅ Prettier on the changed TypeScript files: unchanged.
  • ✅ End-to-end check with the built packages: new PurgeCSS().purge() with purgeFromJsx() on an App.jsx containing import/export used to throw. It now returns .container{}\n#main-header{}\n and removes .unused.

🤖 Generated with [Claude Code]((claude.com/redacted)

Generated by 🌈 Repo Assist, see workflow run. Learn more.
Comment /repo-assist to run again

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@139fa09ef17cde4135e696ff8e7af09bc5ddfa6e

… by default

`purgeFromJsx()` parsed the code as a script, so a JSX file with
`import React from "react"` threw "'import' and 'export' may appear only
with 'sourceType: module'" and PurgeCSS failed. Parse the code as a script
that can have the syntax of ES modules, so that both ES modules and scripts
are parsed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
purgecss Ready Ready Preview Nov 16, 2026 11:31am UTC

Request Review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Protected files were modified in this pull request and require manual scrutiny before merge.

Please verify that each protected-file change is intentional, policy-compliant, and safe:

  • Protected files: .changeset/jsx-extractor-module-syntax.md

…that works on npm

The relative link ../purgecss-from-jsx#api resolves against the root of
the repository on npmjs.com, since the package has no repository.directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ffloriel
Ffloriel marked this pull request as ready for review November 16, 2026 11:26
@Ffloriel
Ffloriel merged commit 34ea798 into main Nov 16, 2026
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant