diff --git a/BUILD.gn b/BUILD.gn index 9ea56582a3..a1ffc750f1 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -71,6 +71,7 @@ ts_sources = [ "js/make_temp_dir.ts", "js/mock_builtin.js", "js/os.ts", + "js/platform.ts", "js/plugins.d.ts", "js/read_file.ts", "js/remove.ts", diff --git a/js/deno.ts b/js/deno.ts index 7d52441ec8..9be05e9167 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -12,4 +12,5 @@ export { symlinkSync, symlink } from "./symlink"; export { writeFileSync, writeFile } from "./write_file"; export { ErrorKind, DenoError } from "./errors"; export { libdeno } from "./libdeno"; +export { arch, platform } from "./platform"; export const argv: string[] = []; diff --git a/js/platform.ts b/js/platform.ts new file mode 100644 index 0000000000..26fc317a10 --- /dev/null +++ b/js/platform.ts @@ -0,0 +1,5 @@ +// Dummy. Injected in rollup.config.js +import { DenoArch, DenoPlatform } from "./types"; + +export const arch: DenoArch = "unknown"; +export const platform: DenoPlatform = "unknown"; diff --git a/js/platform_test.ts b/js/platform_test.ts new file mode 100644 index 0000000000..80a1d93253 --- /dev/null +++ b/js/platform_test.ts @@ -0,0 +1,9 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import { test, assert } from "./test_util.ts"; +import * as deno from "deno"; + +test(function transformPlatformSuccess() { + // Make sure they are transformed + assert(deno.arch !== "unknown"); + assert(deno.platform !== "unknown"); +}); diff --git a/js/types.ts b/js/types.ts index 7af0a52013..7a7bc44d9a 100644 --- a/js/types.ts +++ b/js/types.ts @@ -151,3 +151,29 @@ declare global { stackTraceLimit: number; } } + +// Based on Node's arch +export type DenoArch = + | "arm" + | "arm64" + | "ia32" + | "mips" + | "mipsel" + | "ppc" + | "ppc64" + | "s390" + | "s390x" + | "x32" + | "x64" + | "unknown"; + +export type DenoPlatform = + | "aix" + | "darwin" + | "freebsd" + | "linux" + | "openbsd" + | "sunos" + | "win32" + | "android" + | "unknown"; diff --git a/js/unit_tests.ts b/js/unit_tests.ts index f1eed319e8..a535aea551 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -14,3 +14,4 @@ import "./rename_test.ts"; import "./blob_test.ts"; import "./timers_test.ts"; import "./symlink_test.ts"; +import "./platform_test.ts"; diff --git a/package.json b/package.json index fe00a29165..31f176aa66 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "@types/text-encoding": "0.0.33", "base64-js": "^1.3.0", "flatbuffers": "^1.9.0", + "magic-string": "^0.22.5", "prettier": "^1.14.0", "rollup": "^0.63.2", "rollup-plugin-alias": "^1.4.0", diff --git a/rollup.config.js b/rollup.config.js index d5574a7d5f..2fdaee08c0 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -7,8 +7,10 @@ import nodeResolve from "rollup-plugin-node-resolve"; import typescriptPlugin from "rollup-plugin-typescript2"; import { createFilter } from "rollup-pluginutils"; import typescript from "typescript"; +import MagicString from "magic-string"; const mockPath = path.join(__dirname, "js", "mock_builtin.js"); +const platformPath = path.join(__dirname, "js", "platform.ts"); const tsconfig = path.join(__dirname, "tsconfig.json"); const typescriptPath = `${ process.env.BASEPATH @@ -78,6 +80,37 @@ function strings({ include, exclude } = {}) { }; } +// Inject deno.arch/deno.platform from Node's process.arch/process.platform +function platform({ include, exclude } = {}) { + if (!include) { + throw new Error("include option must be passed"); + } + + const filter = createFilter(include, exclude); + + return { + name: "platform", + /** + * @param {any} _code + * @param {string} id + */ + transform(_code, id) { + if (filter(id)) { + // Adapted from https://github.com/rollup/rollup-plugin-inject/blob/master/src/index.js + const magicString = new MagicString(` +import { DenoArch, DenoPlatform } from "./types"; +export const arch: DenoArch = "${process.arch}"; +export const platform: DenoPlatform = "${process.platform}";`); + // arch and platform comes from Node + return { + code: magicString.toString(), + map: magicString.generateMap() + }; + } + } + }; +} + // This plugin resolves at bundle time any generated resources that are // in the build path under `gen` and specified with a MID starting with `gen/`. // The plugin assumes that the MID needs to have the `.ts` extension appended. @@ -104,6 +137,11 @@ export default function makeConfig(commandOptions) { }, plugins: [ + // inject platform and arch from Node + platform({ + include: [platformPath] + }), + // would prefer to use `rollup-plugin-virtual` to inject the empty module, but there // is an issue with `rollup-plugin-commonjs` which causes errors when using the // virtual plugin (see: rollup/rollup-plugin-commonjs#315), this means we have to use