I wrote a simple component in typescript that has a dependency on d3. I would like to publish this component on npm and use it as a global, commonjs module or typescript module. The component looks something like this:
class MyAwesomeComponent {
data(data: IMyDataFormat) {
}
render() {
}
}
interface IMyDataFormat {
}
Currently, my file is tsconfig.json
as follows:
{
"compilerOptions": {
"module": "none",
"target": "es5",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"declaration": true,
"outFile": "./dist/index.js",
"sourceMap": true
},
"include": [
"./src/**/*.ts"
]
}
When I run tsc
with this configuration, I get a file that works in a global context, so this will work for the consumer:
<script src="./d3.js"></script>
<script src="./my-awesome-component.js"></script>
<script>
var chart = new MyAwesomeComponent();
chart.data([]);
chart.render();
</script>
(, tsconfig ..), commonJS (, webpack ), typescript ?
commonJS:
const MyAwesomeComponent = require('my-awesome-component');
const MyAwesomeComponent = require('my-awesome-component').MyAwesomeComponent;
typescript:
import {MyAwesomeComponent, IMyDataFormat} from 'my-awesome-component';
!