Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SourceMap root calculated wrong on Windows, when platform.output dir points outside of the project directory #1212

@nenadvicentic

Description

@nenadvicentic

Description of the bug:

Calculation of mapSourceRoot variable withing Bundle.write(platform) method gives wrong results on Windows OS, when platform.output directory is outside of Aurelia project dir.

The problematic code in Aurelia CLI v3.0.4 is here:

cli/lib/build/bundle.js

Lines 367 to 370 in 254508c

let mapSourceRoot = path.posix.relative(
path.posix.join(process.cwd(), platform.output),
process.cwd()
);

Example

  • Project dir: "C:\My Documents\MyWeb\ClientApp"
  • platform.output: "../wwwroot/scripts"

Result is mapSourceRoot variable is: "../../C:\My Documents\MyWeb\ClientApp".

This wrong path ends up in vendor-bundle.js.map file. For example:

{
    "version":3,
    "sources":[
        "../../C:\\My Documents\\MyWeb\\ClientApp/node_modules/aurelia-binding/dist/commonjs/aurelia-binding.js"",
        "../../C:\\My Documents\\MyWeb\\ClientApp/node_modules/alameda/alameda.js",
        // ...etc....
    ]
}

Expected result

For the same above example:

  • Project dir: "C:\My Documents\MyWeb\ClientApp"
  • platform.output: "../wwwroot/scripts"

Resulting mapSourceRoot variable should be: "../../ClientApp".

This will result in a correct `vendor-bundle.js.map file. For example:

{
    "version":3,
    "sources":[
        "../../ClientApp/node_modules/aurelia-binding/dist/commonjs/aurelia-binding.js"",
        "../../ClientApp/node_modules/alameda/alameda.js",
        // ...etc....
    ]
}

Nice to have - sourceMapsRoot property

It would be nice to have aurelia.json property, for example platform.sourceMapRoot that can override this calculation completely.

The reason is that current calculation takes in consideration only physical folders, but not URL mapping of the web server. Common example in ASP.NET Core world is wwwroot folder that is mapped to the root url /. Note that in the working example above path .../../ClientApp actually wants to jump further then the root of web, but browser does not allow for that and remaps it to /ClientApp.

Loader uses additional properties platform.useAbsolutePath and platform.baseUrl to provide such functionality here:

cli/lib/build/loader.js

Lines 53 to 57 in 254508c

if (platform.useAbsolutePath) {
location = platform.baseUrl ? location : '/' + location;
} else {
location = '../' + location;
}

However, calculation for the source maps cannot take all of this in consideration when source code is outside of mapped folders accessible from web. Therefore, simplest solution would be sourceMapRoot property. For example, in aurelia.json, it's here:

{
  "build": {
    "targets": [
      {
        // ...other stuff
        "baseUrl": "/scripts",
        "output": "../wwwroot/scripts",
        "useAbsolutePath": true,
        "sourceMapsRoot": "/ClientApp"
      }
    ],
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions