Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Dec 25, 2017
1 parent be4e50c commit a7084d6
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

environment:
matrix:
- nodejs_version: 4.0.0
- nodejs_version: 4

version: "{build}"
build: off
Expand Down
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
node_modules
index.bundle.js
package-lock.json
*.log*
*.result.css
.*
!.appveyor.yml
!.editorconfig
!.gitignore
!.rollup.js
!.tape.js
!.travis.yml
*.log*
*.result.css
index.js
13 changes: 13 additions & 0 deletions .rollup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import babel from 'rollup-plugin-babel';

export default {
input: 'index.js',
output: { file: 'index.bundle.js', format: 'cjs' },
plugins: [
babel({
presets: [
['env', { modules: false, targets: { node: 4 } }]
]
})
]
};
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
language: node_js

node_js:
- 4.0.0
- 4

install:
- npm install
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Changes to PostCSS Node Sass
# Changes to PostCSS Sass

### 2.0.0 (December 25, 2017)

- Added: custom `source-map` implemention with fix for
[sass/libsass#2312](https://github.com/sass/libsass/issues/2312)
- Changed: `index.mjs` module for `index.js` module
- Changed: `merge-source-map` dependency for custom `source-map` implemention
- Changed: first plugin detection for custom `source-map` implemention

### 1.0.0 (November 2, 2017)

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Contributing to PostCSS Node Sass
# Contributing to PostCSS Sass

You want to help? You rock! Now, take a moment to be sure your contributions
make sense to everyone else.
Expand Down
14 changes: 5 additions & 9 deletions index.mjs → index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// tooling
import mergeSourceMap from 'merge-source-map';
import mergeSourceMaps from './lib/merge-source-maps';
import postcss from 'postcss';
import sassResolve from '@csstools/sass-import-resolve';
import sass from 'node-sass';
Expand All @@ -19,16 +19,13 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
// sass resolve cache
const cache = {};

// whether this is the first plugin running
const firstPlugin = result.processor.plugins[0] === result.lastPlugin;

return new Promise(
// promise sass results
(resolve, reject) => sass.render(
// pass options directly into node-sass
Object.assign({}, opts, requiredSassConfig, {
file: postConfig.from,
outFile: postConfig.to,
file: `${postConfig.from}#sass`,
outFile: postConfig.from,
data: postCSS,
importer(id, parentId, done) {
// resolve the absolute parent
Expand Down Expand Up @@ -61,14 +58,13 @@ export default postcss.plugin('postcss-sass', opts => (root, result) => {
(sassError, sassResult) => sassError ? reject(sassError) : resolve(sassResult)
)
).then(
// update root to post-node-sass ast
({ css: sassCSS, map: sassMap }) => {
// update root to post-node-sass ast
result.root = postcss.parse(
sassCSS.toString(),
Object.assign({}, postConfig, {
map: {
// merge source maps
prev: firstPlugin ? JSON.parse(sassMap) : mergeSourceMap(
prev: mergeSourceMaps(
postMap.toJSON(),
JSON.parse(sassMap)
)
Expand Down
102 changes: 102 additions & 0 deletions lib/merge-source-maps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// tooling
import { SourceMapConsumer, SourceMapGenerator } from 'source-map';

// special sass source matcher
const sassMatch = /#sass$/;

// returns merged source maps
export default function (...maps) {
// new sourcemap
const generator = new SourceMapGenerator();

// existing sourcemaps
const consumers = maps.map(
map => new SourceMapConsumer(map)
);

consumers.forEach(
consumer => {
// copy each original mapping to the new sourcemap
consumer.eachMapping(
mapping => {
const originalPosition = originalPositionFor(mapping, consumers);

if (originalPosition.source) {
generator.addMapping({
generated: {
line: mapping.generatedLine,
column: mapping.generatedColumn
},
original: {
// use positive numbers to work around sass/libsass#2312
line: Math.abs(originalPosition.line),
column: Math.abs(originalPosition.column)
},
source: originalPosition.source,
name: originalPosition.name
});
}
}
);

// copy each original source to the new sourcemap
consumer.sources.forEach(
source => {
generator._sources.add(source);

const content = consumer.sourceContentFor(source);

if (content !== null) {
generator.setSourceContent(source, content);
}
}
);
}
);

// merged map as json
const mergedMap = JSON.parse(generator);

// clean all special sass sources in merged map
mergedMap.sources = mergedMap.sources.map(
source => source.replace(sassMatch, '')
);

return mergedMap;
}

function originalPositionFor(mapping, consumers) {
// initial positioning
let originalPosition = {
line: mapping.generatedLine,
column: mapping.generatedColumn
};

// special sass sources are mapped in reverse
consumers.slice(0).reverse().forEach(
consumer => {
const possiblePosition = consumer.originalPositionFor(originalPosition);

if (possiblePosition.source) {
if (sassMatch.test(possiblePosition.source)) {
originalPosition = possiblePosition;
}
}
}
);

// regular sources are mapped regularly
consumers.forEach(
consumer => {
const possiblePosition = consumer.originalPositionFor(originalPosition);

if (possiblePosition.source) {
if (!sassMatch.test(possiblePosition.source)) {
originalPosition = possiblePosition;
}
}
}
);

return originalPosition;
}
47 changes: 28 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,59 +1,68 @@
{
"name": "@csstools/postcss-sass",
"version": "1.0.0",
"version": "2.0.0",
"description": "Use Sass as a PostCSS plugin",
"author": "Jonathan Neal <jonathantneal@hotmail.com>",
"license": "CC0-1.0",
"repository": "jonathantneal/postcss-sass",
"homepage": "https://github.com/jonathantneal/postcss-sass#readme",
"bugs": "https://github.com/jonathantneal/postcss-sass/issues",
"main": "index.js",
"module": "index.mjs",
"main": "index.bundle.js",
"module": "index.js",
"files": [
"index.mjs",
"index.js"
"index.js",
"index.bundle.js",
"lib"
],
"scripts": {
"clean": "git clean -X -d -f",
"prepublishOnly": "npm test",
"pretest": "babel index.mjs --presets=env --plugins=add-module-exports --out-file index.js",
"test": "echo 'Running tests...'; npm run test:js && npm run test:tape",
"test:ec": "echint --ignore index.js *.expect.css *.result.css",
"test:js": "eslint *.mjs --cache --ignore-pattern .gitignore",
"pretest": "rollup --silent -c .rollup.js",
"test": "echo 'Running tests...'; npm run test:ec && npm run test:js && npm run test:tape",
"test:ec": "echint --ignore index.bundle.js test",
"test:js": "eslint index.js --cache",
"test:tape": "postcss-tape"
},
"engines": {
"node": ">=4.0.0"
},
"dependencies": {
"@csstools/sass-import-resolve": "^1.0.0",
"merge-source-map": "^1.0.4",
"node-sass": "^4.7.2",
"postcss": "^6.0.14"
"@csstools/sass-import-resolve": "^1.0",
"node-sass": "^4.7",
"postcss": "^6.0",
"source-map": "^0.6"
},
"devDependencies": {
"babel-cli": "^6.26",
"babel-plugin-add-module-exports": "^0.2",
"babel-core": "^6.26",
"babel-eslint": "^8.1",
"babel-preset-env": "^1.6",
"echint": "^4.0",
"eslint": "^4.12",
"eslint": "^4.14",
"eslint-config-dev": "2.0",
"postcss-import": "^11.0",
"postcss-tape": "2.2",
"postcss-unroot": "^1.0",
"pre-commit": "^1.2"
"pre-commit": "^1.2",
"rollup": "^0.53",
"rollup-plugin-babel": "^3.0"
},
"eslintConfig": {
"extends": "dev"
"extends": "dev",
"parser": "babel-eslint"
},
"keywords": [
"postcss",
"css",
"postcss-plugin",
"sass",
"node",
"lib",
"libsass",
"node-sass",
"preprocessor",
"scss",
"css",
"style"
"style",
"extension"
]
}
2 changes: 1 addition & 1 deletion test/basic.expect.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/basic.sassopts.expect.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/imports.expect.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7084d6

Please sign in to comment.