Skip to content

Commit e74af21

Browse files
committed
Source map support.
Closes #2.
1 parent 954121c commit e74af21

6 files changed

+51
-2
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,21 @@ new EmberApp(defaults, {
416416
});
417417
```
418418

419+
### Source Maps
420+
421+
Ember CLI allows you to [specify source map settings](https://ember-cli.com/user-guide/#source-maps) for your entire build process, and ember-css-modules will honor that configuration. For instance, to enable source maps in all environments for both JS and CSS files, you could put the following in your `ember-cli-build.js`:
422+
423+
```
424+
sourcemaps: {
425+
enabled: true,
426+
extensions: ['js', 'css']
427+
}
428+
```
429+
430+
#### Notes
431+
- You should specify the `css` extension in your source map configuration even if you're using a different extension for your modules themselves, since the final output file will be a `.css` file.
432+
- Currently CSS source maps (for _any_ Ember CLI preprocessor) only work for applications, not for addons. Watch [ember-cli/broccoli-concat#58](https://github.com/ember-cli/broccoli-concat/issues/58) for progress on that front.
433+
419434
### Other Preprocessors
420435

421436
There are two approaches to integrating CSS modules with other style preprocessors like Sass, Less or Stylus.

ember-cli-build.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
44

55
module.exports = function(defaults) {
66
var app = new EmberAddon(defaults, {
7+
sourcemaps: {
8+
extensions: ['js', 'css'],
9+
enabled: true
10+
},
11+
712
cssModules: {
813
virtualModules: {
914
'virtual-constants': {

index.js

+17
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,24 @@ module.exports = {
100100
return this.options.postcssOptions;
101101
},
102102

103+
enableSourceMaps: function() {
104+
if (this._enableSourceMaps === undefined) {
105+
var mapOptions = this._findRootApp().options.sourcemaps;
106+
this._enableSourceMaps = mapOptions.enabled && mapOptions.extensions.indexOf('css') !== -1;
107+
}
108+
109+
return this._enableSourceMaps;
110+
},
111+
103112
belongsToAddon: function() {
104113
return !!this.parent.parent;
114+
},
115+
116+
_findRootApp: function() {
117+
var current = this;
118+
while (current.parent.parent) {
119+
current = current.parent;
120+
}
121+
return current.app;
105122
}
106123
};

lib/modules-preprocessor.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ ModulesPreprocessor.prototype.toTree = function(inputTree, path) {
3838
this.modulesTree = require('broccoli-css-modules')(modulesSources, {
3939
extension: this.owner.getFileExtension(),
4040
plugins: this.getPlugins(),
41+
enableSourceMaps: this.owner.enableSourceMaps(),
42+
sourceMapBaseDir: this.owner.belongsToAddon() ? 'modules' : '',
4143
postcssOptions: this.owner.getPostcssOptions(),
4244
virtualModules: this.owner.getVirtualModules(),
4345
generateScopedName: this.scopedNameGenerator(),

lib/output-styles-preprocessor.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ OutputStylesPreprocessor.prototype.toTree = function(inputNode, inputPath, outpu
1818
var concatOptions = {
1919
inputFiles: ['**/*.' + this.owner.getFileExtension()],
2020
outputFile: outputFile,
21-
allowNone: true
21+
allowNone: true,
22+
sourceMapConfig: this.sourceMapConfig()
2223
};
2324

2425
debug('concatenating module stylesheets: %o', concatOptions);
@@ -114,6 +115,15 @@ OutputStylesPreprocessor.prototype.eachFileWithDependencies = function(type, cal
114115
});
115116
};
116117

118+
OutputStylesPreprocessor.prototype.sourceMapConfig = function() {
119+
if (this.owner.enableSourceMaps()) {
120+
return {
121+
extensions: ['css'],
122+
mapCommentType: 'block'
123+
};
124+
}
125+
}
126+
117127
// Pulled straight from broccoli-concat :(
118128
function makeIndex(a, b) {
119129
var index = Object.create(null);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
],
5151
"dependencies": {
5252
"broccoli-concat": "^2.1.0",
53-
"broccoli-css-modules": "^0.5.0",
53+
"broccoli-css-modules": "^0.5.2",
5454
"broccoli-funnel": "^1.0.7",
5555
"broccoli-merge-trees": "^1.1.1",
5656
"debug": "^2.2.0",

0 commit comments

Comments
 (0)