Skip to content

Commit

Permalink
Merge pull request #12 from NOVASland/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tanpeterson authored Nov 24, 2021
2 parents 4bfb19e + f7f137a commit 69ddc23
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
51 changes: 29 additions & 22 deletions commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,62 @@ import { ensureFile } from "https://deno.land/std@0.113.0/fs/mod.ts";
import { join } from "https://deno.land/std@0.113.0/path/mod.ts";
import { compiler } from "../compiler/compiler.ts";
import boilerplate from "../templates/build.ts";
import denofy from "../compiler/compiler.ts";

// Function to run when given build command
export const BuildProject = async (flag: string, cwd = Deno.cwd(), path = '/src/App.svelte') => { // C:\\Users\\Tanner\\Documents\\GitHub\\NOVAS2\\tests\\src\\App.svelte
const sveltePath = "https://cdn.skypack.dev/svelte@3.44.1";
const encoder = new TextEncoder();
const fullPath = join(cwd, path);
const memoized: {[key: string]: boolean} = {};
const memoized: { [key: string]: boolean } = {};

if (flags['help'][flag]) {
if (flags["help"][flag]) {
console.log(`To run build, type:` + ` %NOVAS build`, "color:#55dac8");
return false;
}

await ensureFile('./build/index.js');
await ensureFile("./build/index.js");
Deno.writeFile("./build/index.js", encoder.encode(boilerplate.indexJs));

const buildImports = async (filePath: string) => {
filePath.endsWith('.svelte') ? await handleSvelte() : handleOther();
filePath.endsWith(".svelte") ? await handleSvelte() : handleOther();

async function handleSvelte() {
const { js, ast } = await compiler(filePath);
const data = encoder.encode(js);

await ensureFile(join("./build", filePath.replace(cwd, '')) + ".js");
await Deno.writeFile(join("./build", filePath.replace(cwd, '')) + ".js", data);

const nestedImports = ast.instance?.content?.body?.filter((script: { type: string; source: { value: string; }; }) => script.type === "ImportDeclaration")
if(!nestedImports) return;
for(const nested of nestedImports){

await ensureFile(join("./build", filePath.replace(cwd, "")) + ".js");
await Deno.writeFile(
join("./build", filePath.replace(cwd, "")) + ".js",
data,
);

const nestedImports = ast.instance?.content?.body?.filter((
script: { type: string; source: { value: string } },
) => script.type === "ImportDeclaration");
if (!nestedImports) return;
for (const nested of nestedImports) {
if (memoized[nested.source.value] === true) continue;
memoized[nested.source.value] = true;
buildImports(join(cwd, nested.source.value.replace('.', 'src/')));
buildImports(join(cwd, nested.source.value.replace(".", "src/")));
}
}
async function handleOther(){

async function handleOther() {
try {
const data = encoder.encode(filePath);
const currentFile = await Deno.readTextFile(filePath);
const denofiedFile = await denofy(currentFile, sveltePath);
const data = encoder.encode(denofiedFile);
await ensureFile("./build" + filePath.replace(cwd, ''));
await Deno.writeFile("./build" + filePath.replace(cwd, ''), data);
}
catch {
return;
}
}
}

await buildImports(fullPath);
};

await buildImports(fullPath);

console.log("Your build was successful!");
return true;
}

};
29 changes: 15 additions & 14 deletions compiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { compile } from "https://cdn.jsdelivr.net/npm/svelte@3.42.3/compiler.mjs";
import { join } from "https://deno.land/std@0.113.0/path/mod.ts";

const getOptions = async() => {
let options: { [key: string]: string|boolean};
try{
const decoderlol = new TextDecoder('utf-8');
const getOptions = async () => {
let options: { [key: string]: string | boolean };
try {
const decoderlol = new TextDecoder("utf-8");
const data = await Deno.readFile(join(Deno.cwd(), "compileOptions.json"));
options = JSON.parse(decoderlol.decode(data))
}
catch{
options = JSON.parse(decoderlol.decode(data));
} catch {
// console.log("No compileOptions.json file found, using default compile options.");
// All options: https://svelte.dev/docs#svelte_compile
options = {
Expand All @@ -18,30 +17,32 @@ const getOptions = async() => {
};
}
return options;
}
};

const denofy = (file:string, sveltePath: string | boolean) => {
const denofy = (file: string, sveltePath: string | boolean) => {
return file.replace(
/import\s(.+?)\sfrom\s*['"](.+?)(.svelte)['"]/igm,
`import $1 from '$2$3.js'`,
)
)
.replace(
/import\s(.+?)\sfrom\s*['"](svelte\/(.+?))['"]/igm,
`import $1 from '${sveltePath}` + `/$3'`,
)
.replace(
/import\s(.+?)\sfrom\s*['"](svelte)['"]/igm,
`import $1 from '${sveltePath}/internal'`,
)
}
);
};

export const compiler = async (file: string) => {
const options = await getOptions();
const currentFile = await Deno.readTextFile(file);
const currentFile = await Deno.readTextFile(file);

const { js, ast } = compile(currentFile, options);
const denoImports = denofy(js?.code ?? '', options?.sveltePath);
const denoImports = denofy(js?.code ?? "", options?.sveltePath);
const denoCompiled = { js: denoImports, ast };

return denoCompiled;
}

export default denofy;
2 changes: 1 addition & 1 deletion dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Application, send, Router } from 'https://deno.land/x/oak@v9.0.1/mod.ts
export default async function devServer() {

const eventTypes: { [key: string]: boolean} = { remove: true, modify: true }; // Other option: create;


async function webSocketServer() {
const listener1 = Deno.listen({ port: 80 });
Expand Down

0 comments on commit 69ddc23

Please sign in to comment.