Skip to content

Instantly share code, notes, and snippets.

@cognitom
Last active July 3, 2016 05:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cognitom/0ee8754eb2ee3ae3f7d270c9f47f9021 to your computer and use it in GitHub Desktop.
Save cognitom/0ee8754eb2ee3ae3f7d270c9f47f9021 to your computer and use it in GitHub Desktop.
How to use postcss with Riot.js
<app>
<h1>Riot</h1>
<p>A React-like user interface micro-library.</p>
<style scoped type="external">
:scope {
display: block;
}
h1 {
border-bottom: 1px solid black;
flex: auto;
}
</style>
</app>
'use strict'
const
co = require('co'),
riot = require('riot-compiler'),
postcss = require('postcss'),
autoprefixer = require('autoprefixer'),
fsp = require('fs-promise'),
path = require('path')
let css = ''
// 独自パーサの定義
riot.parsers.css.external = (tagName, str) => {
css += riot.style(str, tagName, 'scoped-css')
return ''
}
co(function* (config) {
const
src = 'src/app.tag',
dist = 'dist/app.js',
distCss = 'dist/style.css',
tag = yield fsp.readFile(src, 'utf8'), // ファイルの読み出し
js = riot.compile(tag), // タグのコンパイル
p = postcss().use(autoprefixer()), // PostCSSのプラグイン指定
processed = yield p.process(css, { src, distCss }) // PostCSSの処理
yield fsp.mkdirs(path.dirname(dist)) // 書き出し先のフォルダを確保
yield fsp.writeFile(dist, js) // JavaScriptファイルに出力
yield fsp.writeFile(distCss, processed) // CSSファイルに出力
})
app,[riot-tag="app"],[data-is="app"]{ display: block; } app h1,[riot-tag="app"] h1,[data-is="app"] h1{ border-bottom: 1px solid black; -webkit-box-flex: 1; -ms-flex: auto; flex: auto; }
@cognitom
Copy link
Author

cognitom commented Jul 2, 2016

最後のstyle.cssは出力結果です。
やっていることは次の通り。

  1. externalというスタイルのタイプとそのパーサーを用意
  2. パーサー内部でCSSを集める
  3. PostCSSの処理をかける
  4. JavaScriptと別途、CSSをファイルに書き出し

@ampcpmgp
Copy link

ampcpmgp commented Jul 3, 2016

サンプル作っていただき有難うございます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment