babel-plugin-stylus-compiler
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -11,2 +11,4 @@ 'use strict'; | ||
var _path = require('path'); | ||
var _stylus = require('stylus'); | ||
@@ -16,24 +18,40 @@ | ||
var _minimist = require('minimist'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _minimist2 = _interopRequireDefault(_minimist); | ||
var fileExists = function fileExists(filename) { | ||
try { | ||
var stats = _fs2.default.statSync(filename); | ||
return stats.isFile(filename); | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
return false; | ||
} else { | ||
throw Error(e); | ||
} | ||
} | ||
}; /* global process */ | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var compileStylusFile = function compileStylusFile(jsFile, stylusFile) { | ||
var compileStylusFile = function compileStylusFile(path) { | ||
// try to resolve as file path | ||
var from = resolveModulePath(jsFile); | ||
var path = (0, _path.resolve)(from, stylusFile); | ||
if (!fileExists(path)) { | ||
// try to resolve from node modules | ||
path = (0, _path.resolve)('./node_modules', stylusFile); | ||
} | ||
if (!fileExists(path)) { | ||
throw Error('Cannot find stylus file: ' + stylusFile); | ||
} | ||
var stylusContent = _fs2.default.readFileSync(path, 'utf8'); | ||
var cssContent = (0, _stylus2.default)(stylusContent).render(); | ||
return cssContent; | ||
}; /* global process */ | ||
return (0, _stylus2.default)(stylusContent).include((0, _path.dirname)(path)).render(); | ||
}; | ||
var replaceStylusImportWithCssFn = function replaceStylusImportWithCssFn(t) { | ||
return function (path) { | ||
var argv = (0, _minimist2.default)(process.argv.slice(2)); | ||
var node = path.node; | ||
if (node && node.source && node.source.value && node.source.type === 'StringLiteral' && node.source.value.endsWith('.styl')) { | ||
var id = node.specifiers[0].local.name; | ||
var css = compileStylusFile(argv._[0] + '/' + node.source.value); | ||
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(t.identifier(id), t.stringLiteral(css))])); | ||
} | ||
}; | ||
var resolveModulePath = function resolveModulePath(filename) { | ||
var dir = (0, _path.dirname)(filename); | ||
if ((0, _path.isAbsolute)(dir)) return dir; | ||
if (process.env.PWD) return (0, _path.resolve)(process.env.PWD, dir); | ||
return (0, _path.resolve)(dir); | ||
}; | ||
@@ -46,4 +64,11 @@ | ||
visitor: { | ||
ImportDeclaration: { | ||
exit: replaceStylusImportWithCssFn(t) | ||
ImportDeclaration: function ImportDeclaration(path, state) { | ||
var node = path.node; | ||
if (node && node.source && node.source.value && node.source.type === 'StringLiteral' && node.source.value.endsWith('.styl')) { | ||
var jsFile = state.file.opts.filename; | ||
var stylusFile = node.source.value; | ||
var css = compileStylusFile(jsFile, stylusFile); | ||
var id = node.specifiers[0].local.name; | ||
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(t.identifier(id), t.stringLiteral(css))])); | ||
} | ||
} | ||
@@ -50,0 +75,0 @@ } |
/* global process */ | ||
import fs from 'fs'; | ||
import { dirname, isAbsolute, resolve } from 'path'; | ||
import stylus from 'stylus'; | ||
import minimist from 'minimist'; | ||
const compileStylusFile = path => { | ||
const fileExists = filename => { | ||
try { | ||
const stats = fs.statSync(filename); | ||
return stats.isFile(filename); | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
return false; | ||
} else { | ||
throw Error(e); | ||
} | ||
} | ||
} | ||
const compileStylusFile = (jsFile, stylusFile) => { | ||
// try to resolve as file path | ||
const from = resolveModulePath(jsFile); | ||
let path = resolve(from, stylusFile); | ||
if (!fileExists(path)) { | ||
// try to resolve from node modules | ||
path = resolve('./node_modules', stylusFile) | ||
} | ||
if (!fileExists(path)) { | ||
throw Error('Cannot find stylus file: ' + stylusFile); | ||
} | ||
const stylusContent = fs.readFileSync(path, 'utf8'); | ||
const cssContent = stylus(stylusContent).render(); | ||
return cssContent; | ||
}; | ||
return stylus(stylusContent).include(dirname(path)).render(); | ||
} | ||
const replaceStylusImportWithCssFn = t => { | ||
return (path) => { | ||
const argv = minimist(process.argv.slice(2)); | ||
const node = path.node; | ||
if (node && node.source && node.source.value && node.source.type === 'StringLiteral' && node.source.value.endsWith('.styl')) { | ||
const id = node.specifiers[0].local.name; | ||
const css = compileStylusFile(argv._[0] + '/' + node.source.value); | ||
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(t.identifier(id), t.stringLiteral(css))])); | ||
} | ||
}; | ||
}; | ||
const resolveModulePath = (filename) => { | ||
const dir = dirname(filename); | ||
if (isAbsolute(dir)) return dir; | ||
if (process.env.PWD) return resolve(process.env.PWD, dir); | ||
return resolve(dir); | ||
} | ||
@@ -28,7 +47,14 @@ export default ({types: t}) => { | ||
visitor: { | ||
ImportDeclaration: { | ||
exit: replaceStylusImportWithCssFn(t), | ||
}, | ||
ImportDeclaration: (path, state) => { | ||
const node = path.node; | ||
if (node && node.source && node.source.value && node.source.type === 'StringLiteral' && node.source.value.endsWith('.styl')) { | ||
const jsFile = state.file.opts.filename; | ||
const stylusFile = node.source.value; | ||
const css = compileStylusFile(jsFile, stylusFile); | ||
const id = node.specifiers[0].local.name; | ||
path.replaceWith(t.variableDeclaration('var', [t.variableDeclarator(t.identifier(id), t.stringLiteral(css))])); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
{ | ||
"name": "babel-plugin-stylus-compiler", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A Babel 6 plugin for compiling stylus files into css", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -28,2 +28,5 @@ # babel-plugin-stylus-compiler | ||
## Change log | ||
### 1.0.2 | ||
* Add more robust support for resolving `.styl` file in node_modules | ||
### 1.0.1 | ||
@@ -30,0 +33,0 @@ * Add .npmignore so `prepublish` works |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8832
9
138
37
6