Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Doesn't detect any unused files even though there are several #1

Closed
Vadorequest opened this issue Jun 5, 2020 · 11 comments
Closed
Labels
bug Something isn't working

Comments

@Vadorequest
Copy link

Vadorequest commented Jun 5, 2020

I just tried the lib and it doesn't work. I'm using TS and .tsx files m(aybe that's related?)

Commit: UnlyEd/next-right-now@16da46e
Branch: https://github.com/UnlyEd/next-right-now/tree/analyse

 yarn analyze:unused
yarn run v1.22.4
$ next-unused
[debug] Found include config: src/components,src/gql,src/hocs,src/hooks,src/middlewares,src/mocks,src/pages,src/propTypes,src/stores,src/types,src/utils
[debug] Found entrypoints config: src/components,src/pages
[debug] Using exclude regex: /^(?!(src\/components|src\/gql|src\/hocs|src\/hooks|src\/middlewares|src\/mocks|src\/pages|src\/propTypes|src\/stores|src\/types|src\/utils))/i
[debug] Found entrypoints
[]
No unused files!
✨  Done in 43.10s.

Reproduce

  1. git clone https://github.com/UnlyEd/next-right-now.git nrn-quick-start
  2. cd nrn-quick-start && git checkout analyse
  3. yarn
  4. yarn analyze:unused

Unused files:

  • components/ComponentTemplate.tsx
  • utils/mobile.ts

Amongst many others

@pacocoursey
Copy link
Owner

Thanks for the reproduction @Vadorequest 🙏

I'm debugging to make this package more reliable. Just ran on your repo and found these files:

Found 26 unused files:
src/components/ComponentTemplate.tsx
src/components/assets/GraphCMSAsset.test.tsx
src/gql/fragments/favicon.ts
src/hocs/withHOCTemplate.tsx
src/hooks/useHasMounted.tsx
src/types/SerializedRecord.ts
src/types/nextjs/AppInitialProps.ts
src/types/nextjs/GetStaticPropsContext.ts
src/utils/api/fetchJSON.test.ts
src/utils/api/fetchJSON.ts
src/utils/assets/logo.test.ts
src/utils/cookies/UniversalCookiesManager.browser.test.ts
src/utils/cookies/UniversalCookiesManager.server.test.ts
src/utils/cookies/cookies.ts
src/utils/css.test.ts
src/utils/data/record.test.ts
src/utils/data/record.ts
src/utils/env/env.test.ts
src/utils/extend-jest/toContainObject.js
src/utils/gql/graphcms.test.ts
src/utils/js/array.test.ts
src/utils/js/array.ts
src/utils/js/string.test.ts
src/utils/js/url.test.ts
src/utils/mobile.ts
src/utils/svg/svgTemplate.ts

Does that seem accurate?

@Vadorequest
Copy link
Author

Awesome! And yes, it looks very accurate. I'll need to tweak the config to ignore *.test.ts but the other files seem very accurate to me!

@pacocoursey
Copy link
Owner

Great! Published a new version.

Keep in mind you don't want to include components in your entrypoints, it should only be pages or other entrypoints that you pass to webpack via next.config.js.

UnlyEd/next-right-now@16da46e#diff-b9cfc7f2cdf78a7f4b91a753d10865a2R69

@Vadorequest
Copy link
Author

Vadorequest commented Jun 6, 2020

Thanks! I'll try it out quickly.

Yeah, I tried to tweak the entrypoints because I wasn't getting any result, but indeed I'll remove it, good catch!

@Vadorequest
Copy link
Author

I've upgraded to 0.0.2 and here is what I get when running this config:

"next-unused": {
    "alias": {},
    "debug": true,
    "include": [
      "src/components",
      "src/gql",
      "src/hocs",
      "src/hooks",
      "src/middlewares",
      "src/mocks",
      "src/pages",
      "src/propTypes",
      "src/stores",
      "src/types",
      "src/utils"
    ],
    "exclude": [],
    "entrypoints": [
      "src/pages"
    ]
  },

Error: ENOENT: no such file or directory, stat '/Users/vadorequest/dev/next-right-now/pages'

It seems to be looking for a pages folder right from my root directory, but in my project it's nested under src/pages.

yarn analyse:unused
yarn run v1.22.4
$ next-unused
[debug] Found include config: src/components,src/gql,src/hocs,src/hooks,src/middlewares,src/mocks,src/pages,src/propTypes,src/stores,src/types,src/utils
[debug] Found entrypoints config: src/pages
[debug] Using exclude regex: /^(?!(src\/components|src\/gql|src\/hocs|src\/hooks|src\/middlewares|src\/mocks|src\/pages|src\/propTypes|src\/stores|src\/types|src\/utils))/i
(node:30549) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat '/Users/vadorequest/dev/next-right-now/pages'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:30549) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:30549) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
✨  Done in 0.74s.

@Vadorequest
Copy link
Author

@pacocoursey Do you know if I misconfigured anything here? Is there a workaround?

@pacocoursey
Copy link
Owner

I didn't account for monorepos in which pages/ isn't a top level directory. I'll publish a new version with a fix when I get the chance. Sorry for the delay

@pacocoursey
Copy link
Owner

This should be fixed in 0.0.3, let me know if there's any issues.

@Vadorequest
Copy link
Author

Here is the new output:

yarn run v1.22.4
$ next-unused
[debug] Found include config: src/components,src/gql,src/hocs,src/hooks,src/middlewares,src/mocks,src/pages,src/propTypes,src/stores,src/types,src/utils
[debug] Found entrypoints config: src/pages
[debug] Using exclude regex: /^(?!(src\/components|src\/gql|src\/hocs|src\/hooks|src\/middlewares|src\/mocks|src\/pages|src\/propTypes|src\/stores|src\/types|src\/utils))/i and 
[debug] Searching directories: [
  "/Users/vadorequest/dev/next-right-now/src/components",
  "/Users/vadorequest/dev/next-right-now/src/gql",
  "/Users/vadorequest/dev/next-right-now/src/hocs",
  "/Users/vadorequest/dev/next-right-now/src/hooks",
  "/Users/vadorequest/dev/next-right-now/src/middlewares",
  "/Users/vadorequest/dev/next-right-now/src/mocks",
  "/Users/vadorequest/dev/next-right-now/src/pages",
  "/Users/vadorequest/dev/next-right-now/src/propTypes",
  "/Users/vadorequest/dev/next-right-now/src/stores",
  "/Users/vadorequest/dev/next-right-now/src/types",
  "/Users/vadorequest/dev/next-right-now/src/utils"
]
[debug] Found entrypoints
[
  'src/pages/404.tsx',
  'src/pages/[locale]/examples/built-in-features/analytics.tsx',
  'src/pages/[locale]/examples/built-in-features/animations.tsx',
  'src/pages/[locale]/examples/built-in-features/cookies-consent.tsx',
  'src/pages/[locale]/examples/built-in-features/css-in-js.tsx',
  'src/pages/[locale]/examples/built-in-features/docs-site.tsx',
  'src/pages/[locale]/examples/built-in-features/graphql.tsx',
  'src/pages/[locale]/examples/built-in-features/hosting.tsx',
  'src/pages/[locale]/examples/built-in-features/icons.tsx',
  'src/pages/[locale]/examples/built-in-features/index.tsx',
  'src/pages/[locale]/examples/built-in-features/manual-deployments.tsx',
  'src/pages/[locale]/examples/built-in-features/monitoring.tsx',
  'src/pages/[locale]/examples/built-in-features/stages-and-secrets.tsx',
  'src/pages/[locale]/examples/built-in-features/static-i18n.tsx',
  'src/pages/[locale]/examples/built-in-features/ui-components.tsx',
  'src/pages/[locale]/examples/built-in-utilities/api.tsx',
  'src/pages/[locale]/examples/built-in-utilities/bundle-analysis.tsx',
  'src/pages/[locale]/examples/built-in-utilities/errors-handling.tsx',
  'src/pages/[locale]/examples/built-in-utilities/hocs.tsx',
  'src/pages/[locale]/examples/built-in-utilities/hooks.tsx',
  'src/pages/[locale]/examples/built-in-utilities/i18nLink-component.tsx',
  'src/pages/[locale]/examples/built-in-utilities/index.tsx',
  'src/pages/[locale]/examples/built-in-utilities/interactive-error.tsx',
  'src/pages/[locale]/examples/built-in-utilities/packages-upgrade.tsx',
  'src/pages/[locale]/examples/built-in-utilities/security-audit.tsx',
  'src/pages/[locale]/examples/built-in-utilities/svg-to-react.tsx',
  'src/pages/[locale]/examples/built-in-utilities/top-level-500-error.tsx',
  'src/pages/[locale]/examples/built-in-utilities/tracking-useless-re-renders.tsx',
  'src/pages/[locale]/examples/index.tsx',
  'src/pages/[locale]/examples/native-features/example-with-ssg-and-fallback/[albumId].tsx',
  'src/pages/[locale]/examples/native-features/example-with-ssg-and-revalidate.tsx',
  'src/pages/[locale]/examples/native-features/example-with-ssg.tsx',
  'src/pages/[locale]/examples/native-features/example-with-ssr.tsx',
  'src/pages/[locale]/examples/native-features/index.tsx',
  'src/pages/[locale]/index.tsx',
  'src/pages/[locale]/pageTemplateSSG.tsx',
  'src/pages/[locale]/pageTemplateSSR.tsx',
  'src/pages/[locale]/terms.tsx',
  'src/pages/_app.tsx',
  'src/pages/_document.tsx',
  'src/pages/_error.tsx',
  'src/pages/api/autoRedirectToLocalisedPage.ts',
  'src/pages/api/error.test.ts',
  'src/pages/api/error.ts',
  'src/pages/api/preview.ts',
  'src/pages/api/status.test.ts',
  'src/pages/api/status.ts'
]
Found 34 unused files:
src/components/ComponentTemplate.tsx
src/components/assets/GraphCMSAsset.test.tsx
src/components/i18n/I18nLink.test.tsx
src/gql/fragments/favicon.ts
src/hocs/withHOCTemplate.tsx
src/hooks/useHasMounted.tsx
src/types/SerializedRecord.ts
src/types/nextjs/AppInitialProps.ts
src/types/nextjs/GetStaticPropsContext.ts
src/utils/api/fetchJSON.test.ts
src/utils/api/fetchJSON.ts
src/utils/assets/logo.test.ts
src/utils/caching/diskCacheStorage.ts
src/utils/caching/hybridCache.test.ts
src/utils/caching/hybridCache.ts
src/utils/caching/hybridCacheStorage.ts
src/utils/caching/memoryCacheStorage.ts
src/utils/cookies/UniversalCookiesManager.browser.test.ts
src/utils/cookies/UniversalCookiesManager.server.test.ts
src/utils/cookies/cookies.ts
src/utils/css.test.ts
src/utils/data/record.test.ts
src/utils/data/record.ts
src/utils/env/env.test.ts
src/utils/extend-jest/toContainObject.js
src/utils/gql/graphcms.test.ts
src/utils/js/array.test.ts
src/utils/js/array.ts
src/utils/js/string.test.ts
src/utils/js/url.test.ts
src/utils/mobile.ts
src/utils/node/fs-utils.ts
src/utils/svg/svgTemplate.ts
src/utils/time/getTimestampsElapsedTime.ts
✨  Done in 12.99s.

There is a lot of noise coming through test files. I tried to ignore them, without success.

"exclude": ["src/*/*.test.*"], produces the same results while "exclude": ["**/*.test.*"], throws UnhandledPromiseRejectionWarning: SyntaxError: Invalid regular expression: /*/*.test.*/: Nothing to repeat.

Otherwise, it seems to work great! Would love to silent unused tests files though, they're all false positives since they're never imported from anywhere and that's expected.

@pacocoursey
Copy link
Owner

I was trying to think of a good syntax for excludes. Right now, it's a very plain Regex, so:

"exclude": [".test."]

Should ignore any files that include the literal string .test.

I suppose the wildcard syntax is more natural though, I'll look into supporting that instead

@Vadorequest
Copy link
Author

Thanks! Regex is fine, adding an example for a common use case such as test files in the readme would be a good example though!

Worked like a charm.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants