Skip to content

Updated typescript library template #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e22d3c4
standard formatting, preparing for next changes
bbottema Jun 3, 2017
34b34f9
compressed the comments to two lines to get a better sense of howlarg…
bbottema Jun 3, 2017
fc25f63
updated to Webpack 2.4.1
bbottema Jun 3, 2017
ab2986a
'minimize' should be 'compress'. Removed trailing comma.
bbottema Jun 3, 2017
f3d5356
updated to TypeScript 2.3.1
bbottema Jun 3, 2017
1f2ae70
Dependencies are devDependencies! They are not needed by library user…
bbottema Jun 3, 2017
e7b0ac7
updated index.ts to actually reflect how a library would be exported …
bbottema Jun 3, 2017
c4e4c66
As an example, I've added momentjs as *optional* 3rd party dependency…
bbottema Jun 3, 2017
b1dc671
simplified build (paths) and removed unnecessary absolute path resolver
bbottema Jun 3, 2017
b62f555
removed trailing comma
bbottema Jun 3, 2017
3fbf573
made naming consistent and cleared to library users
bbottema Jun 3, 2017
87e2322
index.js doesn't exist in the root, point to commonjs index.js as def…
bbottema Jun 3, 2017
313208c
added MIT license file under your name (in package json it says it's …
bbottema Jun 3, 2017
58ac77d
Added TypeScript support for meta data (to support annotations). Some…
bbottema Jun 3, 2017
e3b4ad4
update package version
bbottema Jun 3, 2017
dbc12fd
updated, author, contributor and git repo entries
bbottema Jun 3, 2017
a0d2dde
type
bbottema Jun 3, 2017
6b14052
added link to the blog explaining this repo setup
bbottema Jun 3, 2017
77a2b72
typo: disy-esmodule -> dist-esmodule
bbottema Oct 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
.DS_Store
node_modules

lib
lib-esm
_bundles
dist-commonjs
dist-esmodule
dist-umd
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 Marco Botto
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Read about this setup at: [Compiling and bundling TypeScript libraries with Webpack](http://marcobotto.com/compiling-and-bundling-typescript-libraries-with-webpack/).
31 changes: 23 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
{
"name": "typescript-lib-example",
"version": "1.0.0",
"version": "1.1.0",
"description": "Example of TypeScript library setup for multiple compilation targets using tsc and webpack",
"main": "index.js",
"repository": "https://www.github.com/elboman/typescript-lib-example",
"author": "Marco Botto",
"main": "dist-commonjs/index.js",
"repository": {
"type": "git",
"url": "https://www.github.com/elboman/typescript-lib-example.git"
},
"author": {
"name": "Marco Botto",
"email": "[email protected]",
"url": "http://marcobotto.com"
},
"contributors": [{
"name": "Benny Bottema",
"email": "[email protected]",
"url": "http://www.bennybottema.com"
}],
"license": "MIT",
"scripts": {
"clean": "shx rm -rf _bundles lib lib-esm",
"build": "npm run clean && tsc && tsc -m es6 --outDir lib-esm && webpack"
"clean": "shx rm -rf dist-umd dist-commonjs dist-esmodule",
"build": "npm run clean && tsc && tsc -m es6 --outDir dist-esmodule && webpack"
},
"dependencies": {
"moment": "^2.18.1"
},
"devDependencies": {
"awesome-typescript-loader": "^3.0.4-rc.2",
"shx": "^0.2.2",
"typescript": "^2.1.6",
"webpack": "^2.2.1"
"typescript": "2.3.1",
"webpack": "2.4.1"
}
}
7 changes: 7 additions & 0 deletions src/Greeter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import * as moment from 'moment';

export class Greeter {
constructor(public greeting: string) { }
greet() {
if (typeof moment !== 'undefined') {
console.log('it is now: ', moment().format());
} else {
console.log('it is now: ', new Date().toString());
}
return `Hello, ${this.greeting}!`;
}
}
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import {Greeter} from './Greeter';

const example = new Greeter('World');

console.log(example.greet());
export * from './Greeter';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This an example of a 'barrel' index, where a library re-exports everything in a single index.

4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"module": "commonjs",
"target": "es5",
"lib": [ "es2015", "dom" ],
"outDir": "lib",
"outDir": "dist-commonjs",
"allowSyntheticDefaultImports": true,
"suppressImplicitAnyIndexErrors": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"declaration": true,
"jsx": "react"
Expand Down
111 changes: 51 additions & 60 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,58 @@
var path = require("path");
var webpack = require("webpack");

var PATHS = {
entryPoint: path.resolve(__dirname, 'src/index.ts'),
bundles: path.resolve(__dirname, '_bundles'),
}

var config = {
// These are the entry point of our library. We tell webpack to use
// the name we assign later, when creating the bundle. We also use
// the name to filter the second entry point for applying code
// minification via UglifyJS
entry: {
'my-lib': [PATHS.entryPoint],
'my-lib.min': [PATHS.entryPoint]
},
// The output defines how and where we want the bundles. The special
// value `[name]` in `filename` tell Webpack to use the name we defined above.
// We target a UMD and name it MyLib. When including the bundle in the browser
// it will be accessible at `window.MyLib`
output: {
path: PATHS.bundles,
filename: '[name].js',
libraryTarget: 'umd',
library: 'MyLib',
umdNamedDefine: true
},
// Add resolve for `tsx` and `ts` files, otherwise Webpack would
// only look for common JavaScript file extension (.js)
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
// Activate source maps for the bundles in order to preserve the original
// source when the user debugs the application
devtool: 'source-map',
plugins: [
// Apply minification only on the second bundle by
// using a RegEx on the name, which must end with `.min.js`
// NB: Remember to activate sourceMaps in UglifyJsPlugin
// since they are disabled by default!
new webpack.optimize.UglifyJsPlugin({
minimize: true,
sourceMap: true,
include: /\.min\.js$/,
})
],
module: {
// These are the entry point of our library. We tell webpack to use the name we assign later, when creating the bundle.
// We also use the name to filter the second entry point for applying code minification via UglifyJS
entry: {
'my-lib': ['./src/index.ts'],
'my-lib.min': ['./src/index.ts']
},
// The output defines how and where we want the bundles. The special value `[name]` in `filename` tell Webpack to use the name we defined above.
// We target a UMD and name it MyLib. When including the bundle in the browser it will be accessible at `window.MyLib`
output: {
path: path.resolve(__dirname, 'dist-umd'),
filename: '[name].js',
libraryTarget: 'umd',
library: 'MyLib',
umdNamedDefine: true
},
// Add resolve for `tsx` and `ts` files, otherwise Webpack would
// only look for common JavaScript file extension (.js)
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
// Activate source maps for the bundles in order to preserve the original
// source when the user debugs the application
devtool: 'source-map',
plugins: [
// Apply minification only on the second bundle by using a RegEx on the name, which must end with `.min.js`
// NB: Remember to activate sourceMaps in UglifyJsPlugin since they are disabled by default!
new webpack.optimize.UglifyJsPlugin({
compress:true,
sourceMap: true,
include: /\.min\.js$/
})
],

// Webpack doesn't understand TypeScript files and a loader is needed.
// `node_modules` folder is excluded in order to prevent problems with
// the library dependencies, as well as `__tests__` folders that
// contain the tests for the library
loaders: [{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader',
exclude: /node_modules/,
query: {
// we don't want any declaration file in the bundles
// folder since it wouldn't be of any use ans the source
// map already include everything for debugging
declaration: false,
}
}]
}
}
module: {
rules: [
{
test: /\.ts$/,
use: [
{loader: 'awesome-typescript-loader'}
]
}
]
},

// this library should not include external dependencies, so our library users
// can choose their own version (or omit it completely if it is optional)
externals: {
// tell Webpack to be compatible with the following format for requiring moment
'moment': {root: 'moment', commonjs: 'moment', commonjs2: 'moment', amd: 'moment'}
}
};

module.exports = config;