diff --git a/package.json b/package.json index 8cb02d4..7ccba9a 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "scripts": { "cleanup": "shx rm -rf umd lib lib-esm lib-fesm typings coverage docs", "prebuild": "npm run cleanup && npm run verify", - "build": " tsc && tsc --target es2017 --outDir lib-esm && webpack", + "build": " tsc && tsc --target es2017 --outDir lib-esm && webpack && webpack --env.prod", "build:fesm:min": "uglifyjs lib-fesm/index.js --compress --mangle --source-map --output lib-fesm/index.min.js", "docs": "typedoc -p . --theme minimal --target 'es6' --excludeNotExported --excludePrivate --ignoreCompilerErrors --exclude \"**/src/**/__tests__/*.*\" --out docs src/", "test": "jest", @@ -94,6 +94,7 @@ "dependencies": {}, "devDependencies": { "@types/jest": "20.0.0", + "@types/node": "7.0.31", "awesome-typescript-loader": "3.1.3", "commitizen": "2.9.6", "cross-var": "1.0.3", @@ -117,6 +118,7 @@ "uglify-js": "git://github.com/mishoo/UglifyJS2#harmony-v2.8.22", "uglifyjs-webpack-plugin": "0.4.3", "validate-commit-msg": "2.12.2", - "webpack": "3.0.0-rc.2" + "webpack": "3.0.0-rc.2", + "webpack-config-utils": "2.3.0" } } diff --git a/src/Greeter.ts b/src/Greeter.ts index eb9155a..4b3ac83 100644 --- a/src/Greeter.ts +++ b/src/Greeter.ts @@ -1,6 +1,16 @@ +import { IS_DEV } from './environment' + export class Greeter { constructor(private greeting: string) {} greet() { return `Hello, ${this.greeting}!` } + + greetMe() { + if (IS_DEV) { + console.warn('this method is deprecated, use #greet instead') + } + + return this.greet() + } } diff --git a/src/environment.ts b/src/environment.ts new file mode 100644 index 0000000..97464fa --- /dev/null +++ b/src/environment.ts @@ -0,0 +1,5 @@ +/** * @internal */ +export const IS_DEV = process.env.NODE_ENV === 'development' + +/** * @internal */ +export const IS_PROD = process.env.NODE_ENV === 'production' diff --git a/tsconfig.json b/tsconfig.json index f8ebfbe..24c2328 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,8 @@ "sourceMap": true, "outDir": "lib", "declaration": true, - "declarationDir": "typings" + "declarationDir": "typings", + "stripInternal": true }, "include": [ "./src" diff --git a/webpack.config.js b/webpack.config.js index 9266f9b..38f9852 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,7 @@ const { resolve } = require('path') const webpack = require('webpack') const UglifyJSPlugin = require('uglifyjs-webpack-plugin') +const { getIfUtils, removeEmpty } = require('webpack-config-utils') const packageJSON = require('./package.json') const packageName = normalizePackageName(packageJSON.name) @@ -11,6 +12,9 @@ const PATHS = { umd: resolve(__dirname, 'umd'), fesm: resolve(__dirname, 'lib-fesm'), } +// https://webpack.js.org/configuration/configuration-types/#exporting-a-function-to-use-env +// this is equal to 'webpack --env=dev' +const DEFAULT_ENV = 'dev' const EXTERNALS = { // lodash: { @@ -24,7 +28,7 @@ const EXTERNALS = { const RULES = { ts: { test: /\.tsx?$/, - exclude: /node_modules/, + include: /src/, use: [ { loader: 'awesome-typescript-loader', @@ -40,7 +44,7 @@ const RULES = { }, tsNext: { test: /\.tsx?$/, - exclude: /node_modules/, + include: /src/, use: [ { loader: 'awesome-typescript-loader', @@ -52,28 +56,34 @@ const RULES = { }, } -const PLUGINS = [ - // enable scope hoisting - new webpack.optimize.ModuleConcatenationPlugin(), - // Apply minification only on the second bundle by using a RegEx on the name, which must end with `.min.js` - new UglifyJSPlugin({ - sourceMap: true, - include: /\.min\.js$/, - }), - new webpack.LoaderOptionsPlugin({ - minimize: true, - }), -] - -const config = env => { +const config = (env = DEFAULT_ENV) => { + const { ifProd, ifNotProd } = getIfUtils(env) + const PLUGINS = removeEmpty([ + // enable scope hoisting + new webpack.optimize.ModuleConcatenationPlugin(), + // Apply minification only on the second bundle by using a RegEx on the name, which must end with `.min.js` + ifProd( + new UglifyJSPlugin({ + sourceMap: true, + include: /\.min\.js$/, + }) + ), + new webpack.LoaderOptionsPlugin({ + debug: false, + minimize: true, + }), + new webpack.DefinePlugin({ + 'process.env': { NODE_ENV: ifProd('"production"', '"development"') }, + }), + ]) + const UMDConfig = { // 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: { - [packageName]: [PATHS.entryPoint], - [`${packageName}.min`]: [PATHS.entryPoint], + [ifProd(`${packageName}.min`, packageName)]: [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. @@ -84,7 +94,7 @@ const config = env => { filename: '[name].js', libraryTarget: 'umd', library: LIB_NAME, - // libraryExport: UMD.libName, + // libraryExport: LIB_NAME, // will name the AMD module of the UMD build. Otherwise an anonymous define is used. umdNamedDefine: true, }, @@ -107,8 +117,7 @@ const config = env => { const FESMconfig = Object.assign({}, UMDConfig, { entry: { - index: [PATHS.entryPoint], - 'index.min': [PATHS.entryPoint], + [ifProd('index.min', 'index')]: [PATHS.entryPoint], }, output: { path: PATHS.fesm, diff --git a/yarn.lock b/yarn.lock index 793d91f..da2a298 100644 --- a/yarn.lock +++ b/yarn.lock @@ -67,6 +67,10 @@ version "7.0.27" resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.27.tgz#ba5e1a87aca2b4f5817289615ffe56472927687e" +"@types/node@7.0.31": + version "7.0.31" + resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.31.tgz#80ea4d175599b2a00149c29a10a4eb2dff592e86" + "@types/shelljs@^0.7.0": version "0.7.2" resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.7.2.tgz#c2bdb3fe80cd7a3da08750ca898ae44c589671f3" @@ -3739,6 +3743,10 @@ q@^1.4.1: version "1.5.0" resolved "https://registry.yarnpkg.com/q/-/q-1.5.0.tgz#dd01bac9d06d30e6f219aecb8253ee9ebdc308f1" +qs@^5.2.0: + version "5.2.1" + resolved "https://registry.yarnpkg.com/qs/-/qs-5.2.1.tgz#801fee030e0b9450d6385adc48a4cc55b44aedfc" + qs@~6.4.0: version "6.4.0" resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233" @@ -4681,6 +4689,18 @@ webidl-conversions@^4.0.0: version "4.0.1" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.1.tgz#8015a17ab83e7e1b311638486ace81da6ce206a0" +webpack-combine-loaders@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/webpack-combine-loaders/-/webpack-combine-loaders-2.0.0.tgz#c8cd033ec797748b569c7823f54d4481670f3e05" + dependencies: + qs "^5.2.0" + +webpack-config-utils@2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/webpack-config-utils/-/webpack-config-utils-2.3.0.tgz#49aa66984a85a7c2c9991343137e8dd11b610afa" + dependencies: + webpack-combine-loaders "2.0.0" + webpack-sources@^0.2.3: version "0.2.3" resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-0.2.3.tgz#17c62bfaf13c707f9d02c479e0dcdde8380697fb"