npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@build-script/typescript-transformer-dual-package

v2.0.4

Published

A TypeScript transformer for (mainly) use with ttypescript that will (semi-)auto create dual package hazard.

Downloads

13

Readme

English

这是个啥子哦

这是一个ttypescript插件:

  • 在 tsc 正常编译文件后,重新编译它,用于生成 commonjs 格式的文件(以.cjs结尾)。
  • 在每个顶级import语句的路径中添加.js(暂不支持修改动态 import 语句)
  • 在 commonjs 格式的文件中,将import.meta.url转换成"file://" + __filename

使用方法

  1. 安装typescriptttypescript,以及这个包
    npm install --save-dev typescript ttypescript @build-script/typescript-transformer-dual-package
  2. 在你的tsconfig.json中插入一段:
    {
    	"compilerOptions": {
    		"module": "esnext", // 必须,可以是老版本(比如es6),但不能是commonjs、systemjs等非ESM目标
    		// ... 其他选项
    		"plugins": [
    			{
    				"transform": " @build-script/typescript-transformer-dual-package",
    				"compilerOptions": {
    					// [可选]
    					// 通常都不需要设置这个,可用于在第二次编译时覆盖父级compilerOptions
    					// 部分选项设置了也无效(例如module)
    				},
    				"verbose": false // 可以设置成true或1 !输出爆炸警告!
    			}
    		]
    	}
    }
  3. 写 TS 的大部分过程都不变,但是需要注意:
    • 别在你自己的代码里用任何一个require,用await import()代替。(使用module::createRequireAPI 的情况除外)
    • 不要在 import 的路径后面加\.(c|m)?js这样的扩展名(比如import "./some-file.js"
  4. 建议:
    • 打开allowSyntheticDefaultImportsesModuleInterop两个选项(在 tsconfig.json 里)
  5. 编译时用ttsc而不是tsc(webpack 等打包工具均支持 ttypescript,具体看他们的文档)
    npm install ttypescript
    ttsc -p path/to/tsconfig.json
  6. 在你的package.json里这样写:
    {
    	"type": "module",
    	"main": "lib/api.cjs",
    	// "browser": "lib/api.cjs",
    	"module": "lib/api.js",
    	// "esnext": "lib/api.js",
    	"bin": {
    		"some-cli-command": "lib/bin.cjs"
    	},
    	"exports": {
    		".": {
    			"require": "lib/api.cjs",
    			"import": "lib/api.js"
    		},
    		"some-path.js": {
    			"require": "lib/some-path.cjs",
    			"import": "lib/some-path.js"
    		}
    	}
    }
  7. 如果你的包含有命令行入口(没有就没事了)并且它不是 tsc 编译的输出
    • 将其扩展名改成.cjs

相关信息:

基本原理

  1. tsc/ttsc启动
    1. 它根据 tsconfig 加载这个包
    2. 创建Program对象,初始化包括这个包在内的 transformer
    3. 这个包内复制一个 Program 对象,用于之后编译 cjs。这次的 module 设置为 commonjs,并且添加了一个“内部 transformer”
  2. Programemit被调用(被 webpack、tsc),对每个被编译的文件:
    1. 修改文件中的ImportDeclarationExportDeclaration,给他们添加.js扩展名
    2. 调用之前复制的Programemit,它会重新编译一次,在此过程中“内部 transformer”生效
      1. 修改文件中的ImportDeclarationExportDeclaration,给他们添加.cjs扩展名,并将import.meta.url替换