Skip to content

Commit

Permalink
Build progress
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Dec 5, 2024
1 parent 8679e61 commit f708d99
Show file tree
Hide file tree
Showing 10 changed files with 766 additions and 990 deletions.
21 changes: 5 additions & 16 deletions packages/ember-repl/addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
},
"license": "MIT",
"author": "NullVoxPopuli",
"typesVersions": {
"*": {
"test-support": [
"declarations/test-support/index.d.ts"
],
"markdown/parse": [
"./declarations/compile/markdown-to-ember.d.ts"
],
"*": [
"declarations/*",
"declarations/*/index.d.ts"
]
}
},
"exports": {
".": {
"types": "./declarations/browser/index.d.ts",
Expand All @@ -35,7 +21,11 @@
"types": "./declarations/compiler-worker/index.d.ts",
"default": "./dist/compiler-worker/index.js"
},
"./service-worker": {
"./workers/markdown": {
"types": "./declarations/markdown-worker/index.d.ts",
"default": "./dist/markdown-worker/index.js"
},
"./sw": {
"types": "./declarations/service-worker/index.d.ts",
"default": "./dist/service-worker/index.js"
},
Expand All @@ -50,7 +40,6 @@
"scripts": {
"build": "rm -rf dist declarations && concurrently 'pnpm:build:*'",
"build:browser": "rollup --config",
"build:workers": "vite build",
"build:types": "tsc --emitDeclarationOnly --noEmit false",
"start": "vite build --watch",
"lint:types": "tsc --noEmit",
Expand Down
72 changes: 40 additions & 32 deletions packages/ember-repl/addon/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Addon } from "@embroider/addon-dev/rollup";

import alias from "@rollup/plugin-alias";
import { babel } from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import { defineConfig } from "rollup";

Expand Down Expand Up @@ -39,28 +40,16 @@ function bundledEmber() {
]);
}

export default defineConfig([
// {
// output: browser.output(),
// plugins: [
// browser.publicEntrypoints(["**/*.js"]),
// browser.appReexports(["./services/ember-repl/compiler.js"]),
// babel({
// extensions: [".js", ".gjs", ".ts", ".gts"],
// babelHelpers: "bundled",
// }),
// browser.dependencies(),
// ],
// },
{
input: "src/compiler-worker/index.ts",
function worker(input, out) {
return {
input: input,
external: [],
output: {
sourcemap: true,
format: "es",
hoistTransitiveImports: false,
inlineDynamicImports: true,
file: "dist/compiler-worker.js",
file: out,
},
plugins: [
babel({
Expand All @@ -69,22 +58,41 @@ export default defineConfig([
}),
bundledEmber(),
nodeResolve(),
// unified>extends
commonjs(),
],
};
}

export default defineConfig([
{
output: browser.output(),
plugins: [
browser.publicEntrypoints(["**/*.js"]),
browser.appReexports(["./services/ember-repl/compiler.js"]),
babel({
extensions: [".js", ".gjs", ".ts", ".gts"],
babelHelpers: "bundled",
}),
browser.dependencies(),
],
},
worker("src/compiler-worker/index.ts", "dist/compiler-worker.js"),
worker("src/markdown-worker/index.ts", "dist/markdown-worker.js"),
{
input: "src/service-worker/index.ts",
output: {
sourcemap: true,
format: "es",
hoistTransitiveImports: false,
file: "dist/service-worker.js",
},
plugins: [
babel({
extensions: [".js", ".gjs", ".ts", ".gts"],
babelHelpers: "bundled",
}),
bundledEmber(),
],
},
// {
// input: "src/service-worker/index.ts",
// output: {
// sourcemap: true,
// format: "es",
// hoistTransitiveImports: false,
// file: "dist/service-worker.js",
// },
// plugins: [
// babel({
// extensions: [".js", ".gjs", ".ts", ".gts"],
// babelHelpers: "bundled",
// }),
// bundledEmber(),
// ],
// },
]);
152 changes: 1 addition & 151 deletions packages/ember-repl/addon/src/compiler-worker/formats.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
// import { resolveImportMap } from './imports.ts';

import { invocationName } from '../browser/utils.ts';

import type { CompileResult, EvalImportMap, ScopeMap, UnifiedPlugin } from '../types.ts';
import type { ExtractedCode } from './formats/markdown.ts';

async function compileGJSArray(js: { code: string }[], importMap?: EvalImportMap) {
let modules = await Promise.all(
js.map(async ({ code }) => {
return await compileGJS(code, importMap);
})
);

return modules;
}
import type { CompileResult, EvalImportMap } from '../types.ts';

export async function compileGJS(
gjsInput: string,
Expand Down Expand Up @@ -47,138 +32,3 @@ export async function compileHBS(
return { error: error as Error, name: 'unknown' };
}
}

async function extractScope(
liveCode: ExtractedCode[],
options?: {
importMap?: EvalImportMap;
topLevelScope?: ScopeMap;
}
): Promise<CompileResult[]> {
let scope: CompileResult[] = [];

let hbs = liveCode.filter((code) => code.lang === 'hbs');
let js = liveCode.filter((code) => ['js', 'gjs'].includes(code.lang));

if (js.length > 0) {
let compiled = await compileGJSArray(js, options?.importMap);

await Promise.all(
compiled.map(async (info) => {
// using web worker + import maps is not available yet (need firefox support)
// (and to somehow be able to point at npm)
//
// if ('importPath' in info) {
// return scope.push({
// moduleName: name,
// component: await import(/* webpackIgnore: true */ info.importPath),
// });
// }

return scope.push(info);
})
);
}

for (let { code } of hbs) {
let compiled = await compileHBS(code, { scope: options?.topLevelScope });

scope.push(compiled);
}

return scope;
}

export async function compileGDM(
glimdownInput: string,
options?: {
importMap?: EvalImportMap;
topLevelScope?: ScopeMap;
remarkPlugins?: UnifiedPlugin[];
rehypePlugins?: UnifiedPlugin[];
CopyComponent?: string;
ShadowComponent?: string;
}
): Promise<CompileResult & { rootTemplate?: string }> {
let topLevelScope = options?.topLevelScope ?? {};
let rootTemplate: string;
let liveCode: ExtractedCode[];
let scope: CompileResult[] = [];

/**
* Step 1: Convert Markdown To HTML (Ember).
*
* The remark plugin, remark-code-extra also extracts
* and transforms the code blocks we care about.
*
* These blocks will be compiled through babel and eval'd so the
* compiled rootTemplate can invoke them
*/
try {
let { parseMarkdown } = await import('./formats/markdown.ts');
let { templateOnlyGlimdown, blocks } = await parseMarkdown(glimdownInput, {
CopyComponent: options?.CopyComponent,
ShadowComponent: options?.ShadowComponent,
remarkPlugins: options?.remarkPlugins,
rehypePlugins: options?.rehypePlugins,
});

rootTemplate = templateOnlyGlimdown;
liveCode = blocks;
} catch (error) {
return { error: error as Error, name: 'unknown' };
}

/**
* Step 2: Compile the live code samples
*/
if (liveCode.length > 0) {
try {
scope = await extractScope(liveCode, options);
} catch (error) {
console.info({ scope });
console.error(error);

return { error: error as Error, rootTemplate, name: 'unknown' };
}
}

/**
* Make sure non of our snippets errored
*
* TODO: for these errors, report them differently so that we
* can render the 'Ember' and still highlight the correct line?
* or maybe there is a way to highlight in the editor instead?
*/
for (let { error, component } of scope) {
if (!component) {
if (error) {
return { error, rootTemplate, name: 'unknown' };
}
}
}

/**
* Step 4: Compile the Ember Template
*/
try {
let localScope = scope.reduce(
(accum, { component, name }) => {
accum[invocationName(name)] = component;

return accum;
},
{} as Record<string, unknown>
);

return await compileHBS(rootTemplate, {
moduleName: 'DynamicRootTemplate',
scope: {
...topLevelScope,
...localScope,
},
});
} catch (error) {
return { error: error as Error, rootTemplate, name: 'unknown' };
}
}
3 changes: 2 additions & 1 deletion packages/ember-repl/addon/src/compiler-worker/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PWBWorker } from 'promise-worker-bi';

import { compileGDM, compileGJS, compileHBS } from './formats.ts';
import { compileGJS, compileHBS } from './formats.ts';
import { debug } from './log.ts';
import { getString } from './util.ts';

Expand Down Expand Up @@ -60,6 +60,7 @@ async function compile(message: NonNullable<object>) {
return compileHBS(message);
case 'glimdown':
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// TODO: Call out to markdown worker
// @ts-ignore
return compileGDM(message);
default:
Expand Down
Loading

0 comments on commit f708d99

Please sign in to comment.