@ ngtools / webpack AngularCompilerPlugin Angular 5 / Ionic 3库项目的配置

时间:2017-12-11 05:28:34

标签: javascript angular typescript webpack

https://github.com/angular/angular-cli/tree/master/packages/%40ngtools/webpack

当我的项目是一个库项目时,('@ngtools/webpack').AngularCompilerPlugin的正确配置是什么,因此没有一个主文件来引导它?

我在下面尝试了这个,得到了底部的输出:只编译了文件,标记为webpack入口模块(而不是AngularCompilerPlugin的入口模块)。

我需要知道如何配置AngularCompilerPlugin以便:

  • 生成UMD AOT构建
  • 生成UMD JIT构建
  • 结果文件可由SystemJS在Ionic 3项目中使用,该项目在构建时不知道MyLibrary

这是我到目前为止所做的:

配置+打字稿来源

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "outDir": "./dist/ngc"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "src/**/*.spec.ts",
    "src/**/__tests__/*.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  },
  "angularCompilerOptions": {
    "debug": true
  }
}

webpack.config.js

const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const path = require('path');
const webpack = require("webpack");
const angularExternals = require('webpack-angular-externals');
const rxjsExternals = require('webpack-rxjs-externals');
const camelCase = require('lodash.camelcase');

module.exports = {
    entry: "./src/auth.module.ts", // bundle's entry point
    output: {
        filename: "my-library.js", // name of the generated bundle
        libraryTarget: "umd",
        library: "MyLibrary",
        path: path.resolve(__dirname, 'dist'), // output directory
        umdNamedDefine: true,
    },
    resolve: {
        extensions: ['.js', '.ts']
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                enforce: "pre",
                loader: 'tslint-loader'
            },
            {
                test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
                loader: '@ngtools/webpack'
            },
        ]
    },
    plugins: [
        new AngularCompilerPlugin({
            tsConfigPath: 'tsconfig.json',
            entryModule: "./src/auth.module.ts#AuthModule",
        })
    ],
    externals: [
        angularExternals(),
        rxjsExternals(),
        (context, request, callback) => {
            if (/^ionic-angular/.test(request)
            ) {
                return callback(null, {
                    root: ['ngIonicAngular', camelCase(request.replace(/^ionic-angular\//, ''))],
                    commonjs: request,
                    commonjs2: request,
                    amd: request
                });
            }
            callback();
        },
    ]
};

的src / auth.module.ts

import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { AuthPage } from './pages/auth/auth';

@NgModule({
  declarations: [AuthPage],
  imports: [IonicPageModule.forChild(AuthPage)],
  exports: [AuthPage]
})
export class AuthModule { }

的src /页/ AUTH / auth.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { IonicPage } from 'ionic-angular/navigation/ionic-page';

@IonicPage({
  name: "Auth",
  priority: "high",
})
@Component({
  selector: 'page-auth',
  template: `
    <ion-header>
      <ion-navbar>
        <ion-title>
          Auth Page
        </ion-title>
      </ion-navbar>
    </ion-header>

    <ion-content padding>
      Auth Page Heading
      <p>
        This is the dynamically lazy loaded auth page.
        You can modify it with a single database update and there is no need to recompile the host
        application because the module loading happens via SystemJS.
      </p>
    </ion-content>
  `
})
export class AuthPage {

  constructor(public navCtrl: NavController) {
  }

}

输出

DIST /我的-library.js

(function webpackUniversalModuleDefinition(root, factory) {
    if(typeof exports === 'object' && typeof module === 'object')
        module.exports = factory();
    else if(typeof define === 'function' && define.amd)
        define("MyLibrary", [], factory);
    else if(typeof exports === 'object')
        exports["MyLibrary"] = factory();
    else
        root["MyLibrary"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "AuthModule", function() { return AuthModule; });
var AuthModule = (function () {
    function AuthModule() {
    }
    return AuthModule;
}());



/***/ })
/******/ ]);

0 个答案:

没有答案