question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to ignore import & require statement

See original GitHub issue

@kitten as a npm package,we always have to import third packages,like so:

import {get} from 'lodash'

class Demo1 extends React.Component {
  render () {
    // do something with get
  }
}

actually,we prefer to use scope to pass global variables。and this will throw an error。

so do you have some support on such an ignore statement?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

3reactions
mathieudutourcommented, Mar 21, 2019

you’re welcome?

1reaction
mathieudutourcommented, Mar 20, 2019

you can use the transformCode props on LiveProvider:

const importRegex = /import(?:["'\s]*([\w*{}\n, ]+)from\s*)["'\s]*([@\w/_-]+)["'\s]*;?/gm
const requireRegex = /(const|let|var)\s*([\w{}\n, ]+\s*)=\s*require\s*\(["'\s]*([@\w/_-]+)["'\s]*\s*\);?/gm
const imports = {
 lodash: 'Lodash'
}

const transformCode = (code) => {
  return code.replace(importRegex, (match, p1, p2) => {
    const matchingImport = imports[p2]
    if (!matchingImport) {
      // leave it alone if we don't have a matching import
      return match
    }

    return 'var ' + p1 + ' = ' + matchingImport + ';'
  })
  .replace(requireRegex, (match, p1, p2, p3) => {
    const matchingImport = imports[p3]
    if (!matchingImport) {
      // leave it alone if we don't have a matching import
      return match
    }

    return p1 + ' ' + p2 + ' = ' + matchingImport + ';'
  })
}

and pass Lodash in the scope

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Answers - 4 - Stack Overflow
ESLint: Require statement not part of import statement.(@typescript-eslint/no-var-requires) · Ask Question. Asked 3 years ago.
Read more >
typescript - # - eslint - DEV Community ‍ ‍
Require statement not part of import statement.eslint[@typescript-eslint/no-var- ... Ignore all instances of var requires at the file level.
Read more >
imports-loader - webpack
The imports loader can add the necessary require('whatever') calls, ... By default loader generate ES module named syntax. ... Disable AMD Import Syntax....
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
You'll need most of these packages when you're building JAMStack ... How to Bypass ES Modules Errors in Next.js with Dynamic Imports.
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
js will treat the following as CommonJS when passed to node as the initial input, or when referenced by import statements, import() expressions,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found