question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom loader's output ignored in append mode

See original GitHub issue

Describe the bug When running @angular-builders/custom-webpack with a custom loader pointed to a single file, the loader’s output appears to be ignored (it does get called and executes) when the merge strategy is set to “append” or when left blank.

To Reproduce Minimal repo: https://github.com/mralbobo/angular-webpack-builder-repro Freshly cli generated, with the following notable modifications/ additions:

  • extra-webpack.config.js
  • angular.json
  • preprocess-analytics-loader.js
  • src/app/AnalyticsService.ts
  • app.component.ts

The two “real” files have been extremely cut down to having the loader just replace the file with essentially nonsense. There are two test cases:

  • run (serve) as is, observe that the app does not error despite the intent of replacing the contents of the file with nonsense
  • modify the merge strategy to “prepend”, observe that the the app does throw an error related to the files content being changed to nonsense

Expected behavior The expectation is that the loader will run, replace the contents of the file with the string of f’s and the app should throw an error when it tries to run the created function. In actuality, the function runs successfully.

Builder:

Libraries

  • angular-devkit/build-angular: 0.802.2

Extra

  • target file is .ts

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
just-jebcommented, Sep 7, 2019

Sure, here are a few URLs:

Now, I think the real issue here is that they don’t really use loaders for Typescript. The entity that processes TS is AngularCompilerPlugin. I have no idea why it works with prepend, and why is that different from append when the real TS compilation happens in AngularCompilerPlugin but I’d say this is the way you should take.
You can hook into transformers by pushing your own transformer.
To add your transformer into _transformers array you’ll probably have to do something like this in your webpack config:

  const index = config.plugins.findIndex(p => p instanceof 
  AngularCompilerPlugin.AngularCompilerPlugin);
  config.plugins[index]._transformers.push(YourTrasnformerHere);
  return config;

In this example I pushed it at the end but here is where you choose when this transformer is applied. You can squeeze it in between or run before all the transformers, whatever you prefer. I think it should work.
To learn more about how this stuff works look into the code of AngularCompilerPlugin.

1reaction
just-jebcommented, Sep 7, 2019

Update: Guys from Angular team say that the right way is to use platformTransformers. It’s actually all the same in terms of execution (they are just added to the _transformers array). I think the reason it is considered the right approach is that platformTransformers are provided in options while _transformers are private property.
In case you’re using platformTransformers you should be able to use non-functional custom config:

module.exports = {
	module: {
		plugins: [
                     new AngularCompilerPlugin({platformTransformers: [YourTrasnformerHere]})
                ]
	}
};

Notice though these are pushed after certain transformers.

Also, Angular is still using a single loader for TS files which works in conjunction with compiler plugin (this is most probably the reason for the mess with append and prepend).

Read more comments on GitHub >

github_iconTop Results From Across the Web

file-loader - webpack - JS.ORG
Specifies a custom function to post-process the generated public path. This can be used to prepend or append dynamic global variables that are...
Read more >
What is Auto Loader? | Databricks on AWS
Auto Loader incrementally and efficiently processes new data files as they arrive in cloud storage without any additional setup.
Read more >
Union Tool | Alteryx Help
Use Union to combine 2 or more datasets on column names or positions. In the output, each column contains the rows from each...
Read more >
API - esbuild
In addition, you can also specify a fully custom output path for each ... For example, the js loader interprets the file as...
Read more >
rollup.js
json are ignored. That's tree-shaking in action. Using output plugins. Some plugins can also be applied specifically to some outputs. See plugin hooks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found