0

This is part of a larger task.

In this first step I just want to export something as a default export from a standard angular app bundle so that later it can be imported using dynamic import using the import expression.

This is what I have so far and it doesn't work. This is the only modification I made to a standard angular app created with ng new.

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

console.log('I got here');

export class Anything {
  pleaseWork() : void {
    console.log('please work FFS');
  }
}

let a = new Anything();
a.pleaseWork();

export default Anything;

I'm loading the bundle in an empty index.html I have to use it this way.

<!doctype html>
<html lang="en">
<head>
</head>
<body>
  <script type="module">
    import('/main.js')
      .then(module => { 
        debugger;
        console.log(module.default); // <-- i want module.default to not be undefined instead of being undefined
      })
      .catch(error => { console.error(error); })
  </script>
</body>
</html>
gyozo kudor
  • 6,284
  • 10
  • 53
  • 80
  • ... why would you want to do this..? This will fail even without your custom code right..? – MikeOne Aug 12 '20 at 20:12
  • I looked at your other question to know why you want to do this, and no the custom element does NOT have to be a default export. – Sherif Elmetainy Aug 12 '20 at 21:08
  • @SherifElmetainy ok good to know, so how can I make my custom element accessible to an import statement like this – gyozo kudor Aug 13 '20 at 06:38
  • @MikeOne if you are referring to the index.html then no it doesn't. (In the linked question at least I found one way to make the bundle work but without default export, that is why I made this dummy example quickly just for the export part which I couldn't figure out) So there is absolutely no way to export something from a javascript bundle? – gyozo kudor Aug 13 '20 at 06:57
  • Well.. I can’t make that statement. However, you seem te be looking at a build time versus runtime thing here. Angular main.ts is compiled build time, no telling what will be exportable in the compiled version. Also, it’s not like the angular compiler itself is available at runtime (it’s not). I’d really like to understand what you actually are trying to achieve (this and the other post seems to be asking for a fix for a solution - no description of what you are actually trying to achieve). Exporting a default is not the goal in itself I guess? – MikeOne Aug 13 '20 at 14:51

0 Answers0