aurelia / cli

The Aurelia 1 command line tool. Use the CLI to create projects, scaffold components, and bundle your app for release.
MIT License
407 stars 133 forks source link

PackageAnalyzer and Bundler incorrectly identifying plugin/ts references as npm packages #1199

Closed jamesg1 closed 2 years ago

jamesg1 commented 2 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior:

Hi I have some aurelia code which is defined in another package (as a plugin which is bundled into one file) and I have some custom typescript code to generate some types for consumer apps.

Example example.d.ts which is imported as a custom type into my consumer aurelia app.

declare module 'services/example-a' { export class ExampleA {
    navigate(url: string): Promise<void>;
    static navigate(url: string): Promise<void>;
    static relocate(url: string): Promise<void>;
}
declare module 'services/example-b' { export class ExampleB {
    navigate(url: string): Promise<void>;
    static navigate(url: string): Promise<void>;
    static relocate(url: string): Promise<void>;
}

This code is set globally with requireJS modules on the page separately so I don’t need to include it in my app bundle.

How can I exclude these errors from occuring when bundling?

ERROR [PackageAnalyzer] Unable to load package metadata (package.json) of services:
INFO [PackageAnalyzer] Error: cannot resolve npm package folder for "services"
ERROR [Bundler] Unable to analyze services
INFO [Bundler] Error: Unable to find package metadata (package.json) of services
ERROR [Bundler] Failed to add Nodejs module services/example-a
INFO [Bundler] Error: Unable to find package metadata (package.json) of services
ERROR [PackageAnalyzer] Unable to load package metadata (package.json) of services:
INFO [PackageAnalyzer] Error: cannot resolve npm package folder for "services"
ERROR [Bundler] Unable to analyze services
INFO [Bundler] Error: Unable to find package metadata (package.json) of services
ERROR [Bundler] Failed to add Nodejs module services/example-b
INFO [Bundler] Error: Unable to find package metadata (package.json) of services

“services” is not an npm package rather a custom ts module. TS passes ok and the code is working when deployed.

Ref - https://discourse.aurelia.io/t/excluding-packageanalyzer-and-bundling-of-external-lib/4901

3cp commented 2 years ago

Bundler doesn't bundle d.ts file. It could be some .ts file importing "services/*". Can you share a mini repo to demo the issue?

3cp commented 2 years ago

This code is set globally with requireJS modules on the page separately

Sounds like you want bundler to ignore this module. Check doc section: "1. ignore certain moduleId at bundling time, supply it at runtime"

https://aurelia.io/docs/cli/cli-bundler/advanced#onrequiringmodule

3cp commented 2 years ago

The old bundler 0.x doesn't do auto tracing. It was not a full featured bundler, it didn't trace a module unless you explicitly add the module to aurelia.json dependencies.

Cli v1+ (including v2 and v3) does auto tracing (like webpack and parcel), but you can explicitly ignore some module (or perform other tricks).

jamesg1 commented 2 years ago

This code is set globally with requireJS modules on the page separately

Sounds like you want bundler to ignore this module. Check doc section: "1. ignore certain moduleId at bundling time, supply it at runtime"

https://aurelia.io/docs/cli/cli-bundler/advanced#onrequiringmodule

Thanks, Yeah I managed to find that page after some digging too and it's exactly what I need. 🎉

I managed to make the moduleId get checked against the d.ts file. Working well :)

function checkIfContainsSync(str) {
  const filename = '../../node_modules/package-example/dist/example-bundle.d.ts';
  const contents = readFileSync(filename, 'utf-8');
  const result = contents.includes(`module '${str}'`);
  return result;
}

function writeBundles() {
  return buildCLI.dest({
    onRequiringModule: function(moduleId) {
      return !(checkIfContainsSync(moduleId));
    }
  });
}