Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the index of stdin instead of zero #681

Merged
merged 4 commits into from May 7, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/loader.js
Expand Up @@ -86,7 +86,12 @@ function sassLoader(content) {
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
// we know that this path is relative to process.cwd(). This is how node-sass works.
// eslint-disable-next-line no-param-reassign
result.map.sources[0] = path.relative(process.cwd(), resourcePath);
let stdinIndex = result.map.sources.indexOf('stdin');
stdinIndex = stdinIndex !== -1 ? stdinIndex : 0;
result.map.sources[stdinIndex] = path.relative(
process.cwd(),
resourcePath
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for investigation, maybe rewrite this like:

if (stdinIndex !== -1) {
  result.map.sources[stdinIndex] = path.relative(
        process.cwd(),
        resourcePath
      );
}

This allow to us protect code if stdin was excluded from sources in future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the point - the way I've written it, stdinIndex will never be -1. If it is, it will be 0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FractalBoy I am afraid what in future node-sass/sass can remove stdin from result.map.sources so better check stdin exists in sources and then modify

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now - If stdin not exists in sources we break source map, because on 0 index will be contain path to source

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I'll make the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would actually be better to remove stdin if it exists and then always add the path?

      const stdinIndex = result.map.sources.findIndex(
        (source) => source.indexOf('stdin') !== -1
      );

      const relativeResourcePath = path.relative(
        process.cwd(),
        resourcePath
      );

      if (stdinIndex !== -1) {
        result.map.sources[stdinIndex] = relativeResourcePath;
      } else {
        result.map.sources.push(relativeResourcePath);
      }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, nevermind, that would probably screw up the source maps since it wouldn't line up correctly.

// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
// This fixes an error on windows where the source-map module cannot resolve the source maps.
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
Expand Down