Webpack regex test that matches *.ts but not *.d.ts

旧街凉风 提交于 2019-12-02 05:51:20

问题


I am using Webpack (2.3.3) to build my Aurelia app in TS. However, since I am using includeAll option for AureliaPlugin (2.0.0-rc.2), ts-loader (2.0.3) cries about d.ts files that has nothing exported emitting no output.

here is my rule for ts files: { test: /\.ts$/, include: /ClientApp/, use: "ts-loader" }

I need to change the test value in a way that it matches files named *.ts except *.d.ts. Can you help me please?

Note: I see there is already a similar regex question but that one did not work for me.


回答1:


A regex that matches strings with .ts at the end but not .d.ts is

/(^.?|\.[^d]|[^.]d|[^.][^d])\.ts$/

See a regex demo. It is a POSIX style expression that will work with any regex engine.

Details

  • (^.?|\.[^d]|[^.]d|[^.][^d]) - either of:
    • ^.? - start of string + any optional char
    • \.[^d] - a dot and any char but d
    • [^.]d - any char but . and d
    • [^.][^d] - any char but . and then any char but d (this way, we match all but .d)
  • \. - a literal dot
  • ts$ - ts at the end of string.

Alternatively, use a lookahead based solution:

/^(?!.*\.d\.ts$).*\.ts$/

See another demo

Details:

  • ^ - start of string
  • (?!.*\.d\.ts$) - the string cannot end with .d.ts
  • .* - any 0+ chars up to the
  • \.ts$ - .ts at the end of the string.

However, you might explore another option described in this SO thread.



来源:https://stackoverflow.com/questions/43490499/webpack-regex-test-that-matches-ts-but-not-d-ts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!