kintone-plugin-ide 0.0.0-stage → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +122 -2
- package/dist/index.js +2576 -0
- package/dist/kintone-plugin-ide.iife.js +3567 -0
- package/package.json +57 -4
- package/src/build/build.js +87 -0
- package/src/build/http-plugin.js +32 -0
- package/src/build/transformers.js +168 -0
- package/src/build/vfs-plugin.js +60 -0
- package/src/config.js +58 -0
- package/src/deps.js +41 -0
- package/src/ide/actions/misc.js +74 -0
- package/src/ide/actions/npm.js +103 -0
- package/src/ide/actions/release.js +57 -0
- package/src/ide/actions/save.js +54 -0
- package/src/ide/actions/zip.js +59 -0
- package/src/ide/commands.js +96 -0
- package/src/ide/css.js +50 -0
- package/src/ide/dialog.js +51 -0
- package/src/ide/editor.js +155 -0
- package/src/ide/index.js +94 -0
- package/src/ide/layout.js +89 -0
- package/src/ide/sample.js +168 -0
- package/src/ide/state.js +58 -0
- package/src/ide/terminal.js +57 -0
- package/src/ide/tree.js +224 -0
- package/src/ide/workspace-fs.js +173 -0
- package/src/index.js +48 -0
- package/src/kintone/api.js +24 -0
- package/src/kintone/plugin.js +37 -0
- package/src/npm/registry.js +32 -0
- package/src/npm/vendor.js +69 -0
- package/src/packer/packer.js +50 -0
- package/src/util/bytes.js +15 -0
- package/src/util/dom.js +15 -0
- package/src/util/idb.js +18 -0
- package/src/util/path.js +14 -0
- package/src/util/settings.js +5 -0
- package/types/index.d.ts +49 -0
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kintone-plugin-ide",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "kintone のレコードを、そのままプラグインの開発環境(エディタ・ターミナル・ビルド・署名・インストール)にする",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./iife": "./dist/kintone-plugin-ide.iife.js",
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist",
|
|
19
|
+
"src",
|
|
20
|
+
"types",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "node build.mjs",
|
|
27
|
+
"watch": "node build.mjs --watch",
|
|
28
|
+
"test": "node test/smoke.mjs",
|
|
29
|
+
"prepublishOnly": "npm run build && npm test"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"fflate": "^0.8.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"esbuild": "^0.25.0"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/yusura0929/kintone-plugin-ide.git"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/yusura0929/kintone-plugin-ide#readme",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/yusura0929/kintone-plugin-ide/issues"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [
|
|
46
|
+
"kintone",
|
|
47
|
+
"kintone-plugin",
|
|
48
|
+
"ide",
|
|
49
|
+
"esbuild",
|
|
50
|
+
"monaco"
|
|
51
|
+
],
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=22"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// manifest.json を読んで、必要なファイルをビルドし、contents.zip を作る
|
|
2
|
+
import { zipSync } from 'fflate';
|
|
3
|
+
import { loadEsbuild, loadValidator, loadCompiler } from '../deps.js';
|
|
4
|
+
import { createTransformer } from './transformers.js';
|
|
5
|
+
import { readPackageJson } from '../npm/registry.js';
|
|
6
|
+
import { kb, toBytes } from '../util/bytes.js';
|
|
7
|
+
import { normalize, isVendor } from '../util/path.js';
|
|
8
|
+
import { vfsPlugin } from './vfs-plugin.js';
|
|
9
|
+
|
|
10
|
+
export function sourceList(manifest) {
|
|
11
|
+
const list = [];
|
|
12
|
+
for (const [k, t] of [['desktop', 'js'], ['desktop', 'css'], ['mobile', 'js'], ['mobile', 'css'], ['config', 'js'], ['config', 'css']]) {
|
|
13
|
+
for (const f of (manifest[k] && manifest[k][t]) || []) if (!/^https?:\/\//.test(f)) list.push(normalize(f));
|
|
14
|
+
}
|
|
15
|
+
if (manifest.config && manifest.config.html) list.push(normalize(manifest.config.html));
|
|
16
|
+
list.push('manifest.json', normalize(manifest.icon));
|
|
17
|
+
return [...new Set(list)];
|
|
18
|
+
}
|
|
19
|
+
// "js/desktop.js" → src/desktop.ts など / "css/desktop.css" → src/desktop.css
|
|
20
|
+
export const entryCandidates = (out) => {
|
|
21
|
+
const base = out.replace(/^(js|css)\//, '').replace(/\.(js|css)$/, '');
|
|
22
|
+
return out.endsWith('.css') ? ['css', 'scss', 'sass', 'less'].map((e) => `src/${base}.${e}`) : ['ts', 'tsx', 'js', 'jsx'].map((e) => `src/${base}.${e}`);
|
|
23
|
+
};
|
|
24
|
+
const fmtMsg = (m) => (m.location ? `${m.location.file.replace(/^vfs:/, '')}:${m.location.line}:${m.location.column + 1} ` : '') + m.text;
|
|
25
|
+
|
|
26
|
+
export async function validateManifest(manifest, outputs, log) {
|
|
27
|
+
try {
|
|
28
|
+
const validate = await loadValidator();
|
|
29
|
+
const r = validate(manifest, {
|
|
30
|
+
relativePath: (p) => outputs.has(normalize(p)),
|
|
31
|
+
maxFileSize: (max, p) => !outputs.has(normalize(p)) || outputs.get(normalize(p)).length <= max,
|
|
32
|
+
fileExists: (p) => outputs.has(normalize(p)),
|
|
33
|
+
});
|
|
34
|
+
for (const w of r.warnings || []) log.warn(`manifest.json: ${w.message}`);
|
|
35
|
+
if (!r.valid) {
|
|
36
|
+
for (const e of r.errors || []) log.error(`manifest.json${e.instancePath || ''}: ${e.message}`);
|
|
37
|
+
throw new Error('manifest.json の検証に失敗しました');
|
|
38
|
+
}
|
|
39
|
+
log.ok('manifest.json の検証: OK');
|
|
40
|
+
} catch (e) {
|
|
41
|
+
if (/検証に失敗/.test(e.message)) throw e;
|
|
42
|
+
log.warn(`manifest.json の検証をスキップしました (${e.message})`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function readManifest(files) {
|
|
47
|
+
if (!files.has('manifest.json')) throw new Error('manifest.json がありません');
|
|
48
|
+
let manifest;
|
|
49
|
+
try { manifest = JSON.parse(files.get('manifest.json')); } catch (e) { throw new Error('manifest.json がJSONとして読めません: ' + e.message); }
|
|
50
|
+
for (const k of ['manifest_version', 'version', 'type', 'name', 'icon']) if (manifest[k] === undefined) throw new Error(`manifest.json に "${k}" がありません`);
|
|
51
|
+
return manifest;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* npm のパッケージは esbuild がバンドルするので、js/*.js の中に入る。
|
|
56
|
+
* node_modules/ そのものは contents.zip には入らない(manifest に書いたファイルだけが入る)。
|
|
57
|
+
*/
|
|
58
|
+
export async function buildPlugin(files, log, { prod = false, vendor } = {}) {
|
|
59
|
+
const manifest = readManifest(files);
|
|
60
|
+
const outputs = new Map();
|
|
61
|
+
const es = await loadEsbuild();
|
|
62
|
+
const transform = createTransformer({ files, log, prod, load: loadCompiler, esbuild: es, deps: readPackageJson(files).dependencies });
|
|
63
|
+
for (const out of sourceList(manifest)) {
|
|
64
|
+
if (files.has(out) && !isVendor(out)) { outputs.set(out, toBytes(files.get(out))); continue; }
|
|
65
|
+
const entry = (out.endsWith('.js') || out.endsWith('.css')) && entryCandidates(out).find((p) => files.has(p));
|
|
66
|
+
if (!entry) throw new Error(`manifest.json の "${out}" がありません${/\.(js|css)$/.test(out) ? `(${entryCandidates(out).join(' / ')} も無し)` : ''}`);
|
|
67
|
+
const t = performance.now();
|
|
68
|
+
const r = await es.build({
|
|
69
|
+
entryPoints: [entry], bundle: true, write: false, outfile: out,
|
|
70
|
+
format: 'iife', target: 'es2020', charset: 'utf8', logLevel: 'silent',
|
|
71
|
+
minify: prod, sourcemap: prod ? false : 'inline', legalComments: prod ? 'none' : 'inline',
|
|
72
|
+
define: {
|
|
73
|
+
'process.env.NODE_ENV': JSON.stringify(prod ? 'production' : 'development'),
|
|
74
|
+
__VUE_OPTIONS_API__: 'true', __VUE_PROD_DEVTOOLS__: 'false', __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
|
|
75
|
+
},
|
|
76
|
+
plugins: [vfsPlugin(files, log, { vendor, transform })],
|
|
77
|
+
}).catch((e) => e);
|
|
78
|
+
for (const w of r.warnings || []) log.warn(fmtMsg(w));
|
|
79
|
+
if (r.errors && r.errors.length) { r.errors.forEach((er) => log.error(fmtMsg(er))); throw new Error(`${entry} のビルドに失敗しました`); }
|
|
80
|
+
const file = r.outputFiles.find((f) => f.path.endsWith(out.endsWith('.css') ? '.css' : '.js'));
|
|
81
|
+
outputs.set(out, file.contents);
|
|
82
|
+
log.ok(`${entry} → ${out} (${kb(file.contents.length)}, ${Math.round(performance.now() - t)} ms${prod ? ', 本番' : ''})`);
|
|
83
|
+
}
|
|
84
|
+
await validateManifest(manifest, outputs, log);
|
|
85
|
+
const entries = {}; for (const [k, v] of outputs) entries[k] = v;
|
|
86
|
+
return { manifest, contentsZip: zipSync(entries, { level: 9 }), outputs };
|
|
87
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// esbuild: http(s) の import を fetch で読む
|
|
2
|
+
import { CONFIG } from '../config.js';
|
|
3
|
+
import { idbGet, idbSet } from '../util/idb.js';
|
|
4
|
+
|
|
5
|
+
// バージョンが確定した URL(/pkg@1.2.3/...)は中身が変わらないので、ブラウザに残しておく
|
|
6
|
+
const immutable = (u) => /@\d+\.\d+\.\d+(-[\w.]+)?\//.test(u) && !/[\^~*]/.test(decodeURIComponent(u));
|
|
7
|
+
|
|
8
|
+
export function httpPlugin(log, cache = new Map()) {
|
|
9
|
+
return {
|
|
10
|
+
name: 'http',
|
|
11
|
+
setup(b) {
|
|
12
|
+
b.onResolve({ filter: /^https?:\/\// }, (a) => ({ path: a.path, namespace: 'http' }));
|
|
13
|
+
// esm.sh から読んだモジュールの中の "/pkg@1.0.0/..." や相対パス
|
|
14
|
+
b.onResolve({ filter: /.*/, namespace: 'http' }, (a) => ({ path: new URL(a.path, a.importer).href, namespace: 'http' }));
|
|
15
|
+
b.onLoad({ filter: /.*/, namespace: 'http' }, async (a) => {
|
|
16
|
+
if (!cache.has(a.path)) {
|
|
17
|
+
const keep = immutable(a.path);
|
|
18
|
+
let text = keep ? await idbGet(`http:${a.path}`) : undefined;
|
|
19
|
+
if (text === undefined) {
|
|
20
|
+
log?.info(`取得: ${a.path.replace(CONFIG.CDN.npm, 'esm.sh')}`);
|
|
21
|
+
const res = await fetch(a.path);
|
|
22
|
+
if (!res.ok) throw new Error(`${res.status} ${a.path}`);
|
|
23
|
+
text = await res.text();
|
|
24
|
+
if (keep) await idbSet(`http:${a.path}`, text);
|
|
25
|
+
}
|
|
26
|
+
cache.set(a.path, text);
|
|
27
|
+
}
|
|
28
|
+
return { contents: cache.get(a.path), loader: /\.css(\?|$)/.test(a.path) ? 'css' : 'js' };
|
|
29
|
+
});
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
// esbuild に渡す前に変換するファイル(コンパイラは使う時に CDN から読む)
|
|
2
|
+
// .svelte Svelte 5 のコンポーネント(CSS は JS に入れて実行時に差し込む)
|
|
3
|
+
// .svelte.ts/.svelte.js Svelte 5 のルーン入りモジュール
|
|
4
|
+
// .vue Vue 3 の SFC(<script setup> / TS / scoped CSS / <style lang="scss">)
|
|
5
|
+
// .scss / .sass / .less CSS に
|
|
6
|
+
// .yaml / .yml export default のオブジェクトに
|
|
7
|
+
// svelte と vue のコンパイラは、package.json に書いたランタイムと同じバージョンを使う。
|
|
8
|
+
import { dirname, normalize } from '../util/path.js';
|
|
9
|
+
import { isBinary } from '../util/bytes.js';
|
|
10
|
+
|
|
11
|
+
export const TRANSFORM_EXT = /\.(svelte|svelte\.[jt]s|vue|s[ac]ss|less|ya?ml)$/;
|
|
12
|
+
|
|
13
|
+
const lineCol = (text, offset) => {
|
|
14
|
+
const before = text.slice(0, offset).split('\n');
|
|
15
|
+
return { line: before.length, column: before[before.length - 1].length };
|
|
16
|
+
};
|
|
17
|
+
const fail = (path, message, loc) => ({ errors: [{ text: message, location: loc ? { file: path, line: loc.line, column: loc.column ?? 0 } : { file: path } }] });
|
|
18
|
+
const injectCss = (css, id) => css.trim()
|
|
19
|
+
? `\n;(()=>{if(typeof document==='undefined'||document.getElementById(${JSON.stringify(id)}))return;const s=document.createElement('style');s.id=${JSON.stringify(id)};s.textContent=${JSON.stringify(css)};document.head.appendChild(s)})();\n`
|
|
20
|
+
: '';
|
|
21
|
+
const hashId = (s) => { let h = 5381; for (let i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0; return h.toString(36); };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {object} o
|
|
25
|
+
* @param {Map<string,string>} o.files
|
|
26
|
+
* @param {(name:string, range?:string)=>Promise<any>} o.load コンパイラを読む(deps.js の loadCompiler)
|
|
27
|
+
* @param {object} o.esbuild esbuild(TS を剥がすのに使う)
|
|
28
|
+
* @param {Record<string,string>} o.deps package.json の dependencies
|
|
29
|
+
*/
|
|
30
|
+
export function createTransformer({ files, log, prod, load, esbuild, deps }) {
|
|
31
|
+
const need = (name) => {
|
|
32
|
+
if (!deps[name]) throw new Error(`${name} が package.json にありません。npm install ${name} してください(ランタイムとコンパイラのバージョンをそろえるため)`);
|
|
33
|
+
return deps[name];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// ---- Sass ----
|
|
37
|
+
async function compileSass(path, text) {
|
|
38
|
+
const sass = await load('sass');
|
|
39
|
+
const toUrl = (p) => new URL(`file:///${p}`);
|
|
40
|
+
const candidates = (p) => {
|
|
41
|
+
const d = dirname(p), b = p.split('/').pop();
|
|
42
|
+
const bases = /\.(s[ac]ss|css)$/.test(b) ? [p] : [`${p}.scss`, `${p}.sass`, `${p}.css`, `${d ? d + '/' : ''}_${b}.scss`, `${d ? d + '/' : ''}_${b}.sass`, `${p}/index.scss`, `${p}/_index.scss`];
|
|
43
|
+
return bases.map(normalize);
|
|
44
|
+
};
|
|
45
|
+
const importer = {
|
|
46
|
+
canonicalize(url, ctx) {
|
|
47
|
+
const base = ctx && ctx.containingUrl ? ctx.containingUrl.pathname.slice(1) : path;
|
|
48
|
+
const raw = url.startsWith('file:///') ? url.slice(8) : normalize(`${dirname(base)}/${url}`);
|
|
49
|
+
const hit = candidates(raw).find((c) => files.has(c));
|
|
50
|
+
return hit ? toUrl(hit) : null;
|
|
51
|
+
},
|
|
52
|
+
load(u) {
|
|
53
|
+
const p = decodeURIComponent(u.pathname.slice(1));
|
|
54
|
+
return { contents: files.get(p), syntax: p.endsWith('.sass') ? 'indented' : p.endsWith('.css') ? 'css' : 'scss' };
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
try {
|
|
58
|
+
const r = sass.compileString(text, { url: toUrl(path), syntax: path.endsWith('.sass') ? 'indented' : 'scss', importers: [importer], style: prod ? 'compressed' : 'expanded', silenceDeprecations: ['import'] });
|
|
59
|
+
return r.css;
|
|
60
|
+
} catch (e) {
|
|
61
|
+
const s = e.span;
|
|
62
|
+
throw Object.assign(new Error(e.sassMessage || e.message), { loc: s ? { line: s.start.line + 1, column: s.start.column } : null, file: s && s.url ? s.url.pathname.slice(1) : path });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ---- Less ----
|
|
67
|
+
async function compileLess(path, text) {
|
|
68
|
+
const mod = await load('less');
|
|
69
|
+
const less = mod.default || mod;
|
|
70
|
+
const vfs = {
|
|
71
|
+
install(lessObj, pm) {
|
|
72
|
+
class VfsFileManager extends lessObj.FileManager {
|
|
73
|
+
supports() { return true; }
|
|
74
|
+
supportsSync() { return false; }
|
|
75
|
+
loadFile(filename, currentDirectory) {
|
|
76
|
+
const base = normalize(`${currentDirectory || dirname(path)}/${filename}`);
|
|
77
|
+
const hit = [base, `${base}.less`, `${base}.css`].find((c) => files.has(c));
|
|
78
|
+
return hit ? Promise.resolve({ contents: files.get(hit), filename: hit }) : Promise.reject({ type: 'File', message: `ファイルが見つかりません: ${filename}` });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
pm.addFileManager(new VfsFileManager());
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
try {
|
|
85
|
+
return (await less.render(text, { filename: path, plugins: [vfs], compress: prod })).css;
|
|
86
|
+
} catch (e) {
|
|
87
|
+
throw Object.assign(new Error(e.message), { loc: e.line ? { line: e.line, column: e.column || 0 } : null, file: e.filename || path });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---- Svelte ----
|
|
92
|
+
async function compileSvelte(path, text) {
|
|
93
|
+
const svelte = await load('svelte', need('svelte'));
|
|
94
|
+
const warn = (w) => log.warn(`${path}${w.start ? `:${w.start.line}:${w.start.column + 1}` : ''} ${w.message}`);
|
|
95
|
+
try {
|
|
96
|
+
if (/\.svelte\.[jt]s$/.test(path)) {
|
|
97
|
+
const js = path.endsWith('.ts') ? (await esbuild.transform(text, { loader: 'ts', sourcefile: path })).code : text;
|
|
98
|
+
const r = svelte.compileModule(js, { filename: path, generate: 'client', dev: !prod });
|
|
99
|
+
r.warnings.forEach(warn);
|
|
100
|
+
return { contents: r.js.code, loader: 'js' };
|
|
101
|
+
}
|
|
102
|
+
const r = svelte.compile(text, { filename: path, generate: 'client', dev: !prod, css: 'injected' });
|
|
103
|
+
r.warnings.forEach(warn);
|
|
104
|
+
return { contents: r.js.code, loader: 'js' };
|
|
105
|
+
} catch (e) {
|
|
106
|
+
throw Object.assign(new Error(e.message), { loc: e.start ? { line: e.start.line, column: e.start.column } : null });
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ---- Vue ----
|
|
111
|
+
async function compileVue(path, text) {
|
|
112
|
+
const sfc = await load('vue', need('vue'));
|
|
113
|
+
const { descriptor, errors } = sfc.parse(text, { filename: path });
|
|
114
|
+
if (errors.length) {
|
|
115
|
+
const e = errors[0];
|
|
116
|
+
throw Object.assign(new Error(e.message), { loc: e.loc ? { line: e.loc.start.line, column: e.loc.start.column - 1 } : null });
|
|
117
|
+
}
|
|
118
|
+
const id = hashId(path);
|
|
119
|
+
const scoped = descriptor.styles.some((s) => s.scoped);
|
|
120
|
+
let code = '', lang = 'js';
|
|
121
|
+
if (descriptor.script || descriptor.scriptSetup) {
|
|
122
|
+
const s = sfc.compileScript(descriptor, { id, inlineTemplate: true, isProd: prod, genDefaultAs: '__sfc__', templateOptions: { compilerOptions: { scopeId: scoped ? `data-v-${id}` : undefined } } });
|
|
123
|
+
code = s.content;
|
|
124
|
+
lang = /tsx?/.test((descriptor.scriptSetup || descriptor.script).lang || '') ? 'ts' : 'js';
|
|
125
|
+
} else code = 'const __sfc__ = {};';
|
|
126
|
+
if (descriptor.template && !descriptor.scriptSetup) {
|
|
127
|
+
const t = sfc.compileTemplate({ source: descriptor.template.content, filename: path, id, scoped, isProd: prod, compilerOptions: { scopeId: scoped ? `data-v-${id}` : undefined } });
|
|
128
|
+
if (t.errors.length) throw new Error(String(t.errors[0].message || t.errors[0]));
|
|
129
|
+
code += `\n${t.code.replace(/\nexport (function|const) (render|ssrRender)/, '\n$1 $2')}\n__sfc__.render = render;`;
|
|
130
|
+
}
|
|
131
|
+
if (scoped) code += `\n__sfc__.__scopeId = "data-v-${id}";`;
|
|
132
|
+
let css = '';
|
|
133
|
+
for (const st of descriptor.styles) {
|
|
134
|
+
let source = st.content;
|
|
135
|
+
if (/^s[ac]ss$/.test(st.lang || '')) source = await compileSass(`${path}.${st.lang}`, source);
|
|
136
|
+
else if (st.lang === 'less') source = await compileLess(`${path}.less`, source);
|
|
137
|
+
const r = sfc.compileStyle({ source, filename: path, id: `data-v-${id}`, scoped: st.scoped, isProd: prod });
|
|
138
|
+
if (r.errors.length) throw new Error(String(r.errors[0].message || r.errors[0]));
|
|
139
|
+
css += r.code + '\n';
|
|
140
|
+
}
|
|
141
|
+
code += injectCss(css, `vue-${id}`);
|
|
142
|
+
code += '\nexport default __sfc__;\n';
|
|
143
|
+
return { contents: code, loader: lang };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// ---- YAML ----
|
|
147
|
+
async function compileYaml(path, text) {
|
|
148
|
+
const yaml = await load('yaml');
|
|
149
|
+
try { return { contents: `export default ${JSON.stringify((yaml.load || yaml.default.load)(text))};`, loader: 'js' }; }
|
|
150
|
+
catch (e) { throw Object.assign(new Error(e.reason || e.message), { loc: e.mark ? { line: e.mark.line + 1, column: e.mark.column } : null }); }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** @returns {Promise<{contents:string, loader:string, resolveDir:string}|{errors:any[]}|null>} */
|
|
154
|
+
return async function transform(path, text) {
|
|
155
|
+
if (!TRANSFORM_EXT.test(path) || isBinary(text)) return null;
|
|
156
|
+
try {
|
|
157
|
+
let r;
|
|
158
|
+
if (/\.svelte(\.[jt]s)?$/.test(path)) r = await compileSvelte(path, text);
|
|
159
|
+
else if (path.endsWith('.vue')) r = await compileVue(path, text);
|
|
160
|
+
else if (/\.s[ac]ss$/.test(path)) r = { contents: await compileSass(path, text), loader: 'css' };
|
|
161
|
+
else if (path.endsWith('.less')) r = { contents: await compileLess(path, text), loader: 'css' };
|
|
162
|
+
else r = await compileYaml(path, text);
|
|
163
|
+
return { ...r, resolveDir: '/' + dirname(path) };
|
|
164
|
+
} catch (e) {
|
|
165
|
+
return fail(e.file || path, e.message.split('\n')[0], e.loc);
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// esbuild: IDE の中のファイル(Map)を読む。npm のパッケージは node_modules/ から
|
|
2
|
+
import { dirname, ext, normalize } from '../util/path.js';
|
|
3
|
+
import { isBinary, toBytes } from '../util/bytes.js';
|
|
4
|
+
import { parseSpecifier, readPackageJson, npmUrl } from '../npm/registry.js';
|
|
5
|
+
import { pkgDir, subFile } from '../npm/vendor.js';
|
|
6
|
+
import { httpPlugin } from './http-plugin.js';
|
|
7
|
+
import { SHARED_RUNTIMES } from '../config.js';
|
|
8
|
+
|
|
9
|
+
const LOADERS = { ts: 'ts', mts: 'ts', tsx: 'tsx', js: 'js', mjs: 'js', jsx: 'jsx', json: 'json', css: 'css', svg: 'text', html: 'text', txt: 'text', md: 'text' };
|
|
10
|
+
const EXTS = ['', '.ts', '.tsx', '.js', '.jsx', '.mjs', '.json', '.css', '.svelte.ts', '.svelte.js', '/index.ts', '/index.tsx', '/index.js'];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {Map<string,string>} files
|
|
14
|
+
* @param {{ vendor?: (name: string, sub: string) => Promise<string|null>, transform?: Function }} opts
|
|
15
|
+
* vendor: node_modules に無いものを取ってきて置き、そのパスを返す(IDE から渡す)
|
|
16
|
+
* transform: .svelte / .vue などを変換する(transformers.js)
|
|
17
|
+
*/
|
|
18
|
+
export function vfsPlugin(files, log, { vendor, transform } = {}) {
|
|
19
|
+
const deps = readPackageJson(files).dependencies;
|
|
20
|
+
const warned = new Set();
|
|
21
|
+
const http = httpPlugin(log);
|
|
22
|
+
return {
|
|
23
|
+
name: 'kintone-vfs',
|
|
24
|
+
setup(b) {
|
|
25
|
+
http.setup(b);
|
|
26
|
+
b.onResolve({ filter: /^(\.{0,2}\/|[^@\w])/ }, (a) => {
|
|
27
|
+
if (a.namespace === 'http') return undefined;
|
|
28
|
+
const base = normalize(a.path.startsWith('/') ? a.path : `${dirname(a.importer)}/${a.path}`);
|
|
29
|
+
for (const e of EXTS) if (files.has(base + e)) return { path: base + e, namespace: 'vfs' };
|
|
30
|
+
// ESM の書き方("./foo.js" と書いて foo.ts を指す)
|
|
31
|
+
const ts = base.replace(/\.js$/, '.ts');
|
|
32
|
+
if (ts !== base && files.has(ts)) return { path: ts, namespace: 'vfs' };
|
|
33
|
+
return { errors: [{ text: `ファイルが見つかりません: ${a.path}` }] };
|
|
34
|
+
});
|
|
35
|
+
b.onResolve({ filter: /^[@\w]/ }, async (a) => {
|
|
36
|
+
if (a.kind === 'entry-point') return { path: normalize(a.path), namespace: 'vfs' };
|
|
37
|
+
if (a.namespace === 'http') return undefined;
|
|
38
|
+
const { name, sub } = parseSpecifier(a.path);
|
|
39
|
+
if (!deps[name]) {
|
|
40
|
+
if (!warned.has(name)) { warned.add(name); log.warn(`${name} が package.json にありません(最新版を CDN から直接使います)。npm install ${name} で node_modules に入ります`); }
|
|
41
|
+
return { path: npmUrl(name, '', sub), namespace: 'http' };
|
|
42
|
+
}
|
|
43
|
+
if (SHARED_RUNTIMES.test(name)) return { path: npmUrl(name, deps[name], sub, { shared: true }), namespace: 'http' };
|
|
44
|
+
const target = sub ? subFile(name, sub) : `${pkgDir(name)}index.mjs`;
|
|
45
|
+
if (files.has(target)) return { path: target, namespace: 'vfs' };
|
|
46
|
+
const got = vendor && (await vendor(name, sub));
|
|
47
|
+
if (got) return { path: got, namespace: 'vfs' };
|
|
48
|
+
return { path: npmUrl(name, deps[name], sub), namespace: 'http' };
|
|
49
|
+
});
|
|
50
|
+
b.onLoad({ filter: /.*/, namespace: 'vfs' }, async (a) => {
|
|
51
|
+
if (!files.has(a.path)) return { errors: [{ text: `ファイルが見つかりません: ${a.path}` }] };
|
|
52
|
+
const text = files.get(a.path);
|
|
53
|
+
const t = transform && (await transform(a.path, text));
|
|
54
|
+
if (t) return t;
|
|
55
|
+
if (isBinary(text)) return { contents: toBytes(text), loader: 'dataurl' };
|
|
56
|
+
return { contents: text, loader: LOADERS[ext(a.path)] || 'text', resolveDir: '/' + dirname(a.path) };
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
package/src/config.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// 既定値。環境ごとの値は setup({ ... }) で渡す(このファイルは書き換えない)
|
|
2
|
+
export const CONFIG = {
|
|
3
|
+
FIELDS: {
|
|
4
|
+
source: 'source_zip', // 添付ファイル: ソース一式の zip(node_modules は入れない)
|
|
5
|
+
key: 'private_key', zip: 'plugin_zip', id: 'plugin_id', testApp: 'test_app',
|
|
6
|
+
// 前の版(サブテーブルにソースを展開していた頃)のフィールド。あれば最初の1回だけ読んで zip に移す
|
|
7
|
+
legacy: { files: 'files', path: 'path', content: 'content' },
|
|
8
|
+
},
|
|
9
|
+
INSTALL_HOSTS: [], // インストールしてよい環境(setup の installHosts で渡す)。空ならインストールしない
|
|
10
|
+
THEME: 'vitesse-dark',
|
|
11
|
+
CDN: {
|
|
12
|
+
monaco: 'https://esm.sh/modern-monaco@0.3.8', // 0.4 系は言語サービスが動かない(kintone 上で確認)
|
|
13
|
+
trees: 'https://esm.sh/@pierre/trees@1.0.0',
|
|
14
|
+
xterm: 'https://esm.sh/@xterm/xterm@5.5.0',
|
|
15
|
+
xtermCss: 'https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/css/xterm.css',
|
|
16
|
+
xtermFit: 'https://esm.sh/@xterm/addon-fit@0.10.0',
|
|
17
|
+
xtermReadline: 'https://esm.sh/xterm-readline@1.1.2',
|
|
18
|
+
esbuild: 'https://cdn.jsdelivr.net/npm/esbuild-wasm@0.28.2/esm/browser.min.js',
|
|
19
|
+
esbuildWasm: 'https://cdn.jsdelivr.net/npm/esbuild-wasm@0.28.2/esbuild.wasm',
|
|
20
|
+
validator: 'https://esm.sh/@kintone/plugin-manifest-validator@12.0.1',
|
|
21
|
+
prettier: 'https://esm.sh/prettier@3/standalone',
|
|
22
|
+
prettierPlugins: ['babel', 'estree', 'typescript', 'postcss', 'html', 'markdown', 'yaml'].map((p) => `https://esm.sh/prettier@3/plugins/${p}`),
|
|
23
|
+
// kintone の型(@kintone/dts-gen)。tsconfig.json の types に URL のまま書ける
|
|
24
|
+
kintoneDts: 'https://cdn.jsdelivr.net/npm/@kintone/dts-gen@10.0.0/kintone.d.ts',
|
|
25
|
+
// .svelte / .vue のコンパイラは package.json のバージョン(:range)に合わせる
|
|
26
|
+
compilers: {
|
|
27
|
+
svelte: 'https://esm.sh/svelte@:range/compiler?target=es2020&bundle',
|
|
28
|
+
vue: 'https://esm.sh/@vue/compiler-sfc@:range?target=es2020&bundle',
|
|
29
|
+
sass: 'https://esm.sh/sass@1?target=es2020&bundle',
|
|
30
|
+
less: 'https://esm.sh/less@4?target=es2020&bundle',
|
|
31
|
+
yaml: 'https://esm.sh/js-yaml@4?target=es2020&bundle',
|
|
32
|
+
},
|
|
33
|
+
npm: 'https://esm.sh',
|
|
34
|
+
registry: 'https://registry.npmjs.org',
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
export const F = CONFIG.FIELDS;
|
|
38
|
+
export const BINARY_PREFIX = 'base64:';
|
|
39
|
+
export const SETTINGS_KEY = 'kintone-plugin-ide:settings';
|
|
40
|
+
export const NODE_MODULES = 'node_modules/';
|
|
41
|
+
// 状態をモジュールで共有するランタイム。サブパスごとに丸ごと抱え込むと二重に入って壊れるので、
|
|
42
|
+
// esm.sh の分割されたモジュールをそのまま読んで esbuild にまとめさせる(node_modules には印だけ置く)
|
|
43
|
+
export const SHARED_RUNTIMES = /^(svelte|vue|@vue\/.+|react|react-dom|preact|solid-js|lit|@lit\/.+)$/;
|
|
44
|
+
|
|
45
|
+
// setup() の引数を CONFIG に重ねる。オブジェクトは差し替えずに中身を書き換える
|
|
46
|
+
// (ほかのモジュールが CONFIG.FIELDS などを直接つかんでいるため)
|
|
47
|
+
const isObj = (v) => v && typeof v === 'object' && !Array.isArray(v);
|
|
48
|
+
function merge(target, src) {
|
|
49
|
+
for (const [k, v] of Object.entries(src)) {
|
|
50
|
+
if (v === undefined) continue;
|
|
51
|
+
if (isObj(v) && isObj(target[k])) merge(target[k], v);
|
|
52
|
+
else target[k] = v;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function configure({ installHosts, fields, theme, cdn } = {}) {
|
|
56
|
+
merge(CONFIG, { INSTALL_HOSTS: installHosts, FIELDS: fields, THEME: theme, CDN: cdn });
|
|
57
|
+
return CONFIG;
|
|
58
|
+
}
|
package/src/deps.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// 外部ライブラリの読み込みは全部ここに集める。
|
|
2
|
+
// 自分のプロジェクトでバンドルするときは、ここの import(CONFIG.CDN.xxx) を
|
|
3
|
+
// import('modern-monaco') のような素の指定に変えれば、npm のパッケージがそのまま入る。
|
|
4
|
+
import { CONFIG } from './config.js';
|
|
5
|
+
import { workspaceFS } from './ide/workspace-fs.js';
|
|
6
|
+
|
|
7
|
+
const once = (fn) => { let p; return () => (p ??= fn()); };
|
|
8
|
+
|
|
9
|
+
export const loadTrees = once(() => import(CONFIG.CDN.trees));
|
|
10
|
+
export const loadValidator = once(() => import(CONFIG.CDN.validator).then((m) => m.default || m));
|
|
11
|
+
export const loadXterm = once(async () => {
|
|
12
|
+
const [{ Terminal }, { FitAddon }, { Readline }] = await Promise.all([
|
|
13
|
+
import(CONFIG.CDN.xterm), import(CONFIG.CDN.xtermFit), import(CONFIG.CDN.xtermReadline),
|
|
14
|
+
]);
|
|
15
|
+
return { Terminal, FitAddon, Readline, css: CONFIG.CDN.xtermCss };
|
|
16
|
+
});
|
|
17
|
+
export const loadPrettier = once(async () => {
|
|
18
|
+
const [prettier, ...plugins] = await Promise.all([import(CONFIG.CDN.prettier), ...CONFIG.CDN.prettierPlugins.map((u) => import(u))]);
|
|
19
|
+
return { prettier, plugins: plugins.map((p) => p.default || p) };
|
|
20
|
+
});
|
|
21
|
+
export const loadEsbuild = once(async () => {
|
|
22
|
+
const es = await import(CONFIG.CDN.esbuild);
|
|
23
|
+
try { await es.initialize({ wasmURL: CONFIG.CDN.esbuildWasm }); } catch (e) {
|
|
24
|
+
if (!/more than once/.test(e.message)) throw e; // 同じページで2回目に開いた時は初期化済み
|
|
25
|
+
}
|
|
26
|
+
return es;
|
|
27
|
+
});
|
|
28
|
+
// modern-monaco はページに1回しか init できないので、ファイルシステムは差し替えのきく1つを渡す
|
|
29
|
+
export const loadMonaco = once(async () => {
|
|
30
|
+
const m = await import(CONFIG.CDN.monaco);
|
|
31
|
+
const workspace = new m.Workspace({ name: 'kintone-plugin-ide', customFS: workspaceFS });
|
|
32
|
+
return m.init({ defaultTheme: CONFIG.THEME, workspace });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// .svelte / .vue / .scss / .less / .yaml のコンパイラ
|
|
36
|
+
const compilers = new Map();
|
|
37
|
+
export function loadCompiler(name, range = '') {
|
|
38
|
+
const url = CONFIG.CDN.compilers[name].replace(':range', range);
|
|
39
|
+
if (!compilers.has(url)) compilers.set(url, import(url).catch((e) => { compilers.delete(url); throw new Error(`${name} のコンパイラを読めませんでした: ${e.message}`); }));
|
|
40
|
+
return compilers.get(url);
|
|
41
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// バージョン・検索・プラグイン管理・テスト用アプリ・履歴
|
|
2
|
+
import { F } from '../../config.js';
|
|
3
|
+
import { api } from '../../kintone/api.js';
|
|
4
|
+
import { readPackageJson } from '../../npm/registry.js';
|
|
5
|
+
import { isBinary } from '../../util/bytes.js';
|
|
6
|
+
import { isVendor } from '../../util/path.js';
|
|
7
|
+
|
|
8
|
+
export function setupMisc(ctx) {
|
|
9
|
+
const { state, log, appId, recordId } = ctx;
|
|
10
|
+
const { files } = state;
|
|
11
|
+
const writeJson = (path, obj) => ctx.addFiles([[path, JSON.stringify(obj, null, 2) + '\n']]);
|
|
12
|
+
|
|
13
|
+
function bumpVersion(kind) {
|
|
14
|
+
let manifest;
|
|
15
|
+
try { manifest = JSON.parse(files.get('manifest.json')); } catch { throw new Error('manifest.json が読めません'); }
|
|
16
|
+
const [a, b, c] = String(manifest.version).split('.').map((x) => parseInt(x, 10) || 0);
|
|
17
|
+
const next = /^\d+(\.\d+){0,2}$/.test(kind) ? kind
|
|
18
|
+
: kind === 'major' ? `${a + 1}.0.0` : kind === 'minor' ? `${a}.${b + 1}.0` : `${a}.${b}.${c + 1}`;
|
|
19
|
+
manifest.version = /^\d+$/.test(String(manifest.version)) && !/\./.test(next) ? Number(next) : next;
|
|
20
|
+
writeJson('manifest.json', manifest);
|
|
21
|
+
if (files.has('package.json')) { const pkg = readPackageJson(files); pkg.version = String(next); writeJson('package.json', pkg); }
|
|
22
|
+
log.ok(`バージョン: ${a}.${b}.${c} → ${next}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function grep(pattern, { all = false } = {}) {
|
|
26
|
+
if (!pattern) { log.warn('grep <文字列 または /正規表現/> [--all で node_modules も]'); return; }
|
|
27
|
+
const re = /^\/.+\/[gimsuy]*$/.test(pattern)
|
|
28
|
+
? new RegExp(pattern.slice(1, pattern.lastIndexOf('/')), pattern.slice(pattern.lastIndexOf('/') + 1).replace('g', '') + 'g')
|
|
29
|
+
: new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
30
|
+
let n = 0;
|
|
31
|
+
for (const [p, text] of [...files].sort()) {
|
|
32
|
+
if (isBinary(text) || (!all && isVendor(p))) continue;
|
|
33
|
+
text.split('\n').forEach((line, i) => {
|
|
34
|
+
for (const m of line.matchAll(re)) { if (n++ < 200) log.plain(`${p}:${i + 1}:${m.index + 1} ${line.trim().slice(0, 120)}`); }
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
log.info(n ? `${n} 件${n > 200 ? '(先頭200件を表示)' : ''}` : '見つかりませんでした');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const pluginId = () => ctx.record[F.id] && ctx.record[F.id].value;
|
|
41
|
+
async function pluginList() {
|
|
42
|
+
const { plugins } = await api('/k/v1/plugins.json', 'GET', { limit: 100 });
|
|
43
|
+
if (!plugins.length) { log.info('インストール済みのプラグインはありません'); return; }
|
|
44
|
+
for (const p of plugins) log.plain(`${p.id === pluginId() ? '\x1b[32m*' : ' '} ${p.id} v${p.version} ${p.name}\x1b[0m`);
|
|
45
|
+
}
|
|
46
|
+
async function pluginApps() {
|
|
47
|
+
const id = pluginId();
|
|
48
|
+
if (!id) { log.warn('まだパッケージしていません'); return; }
|
|
49
|
+
const { apps } = await api('/k/v1/plugin/apps.json', 'GET', { id, limit: 100 });
|
|
50
|
+
if (!apps.length) log.info('このプラグインを追加しているアプリはありません');
|
|
51
|
+
for (const a of apps) log.plain(`#${a.id} ${a.name} /k/${a.id}/`);
|
|
52
|
+
}
|
|
53
|
+
async function pluginUninstall() {
|
|
54
|
+
const id = pluginId();
|
|
55
|
+
if (!id) { log.warn('まだパッケージしていません'); return; }
|
|
56
|
+
if (!(await ctx.confirm('プラグインをアンインストールします', `${location.hostname} から ${id} を外します。追加しているアプリからも外れます`, { ok: 'アンインストール', danger: true }))) return;
|
|
57
|
+
await api('/k/v1/plugin.json', 'DELETE', { id });
|
|
58
|
+
log.ok('アンインストールしました');
|
|
59
|
+
}
|
|
60
|
+
async function setTestApp(n) {
|
|
61
|
+
const app = Number(n);
|
|
62
|
+
if (!app) throw new Error('testapp <アプリID>');
|
|
63
|
+
await api('/k/v1/record.json', 'PUT', { app: appId, id: recordId, record: { [F.testApp]: { value: String(app) } } });
|
|
64
|
+
ctx.record = await ctx.loadRecord();
|
|
65
|
+
ctx.updateStatus();
|
|
66
|
+
log.ok(`テスト用アプリを #${app} にしました /k/${app}/`);
|
|
67
|
+
}
|
|
68
|
+
async function history() {
|
|
69
|
+
const { comments } = await api('/k/v1/record/comments.json', 'GET', { app: appId, record: recordId, order: 'desc', limit: 10 });
|
|
70
|
+
if (!comments.length) log.info('履歴はまだありません');
|
|
71
|
+
for (const c of comments) log.plain(`${new Date(c.createdAt).toLocaleString()} ${c.text.split('\n')[0]}`);
|
|
72
|
+
}
|
|
73
|
+
Object.assign(ctx, { bumpVersion, grep, pluginList, pluginApps, pluginUninstall, setTestApp, history });
|
|
74
|
+
}
|