Skip to content

Commit

Permalink
Fix build script. (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmckeb authored Sep 15, 2021
1 parent 6dd3b72 commit 5c8277d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@
// @ts-check
import { mkdirSync, readFileSync, rmdirSync } from 'fs';
import { writeFile } from 'fs/promises';
import { join } from 'path';
import { join, sep } from 'path';
import ts from 'typescript';

const DIR = './dist';

// Delete and recreate the ouptut directory.
// Delete and recreate the output directory.
rmdirSync(DIR, { recursive: true });
mkdirSync(DIR);

// Read the TypeScript config file.
const { config } = ts.readConfigFile('./tsconfig.json', (fileName) =>
const { config } = ts.readConfigFile('tsconfig.json', (fileName) =>
readFileSync(fileName).toString(),
);

const sourceFile = join('src', 'index.ts');
// Build CommonJS module.
compile(['./src/index.ts'], { module: ts.ModuleKind.CommonJS });
compile([sourceFile], { module: ts.ModuleKind.CommonJS });
// Build an ES2015 module and type declarations.
compile(['./src/index.ts'], {
compile([sourceFile], {
module: ts.ModuleKind.ES2020,
declaration: true,
});
Expand All @@ -36,12 +37,12 @@ function compile(files, options) {

host.writeFile = (fileName, contents) => {
const isDts = fileName.endsWith('.d.ts');
let path = join(DIR, fileName);
let path = join(DIR, fileName.split(sep)[1]);

if (!isDts) {
switch (compilerOptions.module) {
case ts.ModuleKind.CommonJS: {
// Adds backwards-compatibilty for Node.js.
// Adds backwards-compatibility for Node.js.
contents += `module.exports = exports.default;\nmodule.exports.default = exports.default;\n`;
// Use the .cjs file extension.
path = path.replace(/\.js$/, '.cjs');
Expand Down

0 comments on commit 5c8277d

Please sign in to comment.