Using relative paths - sources still contains absolute paths
jsonUK opened this issue · 5 comments
- Operating System: OSX 10.14.4
- Node Version: v11.12.0
- NPM Version: 6.7.0
- webpack Version: 4.28.4
- cache-loader Version: 2.0.1
Hi,
I know the relative paths has been a fairly recent feature addition (thank you!)... but I noticed that the cache contents still contain absolute paths.
This sets the correct relative paths in the cache details, but then I see things like this inside the cache file, under the "results":[...]
object:
Expected Behaviour
...,{"version":3,"sources":["src/js/...
Is it possible to parse these "sources" as relative paths too - so that they context stays the same?
This is for javascript files running through uglifyjs-webpack-plugin
(not sure if that is relevant or not).
Actual Behaviour
...,{"version":3,"sources":["/Users/jason/Sites/...
Code
module.exports = (env, argv) => {
return {
mode: argv.mode,
...
module: {
rules: [
{
{
test: /\.m?js?$/,
exclude: /(node_modules|bower_components)/,
use: [{
loader: 'cache-loader',
options: {
cacheContext: path.normalize(path.relative(__dirname, '.')),
cacheDirectory: 'cache/'
}, {
loader: 'babel-loader'
}]
}
}
]
}
}
}
How Do We Reproduce?
Happens in both modes development
and production
.
Command been using: webpack --mode production
Please use issue template otherwise issue was closed, thanks
Updated previous post with new template as requested.
@jsonUK could please verify if the absolute paths you have are coming from source-maps? Maybe we can just disable and delete the cache to understand if the new generated cache still have absolute paths. If the problem is from source-maps, you can configure webpack to only have relative paths within the sourcemaps:
const path = require('path')
const context = // absolute context path
module.exports = {
context,
// ...
output: {
devtoolModuleFilenameTemplate: (info) => {
const rel = path.relative(context, info.absoluteResourcePath);
return `webpack:///${rel}`;
},
// ...
}
}
Do you have any other idea here @evilebottnawi ?
Hi - sorry for not getting back sooner.
I had a test of what you sent me - the above snippet made no difference to the cache output.
But I noticed that if I disable devtool: 'source-map'
, the source map data no longer appeared.
Not sure if cache-loader should be accommodating that output?
Would toggling on/off source-map data between development
and production
modes affect cache-loaders efficiency?
eg.
devtool: ENV === 'development' ? 'source-map' : false,
Ideally the paths would not be absolute in the source-map data, when setting a context path - but I lack the knowledge to know if that would affect other tools from working etc.
I wrote a loader to convert all abs paths to relative but i would use it as a last result because it will increase the build time...
https://github.com/stavalfi/babel-plugin-module-resolver-loader