Loaders

TypeScript isn't core JavaScript so webpack needs a bit of extra help to parse the .ts files. It does this through the use of loaders. Loaders are a way of configuring how webpack transforms the outputs of specific files in our bundles. Our ts-loader package is handling this transformation for TypeScript files.

Inline

Loaders can be configured – inline – when requiring/importing a module:

  1. const app = require('ts!./src/index.ts');

The loader is specified by using the ! character to separate the module reference and the loader that it will be run through. More than one loader can be used and those are separated with ! in the same way. Loaders are executed right to left.

  1. const app = require('ts!tslint!./src/index.ts');
Although the packages are named ts-loader, tslint-loader, style-loader, we don't need to include the -loader part in our config.

Be careful when configuring loaders this way – it couples implementation details of different stages of your application together so it might not be the right choice in a lot of cases.

Webpack Config

The preferred method is to configure loaders through the webpack.config.js file. For example, the TypeScript loader task will look something like this:

  1. {
  2. test: /\.ts$/,
  3. loader: 'ts-loader',
  4. exclude: /node_modules/
  5. }

This runs the typescript compiler which respects our configuration settings as specified above. We want to be able to handle other files and not just TypeScript files, so we need to specify a list of loaders. This is done by creating an array of tasks.

Tasks specified in this array are chained. If a file matches multiple conditions, it will be processed using each task in order.

  1. {
  2. ...
  3. module: {
  4. rules: [
  5. { test: /\.ts$/, loader: 'tslint' },
  6. { test: /\.ts$/, loader: 'ts', exclude: /node_modules/ },
  7. { test: /\.html$/, loader: 'raw' },
  8. { test: /\.css$/, loader: 'style!css?sourceMap' },
  9. { test: /\.svg/, loader: 'url' },
  10. { test: /\.eot/, loader: 'url' },
  11. { test: /\.woff/, loader: 'url' },
  12. { test: /\.woff2/, loader: 'url' },
  13. { test: /\.ttf/, loader: 'url' },
  14. ],
  15. noParse: [ /zone\.js\/dist\/.+/, /angular2\/bundles\/.+/ ]
  16. }
  17. ...
  18. }

Each task has a few configuration options:

  • test - The file path must match this condition to be handled. This is commonly used to test file extensions eg. /.ts$/.

  • loader - The loaders that will be used to transform the input. This follows the syntax specified above.

  • exclude - The file path must not match this condition to be handled. This is commonly used to exclude file folders, e.g. /node_modules/.

  • include - The file path must match this condition to be handled. This is commonly used to include file folders. eg. path.resolve(__dirname, 'app/src').

Pre-Loaders

The preLoaders array works just like the loaders array only it is a separate task chain that is executed before the loaders task chain.

Non JavaScript Assets

Webpack also allows us to load non JavaScript assets such as: CSS, SVG, font files, etc. In order to attach these assets to our bundle we must require/import them within our app modules. For example:

  1. import './styles/style.css';
  2. // or
  3. const STYLES = require('./styles/style.css');

Other Commonly Used Loaders

  • raw-loader - returns the file content as a string.

  • url-loader - returns a base64 encoded data URL if the file size is under a certain threshold, otherwise it just returns the file.

  • css-loader - resolves @import and url references in CSS files as modules.

  • style-loader - injects a style tag with the bundled CSS in the tag.

原文: https://angular-2-training-book.rangle.io/handout/project-setup/loaders.html