Write TypeScript and release the library for browser and Node

I have an internal library used in Node.js and a browser. It has many files related to the Grunt task and various prologs, one for the browser, one for Node:

browser:

// dependent 3rd-party libs like Mustache are already global
window.myLib = { /*just a namespace object filled with stuff later*/ }

// then comes the plain javascript which just adds elements to myLib.
// This part is identical to that used in Node
// example:
myLib.renderPartDetail = function (...) {...};

Node:

var Mustache = require('mustache');
var myLib = {};
module.exports = myLib;

// then comes the plain javascript which just adds elements to myLib.
// This part is identical to that used in Browser

The result is 2 different single-phase js files, one for the browser, one for Node.

What i would like

  • use typeScript
  • if possible, use only one CommonJS syntax (or ES6 modules) for the browser and node
  • invest in something that does not die in the next couple of months.
  • be a little more modular (maybe someone needs only part of lib)

What bothers me

I find 2 different types of module handling in TypeScript:

import {a, b} from './x'

and

import c = require('./y')

I am using the latter from node, but the former looks like ES6 (it may be the future).

tsc --module commonjs, , ? --module system, , , , export = ... .

browserify, tsify, watchify, jspm, SystemJS, webpack - , , .

require(<a node module living in node_modules>), tsc : "TS2307: " ".

  • Node ?
  • ? , ? ( Gulp, Grunt).
  • TypeScript Node ? 1.4, IntelliJ, 1.6.2 , "TypeError: host.fileExists " ( ). , Node v4.1.1?

, . , , , , .

+4
2

Node ?

es5, . .

? ,

( ) webpack. commonjs/nodejs , webpack . . https://github.com/basarat/tsb/tree/master

TypeScript Node ? 1.4, IntelliJ, 1.6.2 , "TypeError: host.fileExists " ( ). , Node v4.1.1?

TypeScript ( TypeStrong, , atom- typescript/grunt-ts/ts-loader). , , webstorm . ( atom- typescript).

+2

TypeError: host.fileExists WebStorm 10 Typescript ( npm).

WebStorm 11 ( 2 2015 .) https://www.jetbrains.com/webstorm/download/

+2

Source: https://habr.com/ru/post/1609452/


All Articles