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

@zoltu/typescript-transformer-append-js-extension

v1.0.1

Published

A TypeScript transformer for use with ttypescript that will append the JS extension to all relative imports that have no extension.

Downloads

2,274

Readme

Abstract

Teach the TypeScript compiler to emit JavaScript files that can be run natively in the browser using es2015 module syntax.

Motivation

Browsers now support loading modules natively, without needing to rely on bundlers. However, unlike in NodeJS, the browser cannot try a bunch of different paths to find a file, it must fetch the correct file in a single standard HTTP request on the first try. This means that when you do import { Foo } from './foo', the browser will try to fetch a file at the path http://my-domain/path/foo. Unless you configure your HTTP server to serve up JS files when no extension is provided, the web server will likely not find any file at that path because the actual file lives at http://my-domain/path/foo.js. One potential solution to this problem is to write your TS like import { Foo } from './foo.js'. TypeScript is clever enough that it will realize that you really meant foo.ts during compilation, and it will successfully find type information. However, ts-node is not clever enough to handle these faux paths so if you want a library that works in either ts-node or browser you are out of luck.

The hope is that eventually TypeScript will add support for appending the .js extension as a compiler option, but for the time being we'll have to do it ourself.

Usage

  1. Install typescript, ttypescript, and this transformer into your project if you don't already have them.
    npm install --save-dev typescript
    npm install --save-dev ttypescript
    npm install --save-dev @zoltu/typescript-transformer-append-js-extension
  2. Add the transformer to your es2015 module tsconfig-es.json (or whatever tsconfig.json you are using to build es2015 modules)
    // tsconfig-es.json
    {
    	"compilerOptions": {
    		"module": "es2015",
    		"plugins": [
    			{ "transform": "@zoltu/typescript-transformer-append-js-extension/output/index.js" }
    		]
    	},
    }
  3. Write some typescript with normal imports
    // foo.ts
    export function foo() { console.log('foo') }
    // index.ts
    import { foo } from './foo'
    foo()
  4. Compile using ttsc
    npx ttsc --project tsconfig-es.json