履歴: プログラミング/riot.js

各行は、その日時に書かれた内容です。追加・削除は、1 つ前の版との行数の差です (全 7 版)。「差分」は比較する版、「基準」は比べる相手の版を選びます

日時タイトル文字数追加削除内容差分基準
2023/4/5 19:13:23プログラミング/riot.js 最新版8,241+99 -1表示表示中—
2023/3/29 15:28:52プログラミング/riot.js 5,841+193 -81表示差分基準
2018/3/8 5:04:39プログラミング/riot.js 3,269+43 -0表示差分選択
2017/7/20 2:30:46プログラミング/riot.js 2,274+2 -1表示差分選択
2017/7/19 8:56:34プログラミング/riot.js 2,198+39 -1表示差分選択
2017/7/18 13:18:20プログラミング/riot.js 716+10 -0表示差分選択
2017/7/18 12:35:55プログラミング/riot.js 424— —表示—選択

基準: プログラミング/riot.js (2023/3/29 15:28:52) → 比較: プログラミング/riot.js (2023/4/5 19:13:23) 最新版

@@ -190,6 +190,105 @@
install 時には this にあたるところに component と書かなければならないことに注意が必要。
+* esbuild による riot プロジェクトのビルド [#wb4d7e8b]
+
+esbuild のプラグインとして riot 処理を組み込むとスムーズに行った。
+
+https://esbuild.github.io/plugins/#on-load を参考にして、
+riot-build.mjs として以下の内容を入れておく。
+
+ LANG:js
+ import * as esbuild from 'esbuild'
+ import fs from 'node:fs'
+ import * as riot from '@riotjs/compiler'
+ import * as path from 'path'
+ import sass from 'sass'
+
+ // riot に scss プラグインを挿入
+ riot.registerPreprocessor('css', 'scss', (code, { options }) => {
+ const { file } = options
+
+ let result = sass.compileString(
+ code, {
+ loadPaths: [path.dirname(file)],
+ sourceMap: true,
+ color: true,
+ verbose: true,
+ }
+ )
+
+ const map = {...result.sourceMap}
+ map.sources = [path.relative(process.cwd(), file)]
+ map.file = path.basename(file)
+
+ return {
+ code: result.css,
+ map: map
+ }
+ })
+
+ // esbuild の riot 処理プラグイン
+ const riotPlugin = {
+ name: 'riot',
+ setup(build) {
+ // process ".riot" with the riot compiler
+ build.onLoad({ filter: /\.riot$/ }, async (args) => {
+ let src = await fs.promises.readFile(args.path, 'utf8')
+ let {code, map} = riot.compile(src);
+
+ map.sources = [path.relative(process.cwd(), args.path)];
+ map.file = path.basename(args.path);
+ map = Buffer.from(JSON.stringify(map)).toString('base64');
+
+ // ソースマップをインラインに添付する
+ code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${map}`;
+
+ return {
+ contents: code,
+ loader: 'js',
+ }
+ })
+ },
+ }
+
+ const options = {}
+ process.argv.slice(2).forEach((arg)=>{
+ if(arg.slice(0,2) == '--') {
+ let [k, v] = arg.split('=')
+ let array = (v || '').split(',')
+ if(!v) v = true
+ if(v == 'true') v = true
+ if(v == 'false') v = false
+ options[k.slice(2)] = array.length == 1 ? v : array
+ } else {
+ options.entryPoints ??= []
+ options.entryPoints.push(arg)
+ }
+ })
+
+ options.plugins = [riotPlugin]
+ await esbuild.build(options)
+
+後はコンソールから、
+
+ LANG:console
+ $ node riot-build.mjs src/index.js --outfile=dist/index.js --bundle --sourcemap=inline --minifyWhitespace
+
+などとすれば .riot ファイルを import しているのをそのままバンドルできる。
+
+ import riot from 'riot'
+ import tag1 from './app.riot'
+ import tag2 from './component1.riot'
+ import tag3 from './component2.riot'
+
+ const tags = [tag1, tag2, tag3]
+ for(let tag of tags)
+ riot.register(tag.name, tag)
+
+ riot.mount('app')
+
+みたいな。
+
* 注意点 [#t5b9a7f0]
** <slot /> はトップレベルでは使えないみたい? @ 2023-03-29 [#a13e5c23]
@@ -222,4 +321,3 @@
* 古い内容 [#kc3b56c3]
[[プログラミング/riot.js/古い内容]] に移しました。
-