2

I'm trying to use angular2 in a Chrome extension and it isn't recognizing that I've loaded the angular2 bundle. Here's my script tags in the file /www/popup.html:

<script src="lib/js/jquery.min.js"></script>
<script src="lib/js/es6-shim.min.js"></script>
<script src="lib/js/system-polyfills.js"></script>
<script src="lib/js/angular2-polyfills.js"></script>
<script src="lib/js/system.src.js"></script>

<!-- bundles should be used -->
<script src="lib/js/Rx.js"></script>
<script src="lib/js/angular2.dev.js"></script>
<script src="lib/js/router.dev.js"></script>

<script src="app/pages/popup-page.js"></script>

In /www/app/pages/popup-page.js it will work if I setup the paths to the angular2 and rxjs source:

System.config({
  defaultJSExtensions: true, // Will prepend .js to the module names

  paths: {
    'angular2/*': '/node_modules/angular2/*', // finds
    'rxjs/*': '/node_modules/rxjs/*',         // finds
    '*': '/www/app/*'
  },
  packages: {
    "app": {
      format: 'register',
      defaultExtension: 'js',
    }
  }
});

But I don't want to have it load each of those javascript files individually, or even include them in my extension. It looks like those bundles should register themselves when the scripts load.

  1. If I don't include the path '*' to where my code is, I get a 'require is not defined' trying to run the scripts.

  2. If I don't include the paths to angular2 and rxjs, I get the browser trying to load '/www/app/angular2/...' for each import instead of using the bundles

3 Answers 3

2

If you load a bundle that has modules registered as system modules via <script> tag, you don't need anything else in System.config - it will resolve imports by registered names from memory.

By including <script src="lib/js/angular2.dev.js"></script> in your html file import statements like import {Component} from 'angular2/core' will work without any additional config.

Removing path options from System.config should resolve issue with angular imports. ('*': '/www/app/*' - this line basically maps everything to /www/app folder...).

To setup the paths for your own scripts use map option in System.config.package["app"], more info here.

Sign up to request clarification or add additional context in comments.

2 Comments

popup-page is not a module though I've written it in typescript. It doesn't have any imports, it just calls System.config and then calls System.import('./app/pages/popup-boot'). The examples I've seen have this code as an inline script in the html, but chrome extensions prohibit inline scripts so I made it a separate script and include it with a script tag. popup-boot has the first statement import {provide} from 'angular2/core';, and I'm getting a 404 error with that trying to find `/www/app/angular2/core.js', so it is not recognizing the bundles for some reason.
I misunderstood how you use popup-page.js.. Try removing path config (this should resolve angular imports issue). Then try adding map for app package. something like map: { app: "/www/app" }.. to map imports for your app files to the folder where they are.
1

The problem I think was defaultJSExtensions. If I just change my System.config to this it works:

System.config({
  packages: {
    "app": {
      defaultExtension: 'js',
    }
  }
});

I just don't know of any place with documentation on what all these options are. I've looked at these pages (and many more I didn't save links for) and not been able to find a reference or good explanation of what all the options are and do:

Comments

0

You only need to map "app" and not "*", as described below:

System.config({
  defaultJSExtensions: true, // Will prepend .js to the module names

  paths: {
    'angular2/*': '/node_modules/angular2/*', // finds
    'rxjs/*': '/node_modules/rxjs/*',         // finds
    'app': '/www/app' // <------
  },
  packages: {
    "app": {
      format: 'register',
      defaultExtension: 'js',
    }
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.