Skip to content

Commit

Permalink
Add support for Deno 1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoDaniel committed Mar 4, 2021
1 parent d69387c commit bfbdeca
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.1.0] - 2021-03-04
### Added
- README.md to the examples folder
- `defaults.ts` with the default implementation for the bundler
- Support the new `Deno.emit` compiler API
- Support for Deno v1.8.0
- Export the `DenoTagOptions` interface (no longer exported as a type object)
- Updated CLI usage examples in the README file

Expand Down
5 changes: 3 additions & 2 deletions code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

import { denoTag } from "./code.ts";
import { defaultBundler } from "./defaults.ts";
import {
assert,
assertEquals,
Expand Down Expand Up @@ -150,10 +151,10 @@ Deno.test(
"Calls the bundler function with the value from the <deno> tag",
async () => {
const file = "test.ts";
const bundler: typeof Deno.bundle = (value) => {
const bundler: typeof defaultBundler = (value, opts) => {
assertEquals(value, file);
return Promise.resolve(
[undefined, ""] as [Deno.Diagnostic[] | undefined, string],
{ files: { "deno:///bundle.js": "" } } as unknown as Deno.EmitResult,
);
};
const htmlText = `<deno bundle="${file}" />`;
Expand Down
23 changes: 12 additions & 11 deletions code.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2021 Hugo Daniel Henriques Oliveira Gomes. All rights reserved.
// Licensed under the EUPL
import { defaultBundler } from "./defaults.ts";
/**
* The main function of deno-tag, it does three things:
* 1. Looks for `<deno>` tags on the supplied text and reads their attributes
Expand Down Expand Up @@ -58,9 +61,8 @@ export async function denoTag(text: string, options?: DenoTagOptions) {
* Runner is called when a `<deno run="code.ts">` is found.
*/
export interface DenoTagOptions {
bundler?: typeof Deno.bundle;
bundleSources?: Record<string, string>;
bundleOptions?: Deno.CompilerOptions;
bundler?: typeof defaultBundler;
bundleOptions?: Deno.EmitOptions;
runner?: typeof Deno.run;
runOptions?: Deno.RunOptions;
}
Expand Down Expand Up @@ -108,13 +110,13 @@ async function runDeno(args: Map<string, string>, options?: DenoTagOptions) {
stdout: "piped",
}; // ^ by default call "deno run" with the stdout piped to a Uint8Array, this
// allows output to be caught by this process (parent) and handled later on.
const bundler = options?.bundler || Deno.bundle;
const bundler = options?.bundler || defaultBundler;
// Prepare to run the supplied action attribute on the file from its value
// The output of the runner (`Deno.run` by default) is a Uint8Array which is
// processed by the TextDecoder into a JS string after the run finishes.
let runOutput: Uint8Array;
// deno-lint-ignore prefer-const
let bundleOutput: [Deno.Diagnostic[] | undefined, string];
let bundleOutput: Deno.EmitResult;
let result = "";
switch (action) {
case "run":
Expand All @@ -130,16 +132,15 @@ async function runDeno(args: Map<string, string>, options?: DenoTagOptions) {
}
break;
case "bundle":
// The bundle action will by default dump the file and all its
// dependencies into a string.
// The "files" of the result will contain a single key named
// "deno:///bundle.js" of which the value with be the resulting bundle.
bundleOutput = await bundler(
file,
options?.bundleSources,
options?.bundleOptions,
);
// The bundler output is a pair of [Diagnostic, Output]
// The result is the output string - indexed by 1
result = bundleOutput[1];
// The bundler output is an EmitResult object.
// The result is the single key "deno:///bundle.js" of the files object
result = bundleOutput.files["deno:///bundle.js"];
break;
}
// The string with the output of the action that was performed
Expand Down
6 changes: 6 additions & 0 deletions defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export async function defaultBundler(
file: string,
options: Deno.EmitOptions | undefined = {},
) {
return await Deno.emit(file, { ...options, ...{ bundle: "esm" } });
}
2 changes: 2 additions & 0 deletions deno_tag.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2021 Hugo Daniel Henriques Oliveira Gomes. All rights reserved.
// Licensed under the EUPL
import { denoTag } from "./code.ts";

/**
Expand Down
5 changes: 5 additions & 0 deletions denoconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"lib": ["es2016", "dom", "deno.ns"]
}
}
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Running the examples locally

1. Clone the repo
2. Call the command `deno run --allow-read --allow-run --unstable deno_tag.ts ./examples/1\ -\ simple\ output\ bundle/index.html`

0 comments on commit bfbdeca

Please sign in to comment.