简体   繁体   中英

Shorter relative paths with Angular - Webpack and Typescript

So i see myself ending up with a lot of ../../../ in my imports. How can i set a constant in a config so that both webpack and typescript resolve correctly?

I have tried doing the below, but it's unable to resolve the path in the import.

tsconfig.json:

"baseUrl": ".",
"paths": {}

webpack.common.js under module.exports

resolve: {
    extensions: ['.ts', '.js'],
    modules: [path.resolve(__dirname, "src"), "node_modules"]
},

I intended to let typescript look from the main dir (above src), and ask webpack to get modules from "src".

My IDE isn't complaining (So Typescript should be ok). Webpack is.

I almost got it right. The webpack exports should be:

resolve: {
    extensions: ['.ts', '.js'],
    modules: ["src", "node_modules"]
},

instead of

resolve: {
    extensions: ['.ts', '.js'],
    modules: [path.resolve(__dirname, "src"), "node_modules"]
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM