'Webpack and typescript config aliases for imports from root project
can help me?
I try to build a package for publication with webpack+ts and I need to use parent dependencies from my root project
I have Nuxt.js application on the TypeScript and create folder packages/{package_name}/
I do npm init in this folder and install typescript and webpack
My webpack.config.js
module.exports = {
mode: 'production',
entry: path.resolve(__dirname, 'src', 'index.ts'),
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist'),
umdNamedDefine: true,
library: {
name: 'ConstructorWidget',
type: 'umd',
export: 'default'
}
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [{
loader: 'ts-loader',
options: {
appendTsSuffixTo: [/\.vue$/]
}
}],
exclude: /node_modules/
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
...
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js', '.vue', '.scss'],
alias: {
'@': path.resolve(__dirname, './src/'),
'~': path.resolve(__dirname, '..', '..'),
vue$: 'vue/dist/vue.esm.js'
}
},
plugins: [...]
};
My tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es2018",
"moduleResolution": "node",
"declaration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strictNullChecks": false,
"allowJs": true,
"sourceMap": true,
"strict": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
],
"exclude": [
"dist",
"node_modules"
],
"files": [
"./vue-shims.d.ts"
],
"paths": {
"@/*": ["./src/*"],
"~/*": ["../../*"]
}
}
in package.json
...
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
...
This configuration works well. But if I try to import something from the root project
import example from '~/core/transport/example';
I get an error
TS2307: Cannot find module '~/core/transport/example' or its corresponding type declarations.
My aliases for the root project not working. How need configuration that this work?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
