Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Commit

Permalink
init plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Petrov committed Feb 12, 2016
0 parents commit 6add7c4
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"ignore": [
"node_modules/**/*"
],
"presets": [
"es2015",
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
node_modules
coverage
npm-debug.log
dist
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules
coverage
npm-debug.log
6 changes: 6 additions & 0 deletions .travis.yml
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
45 changes: 45 additions & 0 deletions package.json
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"
}
}
30 changes: 30 additions & 0 deletions readme.md
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.
22 changes: 22 additions & 0 deletions src/index.js
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;
})
}
2 changes: 2 additions & 0 deletions test/fixtures/import-real.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import 'sample/1'
@import 'sample/2'
1 change: 1 addition & 0 deletions test/fixtures/import-wildcard-real.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import 'sample/*';
1 change: 1 addition & 0 deletions test/fixtures/import-wildcard.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('sample/*');
2 changes: 2 additions & 0 deletions test/fixtures/import.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import('sample/1')
import('sample/2')
1 change: 1 addition & 0 deletions test/fixtures/not-exist.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import('some/file/which/not/exist');
2 changes: 2 additions & 0 deletions test/fixtures/sample/1.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
div
display inline-block
2 changes: 2 additions & 0 deletions test/fixtures/sample/2.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
span
display block
48 changes: 48 additions & 0 deletions test/index.js
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)
)
)
})

0 comments on commit 6add7c4

Please sign in to comment.