I have this webpack.config.js:
{
target: 'node',
mode: 'production',
// devtool: 'source-map',
entry: {
index: './source/lib/index.ts',
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
include: path.resolve(__dirname, 'source'),
use: [
{
loader: 'ts-loader',
options: {
compiler: 'ttypescript'
}
}
]
}
]
},
plugins: [
new BundleDeclarationsWebpackPlugin({
entry: "./source/lib/index.ts",
outFile: "./index.d.ts"
})
],
externals: [nodeExternals()],
output: {
path: path.resolve(__dirname, 'bundled', 'lib'),
filename: 'index.js',
library: 'mongo-cleaner',
libraryTarget: 'umd',
globalObject: 'this',
umdNamedDefine: true,
}
}
In that you can see that I am bundling a library with webpack, keeping the node_modules as externals.
And this tsconfig.json:
{
"compilerOptions": {
"moduleResolution": "node",
"module": "commonjs",
"target": "ES5",
"strictNullChecks": true,
"lib": [
"ES2021"
],
"outDir": "../dist",
"sourceMap": true,
"declaration": true,
"baseUrl": ".",
"paths": {
"@/*": [
"./lib/*"
],
"@lib/*": [
"./lib/*"
],
"@bin/*": [
"./bin/*"
]
}
},
"plugins": [
{
"transform": "typescript-transform-paths"
},
{
"transform": "typescript-transform-paths",
"afterDeclarations": true
}
],
"include": [
"lib",
"bin"
]
}
When I do tsc -p source everything works, even if I have "@" paths in my requires
But when I do the same with webpack, I obtain these errors:
ERROR in ./source/lib/index.ts 50:16-42
Module not found: Error: Can't resolve '@/utils/cleaner' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 51:19-48
Module not found: Error: Can't resolve '@/utils/askConfirm' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 52:16-42
Module not found: Error: Can't resolve '@/utils/options' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 53:13-45
Module not found: Error: Can't resolve '@/interfaces/exported' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 54:13-32
Module not found: Error: Can't resolve '@/errors' in '/home/euber/Github/mongo-cleaner/source/lib'
How is it possible that webpack is ignoring the @?
To have more information and be able to reproduce it, just look at this repo
UPDATE:
I have tried also this answer, but it does not work:
webpack.config.js
alias: {
'@': path.resolve(__dirname, 'lib'),
'@lib': path.resolve(__dirname, 'lib'),
'@bin': path.resolve(__dirname, 'bin')
}
Errors:
ERROR in ./source/lib/index.ts 50:16-42
Module not found: Error: Can't resolve '@/utils/cleaner' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 51:19-48
Module not found: Error: Can't resolve '@/utils/askConfirm' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 52:16-42
Module not found: Error: Can't resolve '@/utils/options' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 53:13-45
Module not found: Error: Can't resolve '@/interfaces/exported' in '/home/euber/Github/mongo-cleaner/source/lib'
ERROR in ./source/lib/index.ts 54:13-32
Module not found: Error: Can't resolve '@/errors' in '/home/euber/Github/mongo-cleaner/source/lib'