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

fix: source map sources and file paths #753

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ They are not enabled by default because they expose a runtime overhead and incre
}
```

#### Opting out of legacy source maps behavior

To receive source maps as URLs relative to `loader.resourcePath`, set the `legacySourceMaps` option to `false`.


```javascript
{
loader: 'css-loader',
options: {
legacySourceMaps: false,
sourceMap: true
}
}
```

### `camelCase`

By default, the exported JSON keys mirror the class names. If you want to camelize class names (useful in JS), pass the query parameter `camelCase` to css-loader.
Expand Down
66 changes: 58 additions & 8 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,39 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var path = require("path");
var loaderUtils = require("loader-utils");
var processCss = require("./processCss");
var getImportPrefix = require("./getImportPrefix");
var compileExports = require("./compile-exports");

/**
* If the file was "renamed" (for the purposes of source maps), then honor it,
* otherwise just use the resourcePath.
* @param {String} resourcePath - The absolute file system path for the sass file.
* @param {Object|null} map - An existing source map, if any.
* @return {String} - The effective path to use for the `from` argument.
*/
function processFrom(resourcePath, map) {
var effectiveResourcePath;
if (map && map.file && typeof map.file === 'string' && path.dirname(map.file) === '.') {
Copy link
Member

Choose a reason for hiding this comment

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

path.dirname(map.file) === '.' checks for a relative path here ? I'm not sure I'm following.. 😛 . In case my understanding is correct here, there is also path.isAbsolute(filepath) available

Copy link
Author

Choose a reason for hiding this comment

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

I've done some thinking on it and here I may be trying to be too clever. What I originally was thinking of shouldn't be something that css-loader is responsible for. I can provide more details on what I was thinking if you'd like.

I'll try cleaning this up in my next commit.

		from: this.resourcePath,
		to: this.resourcePath,

will be enough.

// Something else has already changed the file name or extension, so
// honor it for the purpose of creating the next source map.
effectiveResourcePath = path.join(path.dirname(resourcePath), map.file);
Copy link
Member

Choose a reason for hiding this comment

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

Please provide an example for effectiveResourcePath here :)

} else {
effectiveResourcePath = resourcePath;
}
Copy link
Member

Choose a reason for hiding this comment

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

nitpick 🐦 \n after the else block

return effectiveResourcePath;
}

module.exports = function(content, map) {
var callback = this.async();
var query = loaderUtils.getOptions(this) || {};
var moduleMode = query.modules;
var camelCaseKeys = query.camelCase;
var sourceMap = query.sourceMap || false;
var processCssFrom;
var processCssTo;

if(sourceMap) {
if (map) {
Expand All @@ -33,10 +54,37 @@ module.exports = function(content, map) {
map = null;
}

if (query.legacySourceMaps !== false) {
processCssFrom = loaderUtils.getRemainingRequest(this).split("!").pop();
processCssTo = loaderUtils.getCurrentRequest(this).split("!").pop();
} else {

/**
* > To ensure that PostCSS generates source maps and displays better syntax
* > errors, runners must specify the from and to options. If your runner
* > does not handle writing to disk (for example, a gulp transform), you
* > should set both options to point to the same file
* @see postcss [PostCSS Runner Guidelines]{@link https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#21-set-from-and-to-processing-options}
*
* `css-loader` isn't responsible for writing the map, so it doesn't have to
* worry about updating the map with a transformation that changes locations
* (suchs as map.file or map.sources).
*
* Changing the file extension counts as changing the location because it
* changes the path.
*
* PostCSS's `from` and `to` arguments are only concerned with the file
* system. They don't know about, care about, or understand the webpack
* loader's current request or remaining request.
*/
processCssFrom = processFrom(this.resourcePath, map);
processCssTo = processCssFrom;
}

processCss(content, map, {
mode: moduleMode ? "local" : "global",
from: loaderUtils.getRemainingRequest(this).split("!").pop(),
to: loaderUtils.getCurrentRequest(this).split("!").pop(),
from: processCssFrom,
Copy link
Member

Choose a reason for hiding this comment

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

{
  ...,
  // TODO use shorthand in next major
  from: from,
  to: to
}

to: processCssTo,
query: query,
loaderContext: this,
sourceMap: sourceMap
Expand Down Expand Up @@ -112,13 +160,15 @@ module.exports = function(content, map) {
if(sourceMap && result.map) {
// add a SourceMap
map = result.map;
if(map.sources) {
npetruzzelli marked this conversation as resolved.
Show resolved Hide resolved
map.sources = map.sources.map(function(source) {
return source.split("!").pop().replace(/\\/g, '/');
}, this);
map.sourceRoot = "";
if (query.legacySourceMaps !== false) {
if(map.sources) {
map.sources = map.sources.map(function(source) {
return source.split("!").pop().replace(/\\/g, '/');
}, this);
map.sourceRoot = "";
}
map.file = map.file.split("!").pop().replace(/\\/g, '/');
}
map.file = map.file.split("!").pop().replace(/\\/g, '/');
map = JSON.stringify(map);
moduleJs = "exports.push([module.id, " + cssAsString + ", \"\", " + map + "]);";
} else {
Expand Down
11 changes: 9 additions & 2 deletions lib/processCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,16 @@ module.exports = function processCss(inputSource, inputMap, options, callback) {
parserPlugin(parserOptions)
]);

pipeline.process(inputSource, {
var postCssFrom;
if (options.query.legacySourceMaps !== false) {
// we need a prefix to avoid path rewriting of PostCSS
from: "/css-loader!" + options.from,
postCssFrom = "/css-loader!" + options.from;
} else {
postCssFrom = options.from;
}

pipeline.process(inputSource, {
from: postCssFrom,
to: options.to,
map: options.sourceMap ? {
prev: inputMap,
Expand Down