-
Notifications
You must be signed in to change notification settings - Fork 45
feat: add --distDir arg to specify built plugin directory #2558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jackw
wants to merge
4
commits into
main
Choose a base branch
from
jackw/react-detect-dist-dir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2fd4a16
feat(react-detect): use distDir directly in findSourceMapFiles withou…
jackw b69173d
feat(react-detect): remove /dist append from getPluginJson, accept di…
jackw 3372273
feat(react-detect): add --distDir arg to decouple built output path f…
jackw 457d1f1
fix(react-detect): remove plugin.json cache, thread pluginRoot throug…
jackw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { findSourceMapFiles } from './file-scanner.js'; | ||
| import { mkdtempSync, writeFileSync, mkdirSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| describe('findSourceMapFiles', () => { | ||
| it('searches the given directory directly without appending a dist subdirectory', async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'react-detect-test-')); | ||
| writeFileSync(join(dir, 'module.js.map'), '{}'); | ||
|
|
||
| const files = await findSourceMapFiles(dir); | ||
|
|
||
| expect(files).toHaveLength(1); | ||
| expect(files[0]).toContain('module.js.map'); | ||
| }); | ||
|
|
||
| it('finds source map files recursively within the given directory', async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'react-detect-test-')); | ||
| mkdirSync(join(dir, 'nested')); | ||
| writeFileSync(join(dir, 'a.js.map'), '{}'); | ||
| writeFileSync(join(dir, 'nested', 'b.js.map'), '{}'); | ||
|
|
||
| const files = await findSourceMapFiles(dir); | ||
|
|
||
| expect(files).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('returns empty array when no source map files exist', async () => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'react-detect-test-')); | ||
|
|
||
| const files = await findSourceMapFiles(dir); | ||
|
|
||
| expect(files).toHaveLength(0); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,16 @@ | ||
| import fg from 'fast-glob'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| export async function findSourceMapFiles(directory: string): Promise<string[]> { | ||
| const distDirectory = join(directory, 'dist'); | ||
|
|
||
| export async function findSourceMapFiles(distDir: string): Promise<string[]> { | ||
| try { | ||
| const files = await fg('**/*.js.map', { | ||
| cwd: distDirectory, | ||
| cwd: distDir, | ||
| absolute: true, | ||
| ignore: ['**/node_modules/**'], | ||
| }); | ||
| return files; | ||
| } catch (error) { | ||
| throw new Error( | ||
| `Error finding source map files in ${distDirectory}: ${error instanceof Error ? error.message : 'Unknown error'}` | ||
| `Error finding source map files in "${distDir}": ${error instanceof Error ? error.message : 'Unknown error'}` | ||
| ); | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { mkdtempSync, mkdirSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { getPluginJson, hasExternalisedJsxRuntime } from './plugin.js'; | ||
|
|
||
| describe('getPluginJson', () => { | ||
| it('reads plugin.json from the given distDir directly without appending /dist', () => { | ||
| const distDir = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
| const pluginJson = { id: 'my-plugin', name: 'My Plugin', type: 'app', info: { version: '2.0.0' } }; | ||
| writeFileSync(join(distDir, 'plugin.json'), JSON.stringify(pluginJson)); | ||
|
|
||
| const result = getPluginJson(distDir); | ||
|
|
||
| expect(result?.id).toBe('my-plugin'); | ||
| expect(result?.info.version).toBe('2.0.0'); | ||
| }); | ||
|
|
||
| it('returns correct data for each distDir when called with different directories', () => { | ||
| const distDir1 = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
| writeFileSync( | ||
| join(distDir1, 'plugin.json'), | ||
| JSON.stringify({ id: 'plugin-1', name: 'P1', type: 'app', info: { version: '1.0.0' } }) | ||
| ); | ||
| const distDir2 = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
| writeFileSync( | ||
| join(distDir2, 'plugin.json'), | ||
| JSON.stringify({ id: 'plugin-2', name: 'P2', type: 'panel', info: { version: '2.0.0' } }) | ||
| ); | ||
|
|
||
| const result1 = getPluginJson(distDir1); | ||
| const result2 = getPluginJson(distDir2); | ||
|
|
||
| expect(result1?.id).toBe('plugin-1'); | ||
| expect(result2?.id).toBe('plugin-2'); | ||
| }); | ||
|
|
||
| it('throws when plugin.json does not exist in the given distDir', () => { | ||
| const distDir = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
|
|
||
| expect(() => getPluginJson(distDir)).toThrow('plugin.json'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasExternalisedJsxRuntime', () => { | ||
| it('detects react/jsx-runtime in webpack externals using the given pluginRoot', () => { | ||
| const pluginRoot = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
| writeFileSync(join(pluginRoot, 'webpack.config.ts'), `module.exports = { externals: ['react/jsx-runtime'] };`); | ||
|
|
||
| expect(hasExternalisedJsxRuntime(pluginRoot)).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false when the given pluginRoot has no matching webpack config', () => { | ||
| const pluginRoot = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
|
|
||
| expect(hasExternalisedJsxRuntime(pluginRoot)).toBe(false); | ||
| }); | ||
|
|
||
| it('detects react/jsx-runtime in bundler externals using the given pluginRoot', () => { | ||
| const pluginRoot = mkdtempSync(join(tmpdir(), 'react-detect-plugin-test-')); | ||
| mkdirSync(join(pluginRoot, '.config', 'bundler'), { recursive: true }); | ||
| writeFileSync(join(pluginRoot, '.config', 'bundler', 'externals.ts'), `const externals = ['react/jsx-runtime'];`); | ||
|
|
||
| expect(hasExternalisedJsxRuntime(pluginRoot)).toBe(true); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generateAnalysisResultsnow takes onlydistDir, but other parts of result generation still rely onprocess.cwd()(e.g., build-tooling checks viahasExternalisedJsxRuntime()and reported file paths). This means running the CLI from outside the plugin root (a key--distDiruse case) can yield incorrect build-tooling detection and misleading file locations. Consider threading apluginRoot/baseDirthrough results generation (separate fromdistDir) and using that instead ofprocess.cwd()for config/file path resolution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
generateAnalysisResultsnow acceptspluginRootas a second argument (afterdistDir). It is threaded through tohasExternalisedJsxRuntime(pluginRoot),generateResult(m, pluginRoot), andbuildDependencyIssues(..., pluginRoot)— replacing allprocess.cwd()calls in the results path.