Unverified Commit 07084d48 authored by Dave Cardwell's avatar Dave Cardwell Committed by GitHub

[Experimental] Only consider files within plugins (#12943)

I have a plugin with a src folder structure following jest’s recommended practice of including a `__tests__` folder:

```
$ find src/
src/
src/document-html-props-server.ts
src/__tests__
src/__tests__/document-html-props-server.unit.ts
```

collect-plugins.ts [reads the src directory](https://github.com/zeit/next.js/blob/960c18da53af89115c7e96dc8188646a51c53c15/packages/next/build/plugins/collect-plugins.ts#L65) and finds:
```js
[ '__tests__', 'document-html-props-server.ts' ]
```

…which it then [converts to](https://github.com/zeit/next.js/blob/960c18da53af89115c7e96dc8188646a51c53c15/packages/next/build/plugins/collect-plugins.ts#L76-L80):
```js
[ '', 'document-html-props-server' ]
```

…and finally [outputs](https://github.com/zeit/next.js/blob/960c18da53af89115c7e96dc8188646a51c53c15/packages/next/build/plugins/collect-plugins.ts#L90-L96):
```
Next.js Plugin: {my plugin} listed invalid middleware
```

This pull request makes collect-plugins.ts only consider files, so my __tests__ directory and its contents are ignored.
parent 4ba2b787
...@@ -62,7 +62,11 @@ async function collectPluginMeta( ...@@ -62,7 +62,11 @@ async function collectPluginMeta(
// TODO: add err.sh explaining requirements // TODO: add err.sh explaining requirements
let middleware: string[] = [] let middleware: string[] = []
try { try {
middleware = await promises.readdir(path.join(pkgDir, 'src')) middleware = (
await promises.readdir(path.join(pkgDir, 'src'), { withFileTypes: true })
)
.filter((dirent) => dirent.isFile())
.map((file) => file.name)
} catch (err) { } catch (err) {
if (err.code !== 'ENOENT') { if (err.code !== 'ENOENT') {
console.error(err) console.error(err)
......
...@@ -18,6 +18,7 @@ jest.setTimeout(1000 * 60 * 2) ...@@ -18,6 +18,7 @@ jest.setTimeout(1000 * 60 * 2)
let app let app
let appPort let appPort
let stdout let stdout
let stderr
const appDir = join(__dirname, '../app') const appDir = join(__dirname, '../app')
const nextConfigPath = join(appDir, 'next.config.js') const nextConfigPath = join(appDir, 'next.config.js')
...@@ -64,6 +65,10 @@ function runTests() { ...@@ -64,6 +65,10 @@ function runTests() {
expect(stdout).toMatch(/loaded plugin: @zeit\/next-plugin-scope/i) expect(stdout).toMatch(/loaded plugin: @zeit\/next-plugin-scope/i)
expect(stdout).toMatch(/loaded plugin: next-plugin-normal/i) expect(stdout).toMatch(/loaded plugin: next-plugin-normal/i)
}) })
it('should ignore directories in plugins', async () => {
expect(stderr).not.toMatch(/listed invalid middleware/i)
})
} }
describe('Next.js plugins', () => { describe('Next.js plugins', () => {
...@@ -78,6 +83,9 @@ describe('Next.js plugins', () => { ...@@ -78,6 +83,9 @@ describe('Next.js plugins', () => {
onStdout(msg) { onStdout(msg) {
stdout += msg stdout += msg
}, },
onStderr(msg) {
stderr += msg
},
}) })
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -107,10 +115,14 @@ describe('Next.js plugins', () => { ...@@ -107,10 +115,14 @@ describe('Next.js plugins', () => {
) )
appPort = await findPort() appPort = await findPort()
stdout = '' stdout = ''
stderr = ''
app = await launchApp(appDir, appPort, { app = await launchApp(appDir, appPort, {
onStdout(msg) { onStdout(msg) {
stdout += msg stdout += msg
}, },
onStderr(msg) {
stderr += msg
},
}) })
}) })
afterAll(() => killApp(app)) afterAll(() => killApp(app))
...@@ -136,8 +148,10 @@ describe('Next.js plugins', () => { ...@@ -136,8 +148,10 @@ describe('Next.js plugins', () => {
) )
const results = await nextBuild(appDir, undefined, { const results = await nextBuild(appDir, undefined, {
stdout: true, stdout: true,
stderr: true,
}) })
stdout = results.stdout stdout = results.stdout
stderr = results.stderr
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
}) })
...@@ -157,8 +171,10 @@ describe('Next.js plugins', () => { ...@@ -157,8 +171,10 @@ describe('Next.js plugins', () => {
) )
const results = await nextBuild(appDir, undefined, { const results = await nextBuild(appDir, undefined, {
stdout: true, stdout: true,
stderr: true,
}) })
stdout = results.stdout stdout = results.stdout
stderr = results.stderr
appPort = await findPort() appPort = await findPort()
app = await nextStart(appDir, appPort) app = await nextStart(appDir, appPort)
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment