Skip to content

Commit

Permalink
Enable relative imports when filename is provided (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcPorciuncula authored and giuseppeg committed Feb 25, 2018
1 parent 615ac47 commit 6ffc6b7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions fixtures/entry.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@import './import-target.scss';

p {
color: red;
}
File renamed without changes.
5 changes: 5 additions & 0 deletions fixtures/import-target.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$font: 'Comic Sans MS';

* {
font-family: $font !important;
}
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const sass = require('node-sass')
const path = require('path')

module.exports = (css, settings) => {
const cssWithPlaceholders = css
Expand All @@ -9,9 +10,16 @@ module.exports = (css, settings) => {
`/*%%styled-jsx-placeholder-${id}%%*/`
)

// Add the directory containing the current file to includePaths to enable relative
// imports, only works when the filename is provided
const includePaths = settings.sassOptions && settings.sassOptions.includePaths || []
if (settings.babel && settings.babel.filename) {
includePaths.push(path.dirname(settings.babel.filename));
}

const preprocessed = sass.renderSync(Object.assign({
data: cssWithPlaceholders
}, settings.sassOptions)).css.toString()
}, settings.sassOptions, { includePaths })).css.toString()

return preprocessed
.replace(/styled-jsx-placeholder-(\d+)-(\w*\s*[),;{])/g, (_, id, p1) =>
Expand Down
30 changes: 24 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const assert = require('assert')
const path = require('path')
const fs = require('fs')
const stripIndent = require('strip-indent')
const plugin = require('./')

Expand Down Expand Up @@ -51,7 +53,7 @@ describe('styled-jsx-plugin-sass', () => {
border-bottom: 1px solid %%styled-jsx-placeholder-0%%; }
p img {
display: block; }
%%styled-jsx-placeholder-1%%
`)
)
Expand All @@ -60,9 +62,9 @@ describe('styled-jsx-plugin-sass', () => {
it('works with media queries placeholders', () => {
assert.equal(
plugin(`
p {
display: block;
@media %%styled-jsx-placeholder-0%% { color: red; }
p {
display: block;
@media %%styled-jsx-placeholder-0%% { color: red; }
@media (min-width: %%styled-jsx-placeholder-0%%px) { color: blue; }
@media (min-width: %%styled-jsx-placeholder-0%%) { color: yellow; }
}`,
Expand All @@ -79,7 +81,7 @@ describe('styled-jsx-plugin-sass', () => {
color: blue; } }
@media (min-width: %%styled-jsx-placeholder-0%%) {
p {
color: yellow; } }
color: yellow; } }
`)
)
})
Expand All @@ -98,7 +100,7 @@ describe('styled-jsx-plugin-sass', () => {

it('works with @import', () => {
assert.equal(
plugin('@import "fixture"; p { color: red }', {}).trim(),
plugin('@import "fixtures/fixture"; p { color: red }', {}).trim(),
cleanup(`
div {
color: red; }
Expand All @@ -109,6 +111,22 @@ describe('styled-jsx-plugin-sass', () => {
)
})

it('works with relative @import', () => {
const filename = 'fixtures/entry.scss'
const file = fs.readFileSync(path.join(__dirname, filename))

assert.equal(
plugin(file.toString(), { babel: { filename } }).trim(),
cleanup(`
* {
font-family: "Comic Sans MS" !important; }
p {
color: red; }
`)
)
});

it('applies sassOptions', () => {
assert.equal(
plugin('div { padding: (1 / 3) * 1em }', {
Expand Down

0 comments on commit 6ffc6b7

Please sign in to comment.