Repository navigation
[repo-assist] fix(purgecss-from-jsx): parse files with import and export statements by default - #1601
Merged
Ffloriel merged 2 commits intoNov 16, 2026
Conversation
… 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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Author
There was a problem hiding this comment.
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
20 tasks
…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
marked this pull request as ready for review
November 16, 2026 11:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Caution
Protected files were modified in this change.
This pull request is in
request-reviewmode 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-jsxthrows on almost every real JSX file.purgeFromJsx()parsed the code with{ ecmaVersion: "latest" }, and acorn defaults that tosourceType: "script". So any file withimportorexportfailed with:The extractor error aborts the whole
purge(). The README's main example (content: ["**/*.jsx"],extractor: purgeFromJsx()) hits this as soon as a component hasimport React from "react". The existing tests didn't catch it because they always passsourceType: "module"explicitly.purgeFromTsx()mostly avoids it because TypeScript transpiles to CommonJS by default. WithtsOptions: { 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:
import,export,export * from,import.meta, dynamicimport()and top-levelawait.require/module.exports, a hashbang and sloppy-mode code such aswithor legacy octal literals.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.Trade-offs
allowAwaitOutsideFunctionmakesawaita keyword at the top level. A script that usesawaitas 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: newDEFAULT_ACORN_OPTIONS, used when no options are given.purgecss-from-jsx, and apurgeFromTsx({ tsOptions: { module: ESNext } })test inpurgecss-from-tsx. Without the fix, both ES module tests fail with thesourceType: moduleerror.@fullhuman/purgecss-from-jsxand@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-loaderin 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.new PurgeCSS().purge()withpurgeFromJsx()on anApp.jsxcontainingimport/exportused to throw. It now returns.container{}\n#main-header{}\nand removes.unused.🤖 Generated with [Claude Code]((claude.com/redacted)
Add this agentic workflow to your repo
To install this agentic workflow, run