Coder Social home page Coder Social logo

typescript-lib-starter's Introduction

{library-name}

TypeScript lib starter

Enjoying/Using TypeScript lib starter ? πŸ’ͺβœ…

Greenkeeper badge

Build Status NPM version Downloads Standard Version styled with prettier Conventional Commits

✨ Features

  • creates package for both Node and Browser
  • build will creates 4 standard "package" formats:
    • umd πŸ‘‰ UMD bundle for Node and Browser

      main field in package.json

    • esm5 πŸ‘‰ transpiled files to ES5 + es2015 modules for tree shaking

      module field in package.json

    • esm2015 πŸ‘‰ raw javascript files transpiled from typescript to latest ES standard ( es2018 )

      es2015 field in package.json

      this is useful if you wanna transpile everything or just wanna ship untranspiled esNext code for evergreen browsers)

    • fesm πŸ‘‰ experimental bundle type introduced by Angular team (TL;DR: it's an es2015 flattened bundle, like UMD but with latest ECMAscript and JS modules)
  • type definitions are automatically generated and shipped with your package
    • types field in package.json

  • sideEffects πŸ‘‰ support proper tree-shaking for whole library ( Webpack >= 4). Turn this off or adjust as needed if your modules are not pure!

✌️ start coding in 2 steps

  1. git clone https://github.com/Hotell/typescript-lib-starter <your-libary-folder-name> && cd $_

  2. yarn

setting-up-your-library

Yes that's it. Happy coding ! πŸ––

πŸ’‰ Consumption of published library:

  1. install it πŸ€–
yarn add my-new-library
# OR
npm install my-new-library
  1. use it πŸ’ͺ

Webpack

NOTE:

Don't forget to turn off ES modules transpilation to enable tree-shaking!

  • babel: {"modules": false}
  • typescript: {"module": "esnext"}
// main.ts or main.js
import { Greeter } from 'my-new-library'

const mountPoint = document.getElementById('app')
const App = () => {
  const greeter = new Greeter('Stranger')
  return `<h1>${greeter.greet()}</h1>`
}
const render = (Root: Function, where: HTMLElement) => {
  where.innerHTML = Root()
}

render(App, mountPoint)
<!-- index.htm -->
<html>
  <head>
    <script src="bundle.js" async></script>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

UMD/ES2015 module aware browsers (no bundler)

<html>
  <head>
    <script type="module">
      import { Greeter } from './node_modules/my-lib/esm2015/index.js'

      const mountPoint = document.querySelector('#root')

      const App = () => {
        const greeter = new Greeter('Stranger')
        return `<h1>${greeter.greet()}</h1>`
      }

      const render = (Root, where) => {
        where.innerHTML = Root()
      }

      render(App, mountPoint)
    </script>
    <script
      nomodule
      src="node_modules/my-lib/bundles/my-new-library.umd.min.js"
    ></script>
    <script nomodule async>
      var Greeter = MyLib.Greeter

      var mountPoint = document.querySelector('#root')

      var App = function() {
        var greeter = new Greeter('Stranger')
        return '<h1>' + greeter.greet() + '</h1>'
      }

      var render = function(Root, where) {
        where.innerHTML = Root()
      }

      render(App, mountPoint)
    </script>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

πŸ•΅οΈβ€β™€οΈ Troubleshooting

dynamic import()

This starter uses latest TypeScript >=3.x which has support for lazy loading chunks/modules via import() and also definition acquisition via import('../path-to-module').TypeFoo

Before TS 2.9, it wasn't possible to properly generate ambient definitions if you used dynamic import(). This works now as expected without any hacks ❀️ !

Before TS 2.9

Please note that if you wanna use that feature, compiler will complain because declaration generation is turned on, and currently TS can't handle type generation with types that will be loaded in the future ( lazily )

How to solve this:

  • turn of type checking and don't generate types for that lazy import: import('./components/button') as any
  • or you can use this temporary workaround

πŸ₯‚ License

MIT as always

typescript-lib-starter's People

Contributors

elboman avatar greenkeeper[bot] avatar hotell avatar langpavel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

typescript-lib-starter's Issues

Get rid of most `config/` files

Ahoj Martine!

It will be great if most of tools can work just as expected by running them.
What I can see in package.json scripts β€” there are mostly explicit calls with loading args from ./config/…

It isn't good for almost everyone who use command line ;-)

I expect that root folder will be bloated with hidden dot files β€” like:

  • .gitignore
  • .editorconfig
  • .npmignore, .npmrc
  • .prettierrc.js, .prettierignore
  • .travis.yml
  • .yarnrc ?
  • … I can imagine another

but, seriously, really… I thing it is worse to keep expected config files outside than add them to their default path. Tools will be happy with everything in implicit path and tooling will behave more correctly with editors.

So, please, consider move everything which must be specified explicitly in package.json to the root,
if possible, remove config dir entirely β€” files inside IMHO can be moved on better path with much better semantic meaning

Do not force CodeLens (or explain it well)

Hi
you are forcing VSCode users to:

  "typescript.referencesCodeLens.enabled": true,
  "javascript.referencesCodeLens.enabled": true,

As I can imagine β€” it can be very powerful tool. But it doesn't work for me and I must explicitly disable it.

Will be much better to instruct people how to turn this on instead of letting us (scratching on the head) what is that 0 references disturbing line…

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml
  • The new Node.js version is in-range for the engines in 1 of your package.json files, so that was left alone

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected πŸ€–


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

scripts/migrate: implement proper tslint merge resolution

Feature request

Use case(s)

we are not updating tslint.json in existing project. Currently migrate script just echoes that nothing was updated, because it does no-op. This should be fixed/implemented. All we need to do is to find out how to properly merge .jsonc files ( json files with comments )

handle termination of init script gracefully

Feature request

Use case(s)

Currently when used runs yarn after clone and terminates prompts, "initialization" process will run anyways with destructive consequences ( removed files... re-initialised git etc).

All that needs to be done is to handle CTRL+C gracefully -> just terminate without executing the script logic

[Feedback] - Use TypeScript for config/ and scripts/ and use rollup-plugin-uglify-es to target es6 and run tests in browser too

Just a feedback:

  • since this is a typescript related library, It would be awesome that the devtools also use TypeScript. In particular config/rollup.config.js is hard to follow due to the jsdoc. For scripts you can use ts-node for run the typescript directly but you will need to transpile them in order to the tools like rollup, jest, etc to "undertand" the config (or maybe not if using valid js?)

  • you are targeting es5 which is fine but for those which can target more than that it won't work because rollup-plugin-uglify doesn't understand es6. using rollup-plugin-uglify-es will work for all.

  • I think jest-puppeteer can be easily added to run the specs in the browser too

  • (more ambitious) would be awesome to use yeoman generator to ask: library name, target js version, if install jest and tests or not, if install lint tool, travis, etc. In my case I only needed the tsc & rollup part. Maybe this could be another project and use this one as a template

Just a feedback, the tool is very helpful and the project has lot of quality. I was looking for building libraries written with typescript, bundling in different module formats and didn't found much since in general people bundle applications not libraries . Congrats, keep it up - you can close this. Sorry for the noise

Make this repo template

Feature request

This repo seems to be a nice template.
GitHub created new feature Templates I think it will be nice to switch it on for this repo :)
For example:
image

Also, do you want me to add linting of TS using ESLint? :) I can :)

add monorepo support

Feature request

Use case(s)

support monorepo setup on init.

https://github.com/Hotell/ts-setup already uses this starter with various tweaks. It would be nice to incorporate those, so use would get asked during init if he wan't to create monorepo or standard single repo.

feat: use webpack 3.0 for creating flat module bundles for es2015

  • this will lower library size, because webpack boilerplate would be not needed anymore for simulating import/export
  • also consumer bundler will process just one file so bundling will be much quicker
  • leverage uglify-es for minifying es2015 code

make no mistake, all of this is already possible with rollup, but since we are using webpack here well ... :) πŸ‘

DX: support init via npx/yarn create

Feature request

Use case(s)

It's 2019 already huh! cloning repos manually is kinda "old school/cumbersome".

We should support modern project bootstraping experience, like yarn create or npx create-typescript-lib.

Work that needs to be done:

TODO:

  • change package name, to contain create prefix (@martin_hotell/create-typescript-lib)
  • publish bin script to npm

How would bin work?:

  • here is most simple implementation that comes up on my mind:

the package would just clone this repo and execute init script

When I first pack, there will be a file loss. If I copy the CHANGELOG.md file back, it's normal.

Bug report

  • {package-name} version: x.x.x ()

Current behaviour

When I first pack, there will be a file loss. If I copy the CHANGELOG.md file back, it's normal.

npm run build

> [email protected] prebuild /Volumes/MAC1/file/test/npm/typescript-lib-starter
> npm run cleanup && npm run verify


> [email protected] cleanup /Volumes/MAC1/file/test/npm/typescript-lib-starter
> shx rm -rf dist


> [email protected] verify /Volumes/MAC1/file/test/npm/typescript-lib-starter
> npm run validate-js && npm run style && npm run test:ci


> [email protected] validate-js /Volumes/MAC1/file/test/npm/typescript-lib-starter
> tsc -p ./config && tsc -p ./scripts


> [email protected] style /Volumes/MAC1/file/test/npm/typescript-lib-starter
> npm run format -- --list-different && npm run lint


> [email protected] format /Volumes/MAC1/file/test/npm/typescript-lib-starter
> prettier "**/*.{ts,tsx,js,jsx,css,scss,sass,less,md}" "--list-different"


> [email protected] lint /Volumes/MAC1/file/test/npm/typescript-lib-starter
> tslint --project tsconfig.json --format codeFrame


> [email protected] test:ci /Volumes/MAC1/file/test/npm/typescript-lib-starter
> npm t -- --ci


> [email protected] test /Volumes/MAC1/file/test/npm/typescript-lib-starter
> jest -c ./config/jest.config.js "--ci"

 PASS  src/__tests__/greeter.spec.ts
  Greeter
    βœ“ should greet (3ms)
    βœ“ should greet and print deprecation message if in dev mode (9ms)

  console.warn node_modules/jest-mock/build/index.js:711
    this method is deprecated, use #greet instead

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        4.048s
Ran all test suites.

> [email protected] build /Volumes/MAC1/file/test/npm/typescript-lib-starter
> tsc && tsc --target es2018 --outDir dist/esm2015 && rollup -c config/rollup.config.js && rollup -c config/rollup.config.js --environment NODE_ENV:production


/Volumes/MAC1/file/test/npm/typescript-lib-starter/dist/esm5/index.js β†’ dist/bundles/index.umd.js...
created dist/bundles/index.umd.js in 28ms

/Volumes/MAC1/file/test/npm/typescript-lib-starter/dist/esm2015/index.js β†’ dist/bundles/index.esm.js...
created dist/bundles/index.esm.js in 8ms

/Volumes/MAC1/file/test/npm/typescript-lib-starter/dist/esm5/index.js β†’ dist/bundles/index.umd.min.js...
created dist/bundles/index.umd.min.js in 259ms

/Volumes/MAC1/file/test/npm/typescript-lib-starter/dist/esm2015/index.js β†’ dist/bundles/index.esm.min.js...
created dist/bundles/index.esm.min.js in 182ms

> [email protected] postbuild /Volumes/MAC1/file/test/npm/typescript-lib-starter
> node scripts/copy && npm run size

fs.js:119
    throw err;
    ^

Error: ENOENT: no such file or directory, copyfile '/Volumes/MAC1/file/test/npm/typescript-lib-starter/CHANGELOG.md' -> '/Volumes/MAC1/file/test/npm/typescript-lib-starter/dist/CHANGELOG.md'
    at copyFileSync (fs.js:1743:3)
    at source.forEach (/Volumes/MAC1/file/test/npm/typescript-lib-starter/scripts/copy.js:43:7)
    at Array.forEach (<anonymous>)
    at cp (/Volumes/MAC1/file/test/npm/typescript-lib-starter/scripts/copy.js:42:12)
    at main (/Volumes/MAC1/file/test/npm/typescript-lib-starter/scripts/copy.js:22:3)
    at Object.<anonymous> (/Volumes/MAC1/file/test/npm/typescript-lib-starter/scripts/copy.js:11:1)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postbuild: `node scripts/copy && npm run size`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] postbuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/wsx/.npm/_logs/2019-03-04T01_33_39_760Z-debug.log

Expected behaviour

Please explain how you'd expect it to behave.

Feature request

Use case(s)

Explain the rationale for this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.