Angular CLI 3rd party lib installation failing

2019-09-16 23:58发布

问题:

Well, i have a strange situation. I followed all the guides for 3rd party lib installation. When i run ng serve actually i can see the library under dis/vendor/redux. But when i open the browser my app doesnt work and when i check for the resources in developer tools, the library doesnt exist. Where should i look for the problem ? i mean how it is possible that in dist folder i can see the library(in webstorm) but on the browser it doesnt appear ?

// SystemJS configuration file, see links for more information
// https://github.com/systemjs/systemjs
// https://github.com/systemjs/systemjs/blob/master/docs/config-api.md

/***********************************************************************************************
 * User Configuration.
 **********************************************************************************************/
/** Map relative paths to URLs. */
const map: any = {
  'redux': 'vendor/redux/dist'
};

/** User packages configuration. */
const packages: any = {
  'redux':{defaultExtension: 'js', main: 'redux.js'}
};

////////////////////////////////////////////////////////////////////////////////////////////////
/***********************************************************************************************
 * Everything underneath this line is managed by the CLI.
 **********************************************************************************************/
const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',
  '@angular/router-deprecated',

  // Thirdparty barrels.
  'rxjs',
  // App specific barrels.
  'app',
  'app/shared',
  /** @cli-barrel */
];

const cliSystemConfigPackages: any = {};
barrels.forEach((barrelName: string) => {
  cliSystemConfigPackages[barrelName] = { main: 'index' };
});

/** Type declaration for ambient System. */
declare var System: any;

// Apply the CLI SystemJS configuration.
System.config({
  map: {
    '@angular': 'vendor/@angular',
    'rxjs': 'vendor/rxjs',
    'main': 'main.js'
  },
  packages: cliSystemConfigPackages
});

// Apply the user's configuration.
System.config({ map, packages });

And angular-cli-build.js:

// Angular-CLI build configuration
    // This file lists all the node_modules files that will be used in a build
    // Also see https://github.com/angular/angular-cli/wiki/3rd-party-libs

    /* global require, module */

    var Angular2App = require('angular-cli/lib/broccoli/angular2-app');

    module.exports = function(defaults) {
      return new Angular2App(defaults, {
        vendorNpmFiles: [
          'systemjs/dist/system-polyfills.js',
          'systemjs/dist/system.src.js',
          'zone.js/dist/**/*.+(js|js.map)',
          'es6-shim/es6-shim.js',
          'reflect-metadata/**/*.+(ts|js|js.map)',
          'rxjs/**/*.+(js|js.map)',
          '@angular/**/*.+(js|js.map)',
          'redux/dist/**/*.js'
        ]
      });
    };

回答1:

I can suggest you a workaround until they get better support for 3rd party libs. It worked for me :)

Just make sure you linked your redux path in your src/index.html

<script src="/vendor/redux/dist/redux.js" type="text/javascript"></script>

If it doesnot work make sure your system-config.js remains like this

const packages: any = {
  'redux':{format: 'cjs',defaultExtension: 'js', main: 'redux.js'}
};

Hope you make it running :)