Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
feat: make compatible with deno 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
jeiea committed Jan 30, 2021
1 parent 863ce8e commit 29ab833
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
24 changes: 10 additions & 14 deletions plugin/typescriptCompile/mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { path, Plugin } from "../../deps.ts";
import { isHttpUrl } from "../../util/isHttpUrl.ts";
import { resolver } from "../../util/resolver.ts";
import { stripFileProtocol } from "../../util/stripFileProtocol.ts";
import { usesProtocol } from "../../util/usesProtocol.ts";

type Options = {
useAsLoader?: boolean;
Expand All @@ -12,11 +12,8 @@ let modules: Record<string, string>;
let files: string[];

function resolveFromModules(id: string) {
const resolvedFile = isHttpUrl(id) ? id : path.resolve(id);
const originalFile = path.parse(resolvedFile);
const jsFile = originalFile.ext === ".js"
? resolvedFile
: `${originalFile.dir}/${originalFile.name}.js`;
const resolvedFile = usesProtocol(id) ? id : path.resolve(id);
const jsFile = `${resolvedFile}.js`;

if (!files.includes(jsFile)) {
// Allows files like React (.js) to still be bundled since they don't get re-emitted
Expand All @@ -34,22 +31,21 @@ async function resolveId(
) {
if (importer || modules) return resolver(_importee, importer);

let importee = _importee;
let importee = resolver(_importee);

if (isHttpUrl(importee)) {
const { url, redirected } = await fetch(importee);
redirected && (importee = url);
}

const [diagnostics, emitMap] = await Deno.compile(
importee,
undefined,
compilerOptions,
);
const result = await Deno.emit(importee, { compilerOptions });
const { diagnostics } = result;

if (diagnostics) throw new Error(Deno.formatDiagnostics(diagnostics));
if (diagnostics.length) {
throw new Error(Deno.formatDiagnostics(diagnostics));
}

modules = stripFileProtocol(emitMap);
modules = result.files;
files = Object.keys(modules);

return importee;
Expand Down
23 changes: 15 additions & 8 deletions plugin/typescriptTransform/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ export function pluginTypescriptTransform(opts?: Deno.CompilerOptions): Plugin {
return {
name: "denopack-plugin-typescriptTransform",
async transform(code, id) {
const unlock = await transpilerMutex.lock();
const result = await Deno.transpileOnly({ [id]: code }, opts).catch(
(e) => {
throw new Error(`Failed to transpile ${id}: ${e}`);
},
);
unlock();
return { code: result[id].source, map: result[id].map };
let unlock;
try {
unlock = await transpilerMutex.lock();
const result = await Deno.emit(id, {
sources: { [id]: code },
compilerOptions: opts,
check: false,
});
const { [`${id}.js`]: output, [`${id}.js.map`]: map } = result.files;
return { code: output, map };
} catch (error) {
throw new Error(`Failed to transpile ${id}: ${error}`);
} finally {
unlock?.();
}
},
};
}
Expand Down
13 changes: 5 additions & 8 deletions util/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { path } from "../deps.ts";
import { usesProtocol } from "./usesProtocol.ts";

export function resolver(
importee: string,
importer: string | undefined,
): string {
export function resolver(importee: string, importer?: string): string {
if (!importer) {
// If this is a raw absolute path, make it a `file://` path.
if (!usesProtocol(importee)) {
Expand All @@ -13,10 +10,10 @@ export function resolver(
return new URL(`file:///${importee}`).toString();
// else if relative path resolve based on Deno.cwd()
} else if (
importee.startsWith("./") || importee.startsWith("../") ||
(Deno.build.os === "windows" && (
importee.startsWith(".\\") || importee.startsWith("..\\")
))
importee.startsWith("./") ||
importee.startsWith("../") ||
(Deno.build.os === "windows" &&
(importee.startsWith(".\\") || importee.startsWith("..\\")))
) {
return new URL(`file:///${path.join(Deno.cwd(), importee)}`).toString();
}
Expand Down

0 comments on commit 29ab833

Please sign in to comment.