Skip to content

Commit

Permalink
introduce support for CommonJS modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Nipheris committed Aug 10, 2021
1 parent bad0934 commit b55f722
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@typescript-eslint/parser": "^4.28.2",
"copyfiles": "2.1.0",
"eslint": "^7.30.0",
"replace-ext": "^2.0.0",
"rollup": "^0.66.6",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-typescript2": "^0.25.2",
Expand Down
12 changes: 10 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"version": "0.0.0",
"description": "Functionality on top of HTML canvas element, including support for HiDPI and pixel-perfect rendering",
"scripts": {
"build": "tsc --project tsconfig.json && node ../tools/build-package-json package.json ../dist/fancy-canvas/package.json"
"build": "npm run build:commonjs && npm run build:esmodule && node ../tools/build-package-json package.json ../dist/fancy-canvas/package.json",
"build:commonjs": "tsc --project tsconfig.json --module commonjs && node ../tools/replace-extension ../dist/fancy-canvas .js .cjs",
"build:esmodule": "tsc --project tsconfig.json --module es2015 && node ../tools/replace-extension ../dist/fancy-canvas .js .mjs"
},
"keywords": [
"html",
Expand All @@ -14,5 +16,11 @@
],
"author": "smakarov@tradingview.com",
"license": "MIT",
"private": true
"private": true,
"main": "./index.cjs",
"module": "./index.mjs",
"exports": {
"import": "./index.mjs",
"require": "./index.cjs"
}
}
2 changes: 2 additions & 0 deletions tools/build-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ var output = {

// entry points
bin: input.bin,
exports: input.exports,
main: input.main,
man: input.man,
module: input.module,

// contents
files: ["**/*.js", "**/*.d.ts"],
Expand Down
16 changes: 16 additions & 0 deletions tools/replace-extension.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { readdirSync, renameSync } = require('fs');
const { extname, join } = require('path');
const replaceExt = require('replace-ext');

const args = process.argv.slice(2);
const dir = args[0];
const oldExtension = args[1];
const newExtension = args[2];

readdirSync(dir).forEach(fileName => {
if (extname(fileName) === oldExtension) {
const oldFilePath = join(dir, fileName);
const newFilePath = join(dir, replaceExt(fileName, newExtension));
renameSync(oldFilePath, newFilePath);
}
});
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"noUnusedLocals": true,
"sourceMap": false,
"target": "es5",
"module": "es2015",
"lib": [
"es5",
"es2015.symbol.wellknown",
Expand Down

0 comments on commit b55f722

Please sign in to comment.