Search code examples
regexangularconfigurationjasmineaot

Reg ex to ignore .spec.ts files for AOT angular 5 build


Angular 5 ngtools isn't ignoring all ".spec.ts" files in my production build.

How would I exclude **.spec.ts* but keep any other .ts ?

/(?:.ngfactory.js|.ngstyle.js|.ts)$/

From my webpack.prod.config.js...

module.exports = merge(baseConfig, {
    devtool: "source-map",
     module: {
         rules:
         [
            {   // AOT mode support for production
                test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
                loader: '@ngtools/webpack'
            }
        ]
    },

Click here on link example of image to regex101.com

enter image description here


Solution

  • If you plan to allow files with .ngfactory.js, .ngstyle.js and .ts extensions, that means you need to match any string ending with these extensions and not ending with .spec.ts.

    Use

    /^(?!.*\.spec\.ts$).*(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/
    

    See the regex demo.

    Details

    • ^ - start of string
    • (?!.*\.spec\.ts$) - a negative lookahead that fails the match if there are any 0+ chars other than line break chars, as many as possible (.*) and then .spec.ts at the end of the string ($) immediately to the right of the current location
    • .* - any 0+ chars other than line break chars, as many as possible
    • (?:\.ngfactory\.js|\.ngstyle\.js|\.ts) - .ngfactory.js, .ngstyle.js or .ts
    • $ - end of string.