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.

ng-annotate-loader Typescript processing order

See original GitHub issue

Describe the bug I’m upgrading an AngularJS 1.6 project with ngUpgrade to run both ng+ and AngularJS code in the same project.

The AngularJS code requires ng-annotate-loader for DI annotations.

ng-annotate-loader should run AFTER the Angular CLI webpack rules have parsed TypeScript, as ng-annotate-loader cannot parse TypeScript.

angular.json

"customWebpackConfig": {
  "path": "src/extra-webpack.config.ts"
},

webpack.config.ts

const path = require('path');

module.exports = {
  // @todo ng-annotate-loader is trying to annotate TypeScript before it has been processed by ts-loader.
  module: {
    rules: [
      {
        // Do we target TypeScript that has been processed by ts-loader?
        test: /\.ts?$/,
        // Or do we somehow target JS after ts-loader has finished processing?
        // test: /\.js?$/,
        // Only parse the AngularJS scripts (that require DI annotations).
        include: [
          path.resolve(__dirname, 'legacy'),
        ],
        use: [
          {
            loader: 'ng-annotate-loader',
            options: {
              ngAnnotate: 'ng-annotate-patched',
            }
          },
        ]
      },
    ]
  }
};

Error:

ERROR in ./src/legacy/shared/components/app-version/app-version.component.ts
Module build failed (from ./node_modules/ng-annotate-loader/loader.js):
NonErrorEmittedError: (Emitted value instead of an instance of Error) error: couldn't process source due to parse error,Unexpected token (5:25)
    at runLoaders (/Users/mastone/Work/Everwealth.WebAppMigration/node_modules/webpack/lib/NormalModule.js:283:13)
    at /Users/mastone/Work/Everwealth.WebAppMigration/node_modules/loader-runner/lib/LoaderRunner.js:364:11
    at /Users/mastone/Work/Everwealth.WebAppMigration/node_modules/loader-runner/lib/LoaderRunner.js:230:18
    at Object.context.callback (/Users/mastone/Work/Everwealth.WebAppMigration/node_modules/loader-runner/lib/LoaderRunner.js:111:13)
    at Object.module.exports (/Users/mastone/Work/Everwealth.WebAppMigration/node_modules/ng-annotate-loader/loader.js:87:10)

To Reproduce Steps to reproduce the behavior:

  1. Go to ‘…’
  2. Click on ‘…’
  3. Scroll down to ‘…’
  4. See error

OR (preferred way)

a link to a repository with minimal reproduction

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Builder:

  • The name of the builder
  • The version of the builder

Libraries

"@angular-builders/custom-webpack": "^7.1.1",
"@angular-builders/dev-server": "^7.1.1",
"@angular-devkit/build-angular": "^0.11.4",
"ng-annotate-loader": "^0.6.1",
"ng-annotate-patched": "^1.7.0",

Additional context Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nandastonecommented, Jan 9, 2019

@meltedspark Both options have solved my issue. I was trying to merge loaders as outlined in the documentation:

"mergeStrategies": { "loaders": "prepend" },

Thank you for your help!

0reactions
alexmacaveicommented, Jun 30, 2020

Hey! My extended.webpack.config file is something like:

const path = require('path');
const webpack = require('webpack');

module.exports = (config) => {
  config.plugins.push(new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery'
  }));
  config.module.rules.filter(rule => rule.test.test('.scss'))[0].exclude.push(path.resolve(__dirname, 'src/app/angularjs'));

  config.module.rules.push({
    test: /\.ts$/,
    include: path.resolve(__dirname, 'src/app/angularjs'),
    loader: 'ng-annotate-loader',
    options: { ngAnnotate: 'ng-annotate-patched', es6: true }
  }, {
    test: /\.html$/,
    use: ['raw-loader']
  }, {
    test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
    loader: 'file-loader'
  });

  return config;
};

Errors I’m getting are something like:

Module build failed (from ./node_modules/ng-annotate-loader/loader.js):
NonErrorEmittedError: (Emitted value instead of an instance of Error) error: couldn't process source due to parse error,Unexpected token (9:10)

Should ng-annotate run before Angular’s webpack-loader ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ng-annotate-loader Typescript processing order #186 - GitHub
Describe the bug I'm upgrading an AngularJS 1.6 project with ngUpgrade to run both ng+ and AngularJS code in the same project.
Read more >
How to use ng-annotate with hybrid app based on angular-cli
So, the way to do this is by using webpack-merge and custom-webpack. This is the configuration to run ng-annotate with Typescript files :...
Read more >
ng-annotate-loader - npm
Webpack loader that runs ng-annotate on your bundles. Latest version: 0.7.0, last published: 3 years ago. Start using ng-annotate-loader in ...
Read more >
Using ng-annotate for min-safe AngularJS - Egghead.io
This is a problem for AngualrJS, which uses parameter names to provide injected dependencies. You could use the array notation manually, but no ......
Read more >
Ng-annotate NPM - npm.io
ng -annotate supports ES5 as input so run it with the output from Babel, Traceur, TypeScript (tsc) and the likes. Use "ngInject" on...
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