标签: webpack-watch

为什么 webpack --watch 在不相关的文件上调用我的自定义加载器?

我有一个简单的自定义 Webpack 加载器,它从.txt文件生成 TypeScript 代码:

txt-loader.js

module.exports = function TxtLoader(txt) {
  console.log(`TxtLoader invoked on ${this.resourcePath} with content ${JSON.stringify(txt)}`)
  if (txt.indexOf('Hello') < 0) {
    throw new Error(`No "Hello" found`)
  }
  return `export const TEXT: string = ${JSON.stringify(txt)}`
}
Run Code Online (Sandbox Code Playgroud)

在现实生活中,我会对输入进行一些解析;在此示例中,我们假设文件必须包含Hello有效的文本。

该加载程序允许我导入文本文件,如下所示:

索引.ts

import { TEXT } from './hello.txt'

console.log(TEXT)
Run Code Online (Sandbox Code Playgroud)

一切都很好,除了一件事:(webpack watch及其表弟webpack serve)。第一个编译很好:

$ /tmp/webpack-loader-repro/node_modules/.bin/webpack watch
TxtLoader invoked on /tmp/webpack-loader-repro/hello.txt with content "Hello world!\n"
asset main.js 250 bytes [compared for emit] [minimized] (name: main)
./index.ts …
Run Code Online (Sandbox Code Playgroud)

typescript webpack ts-loader webpack-watch webpack-loader

3
推荐指数
1
解决办法
1497
查看次数