Skip to content
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

Importing node_module without default export results in "Unnecessary block" error #18

Closed
LukasHirt opened this issue Jan 14, 2020 · 3 comments
Assignees
Labels

Comments

@LukasHirt
Copy link

LukasHirt commented Jan 14, 2020

I'm importing a terser node_module and since it doesn't have default export I need to import the terser function specifically. This then results in an error in Codacy PR review saying "Unnecessary block".

Here is the code where the error happens:

import vue from 'rollup-plugin-vue'
import { terser } from 'rollup-plugin-terser'
import replace from '@rollup/plugin-replace'
import filesize from 'rollup-plugin-filesize'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import babel from 'rollup-plugin-babel'
import json from '@rollup/plugin-json'
import builtins from 'rollup-plugin-node-builtins'
import globals from 'rollup-plugin-node-globals'

const production = !process.env.ROLLUP_WATCH

// We can't really do much about circular dependencies in node_modules
function onwarn (warning) {
  if (warning.code !== 'CIRCULAR_DEPENDENCY') {
    console.error(`(!) ${warning.message}`)
  }
}

export default {
  input: 'ui/app.js',
  output: {
    file: 'assets/hello.js',
    format: 'amd',
    sourcemap: !production
  },
  onwarn,
  plugins: [
    vue(),
    replace({
      'process.env.NODE_ENV': JSON.stringify('production')
    }),
    resolve({
      mainFields: ['browser', 'jsnext', 'module', 'main'],
      include: 'node_modules/**',
      preferBuiltins: true
    }),
    babel({
      exclude: 'node_modules/**',
      runtimeHelpers: true
    }),
    commonjs({
      include: 'node_modules/**'
    }),
    json(),
    globals(),
    builtins(),
    production && terser(),
    production && filesize()
  ]
}

I don't think this error should happen because what if this would be some really big library and we'd want to use only some small pieces of it but would have to import it whole in order to not get this error?

@franciscodua
Copy link
Contributor

Hi @LukasHirt

You're absolutely right, that should not be flagged as a potential problem. And PMD's maintainers are already aware of this problem as you can see in this issue on their repository.

Until this issue is fixed, as a workaround, you can either
a) ignore the issue's occurrence: expand the issue > click the configuration gear > Ignore issue
Or
b) ignore the pattern: go to Code patterns (on the side bar) > PMD > Untick the box next to the "Unnecessary block." pattern.

Feel free to reach out if you have any question or concern.

@LukasHirt
Copy link
Author

LukasHirt commented Mar 9, 2020

Thanks a lot for the link to the issue and for the workarounds @franciscodua

@franciscodua
Copy link
Contributor

No problem.

I'll close this issue. If you have anything else to add re-open it or create another one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants