ng build gives an error because of regExp

2.7k Views Asked by At

i have regExp in constants:

export const Constants = {
    QUERY_PARAM_CONFIG: {
        REGEX: /\+/gi,
        REGEX_ENCODED: /%2B/g
    }
};

but when i run ng build i get an error:

ERROR in app\app.module.ts(58,21): Error during template compile of 'AppModule'
  Expression form not supported in 'Constants'
    'Constants' contains the error at app\configuration\constants.ts.

npm build script:

ng build --prod --output-hashing none --sourcemaps=true

any ideas?

1

There are 1 best solutions below

2
smnbbrv On BEST ANSWER

The regex literals are not supported by AOT. See the list of supported syntax.

In order to make it work transform your regex to something supported e.g. string:

const before = /\+/gi, // before, could be the same as below

const after = { re: '\\+', flags: 'gi' }
const canBeUsedAs = new RegExp(after.re, after.flags);

By this you can still pass the meaning of the RegExp and create one on the fly whenever you need them in the runtime