Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With just typescript (no webpack and no bable) can I get a multiple file solution to run in a browser

Tags:

What would the tsconfig.json need to be to make this work in Chrome? So that I only had to run tsc and could then view the file in a browser and the appropriate result would display in the console?

index.html contains:

<!DOCTYPE html>
<html>
    <head><title>TypeScript app</title></head>
    <body>
        <script src="dist/app.js"></script>
    </body>
</html>

index.ts contains

import { alpha } from "alpha";
import { beta } from "beta";
console.log(alpha + " " + beta);

alpha contains

export const alpha = 'alpha';

beta contains

export const beta = 'beta';

The entry point would be index.ts and I would like it all bundled into a single file named app.js.

like image 608
Steven T. Cramer Avatar asked Mar 18 '17 18:03

Steven T. Cramer


People also ask

Does TypeScript need webpack?

TypeScript compiler is designed to compile TypeScript and if you want to do more, you need to use Webpack or another bundling tool that provides seamless integration with TypeScript. Now that we have settled with Webpack, let's understand how Webpack works with TypeScript. As we know, TSC needs a tsconfig.

Why webpack is needed?

Webpack gives you control over how to treat different assets it encounters. For example, you can decide to inline assets to your JavaScript bundles to avoid requests. Webpack also allows you to use techniques like CSS Modules to couple styling with components.

Can I mix TypeScript and JavaScript?

The TypeScript compiler supports a mix of JavaScript and TypeScript files if we use the compiler option --allowJs : TypeScript files are compiled. JavaScript files are simply copied over to the output directory (after a few simple type checks).


2 Answers

No browser implements ES6 and its module system natively yet. However, if you want to avoid Webpack and Babel specifically, there are options which have less required configuration, though are perhaps less powerful. The TypeScript compiler itself can handle bundling and transpiling to ES5 (which modern browsers support), leaving only the module system to be covered by a library. Here's one such solution using RequireJS:

tsconfig.json

{
    "compilerOptions": {
        "module": "amd",
        "target": "es5",
        "outFile": "dist/app.js"
    },
    "include": [
        "src/**/*"
    ]
}

index.html

<!DOCTYPE html>
<html>
    <head><title>TypeScript app</title></head>
    <body>
        <script data-main="dist/app" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.3/require.min.js"></script>
    </body>
</html>

src/app.ts

// normally you would use a .d.ts file for RequireJS instead of declare
declare var require: (deps: string[]) => void;

require(['index']);

src/index.ts

import { alpha } from "./alpha";
import { beta } from "./beta";
console.log(alpha + " " + beta);

src/alpha.ts

export const alpha = 'alpha';

src/beta.ts

export const beta = 'beta';
like image 136
Steven Barnett Avatar answered Apr 29 '23 03:04

Steven Barnett


Add this snippet before loading bundle generated by TS, you need to specify TS to use AMD loader in TS config.

window.define = function(name, required, moduleFn) {
  var require = function() { throw new Error("AMD require not supported!")}
  var exports = window.define.modules[name] = {}
  var resolved = [require, exports]
  for (var i = 2; i < required.length; i++) {
    var m = window.define.modules[required[i]]
    if (!m) throw new Error("AMD module `" + required[i] + "` not found!")
    resolved.push(m)
  }
  moduleFn.apply(null, resolved)
}
window.define.modules = {}

Example

like image 22
Alex Craft Avatar answered Apr 29 '23 02:04

Alex Craft