Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build script #166

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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