Skip to content

Commit 6ffc6b7

Browse files
MarcPorciunculagiuseppeg
authored andcommitted
Enable relative imports when filename is provided (#11)
1 parent 615ac47 commit 6ffc6b7

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

fixtures/entry.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import './import-target.scss';
2+
3+
p {
4+
color: red;
5+
}
File renamed without changes.

fixtures/import-target.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$font: 'Comic Sans MS';
2+
3+
* {
4+
font-family: $font !important;
5+
}

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const sass = require('node-sass')
2+
const path = require('path')
23

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

13+
// Add the directory containing the current file to includePaths to enable relative
14+
// imports, only works when the filename is provided
15+
const includePaths = settings.sassOptions && settings.sassOptions.includePaths || []
16+
if (settings.babel && settings.babel.filename) {
17+
includePaths.push(path.dirname(settings.babel.filename));
18+
}
19+
1220
const preprocessed = sass.renderSync(Object.assign({
1321
data: cssWithPlaceholders
14-
}, settings.sassOptions)).css.toString()
22+
}, settings.sassOptions, { includePaths })).css.toString()
1523

1624
return preprocessed
1725
.replace(/styled-jsx-placeholder-(\d+)-(\w*\s*[),;{])/g, (_, id, p1) =>

test.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const assert = require('assert')
2+
const path = require('path')
3+
const fs = require('fs')
24
const stripIndent = require('strip-indent')
35
const plugin = require('./')
46

@@ -51,7 +53,7 @@ describe('styled-jsx-plugin-sass', () => {
5153
border-bottom: 1px solid %%styled-jsx-placeholder-0%%; }
5254
p img {
5355
display: block; }
54-
56+
5557
%%styled-jsx-placeholder-1%%
5658
`)
5759
)
@@ -60,9 +62,9 @@ describe('styled-jsx-plugin-sass', () => {
6062
it('works with media queries placeholders', () => {
6163
assert.equal(
6264
plugin(`
63-
p {
64-
display: block;
65-
@media %%styled-jsx-placeholder-0%% { color: red; }
65+
p {
66+
display: block;
67+
@media %%styled-jsx-placeholder-0%% { color: red; }
6668
@media (min-width: %%styled-jsx-placeholder-0%%px) { color: blue; }
6769
@media (min-width: %%styled-jsx-placeholder-0%%) { color: yellow; }
6870
}`,
@@ -79,7 +81,7 @@ describe('styled-jsx-plugin-sass', () => {
7981
color: blue; } }
8082
@media (min-width: %%styled-jsx-placeholder-0%%) {
8183
p {
82-
color: yellow; } }
84+
color: yellow; } }
8385
`)
8486
)
8587
})
@@ -98,7 +100,7 @@ describe('styled-jsx-plugin-sass', () => {
98100

99101
it('works with @import', () => {
100102
assert.equal(
101-
plugin('@import "fixture"; p { color: red }', {}).trim(),
103+
plugin('@import "fixtures/fixture"; p { color: red }', {}).trim(),
102104
cleanup(`
103105
div {
104106
color: red; }
@@ -109,6 +111,22 @@ describe('styled-jsx-plugin-sass', () => {
109111
)
110112
})
111113

114+
it('works with relative @import', () => {
115+
const filename = 'fixtures/entry.scss'
116+
const file = fs.readFileSync(path.join(__dirname, filename))
117+
118+
assert.equal(
119+
plugin(file.toString(), { babel: { filename } }).trim(),
120+
cleanup(`
121+
* {
122+
font-family: "Comic Sans MS" !important; }
123+
124+
p {
125+
color: red; }
126+
`)
127+
)
128+
});
129+
112130
it('applies sassOptions', () => {
113131
assert.equal(
114132
plugin('div { padding: (1 / 3) * 1em }', {

0 commit comments

Comments
 (0)