-
Notifications
You must be signed in to change notification settings - Fork 22
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
bbottema
wants to merge
19
commits into
elboman:master
Choose a base branch
from
bbottema:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 34b34f9
compressed the comments to two lines to get a better sense of howlarg…
bbottema fc25f63
updated to Webpack 2.4.1
bbottema ab2986a
'minimize' should be 'compress'. Removed trailing comma.
bbottema f3d5356
updated to TypeScript 2.3.1
bbottema 1f2ae70
Dependencies are devDependencies! They are not needed by library user…
bbottema e7b0ac7
updated index.ts to actually reflect how a library would be exported …
bbottema c4e4c66
As an example, I've added momentjs as *optional* 3rd party dependency…
bbottema b1dc671
simplified build (paths) and removed unnecessary absolute path resolver
bbottema b62f555
removed trailing comma
bbottema 3fbf573
made naming consistent and cleared to library users
bbottema 87e2322
index.js doesn't exist in the root, point to commonjs index.js as def…
bbottema 313208c
added MIT license file under your name (in package json it says it's …
bbottema 58ac77d
Added TypeScript support for meta data (to support annotations). Some…
bbottema e3b4ad4
update package version
bbottema dbc12fd
updated, author, contributor and git repo entries
bbottema a0d2dde
type
bbottema 6b14052
added link to the blog explaining this repo setup
bbottema 77a2b72
typo: disy-esmodule -> dist-esmodule
bbottema File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
.DS_Store | ||
node_modules | ||
|
||
lib | ||
lib-esm | ||
_bundles | ||
dist-commonjs | ||
dist-esmodule | ||
dist-umd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!`; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.