Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Using relative paths - sources still contains absolute paths #70

Open
jsonUK opened this issue Apr 18, 2019 · 5 comments
Open

Using relative paths - sources still contains absolute paths #70

jsonUK opened this issue Apr 18, 2019 · 5 comments

Comments

@jsonUK
Copy link

jsonUK commented Apr 18, 2019

  • 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

@alexander-akait
Copy link
Member

Please use issue template otherwise issue was closed, thanks

@jsonUK
Copy link
Author

jsonUK commented Apr 18, 2019

Updated previous post with new template as requested.

@mistic
Copy link
Collaborator

mistic commented Apr 19, 2019

@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 ?

@jsonUK
Copy link
Author

jsonUK commented Apr 30, 2019

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.

@stavalfi
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants