javascript - In VSCode how to properly configure jsconfig.json in order for absolute paths with index.js imports to work? - Stac

In my React project, where . is the root directory, I have configured webpack so that I can import any

In my React project, where ./ is the root directory, I have configured webpack so that I can import any file from within my ./src directory.

For example, consider the following directory structure:

./
|-src/
| |-ponents/
| | |-Button/
| | | |-index.js
| | | |-Button.jsx

where src/ponents/Button/index.js contains just this:

export { default } from './Button';

Now let's say I create another ponent in src/ponents/NewComponent, which is structured similarly to src/ponents/Button, but inside my NewComponent.jsx I am doing the following:

import Button from 'ponents/Button'

The above piles just fine because I have set up my webpack correctly.

What I would like to be able to do, is for VSCode to be able to take me to the definition of the Button ponent when I alt+click on the word Button of the import statement, and take me to the index.js file's contents when I alt+click on the ponents/Button part.

I cannot seem to be able to do that.

My jsconfig.json is the following, as of the time of writing:

{
  "pilerOptions": {
    "target": "es6",
    "module": "monjs",
    "baseUrl": "./",
    "paths": {
      "src/*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "build", "dist"]
}

And, for good measure, the relevant part of my webpack config:

const PATHS = {
  node_modules: path.resolve(__dirname, 'node_modules'),
  src: path.resolve(__dirname, 'src'),
  style: path.resolve(__dirname, 'style'),
  assets: path.resolve(__dirname, 'assets'),
  test: path.resolve(__dirname, 'test')
};

module.exports = {
  resolve: {
    modules: [
      path.resolve(__dirname),
      PATHS.node_modules,
      PATHS.src,
      PATHS.style,
      PATHS.assets,
      PATHS.test
    ],
    mainFiles: ['index', 'index.js', 'index.jsx', 'index.scss'],
    extensions: ['.jsx', '.js', '.json', '.scss', '.css']
  },
....

In my React project, where ./ is the root directory, I have configured webpack so that I can import any file from within my ./src directory.

For example, consider the following directory structure:

./
|-src/
| |-ponents/
| | |-Button/
| | | |-index.js
| | | |-Button.jsx

where src/ponents/Button/index.js contains just this:

export { default } from './Button';

Now let's say I create another ponent in src/ponents/NewComponent, which is structured similarly to src/ponents/Button, but inside my NewComponent.jsx I am doing the following:

import Button from 'ponents/Button'

The above piles just fine because I have set up my webpack correctly.

What I would like to be able to do, is for VSCode to be able to take me to the definition of the Button ponent when I alt+click on the word Button of the import statement, and take me to the index.js file's contents when I alt+click on the ponents/Button part.

I cannot seem to be able to do that.

My jsconfig.json is the following, as of the time of writing:

{
  "pilerOptions": {
    "target": "es6",
    "module": "monjs",
    "baseUrl": "./",
    "paths": {
      "src/*": ["./src/*"]
    }
  },
  "exclude": ["node_modules", "build", "dist"]
}

And, for good measure, the relevant part of my webpack config:

const PATHS = {
  node_modules: path.resolve(__dirname, 'node_modules'),
  src: path.resolve(__dirname, 'src'),
  style: path.resolve(__dirname, 'style'),
  assets: path.resolve(__dirname, 'assets'),
  test: path.resolve(__dirname, 'test')
};

module.exports = {
  resolve: {
    modules: [
      path.resolve(__dirname),
      PATHS.node_modules,
      PATHS.src,
      PATHS.style,
      PATHS.assets,
      PATHS.test
    ],
    mainFiles: ['index', 'index.js', 'index.jsx', 'index.scss'],
    extensions: ['.jsx', '.js', '.json', '.scss', '.css']
  },
....
Share Improve this question asked Oct 2, 2018 at 21:49 Dimitris KaragiannisDimitris Karagiannis 9,3969 gold badges45 silver badges74 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

I think your configuration is correct the only mistake you've made is in the path setting.

Try this:

{
  "pilerOptions": {
    "target": "es6",
    "module": "monjs",
    "baseUrl": "./",
    "paths": {
      "ponents/*": ["./src/ponents/*/index"]
    }
  },
  “Include”: [“./src/**/*”],
  "exclude": ["node_modules", "build", "dist"]
}

or just type import Button from 'src/ponents/Button'

after this close-reopen the project et voilà ;)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744842288a4596630.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信