Skip to content

Commit a7084d6

Browse files
committed
2.0.0
1 parent be4e50c commit a7084d6

12 files changed

+167
-38
lines changed

.appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
environment:
44
matrix:
5-
- nodejs_version: 4.0.0
5+
- nodejs_version: 4
66

77
version: "{build}"
88
build: off

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
node_modules
2+
index.bundle.js
23
package-lock.json
4+
*.log*
5+
*.result.css
36
.*
47
!.appveyor.yml
58
!.editorconfig
69
!.gitignore
10+
!.rollup.js
711
!.tape.js
812
!.travis.yml
9-
*.log*
10-
*.result.css
11-
index.js

.rollup.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import babel from 'rollup-plugin-babel';
2+
3+
export default {
4+
input: 'index.js',
5+
output: { file: 'index.bundle.js', format: 'cjs' },
6+
plugins: [
7+
babel({
8+
presets: [
9+
['env', { modules: false, targets: { node: 4 } }]
10+
]
11+
})
12+
]
13+
};

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
language: node_js
44

55
node_js:
6-
- 4.0.0
6+
- 4
77

88
install:
99
- npm install

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# Changes to PostCSS Node Sass
1+
# Changes to PostCSS Sass
2+
3+
### 2.0.0 (December 25, 2017)
4+
5+
- Added: custom `source-map` implemention with fix for
6+
[sass/libsass#2312](https://github.com/sass/libsass/issues/2312)
7+
- Changed: `index.mjs` module for `index.js` module
8+
- Changed: `merge-source-map` dependency for custom `source-map` implemention
9+
- Changed: first plugin detection for custom `source-map` implemention
210

311
### 1.0.0 (November 2, 2017)
412

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Contributing to PostCSS Node Sass
1+
# Contributing to PostCSS Sass
22

33
You want to help? You rock! Now, take a moment to be sure your contributions
44
make sense to everyone else.

index.mjs renamed to index.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// tooling
2-
import mergeSourceMap from 'merge-source-map';
2+
import mergeSourceMaps from './lib/merge-source-maps';
33
import postcss from 'postcss';
44
import sassResolve from '@csstools/sass-import-resolve';
55
import sass from 'node-sass';
@@ -19,16 +19,13 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
1919
// sass resolve cache
2020
const cache = {};
2121

22-
// whether this is the first plugin running
23-
const firstPlugin = result.processor.plugins[0] === result.lastPlugin;
24-
2522
return new Promise(
2623
// promise sass results
2724
(resolve, reject) => sass.render(
2825
// pass options directly into node-sass
2926
Object.assign({}, opts, requiredSassConfig, {
30-
file: postConfig.from,
31-
outFile: postConfig.to,
27+
file: `${postConfig.from}#sass`,
28+
outFile: postConfig.from,
3229
data: postCSS,
3330
importer(id, parentId, done) {
3431
// resolve the absolute parent
@@ -61,14 +58,13 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
6158
(sassError, sassResult) => sassError ? reject(sassError) : resolve(sassResult)
6259
)
6360
).then(
64-
// update root to post-node-sass ast
6561
({ css: sassCSS, map: sassMap }) => {
62+
// update root to post-node-sass ast
6663
result.root = postcss.parse(
6764
sassCSS.toString(),
6865
Object.assign({}, postConfig, {
6966
map: {
70-
// merge source maps
71-
prev: firstPlugin ? JSON.parse(sassMap) : mergeSourceMap(
67+
prev: mergeSourceMaps(
7268
postMap.toJSON(),
7369
JSON.parse(sassMap)
7470
)

lib/merge-source-maps.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// tooling
2+
import { SourceMapConsumer, SourceMapGenerator } from 'source-map';
3+
4+
// special sass source matcher
5+
const sassMatch = /#sass$/;
6+
7+
// returns merged source maps
8+
export default function (...maps) {
9+
// new sourcemap
10+
const generator = new SourceMapGenerator();
11+
12+
// existing sourcemaps
13+
const consumers = maps.map(
14+
map => new SourceMapConsumer(map)
15+
);
16+
17+
consumers.forEach(
18+
consumer => {
19+
// copy each original mapping to the new sourcemap
20+
consumer.eachMapping(
21+
mapping => {
22+
const originalPosition = originalPositionFor(mapping, consumers);
23+
24+
if (originalPosition.source) {
25+
generator.addMapping({
26+
generated: {
27+
line: mapping.generatedLine,
28+
column: mapping.generatedColumn
29+
},
30+
original: {
31+
// use positive numbers to work around sass/libsass#2312
32+
line: Math.abs(originalPosition.line),
33+
column: Math.abs(originalPosition.column)
34+
},
35+
source: originalPosition.source,
36+
name: originalPosition.name
37+
});
38+
}
39+
}
40+
);
41+
42+
// copy each original source to the new sourcemap
43+
consumer.sources.forEach(
44+
source => {
45+
generator._sources.add(source);
46+
47+
const content = consumer.sourceContentFor(source);
48+
49+
if (content !== null) {
50+
generator.setSourceContent(source, content);
51+
}
52+
}
53+
);
54+
}
55+
);
56+
57+
// merged map as json
58+
const mergedMap = JSON.parse(generator);
59+
60+
// clean all special sass sources in merged map
61+
mergedMap.sources = mergedMap.sources.map(
62+
source => source.replace(sassMatch, '')
63+
);
64+
65+
return mergedMap;
66+
}
67+
68+
function originalPositionFor(mapping, consumers) {
69+
// initial positioning
70+
let originalPosition = {
71+
line: mapping.generatedLine,
72+
column: mapping.generatedColumn
73+
};
74+
75+
// special sass sources are mapped in reverse
76+
consumers.slice(0).reverse().forEach(
77+
consumer => {
78+
const possiblePosition = consumer.originalPositionFor(originalPosition);
79+
80+
if (possiblePosition.source) {
81+
if (sassMatch.test(possiblePosition.source)) {
82+
originalPosition = possiblePosition;
83+
}
84+
}
85+
}
86+
);
87+
88+
// regular sources are mapped regularly
89+
consumers.forEach(
90+
consumer => {
91+
const possiblePosition = consumer.originalPositionFor(originalPosition);
92+
93+
if (possiblePosition.source) {
94+
if (!sassMatch.test(possiblePosition.source)) {
95+
originalPosition = possiblePosition;
96+
}
97+
}
98+
}
99+
);
100+
101+
return originalPosition;
102+
}

package.json

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,68 @@
11
{
22
"name": "@csstools/postcss-sass",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Use Sass as a PostCSS plugin",
55
"author": "Jonathan Neal <[email protected]>",
66
"license": "CC0-1.0",
77
"repository": "jonathantneal/postcss-sass",
88
"homepage": "https://github.com/jonathantneal/postcss-sass#readme",
99
"bugs": "https://github.com/jonathantneal/postcss-sass/issues",
10-
"main": "index.js",
11-
"module": "index.mjs",
10+
"main": "index.bundle.js",
11+
"module": "index.js",
1212
"files": [
13-
"index.mjs",
14-
"index.js"
13+
"index.js",
14+
"index.bundle.js",
15+
"lib"
1516
],
1617
"scripts": {
1718
"clean": "git clean -X -d -f",
1819
"prepublishOnly": "npm test",
19-
"pretest": "babel index.mjs --presets=env --plugins=add-module-exports --out-file index.js",
20-
"test": "echo 'Running tests...'; npm run test:js && npm run test:tape",
21-
"test:ec": "echint --ignore index.js *.expect.css *.result.css",
22-
"test:js": "eslint *.mjs --cache --ignore-pattern .gitignore",
20+
"pretest": "rollup --silent -c .rollup.js",
21+
"test": "echo 'Running tests...'; npm run test:ec && npm run test:js && npm run test:tape",
22+
"test:ec": "echint --ignore index.bundle.js test",
23+
"test:js": "eslint index.js --cache",
2324
"test:tape": "postcss-tape"
2425
},
2526
"engines": {
2627
"node": ">=4.0.0"
2728
},
2829
"dependencies": {
29-
"@csstools/sass-import-resolve": "^1.0.0",
30-
"merge-source-map": "^1.0.4",
31-
"node-sass": "^4.7.2",
32-
"postcss": "^6.0.14"
30+
"@csstools/sass-import-resolve": "^1.0",
31+
"node-sass": "^4.7",
32+
"postcss": "^6.0",
33+
"source-map": "^0.6"
3334
},
3435
"devDependencies": {
35-
"babel-cli": "^6.26",
36-
"babel-plugin-add-module-exports": "^0.2",
36+
"babel-core": "^6.26",
37+
"babel-eslint": "^8.1",
3738
"babel-preset-env": "^1.6",
3839
"echint": "^4.0",
39-
"eslint": "^4.12",
40+
"eslint": "^4.14",
4041
"eslint-config-dev": "2.0",
4142
"postcss-import": "^11.0",
4243
"postcss-tape": "2.2",
4344
"postcss-unroot": "^1.0",
44-
"pre-commit": "^1.2"
45+
"pre-commit": "^1.2",
46+
"rollup": "^0.53",
47+
"rollup-plugin-babel": "^3.0"
4548
},
4649
"eslintConfig": {
47-
"extends": "dev"
50+
"extends": "dev",
51+
"parser": "babel-eslint"
4852
},
4953
"keywords": [
5054
"postcss",
5155
"css",
5256
"postcss-plugin",
57+
"sass",
58+
"node",
59+
"lib",
5360
"libsass",
61+
"node-sass",
5462
"preprocessor",
5563
"scss",
5664
"css",
57-
"style"
65+
"style",
66+
"extension"
5867
]
5968
}

test/basic.expect.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)