Skip to content

Commit

Permalink
Fix sync issue (#35)
Browse files Browse the repository at this point in the history
It should fix #31 #32 #34

Remove `deasync` in favor of `spawnSync`.
  • Loading branch information
tldrd0117 committed Sep 6, 2020
1 parent ec8a46a commit 52a30d9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
24 changes: 11 additions & 13 deletions index.js
@@ -1,23 +1,21 @@
const loopWhile = require('deasync').loopWhile
const processor = require('./processor')

const { spawnSync } = require('child_process');
const path = require('path');

module.exports = (css, settings) => {
const cssWithPlaceholders = css
.replace(/%%styled-jsx-placeholder-(\d+)%%/g, (_, id) =>
`/*%%styled-jsx-placeholder-${id}%%*/`
)
let processedCss
let wait = true

function resolved(result) {
processedCss = result
wait = false
}
const result = spawnSync("node",[path.resolve(__dirname, "processor.js")],{
input: JSON.stringify({
css: cssWithPlaceholders,
settings
}),
encoding: "utf8"
});

processor(cssWithPlaceholders, settings)
.then(resolved)
.catch(resolved)
loopWhile(() => wait)
processedCss = result.stdout

if (processedCss instanceof Error || processedCss.name === 'CssSyntaxError') {
throw processedCss
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -22,7 +22,6 @@
},
"homepage": "https://github.com/giuseppeg/styled-jsx-plugin-postcss#readme",
"dependencies": {
"deasync": "^0.1.13",
"postcss": "^7.0.2",
"postcss-load-plugins": "^2.3.0"
},
Expand Down
15 changes: 13 additions & 2 deletions processor.js
Expand Up @@ -6,7 +6,6 @@ let _processor

function processor(src, options) {
options = options || {}

let loaderPromise
if (!plugins) {
loaderPromise = loader(options.env || process.env, options.path, { argv: false })
Expand All @@ -27,4 +26,16 @@ function processor(src, options) {
.then(result => result.css)
}

module.exports = processor
let input = "";
process.stdin.on("data", (data) => {
input += data.toString();
});

process.stdin.on("end", () => {
const inputData = JSON.parse(input)
processor(inputData.css, inputData.settings).then(function(result){
process.stdout.write(result)
})
})

// module.exports = processor
13 changes: 12 additions & 1 deletion test.js
Expand Up @@ -15,7 +15,8 @@ describe('styled-jsx-plugin-postcss', () => {

it('works with placeholders', () => {
assert.equal(
plugin('p { color: %%styled-jsx-placeholder-0%%; & img { display: block; } } %%styled-jsx-placeholder-1%%'), 'p { color: %%styled-jsx-placeholder-0%% } p img { display: block; } %%styled-jsx-placeholder-1%%'
plugin('p { color: %%styled-jsx-placeholder-0%%; & img { display: block; } } %%styled-jsx-placeholder-1%%')
, 'p { color: %%styled-jsx-placeholder-0%% } p img { display: block; } %%styled-jsx-placeholder-1%%'
)
})

Expand All @@ -25,4 +26,14 @@ describe('styled-jsx-plugin-postcss', () => {
'div { color: red; } p { color: red }'
)
})

it("works with quotes and other characters", () => {
assert.equal(
plugin(`@import "./fixture.css"; * { color: red; font-family: 'Times New Roman'; }
li:after{ content: "!@#$%^&*()_+"}
ul li:before{ content: "{res:{res:'korea'}}"; }`),
`div { color: red; } * { color: red; font-family: 'Times New Roman'; } li:after{ content: "!@#$%^&*()_+"} ul li:before{ content: "{res:{res:'korea'}}"; }`
)
})

})

0 comments on commit 52a30d9

Please sign in to comment.