This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aleksandr Petrov
committed
Feb 12, 2016
0 parents
commit 6add7c4
Showing
15 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"ignore": [ | ||
"node_modules/**/*" | ||
], | ||
"presets": [ | ||
"es2015", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
node_modules | ||
coverage | ||
npm-debug.log | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
node_modules | ||
coverage | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "4" | ||
- "5" | ||
script: | ||
- npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "stylus-import-if-exist", | ||
"version": "1.0.0", | ||
"description": "Stylus import which does not fail when import non-exist files", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-core/register", | ||
"build": "babel src --out-dir dist", | ||
"coverage": "isparta cover _mocha index.js --include-all-sources -- --require babel-core/register", | ||
"prepublish": "npm run build", | ||
"precoveralls": "npm run coverage", | ||
"coveralls": "coveralls < coverage/lcov.info" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/dzhiriki/stylus-import-if-exist.git" | ||
}, | ||
"keywords": [ | ||
"stylus", | ||
"import" | ||
], | ||
"author": "Aleksandr Petrov <[email protected]> (https://github.com/dzhiriki)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/dzhiriki/stylus-import-if-exist/issues" | ||
}, | ||
"homepage": "https://github.com/dzhiriki/stylus-import-if-exist#readme", | ||
"devDependencies": { | ||
"babel": "^6.5.1", | ||
"babel-cli": "^6.5.1", | ||
"babel-core": "^6.5.1", | ||
"babel-preset-es2015": "^6.5.0", | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.6", | ||
"isparta": "^4.0.0", | ||
"mocha": "^2.4.5", | ||
"stylus": "^0.53.0" | ||
}, | ||
"dependencies": { | ||
"glob": "^7.0.0" | ||
}, | ||
"peerDependencies": { | ||
"stylus": "1.x" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Stylus Import If Exist | ||
================= | ||
|
||
[![Build Status](https://travis-ci.org/dzhiriki/stylus-import-if-exist.svg?branch=master)](https://travis-ci.org/dzhiriki/stylus-import-if-exist) | ||
[![Coverage Status](https://coveralls.io/repos/github/dzhiriki/stylus-import-if-exist/badge.svg?branch=master)](https://coveralls.io/github/dzhiriki/stylus-import-if-exist?branch=master) | ||
[![Dependency Status](https://david-dm.org/dzhiriki/stylus-import-if-exist.svg?style=flat-square)](https://david-dm.org/dzhiriki/stylus-import-if-exist) | ||
|
||
Stylus fail build when you try import file which not exist. In some cases it's bad behavior. | ||
This plugin solve this problem. | ||
|
||
### Installation | ||
|
||
You can install through npm as such: `npm install stylus-import-if-exist` | ||
|
||
### Usage | ||
|
||
You can include stylus-import-if-exist as a normal stylus plugin. Example: | ||
|
||
```js | ||
var stylus = require('stylus'); | ||
var importIfExist = require('stylus-import-if-exist'); | ||
|
||
stylus(css) | ||
.use(importIfExist()) | ||
.render(function(err, output){ | ||
console.log(output); | ||
}); | ||
``` | ||
|
||
You should replace all your `@import` with `import()`. For example, instead use `@import 'example'` you should use `import(example)`. If file exist, it will be imported; if not – nothing will happen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import stylus, { nodes } from 'stylus'; | ||
import path from 'path'; | ||
import glob from 'glob'; | ||
|
||
export default () => (style) => { | ||
style.define('import', (file) => { | ||
const res = new nodes.Root(); | ||
const dirname = path.dirname(file.filename); | ||
const wildcard = path.join(dirname, file.val).replace(/(\.styl)?$/, '.styl'); | ||
const files = glob.sync(wildcard); | ||
|
||
files.forEach((file) => { | ||
res.push( | ||
new nodes.Import( | ||
new nodes.String(file) | ||
) | ||
); | ||
}); | ||
|
||
return res; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@import 'sample/1' | ||
@import 'sample/2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import 'sample/*'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import('sample/*'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import('sample/1') | ||
import('sample/2') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import('some/file/which/not/exist'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
div | ||
display inline-block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
span | ||
display block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect } from 'chai'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import stylus from 'stylus'; | ||
import importIfExist from '../src/index.js'; | ||
|
||
const testPath = path.join(__dirname, 'fixtures'); | ||
|
||
function getFile(file) { | ||
return fs.readFileSync(file, 'utf8'); | ||
} | ||
|
||
function render(file) { | ||
return new Promise(function(resolve, reject) { | ||
const fileName = path.join(testPath, `${file}.styl`) | ||
stylus(getFile(fileName)) | ||
.use(importIfExist()) | ||
.set('filename', fileName) | ||
.render((err, css) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
}; | ||
|
||
resolve(css); | ||
}) | ||
}) | ||
} | ||
|
||
describe('import', () => { | ||
it('should not fail on import not-exist file', () => | ||
render('not-exist') | ||
) | ||
|
||
it('should contain same styles as real import when import files', () => | ||
Promise.all([render('import'), render('import-real')]) | ||
.then(([importContent, importRealContent]) => | ||
expect(importContent).to.equal(importRealContent) | ||
) | ||
) | ||
|
||
it('should contain same styles as real import when import by wildcard', () => | ||
Promise.all([render('import-wildcard'), render('import-wildcard-real')]) | ||
.then(([importContent, importRealContent]) => | ||
expect(importContent).to.equal(importRealContent) | ||
) | ||
) | ||
}) |