Skip to main content
Deno 2 is finally here 🎉️
Learn more

Build Deno

build-deno is a Node.js package that helps you build your Deno source code from your Node source code. It can copy files, change import paths, and skip files during the build process.

Installation

Node

# npm
npm install --save-dev build-deno

# yarn
yarn add -D build-deno

# pnpm
pnpm add -D build-deno

Deno

Unlike Node, Deno doesn’t use a package management like NPM and instead depends on direct URL imports. You can access build-deno on deno.land/x. This is how the most recent version may be imported:

You can also specify a particular version:

import { build } from 'https://deno.land/x/build_deno@1.5.0/mod.ts';

or letest version:

import { build } from 'https://deno.land/x/build_deno/mod.ts';

NOTE: There isn’t much of a change in how it’s used, but the remainder of this README assumes you’re using npm and importing straight from the build-deno package.

Usage

API

import type { Path, ChangePackage, CopyFiles, Options } from 'build-deno';
import { build } from 'build-deno';

const root: Path = '';

const rootDir: Path = 'src';

const outDir: Path = 'deno';

const changePackage: ChangePackage[] = [
  {
    package: `import { join as joinPath } from 'path';`,
    replace: `import { join as joinPath } from 'npm:path';`,
  },
  {
    package: `import { dirname, extname } from 'path';`,
    replace: `import { dirname, extname } from 'npm:path';`,
  },
  {
    package: `import { copyFile } from 'fs';`,
    replace: `import { copyFile } from 'npm:fs';`,
  },
  {
    byPackageName: true,
    package: `util`,
    replace: `import { promisify } from 'npm:util';`,
  },
  {
    package: `import { statSync } from 'fs';`,
    replace: `import { statSync } from 'npm:fs';`,
  },
  {
    byPackageName: true,
    package: 'fs/promises',
    replace: `import { readdir, readFile, mkdir, writeFile } from 'npm:fs/promises';`,
  },
  {
    package: `import { dirname } from 'path';`,
    replace: `import { dirname } from 'npm:path';`,
  },
];

const skipFile: SkipFile[] = [
  {
    dir: '',
    name: 'cli.ts',
  },
];

const copyFiles: CopyFiles[] = [
  {
    from: 'README.md',
    to: 'README.md',
  },
];

const options: Options = {
  root,
  rootDir,
  outDir,
  changePackage,
  skipFile,
  copyFiles,
};

build(options);

Options

  • root: The root directory of your Node project. Required.
  • rootDir: The directory of the Node source code. Required.
  • outDir: The directory where the Deno source code will be generated. Required.
  • changePackage: An array of objects that specify the package names to change the import path for. Optional.
  • skipFile: An array of file paths to skip during the build. Optional.
  • copyFiles: An array of file paths to copy from the Node source code to the Deno source code. Optional.

Types

build-deno exports the following types:

  • Path: A string representing a file path.
  • SkipFile: An object containing the dir and name of a file to skip.
  • ChangePackage: An object containing the packageName and path of a package to change the import path for.
  • CopyFiles: An object containing the from and to paths of a file to copy.
  • Options: An object containing the above properties.

Example

You can find an example of build-deno in use in the Denoify example project.

CLI Commands

To use build-deno, you can run the following commands:

build-deno

Builds your project with the configuration file. Make sure to add the configuration file in the root directory of your project. The configuration file name can be one of the following:

  • build-deno.config.js
  • build-deno.config.cjs
  • build-deno.config.mjs
  • build-deno.config.json

Example:

build-deno

build-deno -H or build-deno --help

Displays the help menu for build-deno.

Example:

build-deno -H

build-deno -V or build-deno --version

Displays the version of build-deno.

Example:

build-deno -V

Configuration

build-deno.config.js

Example Configuration File `build-deno.config.js`
module.exports = {
  root: '',
  rootDir: 'src',
  outDir: 'deno',
  changePackage: [
    {
      package: `import { join as joinPath } from 'path';`,
      replace: `import { join as joinPath } from 'npm:path';`,
    },
    {
      package: `import { dirname, extname } from 'path';`,
      replace: `import { dirname, extname } from 'npm:path';`,
    },
    {
      package: `import { copyFile } from 'fs';`,
      replace: `import { copyFile } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: `util`,
      replace: `import { promisify } from 'npm:util';`,
    },
    {
      package: `import { statSync } from 'fs';`,
      replace: `import { statSync } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: 'fs/promises',
      replace: `import { readdir, readFile, mkdir, writeFile } from 'npm:fs/promises';`,
    },
    {
      package: `import { dirname } from 'path';`,
      replace: `import { dirname } from 'npm:path';`,
    },
  ],
  skipFile: [
    {
      dir: '',
      name: 'cli.ts',
    },
  ],
  copyFiles: [
    {
      from: 'README.md',
      to: 'README.md',
    },
  ],
};

build-deno.config.cjs

Example Configuration File `build-deno.config.cjs`
module.exports = {
  root: '',
  rootDir: 'src',
  outDir: 'deno',
  changePackage: [
    {
      package: `import { join as joinPath } from 'path';`,
      replace: `import { join as joinPath } from 'npm:path';`,
    },
    {
      package: `import { dirname, extname } from 'path';`,
      replace: `import { dirname, extname } from 'npm:path';`,
    },
    {
      package: `import { copyFile } from 'fs';`,
      replace: `import { copyFile } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: `util`,
      replace: `import { promisify } from 'npm:util';`,
    },
    {
      package: `import { statSync } from 'fs';`,
      replace: `import { statSync } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: 'fs/promises',
      replace: `import { readdir, readFile, mkdir, writeFile } from 'npm:fs/promises';`,
    },
    {
      package: `import { dirname } from 'path';`,
      replace: `import { dirname } from 'npm:path';`,
    },
  ],
  skipFile: [
    {
      dir: '',
      name: 'cli.ts',
    },
  ],
  copyFiles: [
    {
      from: 'README.md',
      to: 'README.md',
    },
  ],
};

build-deno.config.mjs

Example Configuration File `build-deno.config.mjs`
export default {
  root: '',
  rootDir: 'src',
  outDir: 'deno',
  changePackage: [
    {
      package: `import { join as joinPath } from 'path';`,
      replace: `import { join as joinPath } from 'npm:path';`,
    },
    {
      package: `import { dirname, extname } from 'path';`,
      replace: `import { dirname, extname } from 'npm:path';`,
    },
    {
      package: `import { copyFile } from 'fs';`,
      replace: `import { copyFile } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: `util`,
      replace: `import { promisify } from 'npm:util';`,
    },
    {
      package: `import { statSync } from 'fs';`,
      replace: `import { statSync } from 'npm:fs';`,
    },
    {
      byPackageName: true,
      package: 'fs/promises',
      replace: `import { readdir, readFile, mkdir, writeFile } from 'npm:fs/promises';`,
    },
    {
      package: `import { dirname } from 'path';`,
      replace: `import { dirname } from 'npm:path';`,
    },
  ],
  skipFile: [
    {
      dir: '',
      name: 'cli.ts',
    },
  ],
  copyFiles: [
    {
      from: 'README.md',
      to: 'README.md',
    },
  ],
};

build-deno.config.json

Example Configuration File `build-deno.config.json`
{
  "root": "",
  "rootDir": "src",
  "outDir": "deno",
  "changePackage": [
    {
      "package": "import { join as joinPath } from 'path';",
      "replace": "import { join as joinPath } from 'npm:path';"
    },
    {
      "package": "import { dirname, extname } from 'path';",
      "replace": "import { dirname, extname } from 'npm:path';"
    },
    {
      "package": "import { copyFile } from 'fs';",
      "replace": "import { copyFile } from 'npm:fs';"
    },
    {
      "byPackageName": true,
      "package": "util",
      "replace": "import { promisify } from 'npm:util';"
    },
    {
      "package": "import { statSync } from 'fs';",
      "replace": "import { statSync } from 'npm:fs';"
    },
    {
      "byPackageName": true,
      "package": "fs/promises",
      "replace": "import { readdir, readFile, mkdir, writeFile } from 'npm:fs/promises';"
    },
    {
      "package": "import { dirname } from 'path';",
      "replace": "import { dirname } from 'npm:path';"
    }
  ],
  "skipFile": [
    {
      "dir": "",
      "name": "cli.ts"
    }
  ],
  "copyFiles": [
    {
      "from": "README.md",
      "to": "README.md"
    }
  ]
}

License

build-deno is licensed under the MIT License.