更新

我本来想 skip 掉 bundler (webpack), 感觉单侧不需要搞那么复杂, 所以用了 TypeScript 自带的 bundle (outFile) + SystemJS. 谁知道这技术早就落后 n 年了. 真的是太久没有玩前端了.

想 bundle TypeScript 用 esbuild 是最好的, 又快又简单. 看这篇 TypeScript – Work with JavaScript Library (using esbuild).

前言

之前写的 TypeScript – Get Started 太简单了, 想依赖个 library 都做不到. 于是就有了这篇进阶版本.

这已经是尽可能不依赖 bundler (e.g. Webpack) 情况下, 让它跑起来了.

TypeScript 有基本的 bundler 功能, 配上 SystemJS 就可以做到模块化单元测试了.

参考

medium – TypeScript: Start a Browser-based Project Using the System.js

Youtube – SystemJS Explaination

SystemJS 介绍

在 JavaScript – Modular 有提到过 SystemJS, 前端模块 AMD (RequireJS) > UMD (SystemJS) > ES Module

它属于现阶段最好的过渡方案.

bundle 会把多个 .js 模块变成一个, 这时 ES Module 的 import 就失效了. 所以就要有另一个模块方案. SystemJS 主要就是干这些.

另外, ES Module import 只能写 path (importmap 目前许多 browser 都不支持), 但是我们更希望写名字, 比如 import $ from 'jquery',

SystemJS 的其中一个功能就是 importmap 的 polyfill.

启动项目

mkdir play-typescript
cd play-typescript
yarn init
tsc --init
yarn add typescript --dev

HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- SystemJS -->
<script
src="https://cdn.jsdelivr.net/npm/systemjs@6.12.1/dist/s.min.js"
defer
></script>
<script
src="https://cdn.jsdelivr.net/npm/systemjs@6.12.1/dist/extras/named-register.min.js"
defer
></script>
<script
src="https://cdn.jsdelivr.net/npm/systemjs@6.12.1/dist/extras/amd.min.js"
defer
></script> <!-- importmap -->
<script type="systemjs-importmap">
{
"imports": {
"lodash": "https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js",
"jquery": "https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"
}
}
</script> <!-- bundle.js -->
<script type="systemjs-module" src="./bundle.js"></script>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>

首先是 3 个 SystemJS 的 files

然后是项目依赖的 Library (jquerylodash) importmap

最后是 TypeScript bundle 出来的 bundle.js

注: 都加了 defer 哦

tsconfig

{
"compilerOptions": {
"target": "es2016",
"module": "System", // 关键
"outFile": "./bundle.js", // 关键
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

关键就是 module 和 outFile

Typescript

先添加依赖 Library

yarn add jquery
yarn add @types/jquery --dev yarn add lodash
yarn add @types/lodash --dev

jquery 和 lodash 都没有默认的 .d.ts, 所以需要另外添加 @types

module.ts

import $ from "jquery";
import _ from "lodash"; export function myFunction() {
$("h1").css("color", "blue");
_.forEach([1, 2, 3], (index) => $("body").append($(`<h1>${index}</h1>`)));
}

index.ts

import { myFunction } from "./module";

myFunction();

运行 tsc and Open with Live Server

运行 command tsc

然后开启 Live Server

效果

TypeScript bundle 出来的 .js 会包含所有的 module, 全部都被 System.register

而最后一个 System.register 会走位 entry point. 这个是 SystemJS 的规则.

视乎没有办法指定 entry point. 这个很麻烦丫. 于是我决定不用 SystemJS, 改用 esbuild bundler.

TypeScript – Get Started Advanced (Work with SystemJS)的更多相关文章

  1. Dynamics AX7 materials

    Dynamics AX community https://community.dynamics.com/ax Dynamics AX Wiki https://ax.help.dynamics.co ...

  2. [TypeScript] Using Lodash in TypeScript with Typings and SystemJS

    One of the most confusing parts of getting started with TypeScript is figuring out how to use all th ...

  3. [TypeScript] Loading Compiled TypeScript Files in Browser with SystemJS

    TypeScript outputs JavaScript, but what are you supposed to do with it? This lesson shows how to tak ...

  4. TypeScript & Advanced Types

    TypeScript & Advanced Types https://www.typescriptlang.org/docs/handbook/advanced-types.html#typ ...

  5. [译]Angular2 和TypeScript -- 一次简要的预览

    原文链接:https://www.infoq.com/articles/Angular2-TypeScript-High-Level-Overview 作者:  Yakov Fain Posted o ...

  6. 转载:《TypeScript 中文入门教程》 7、模块

    版权 文章转载自:https://github.com/zhongsp 建议您直接跳转到上面的网址查看最新版本. 关于术语的一点说明: 请务必注意一点,TypeScript 1.5里术语名已经发生了变 ...

  7. TypeScript & JavaScript

    http://www.typescriptlang.org/docs/tutorial.html handbook: Basic Types Variable Declarations Interfa ...

  8. Angular2+typescript+webpack2(支持aot, tree shaking, lazy loading)

    概述 Angular2官方推荐的应该是使用systemjs加载, 但是当我使用到它的tree shaking的时候,发现如果使用systemjs+rollup,只能打包成一个文件,然后lazy loa ...

  9. 如何在ASP.NET 5上搭建基于TypeScript的Angular2项目

    一.前言 就在上月,公司的一个同事建议当前的前端全面改用AngularJs进行开发,而我们采用的就是ASP.NET 5项目,原本我的计划是采用TypeScript直接进行Angular2开发.所以借用 ...

  10. Nodejs生态圈的TypeScript+React

    基于Nodejs生态圈的TypeScript+React开发入门教程   基于Nodejs生态圈的TypeScript+React开发入门教程 概述 本教程旨在为基于Nodejs npm生态圈的前端程 ...

随机推荐

  1. PowerBuilder现代编程方法X01:PowerPlume的X模式

    临渊羡鱼,不如退而结网. PB现代编程方法X01:PowerPlume的X模式 前言 PowerPlume是PowerBuilder深度创新的扩展开发框架(免费商用). 它不是一个大而全的类库(取决于 ...

  2. [oeasy]python0083_[趣味拓展]字体样式_正常_加亮_变暗_控制序列

    字体样式 回忆上次内容 上次了解了 一个新的转义模式 \033 逃逸控制字符 esc   esc 让输出 退出 标准输出流 进行 控制信息的设置 可以 清屏 也可以 设置光标输出的位置     还能做 ...

  3. vue小知识~使用$attrs和$listeners接收父组件转来的属性和方法

    一般下我们子组件获取父组件传过来的的值得时候,我们是使用props来接收,但是如果我们父组件转过来的值是给孙组件使用时,使用哪个props接收的方式太过麻烦. 这是vue为我们提供了一个实例$attr ...

  4. C语言指针知识总结

    指针 定义 指针是一个变量,存储另一个变量的内存地址,它允许直接访问和操作内存中的数据,使得程序能够以更灵活和高效的方式处理数据和内存. 获取变量地址:使用取地址符 &. 访问地址上的数据:使 ...

  5. 使用后台模板,cnpm install报错

  6. 18B20的CRC官方讲解

    理解和运用MAXIM IBUTTON产品中的循环冗余校验(CRC) 摘要 : 全部1-Wire器件,包括iButton器件,都具有唯一的8字节注册码,储存在只读存储器(ROM)中.该注册码在1-Wir ...

  7. cgroup限制进程cpu

    编写一个死循环脚本 [root@workstation ~]# cat circle.sh #!/bin/bash a=1 while true do let a++ done 查看top 使用cgr ...

  8. 【Docker】08 部署挂载本地目录的MySQL

    拉取MySQL镜像: docker pull mysql:8.0.21 执行挂载运行MySQL容器的命令: docker run -dit \ --name mysql-test \ -p 3306: ...

  9. 《Python数据可视化之matplotlib实践》 源码 第一篇 入门 第二章

    图 2.1 import matplotlib as mpl import matplotlib.pyplot as plt mpl.rcParams['font.sans-serif']=['Sim ...

  10. HDMI和DP双屏幕连接,对于BIOS来说哪个优先级高——DP连接优先级高于HDMI

    最近被博导忽悠了,说是实验室的国家项目结项了,有几十万的资金没有花掉,于是每个人都有了1W的报销金额,由于是结项所用因此只能报销耗材.我这人呢,平时是绝对不占小便宜的,但这次是个大便宜,于是就有些没把 ...