我有一个TypeScript项目,我正在捆绑Webpack . 它是我正在编写的开源库的demo / docs应用程序,所以我想将一些源代码显示为文档的一部分 .
在我的webpack配置中,我有:
loaders: [
{ test: /\.ts$/, loader: 'ts'},
{ test: /\.css$/, loader: 'style!raw' },
{ test: /\.html/, loader: 'html' }
]
这适用于转换和捆绑我的TypeScript文件 . 在我的一个应用程序组件中,我这样做:
basicCodeT: string = require('./basic-example-cmp.html');
basicCodeC: string = require('!raw!./basic-example-cmp.ts');
将源代码加载到我想要在文档中显示的字符串中 .
正如您所看到的,在第二行中有一个领先的 !
,我发现似乎是"bypass"来自配置的默认加载器并将原始TypeScript作为字符串加载 .
在我的开发版中,这可行,但是当我使用UglifyJsPlugin和OccurrenceOrderPlugin进行“ 生产环境 ”构建时,我得到以下输出:
ERROR in ./demo/src/basic-example-cmp.html
Module build failed:
@ ./demo/src/demo-app.ts 24:26-61
这对应于我尝试要求原始TypeScript的源代码行 .
所以, I want to pass basic-example-cmp.ts through the TS compiler as part of the app build, but also want to require it as raw text in the app .
我的问题是: Is there a proper way to tell webpack to "ignore" loaders in specific require cases?
Is my way of prepending a ! correct? Is it a hack?
更新
事实证明我的问题仅仅是由于Webpack处理HTML模板的方式 - 它不喜欢Angular 2模板语法,请参阅:https://github.com/webpack/webpack/issues/992
3 回答
据我所知,这是你能够以两种不同的方式加载文件的唯一方法 . 我希望问题是您的 生产环境 构建中的路径不同 .
我建议使用
--display-error-details
标志运行webpack以获取有关失败原因的更多信息 .是 . 根据需要更新
{ test: /\.ts$/, loader: 'ts'},
中的test
.您可以添加两个感叹号来忽略webpack配置文件中的加载器
!!raw!file.ts
一个感叹号只会禁用预加载器!
https://webpack.github.io/docs/loaders.html