Skip to content

Commit 9ca4b9a

Browse files
committed
add @orgajs/rollup
1 parent f2c09e3 commit 9ca4b9a

File tree

12 files changed

+703
-23
lines changed

12 files changed

+703
-23
lines changed

packages/loader/tests/loader.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'node:test'
2-
import assert from 'node:assert'
2+
import * as assert from 'node:assert'
33
import { compile } from './compiler'
44

55
test(

packages/orgx/src/evaluate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { VFileCompatible } from 'vfile'
22
import { compile, compileSync } from './compile.js'
33
import { ProcessorOptions } from './processor.js'
44
import { OrgaContent } from './types.js'
5-
import { run, runSync } from './run'
5+
import { run, runSync } from './run.js'
66

77
export interface ExportMap extends Record<string, unknown> {
88
default: OrgaContent

packages/orgx/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
export { createProcessor } from './processor.js'
2-
export { compile, compileSync } from './compile.js'
3-
export { evaluate, evaluateSync, RuntimeOptions } from './evaluate.js'
2+
export { compile, compileSync, CompileOptions } from './compile.js'
3+
export {
4+
evaluate,
5+
evaluateSync,
6+
RuntimeOptions,
7+
EvaluateOptions,
8+
} from './evaluate.js'
49
export type { ProcessorOptions } from './processor.js'
510
export type { OrgaComponents, OrgaProps } from './types.js'

packages/orgx/src/plugin/estree-stringify.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export function estreeStringify(options: Options) {
1919
const result = SourceMapGenerator
2020
? toJs(tree, {
2121
filePath: file.path || 'unknown.org',
22-
// @ts-expect-error: `SourceMapGenerator` is a class.
2322
SourceMapGenerator,
2423
handlers: jsx,
2524
})

packages/orgx/tests/compile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it } from 'node:test'
2-
import assert from 'node:assert'
2+
import * as assert from 'node:assert'
33
import { compile } from '../src/compile'
44

55
const fixture = `

packages/rollup/package.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "@orgajs/rollup",
3+
"version": "1.0.0",
4+
"description": "Rollup plugin for Orga",
5+
"license": "MIT",
6+
"files": [
7+
"dist/index.js",
8+
"dist/index.d.ts"
9+
],
10+
"main": "dist/index.js",
11+
"type": "module",
12+
"author": "Xiaoxing Hu <[email protected]>",
13+
"homepage": "https://orga.js.org",
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/orgapp/orgajs.git",
17+
"directory": "packages/rollup"
18+
},
19+
"scripts": {
20+
"build": "yarn clean && yarn compile",
21+
"clean": "del ./dist tsconfig.tsbuildinfo",
22+
"test": "ava",
23+
"compile": "tsc -b"
24+
},
25+
"ava": {
26+
"typescript": {
27+
"extensions": [
28+
"ts",
29+
"tsx"
30+
],
31+
"rewritePaths": {
32+
"src/": "dist/"
33+
},
34+
"compile": "tsc"
35+
}
36+
},
37+
"devDependencies": {
38+
"@ava/typescript": "^4.0.0",
39+
"@types/react": "^18.2.6",
40+
"@types/react-dom": "^18.2.4",
41+
"ava": "^5.2.0",
42+
"del-cli": "^3.0.1",
43+
"react": "^18.2.0",
44+
"react-dom": "^18.2.0",
45+
"rollup": "^3.21.7",
46+
"typescript": "^5.0.4"
47+
},
48+
"peerDependencies": {
49+
"rollup": ">=2"
50+
},
51+
"dependencies": {
52+
"@orgajs/orgx": "workspace:^",
53+
"@rollup/pluginutils": "^5.0.2",
54+
"source-map": "^0.7.4",
55+
"vfile": "^5.3.7"
56+
}
57+
}

packages/rollup/src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { VFile } from 'vfile'
2+
import { createFilter, type FilterPattern } from '@rollup/pluginutils'
3+
import { createProcessor, type CompileOptions } from '@orgajs/orgx'
4+
import { SourceMapGenerator } from 'source-map'
5+
import type { Plugin, SourceDescription } from 'rollup'
6+
7+
interface RollupPluginOptions {
8+
include: FilterPattern
9+
exclude: FilterPattern
10+
}
11+
12+
interface Options extends CompileOptions, RollupPluginOptions {}
13+
14+
/**
15+
* Compile org-mode w/ rollup.
16+
*/
17+
export default function (
18+
options: Options | null | undefined = undefined
19+
): Plugin {
20+
const { include, exclude, ...rest } = options || {}
21+
const processor = createProcessor({
22+
// SourceMapGenerator,
23+
...rest,
24+
SourceMapGenerator,
25+
})
26+
const filter = createFilter(include, exclude)
27+
28+
return {
29+
name: '@orgajs/rollup',
30+
async transform(value, path) {
31+
const file = new VFile({ value, path })
32+
33+
if (file.extname && filter(file.path)) {
34+
const compiled = await processor.process(file)
35+
const code = String(compiled.value)
36+
const result: SourceDescription = { code, map: compiled.map }
37+
return result
38+
}
39+
},
40+
}
41+
}

packages/rollup/src/test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import test from 'ava'
2+
import { promises as fs } from 'fs'
3+
import { rollup } from 'rollup'
4+
import { fileURLToPath } from 'url'
5+
import rollupOrg from './index.js'
6+
import { renderToStaticMarkup } from 'react-dom/server'
7+
import { createElement } from 'react'
8+
9+
test('@orgajs/rollup', async (t) => {
10+
await fs.writeFile(new URL('rollup.org', import.meta.url), '* Hi')
11+
12+
const bundle = await rollup({
13+
input: fileURLToPath(new URL('rollup.org', import.meta.url)),
14+
external: ['react/jsx-runtime'],
15+
plugins: [rollupOrg()],
16+
})
17+
18+
const { output } = await bundle.generate({ format: 'es', sourcemap: true })
19+
20+
await fs.writeFile(new URL('rollup.js', import.meta.url), output[0].code)
21+
22+
/* @ts-expect-error file is dynamically generated */
23+
// eslint-disable-next-line import/no-unresolved
24+
const { default: Content } = await import('./rollup.js')
25+
26+
// t.is(output[0].map ? output[0].map.mappings : undefined, undefined)
27+
28+
t.is(
29+
renderToStaticMarkup(createElement(Content)),
30+
'<div class="section"><h1>Hi</h1></div>'
31+
)
32+
33+
await fs.unlink(new URL('rollup.org', import.meta.url))
34+
await fs.unlink(new URL('rollup.js', import.meta.url))
35+
})

packages/rollup/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.esm.json",
3+
"compilerOptions": {
4+
"rootDir": "./src",
5+
"outDir": "./dist"
6+
},
7+
"references": [{ "path": "../orgx" }],
8+
"include": ["./src"]
9+
}

tsconfig.esm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"target": "ES2020",
55
"module": "ES2020",
66
"declaration": true,
7-
"moduleResolution": "Node",
7+
"moduleResolution": "node",
88
"jsx": "react",
99
"skipLibCheck": true,
1010
"esModuleInterop": true,

0 commit comments

Comments
 (0)