diff --git a/.editorconfig b/.editorconfig index 87a8c212..a443ba78 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,5 @@ root = true charset = utf-8 indent_style = space indent_size = 2 -end_of_line = lf insert_final_newline = true -trim_trailing_whitespace = true \ No newline at end of file +trim_trailing_whitespace = true diff --git a/CHANGELOG.md b/CHANGELOG.md index ef4ca1e1..ba275de1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,29 @@ # Changelog +# 04 October 2016 + +- Migrate to webpack 2 + +# 18 September 2016 + +- Using `awesome-typescript-loader` instead of `ts-loader`. + +# 5 September 2016 + +- Migration to TS2 and @types + +## 1 September 2016 + +- Using RC6. + +## 28 August 2016 + +- Adding webpack-dashboard. + +## 23 August 2016 + +- Adding HMR support. + ## 3 July 2016 - Use html5mode instead of hash navigation. diff --git a/README.md b/README.md index 214ae500..5f91819a 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,27 @@ -# angular2-webpack +# angular-webpack -[![Dependency Status](https://david-dm.org/preboot/angular2-webpack/status.svg)](https://david-dm.org/preboot/angular2-webpack#info=dependencies) [![devDependency Status](https://david-dm.org/preboot/angular2-webpack/dev-status.svg)](https://david-dm.org/preboot/angular2-webpack#info=devDependencies) +[![Dependency Status](https://david-dm.org/preboot/angular-webpack/status.svg)](https://david-dm.org/preboot/angular-webpack#info=dependencies) [![devDependency Status](https://david-dm.org/preboot/angular-webpack/dev-status.svg)](https://david-dm.org/preboot/angular-webpack#info=devDependencies) +[![Join the chat at https://gitter.im/preboot/angular-webpack](https://badges.gitter.im/preboot/angular-webpack.svg)](https://gitter.im/preboot/angular-webpack?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -**Note: This guide is following the Angular's [Style Guide](http://angular.io/styleguide) so I will be changing conventions here and there eventually. You are free to use your own conventions with this starter.** -**Note 2: There is no conventions yet for RC5 on the style guide so there will be a future update here for that.** +A complete, yet simple, starter for Angular v2+ using Webpack. -A complete, yet simple, starter for Angular 2 using Webpack. - -This seed repo serves as an Angular 2 starter for anyone looking to get up and running with Angular 2 and TypeScript fast. Using [Webpack](http://webpack.github.io/) for building our files and assisting with boilerplate. We're also using Protractor for our end-to-end story and Karma for our unit tests. -* Best practices in file and application organization for [Angular 2](https://angular.io/). +This seed repo serves as an Angular starter for anyone looking to get up and running with Angular and TypeScript fast. Using [Webpack](http://webpack.github.io/) for building our files and assisting with boilerplate. We're also using Protractor for our end-to-end story and Karma for our unit tests. +* Best practices in file and application organization for [Angular](https://angular.io/). * Ready to go build system using [Webpack](https://webpack.github.io/docs/) for working with [TypeScript](http://www.typescriptlang.org/). -* Testing Angular 2 code with [Jasmine](http://jasmine.github.io/) and [Karma](http://karma-runner.github.io/). +* Testing Angular code with [Jasmine](http://jasmine.github.io/) and [Karma](http://karma-runner.github.io/). * Coverage with [Istanbul](https://github.com/gotwarlost/istanbul) -* End-to-end Angular 2 code using [Protractor](https://angular.github.io/protractor/). +* End-to-end Angular code using [Protractor](https://angular.github.io/protractor/). * Stylesheets with [SASS](http://sass-lang.com/) (not required, it supports regular css too). * Error reported with [TSLint](http://palantir.github.io/tslint/) and [Codelyzer](https://github.com/mgechev/codelyzer). -* Documentation with [TypeDoc](http://typedoc.io/). +* Documentation with [TypeDoc](http://typedoc.org/). >Warning: Make sure you're using the latest version of Node.js and NPM -[Is Angular 2 Ready Yet?](http://splintercode.github.io/is-angular-2-ready/) - ### Quick start -> Clone/Download the repo then edit `app.ts` inside [`/src/app/app.component.ts`](/src/app/app.component.ts) - ```bash # clone our repo -$ git clone https://github.com/preboot/angular2-webpack.git my-app +$ git clone https://github.com/preboot/angular-webpack.git my-app # change directory to your app $ cd my-app @@ -45,9 +39,9 @@ go to [http://localhost:8080](http://localhost:8080) in your browser. * [Getting Started](#getting-started) * [Dependencies](#dependencies) * [Installing](#installing) - * [Running the app](#running-the-app) * [Developing](#developing) * [Testing](#testing) + * [Production](#production) * [Documentation](#documentation) * [Frequently asked questions](#faq) * [TypeScript](#typescript) @@ -59,7 +53,7 @@ go to [http://localhost:8080](http://localhost:8080) in your browser. What you need to run this app: * `node` and `npm` (Use [NVM](https://github.com/creationix/nvm)) -* Ensure you're running Node (`v5.x.x`+) and NPM (`3.x.x`+) +* Ensure you're running Node (`v6.x.x`+) and NPM (`3.x.x`+) ## Installing @@ -67,22 +61,19 @@ What you need to run this app: * `clone` your fork * `npm install` to install all dependencies -## Running the app +## Developing -After you have installed all dependencies you can now run the app with: +After you have installed all dependencies you can now start developing with: -```bash -npm start -``` +* `npm start` -It will start a local server using `webpack-dev-server` which will watch, build (in-memory), and reload for you. The port will be displayed to you as `http://localhost:8080`. +It will start a local server using `webpack-dev-server` which will watch, build (in-memory), and reload for you. The application can be checked at `http://localhost:8080`. -## Developing +As an alternative, you can work using Hot Module Replacement (HMR): -### Build files +* `npm run start:hmr` -* single run: `npm run build` -* build files and watch: `npm run watch` +And you are all set! You can now modify your components on the fly without having to reload the entire page. ## Testing @@ -102,12 +93,19 @@ It will start a local server using `webpack-dev-server` which will watch, build * when debugging or first writing test suites, you may find it helpful to try out Protractor commands without starting up the entire test suite. You can do this with the element explorer. * you can learn more about [Protractor Interactive Mode here](https://github.com/angular/protractor/blob/master/docs/debugging.md#testing-out-protractor-interactively) +## Production + +To build your application, run: + +* `npm run build` + +You can now go to `/dist` and deploy that to your server! + ## Documentation -You can generate api docs (using [TypeDoc](http://typedoc.io/)) for your code with the following: -```bash -npm run docs -``` +You can generate api docs (using [TypeDoc](http://typedoc.org/)) for your code with the following: + +* `npm run docs` # FAQ @@ -115,19 +113,19 @@ npm run docs No, Webpack will add all the needed Javascript bundles as script tags and all the CSS files as link tags. The advantage is that you don't need to modify the index.html every time you build your solution to update the hashes. -#### How to include external angular 2 libraries ? +#### How to include external angular libraries ? -It's simple, just install the lib via npm and import it in your code when you need it. Don't forget that you need to configure some external libs in the [bootstrap](https://github.com/preboot/angular2-webpack/blob/master/src/main.ts) of your application. +It's simple, just install the lib via npm and import it in your code when you need it. Don't forget that you need to configure some external libs in the [bootstrap](https://github.com/preboot/angular-webpack/blob/master/src/main.ts) of your application. -### How to include external css files such as bootstrap.css ? +#### How to include external css files such as bootstrap.css ? -Just install the lib and import the css files in [vendor.ts](https://github.com/preboot/angular2-webpack/blob/master/src/vendor.ts). For example this is how to do it with bootstrap: +Just install the lib and import the css files in [vendor.ts](https://github.com/preboot/angular-webpack/blob/master/src/vendor.ts). For example this is how to do it with bootstrap: ```sh -npm install bootstrap@4.0.0 --save +npm install bootstrap@next --save ``` -And in [vendor.ts](https://github.com/preboot/angular2-webpack/blob/master/src/vendor.ts) add the following: +And in [vendor.ts](https://github.com/preboot/angular-webpack/blob/master/src/vendor.ts) add the following: ```ts import 'bootstrap/dist/css/bootstrap.css'; diff --git a/karma-shim.js b/karma-shim.js index 6ec3c8c5..def06d7c 100644 --- a/karma-shim.js +++ b/karma-shim.js @@ -1,15 +1,17 @@ Error.stackTraceLimit = Infinity; -require('es6-shim'); +require('core-js/client/shim'); require('reflect-metadata'); require('ts-helpers'); require('zone.js/dist/zone'); +require('zone.js/dist/long-stack-trace-zone'); +require('zone.js/dist/proxy'); +require('zone.js/dist/sync-test'); +require('zone.js/dist/jasmine-patch'); require('zone.js/dist/async-test'); require('zone.js/dist/fake-async-test'); -require('zone.js/dist/sync-test'); -require('zone.js/dist/proxy-zone'); /* Ok, this is kinda crazy. We can use the the context method on diff --git a/karma.conf.js b/karma.conf.js index 5b654613..91b79ad0 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,6 +2,9 @@ var path = require('path'); var webpackConfig = require('./webpack.config'); +var ENV = process.env.npm_lifecycle_event; +var isTestWatch = ENV === 'test-watch'; + module.exports = function (config) { var _config = { @@ -34,27 +37,6 @@ module.exports = function (config) { stats: 'errors-only' }, - coverageReporter: { - dir: 'coverage/', - reporters: [{ - type: 'json', - dir: 'coverage', - subdir: 'json', - file: 'coverage-final.json' - }] - }, - - remapIstanbulReporter: { - src: 'coverage/json/coverage-final.json', - reports: { - lcovonly: 'coverage/json/lcov.info', - html: 'coverage/html', - 'text': null - }, - timeoutNotCreated: 1000, // default value - timeoutNoMoreFiles: 1000 // default value - }, - webpackServer: { noInfo: true // please don't spam the console when running in karma! }, @@ -62,7 +44,7 @@ module.exports = function (config) { // test results reporter to use // possible values: 'dots', 'progress', 'mocha' // available reporters: https://npmjs.org/browse/keyword/karma-reporter - reporters: ["mocha", "coverage", "karma-remap-istanbul"], + reporters: ["mocha"], // web server port port: 9876, @@ -79,13 +61,27 @@ module.exports = function (config) { // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher - browsers: ['PhantomJS'], // you can also use Chrome + browsers: ['Chrome'], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: true }; + if (!isTestWatch) { + _config.reporters.push("coverage"); + + _config.coverageReporter = { + dir: 'coverage/', + reporters: [{ + type: 'json', + dir: 'coverage', + subdir: 'json', + file: 'coverage-final.json' + }] + }; + } + config.set(_config); }; diff --git a/package.json b/package.json index 02e56ef3..27ea2952 100644 --- a/package.json +++ b/package.json @@ -16,69 +16,74 @@ "e2e-live": "protractor --elementExplorer", "pretest": "npm run lint", "test": "karma start", + "posttest": "remap-istanbul -i coverage/json/coverage-final.json -o coverage/html -t html", "test-watch": "karma start --no-single-run --auto-watch", "ci": "npm run e2e && npm run test", "docs": "typedoc --options typedoc.json src/app/app.component.ts", "start": "npm run server", - "postinstall": "npm run webdriver-update && typings install" + "start:hmr": "npm run server -- --hot", + "postinstall": "npm run webdriver-update" }, "dependencies": { - "@angular/common": "2.0.0-rc.5", - "@angular/compiler": "2.0.0-rc.5", - "@angular/core": "2.0.0-rc.5", - "@angular/forms": "0.3.0", - "@angular/http": "2.0.0-rc.5", - "@angular/platform-browser": "2.0.0-rc.5", - "@angular/platform-browser-dynamic": "2.0.0-rc.5", - "@angular/router": "3.0.0-rc.1", - "es6-shim": "^0.35.0", - "reflect-metadata": "0.1.3", - "rxjs": "5.0.0-beta.6", - "zone.js": "^0.6.12" + "@angular/common": "~4.2.6", + "@angular/compiler": "~4.2.6", + "@angular/core": "~4.2.6", + "@angular/forms": "~4.2.6", + "@angular/http": "~4.2.6", + "@angular/platform-browser": "~4.2.6", + "@angular/platform-browser-dynamic": "~4.2.6", + "@angular/router": "~4.2.6", + "core-js": "^2.4.1", + "reflect-metadata": "^0.1.3", + "tslib": "^1.7.1", + "rxjs": "^5.0.1", + "zone.js": "^0.8.10" }, "devDependencies": { - "angular2-template-loader": "^0.4.0", + "@angularclass/hmr": "^1.0.1", + "@angularclass/hmr-loader": "^3.0.2", + "@types/jasmine": "^2.5.41", + "@types/node": "^6.0.38", + "@types/selenium-webdriver": "2.53.39", + "angular2-template-loader": "^0.6.0", "autoprefixer": "^6.3.2", - "codelyzer": "0.0.26", - "copy-webpack-plugin": "^3.0.0", - "css-loader": "^0.23.0", - "extract-text-webpack-plugin": "^1.0.1", - "file-loader": "^0.9.0", + "awesome-typescript-loader": "^3.1.2", + "codelyzer": "2.0.0", + "copy-webpack-plugin": "^4.0.0", + "css-loader": "^0.28.4", + "extract-text-webpack-plugin": "^2.0.0-beta.4", + "file-loader": "^0.10.0", "html-loader": "^0.4.0", "html-webpack-plugin": "^2.8.1", "istanbul-instrumenter-loader": "^0.2.0", "jasmine-core": "^2.3.4", - "jasmine-spec-reporter": "^2.4.0", + "jasmine-spec-reporter": "^3.2.0", "json-loader": "^0.5.3", - "karma": "1.1.2", - "karma-chrome-launcher": "^1.0.1", + "karma": "1.4.1", + "karma-chrome-launcher": "^2.0.0", "karma-coverage": "^1.0.0", "karma-jasmine": "^1.0.2", "karma-mocha-reporter": "^2.0.3", - "karma-phantomjs-launcher": "^1.0.0", - "karma-remap-istanbul": "0.1.1", + "karma-remap-istanbul": "0.2.1", "karma-sourcemap-loader": "^0.3.7", - "karma-webpack": "1.7.0", - "node-sass": "^3.4.2", + "karma-webpack": "2.0.2", + "node-sass": "^4.5.0", "null-loader": "0.1.1", - "phantomjs-prebuilt": "^2.1.4", - "postcss-loader": "^0.9.1", - "protractor": "^3.1.1", + "postcss-loader": "^1.1.0", + "protractor": "^4.0.10", "raw-loader": "0.5.1", "remap-istanbul": "^0.6.4", "rimraf": "^2.5.1", - "sass-loader": "^4.0.0", + "sass-loader": "^6.0.1", "shelljs": "^0.7.0", "style-loader": "^0.13.0", "ts-helpers": "^1.1.1", - "ts-loader": "^0.8.1", - "tslint": "^3.4.0", - "tslint-loader": "^2.1.0", - "typedoc": "^0.4.4", - "typescript": "^1.8.0", - "typings": "^1.0.4", + "tslint": "^4.3.1", + "tslint-loader": "^3.3.0", + "typedoc": "^0.5.1", + "typescript": "^2.3.1", "url-loader": "^0.5.6", - "webpack": "^1.12.13", - "webpack-dev-server": "^1.14.1" + "webpack": "2.2.1", + "webpack-dev-server": "2.3.0" } } diff --git a/protractor.conf.js b/protractor.conf.js index 12b68e5d..f2253b39 100644 --- a/protractor.conf.js +++ b/protractor.conf.js @@ -24,7 +24,7 @@ exports.config = { }, onPrepare: function () { - var SpecReporter = require('jasmine-spec-reporter'); + var SpecReporter = require('jasmine-spec-reporter').SpecReporter; // add jasmine spec reporter jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: true})); diff --git a/src/app/app.component.html b/src/app/app.component.html index 208e6687..b3b9f295 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -5,7 +5,7 @@
-

Hello from {{api.title}}!

+

Hello from {{title}}!

diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 4fc43212..01950b8a 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -11,8 +11,9 @@ import '../style/app.scss'; }) export class AppComponent { url = 'https://github.com/preboot/angular2-webpack'; + title: string; constructor(private api: ApiService) { - // Do something with api + this.title = this.api.title; } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 2ed84ed4..cea4ed87 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,4 +1,4 @@ -import { NgModule } from '@angular/core'; +import { NgModule, ApplicationRef } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HttpModule } from '@angular/http'; import { FormsModule } from '@angular/forms'; @@ -9,6 +9,8 @@ import { AboutComponent } from './about/about.component'; import { ApiService } from './shared'; import { routing } from './app.routing'; +import { removeNgStyles, createNewHosts } from '@angularclass/hmr'; + @NgModule({ imports: [ BrowserModule, @@ -26,4 +28,21 @@ import { routing } from './app.routing'; ], bootstrap: [AppComponent] }) -export class AppModule {} +export class AppModule { + constructor(public appRef: ApplicationRef) {} + hmrOnInit(store) { + console.log('HMR store', store); + } + hmrOnDestroy(store) { + let cmpLocation = this.appRef.components.map(cmp => cmp.location.nativeElement); + // recreate elements + store.disposeOldHosts = createNewHosts(cmpLocation); + // remove styles + removeNgStyles(); + } + hmrAfterDestroy(store) { + // display new elements + store.disposeOldHosts(); + delete store.disposeOldHosts; + } +} diff --git a/src/main.ts b/src/main.ts index 5d247cbf..affe5e70 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,5 @@ import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; - import { AppModule } from './app/app.module'; // depending on the env mode, enable prod mode or add debugging modules @@ -8,4 +7,12 @@ if (process.env.ENV === 'build') { enableProdMode(); } -platformBrowserDynamic().bootstrapModule(AppModule); +export function main() { + return platformBrowserDynamic().bootstrapModule(AppModule); +} + +if (document.readyState === 'complete') { + main(); +} else { + document.addEventListener('DOMContentLoaded', main); +} diff --git a/src/polyfills.ts b/src/polyfills.ts index 02c74ac8..9e174ed5 100644 --- a/src/polyfills.ts +++ b/src/polyfills.ts @@ -1,4 +1,4 @@ -import 'es6-shim'; +import 'core-js/client/shim'; import 'reflect-metadata'; require('zone.js/dist/zone'); diff --git a/src/vendor.ts b/src/vendor.ts index 145acab1..8871b0e3 100644 --- a/src/vendor.ts +++ b/src/vendor.ts @@ -6,8 +6,8 @@ import '@angular/common'; import '@angular/http'; import '@angular/router'; -// RxJS import 'rxjs'; +import '@angularclass/hmr'; // Other vendors for example jQuery, Lodash or Bootstrap // You can import js, ts, css, sass, ... diff --git a/tsconfig.json b/tsconfig.json index 816f4934..0d06b7cf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,8 +5,15 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "sourceMap": true, - "noEmitHelpers": true + "noEmitHelpers": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "lib": ["es2015", "dom"] }, "compileOnSave": false, - "buildOnSave": false + "buildOnSave": false, + "awesomeTypescriptLoaderOptions": { + "forkChecker": true, + "useWebpackText": true + } } diff --git a/tslint.json b/tslint.json index 41c6c0e1..2c31441e 100644 --- a/tslint.json +++ b/tslint.json @@ -16,7 +16,6 @@ "spaces" ], "label-position": true, - "label-undefined": true, "max-line-length": [ true, 140 @@ -39,7 +38,6 @@ ], "no-construct": true, "no-debugger": true, - "no-duplicate-key": true, "no-duplicate-variable": true, "no-empty": false, "no-eval": true, @@ -49,8 +47,6 @@ "no-switch-case-fall-through": true, "no-trailing-whitespace": true, "no-unused-expression": true, - "no-unused-variable": true, - "no-unreachable": true, "no-use-before-declare": true, "no-var-keyword": true, "object-literal-sort-keys": false, @@ -92,11 +88,18 @@ "check-separator", "check-type" ], - - "directive-selector-name": [true, "camelCase"], - "component-selector-name": [true, "kebab-case"], - "directive-selector-type": [true, "attribute"], - "component-selector-type": [true, "element"], + "directive-selector": [ + true, + "attribute", + "my", + "camelCase" + ], + "component-selector": [ + true, + "element", + "my", + "kebab-case" + ], "use-input-property-decorator": true, "use-output-property-decorator": true, "use-host-property-decorator": true, @@ -106,8 +109,16 @@ "use-pipe-transform-interface": true, "component-class-suffix": true, "directive-class-suffix": true, - "directive-selector-prefix": [true, "my"], - "component-selector-prefix": [true, "my"], - "pipe-naming": [true, "camelCase", "my"] + "pipe-naming": [ + true, + "camelCase", + "my" + ], + "no-attribute-parameter-decorator": true, + "no-forward-ref": true, + "import-destructuring-spacing": true, + "no-access-missing-member": true, + "templates-use-public": true, + "invoke-injectable": true } } diff --git a/typings.json b/typings.json deleted file mode 100644 index a12608f1..00000000 --- a/typings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "globalDevDependencies": { - "jasmine": "registry:dt/jasmine#2.2.0+20160412134438" - }, - "globalDependencies": { - "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654", - "node": "registry:dt/node#4.0.0+20160501135006" - } -} diff --git a/webpack.config.js b/webpack.config.js index 16be247b..93e0ced9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,4 +1,4 @@ -// Helper: root(), and rootDir() are defined at the bottom +// Helper: root() is defined at the bottom var path = require('path'); var webpack = require('webpack'); @@ -14,7 +14,8 @@ var CopyWebpackPlugin = require('copy-webpack-plugin'); * Get npm lifecycle event to identify the environment */ var ENV = process.env.npm_lifecycle_event; -var isTest = ENV === 'test' || ENV === 'test-watch'; +var isTestWatch = ENV === 'test-watch'; +var isTest = ENV === 'test' || isTestWatch; var isProd = ENV === 'build'; module.exports = function makeWebpackConfig() { @@ -32,22 +33,25 @@ module.exports = function makeWebpackConfig() { */ if (isProd) { config.devtool = 'source-map'; - } else { + } + else if (isTest) { + config.devtool = 'inline-source-map'; + } + else { config.devtool = 'eval-source-map'; } - // add debug messages - config.debug = !isProd || !isTest; - - /** - * Entry - * Reference: http://webpack.github.io/docs/configuration.html#entry - */ - config.entry = isTest ? {} : { - 'polyfills': './src/polyfills.ts', - 'vendor': './src/vendor.ts', - 'app': './src/main.ts' // our angular app - }; + if (!isTest) { + /** + * Entry + * Reference: http://webpack.github.io/docs/configuration.html#entry + */ + config.entry = isTest ? {} : { + 'polyfills': './src/polyfills.ts', + 'vendor': './src/vendor.ts', + 'app': './src/main.ts' // our angular app + }; + } /** * Output @@ -65,16 +69,16 @@ module.exports = function makeWebpackConfig() { * Reference: http://webpack.github.io/docs/configuration.html#resolve */ config.resolve = { - cache: !isTest, - root: root(), // only discover files that have those extensions - extensions: ['', '.ts', '.js', '.json', '.css', '.scss', '.html'], - alias: { - 'app': 'src/app', - 'common': 'src/common' - } + extensions: ['.ts', '.js', '.json', '.css', '.scss', '.html'], }; + var atlOptions = ''; + if (isTest && !isTestWatch) { + // awesome-typescript-loader needs to output inlineSourceMap for code coverage to work with source maps. + atlOptions = 'inlineSourceMap=true&sourceMap=false'; + } + /** * Loaders * Reference: http://webpack.github.io/docs/configuration.html#module-loaders @@ -82,23 +86,22 @@ module.exports = function makeWebpackConfig() { * This handles most of the magic responsible for converting modules */ config.module = { - preLoaders: isTest ? [] : [{test: /\.ts$/, loader: 'tslint'}], - loaders: [ + rules: [ // Support for .ts files. { test: /\.ts$/, - loaders: ['ts', 'angular2-template-loader'], + loaders: ['awesome-typescript-loader?' + atlOptions, 'angular2-template-loader', '@angularclass/hmr-loader'], exclude: [isTest ? /\.(e2e)\.ts$/ : /\.(spec|e2e)\.ts$/, /node_modules\/(?!(ng2-.+))/] }, // copy those assets to output { test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?v=[0-9]\.[0-9]\.[0-9])?$/, - loader: 'file?name=fonts/[name].[hash].[ext]?' + loader: 'file-loader?name=fonts/[name].[hash].[ext]?' }, // Support for *.json files. - {test: /\.json$/, loader: 'json'}, + {test: /\.json$/, loader: 'json-loader'}, // Support for CSS as raw text // use 'null' loader in test mode (https://github.com/webpack/null-loader) @@ -106,46 +109,46 @@ module.exports = function makeWebpackConfig() { { test: /\.css$/, exclude: root('src', 'app'), - loader: isTest ? 'null' : ExtractTextPlugin.extract('style', 'css?sourceMap!postcss') + loader: isTest ? 'null-loader' : ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'postcss-loader']}) }, // all css required in src/app files will be merged in js files - {test: /\.css$/, include: root('src', 'app'), loader: 'raw!postcss'}, + {test: /\.css$/, include: root('src', 'app'), loader: 'raw-loader!postcss-loader'}, // support for .scss files // use 'null' loader in test mode (https://github.com/webpack/null-loader) // all css in src/style will be bundled in an external css file { - test: /\.scss$/, + test: /\.(scss|sass)$/, exclude: root('src', 'app'), - loader: isTest ? 'null' : ExtractTextPlugin.extract('style', 'css?sourceMap!postcss!sass') + loader: isTest ? 'null-loader' : ExtractTextPlugin.extract({ fallback: 'style-loader', use: ['css-loader', 'postcss-loader', 'sass-loader']}) }, // all css required in src/app files will be merged in js files - {test: /\.scss$/, exclude: root('src', 'style'), loader: 'raw!postcss!sass'}, + {test: /\.(scss|sass)$/, exclude: root('src', 'style'), loader: 'raw-loader!postcss-loader!sass-loader'}, // support for .html as raw text // todo: change the loader to something that adds a hash to images - {test: /\.html$/, loader: 'raw'} - ], - postLoaders: [] + {test: /\.html$/, loader: 'raw-loader', exclude: root('src', 'public')} + ] }; - if (isTest) { + if (isTest && !isTestWatch) { // instrument only testing sources with Istanbul, covers ts files - config.module.postLoaders.push({ + config.module.rules.push({ test: /\.ts$/, + enforce: 'post', include: path.resolve('src'), loader: 'istanbul-instrumenter-loader', exclude: [/\.spec\.ts$/, /\.e2e\.ts$/, /node_modules/] }); + } - // needed for remap-instanbul - config.ts = { - compilerOptions: { - sourceMap: false, - sourceRoot: './src', - inlineSourceMap: true - } - }; + if (!isTest || !isTestWatch) { + // tslint support + config.module.rules.push({ + test: /\.ts$/, + enforce: 'pre', + loader: 'tslint-loader' + }); } /** @@ -161,10 +164,49 @@ module.exports = function makeWebpackConfig() { 'process.env': { ENV: JSON.stringify(ENV) } + }), + + // Workaround needed for angular 2 angular/angular#11580 + new webpack.ContextReplacementPlugin( + // The (\\|\/) piece accounts for path separators in *nix and Windows + /angular(\\|\/)core(\\|\/)@angular/, + root('./src') // location of your src + ), + + // Tslint configuration for webpack 2 + new webpack.LoaderOptionsPlugin({ + options: { + /** + * Apply the tslint loader as pre/postLoader + * Reference: https://github.com/wbuchwalter/tslint-loader + */ + tslint: { + emitErrors: false, + failOnHint: false + }, + /** + * Sass + * Reference: https://github.com/jtangelder/sass-loader + * Transforms .scss files to .css + */ + sassLoader: { + //includePaths: [path.resolve(__dirname, "node_modules/foundation-sites/scss")] + }, + /** + * PostCSS + * Reference: https://github.com/postcss/autoprefixer-core + * Add vendor prefixes to your css + */ + postcss: [ + autoprefixer({ + browsers: ['last 2 version'] + }) + ] + } }) ]; - if (!isTest) { + if (!isTest && !isTestWatch) { config.plugins.push( // Generate common chunks if necessary // Reference: https://webpack.github.io/docs/code-splitting.html @@ -183,7 +225,7 @@ module.exports = function makeWebpackConfig() { // Extract css files // Reference: https://github.com/webpack/extract-text-webpack-plugin // Disabled when in test mode or not in build mode - new ExtractTextPlugin('css/[name].[hash].css', {disable: !isProd}) + new ExtractTextPlugin({filename: 'css/[name].[hash].css', disable: !isProd}) ); } @@ -192,15 +234,15 @@ module.exports = function makeWebpackConfig() { config.plugins.push( // Reference: http://webpack.github.io/docs/list-of-plugins.html#noerrorsplugin // Only emit files when there are no errors - new webpack.NoErrorsPlugin(), + new webpack.NoEmitOnErrorsPlugin(), - // Reference: http://webpack.github.io/docs/list-of-plugins.html#dedupeplugin - // Dedupe modules in the output - new webpack.optimize.DedupePlugin(), + // // Reference: http://webpack.github.io/docs/list-of-plugins.html#dedupeplugin + // // Dedupe modules in the output + // new webpack.optimize.DedupePlugin(), // Reference: http://webpack.github.io/docs/list-of-plugins.html#uglifyjsplugin // Minify all javascript, switch loaders to minimizing mode - new webpack.optimize.UglifyJsPlugin({mangle: { keep_fnames: true }}), + new webpack.optimize.UglifyJsPlugin({sourceMap: true, mangle: { keep_fnames: true }}), // Copy assets from the public folder // Reference: https://github.com/kevlened/copy-webpack-plugin @@ -210,35 +252,6 @@ module.exports = function makeWebpackConfig() { ); } - /** - * PostCSS - * Reference: https://github.com/postcss/autoprefixer-core - * Add vendor prefixes to your css - */ - config.postcss = [ - autoprefixer({ - browsers: ['last 2 version'] - }) - ]; - - /** - * Sass - * Reference: https://github.com/jtangelder/sass-loader - * Transforms .scss files to .css - */ - config.sassLoader = { - //includePaths: [path.resolve(__dirname, "node_modules/foundation-sites/scss")] - }; - - /** - * Apply the tslint loader as pre/postLoader - * Reference: https://github.com/wbuchwalter/tslint-loader - */ - config.tslint = { - emitErrors: false, - failOnHint: false - }; - /** * Dev server configuration * Reference: http://webpack.github.io/docs/configuration.html#devserver @@ -247,6 +260,7 @@ module.exports = function makeWebpackConfig() { config.devServer = { contentBase: './src/public', historyApiFallback: true, + quiet: true, stats: 'minimal' // none (or false), errors-only, minimal, normal (or true) and verbose };