JavaScript, Tools, Webpack

Webpack: How to Import from Root

It can be easily set up with a module resolver:

1. Babel for Webpack

$ npm i babel-plugin-module-resolver --save-dev

Then create a .babelrc file under root:

{
    "plugins": [
        [
            "module-resolver",
            {
                "root": ["./"]
            }
        ]
    ]
}

2. EsLint for VSCode

For VSCode to acknowledge the import and run go-to-definition correctly, add this in your jsconfig.json, or create this file under project root if you don’t already have one:

{
    "compilerOptions": {
        "baseUrl": "."
    }
}
Standard

One thought on “Webpack: How to Import from Root

  1. Pingback: 使用webpack解决需求路径_javascript问答

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.