The βcleanestβ way that I know is to write a modular script at both ends and create a library of objects that you want to split (so that shared objects are defined in one place)
Customization
Foundation: Typescript with
- target ES6 module: commonjs + Babel (Typescript 1.7 required)
- or target ES5 module: commonjs
using webpack or browser
Backend: ES6 (with -harmony flag on node) or ES5
Library
Create a library, say shared , written in Typescript and create an exported Object class
export default class MyObject{ ... }
Make sure the library is compiled with declaration: true (for example, in tsconfig): tsc will create js + typing (declarations).
In the package.json the shared library, verify that the typings entry typings set to the generated MyObject.d.ts file. If there are several objects; create an index.ts file that re-exports all objects and the typings point in index.d.ts
Using
Frontend: since you are now using modular JS / TS, import your object directly from Typescript
import MyObject from 'shared'
The Typescript transpiler will automatically find the .d.ts definition from the typings shared package.json entry.
Backend: just require('shared')
Note. If there are several shared objects, do not use the default export in shared : they cannot be re-exported.
Bruno Grieder Dec 11 '15 at 13:47 2015-12-11 13:47
source share