diff options
| author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2018-09-19 23:13:59 -0700 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2018-09-20 02:13:59 -0400 |
| commit | fab4bdf40d1fe1a53d00dbc0bc902c71d4446403 (patch) | |
| tree | 843aa0d5cf867fdcefeee991655332da372d9b47 /rollup.config.js | |
| parent | 017ef096dfb0592d828804175e582895a3f39954 (diff) | |
Add deno.arch/deno.platform (#773)
Diffstat (limited to 'rollup.config.js')
| -rw-r--r-- | rollup.config.js | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rollup.config.js b/rollup.config.js index d5574a7d5..2fdaee08c 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 |
