Skip to content

Commit

Permalink
[WIP] Functionally complete
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Dec 9, 2019
1 parent 9e9a74c commit 83d855f
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 216 deletions.
7 changes: 6 additions & 1 deletion cli/compilers/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,22 @@ fn req(
root_names: Vec<String>,
compiler_config: CompilerConfig,
out_file: Option<String>,
bundle: bool,
) -> Buf {
let j = match (compiler_config.path, compiler_config.content) {
(Some(config_path), Some(config_data)) => json!({
"type": request_type as i32,
"rootNames": root_names,
"outFile": out_file,
"bundle": bundle,
"configPath": config_path,
"config": str::from_utf8(&config_data).unwrap(),
}),
_ => json!({
"type": request_type as i32,
"rootNames": root_names,
"outFile": out_file,
"bundle": bundle,
}),
};

Expand Down Expand Up @@ -266,10 +269,11 @@ impl TsCompiler {

let root_names = vec![module_name.clone()];
let req_msg = req(
msg::CompilerRequestType::Bundle,
msg::CompilerRequestType::Compile,
root_names,
self.config.clone(),
out_file,
true,
);

let worker = TsCompiler::setup_worker(global_state.clone());
Expand Down Expand Up @@ -364,6 +368,7 @@ impl TsCompiler {
root_names,
self.config.clone(),
None,
false,
);

let worker = TsCompiler::setup_worker(global_state.clone());
Expand Down
45 changes: 8 additions & 37 deletions cli/js/bundler.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

import { Console } from "./console.ts";
import * as dispatch from "./dispatch.ts";
import { sendSync } from "./dispatch_json.ts";
import { TextEncoder } from "./text_encoding.ts";
import { assert, commonPath, humanFileSize } from "./util.ts";
import { writeFileSync } from "./write_file.ts";

declare global {
const console: Console;
}
import { assert, commonPath } from "./util.ts";

const BUNDLE_LOADER = "bundle_loader.js";

const encoder = new TextEncoder();

let bundleLoader: string;

let rootExports: string[] | undefined;

/** Given a fileName and the data, emit the file to the file system. */
export function emitBundle(
rootNames: string[],
fileName: string | undefined,
export function buildBundle(
rootName: string,
data: string,
sourceFiles: readonly ts.SourceFile[]
): void {
// if the fileName is set to an internal value, just noop
if (fileName && fileName.startsWith("$deno$")) {
return;
}
// This should never happen at the moment, but this code can't currently
// support it
assert(
rootNames.length === 1,
"Only single root modules supported for bundling."
);
): string {
if (!bundleLoader) {
bundleLoader = sendSync(dispatch.OP_FETCH_ASSET, { name: BUNDLE_LOADER });
}
Expand All @@ -45,7 +25,7 @@ export function emitBundle(
// publicly, so we have to try to replicate
const sources = sourceFiles.map(sf => sf.fileName);
const sharedPath = commonPath(sources);
const rootName = rootNames[0].replace(sharedPath, "").replace(/\.\w+$/i, "");
rootName = rootName.replace(sharedPath, "").replace(/\.\w+$/i, "");
let instantiate: string;
if (rootExports && rootExports.length) {
instantiate = `const __rootExports = instantiate("${rootName}");\n`;
Expand All @@ -59,28 +39,19 @@ export function emitBundle(
} else {
instantiate = `instantiate("${rootName}");\n`;
}
const bundle = `${bundleLoader}\n${data}\n${instantiate}`;
if (fileName) {
const encodedData = encoder.encode(bundle);
console.warn(`Emitting bundle to "${fileName}"`);
writeFileSync(fileName, encodedData);
console.warn(`${humanFileSize(encodedData.length)} emitted.`);
} else {
console.log(bundle);
}
return `${bundleLoader}\n${data}\n${instantiate}`;
}

/** Set the rootExports which will by the `emitBundle()` */
export function setRootExports(
program: ts.Program,
rootModules: string[]
rootModule: string
): void {
// get a reference to the type checker, this will let us find symbols from
// the AST.
const checker = program.getTypeChecker();
assert(rootModules.length === 1);
// get a reference to the main source file for the bundle
const mainSourceFile = program.getSourceFile(rootModules[0]);
const mainSourceFile = program.getSourceFile(rootModule);
assert(mainSourceFile);
// retrieve the internal TypeScript symbol for this AST node
const mainSymbol = checker.getSymbolAtLocation(mainSourceFile);
Expand Down
Loading

0 comments on commit 83d855f

Please sign in to comment.