2017-04-05 168 views
3

是否可以配置awesome-typescript-loader或webpack或tsc来编译从条目引用(直接,间接)的文件?我想移植一个现有的应用程序,并希望能够编译我部分移植的代码,而某些未引用的文件不可编译。awesome-typescript-loader仅加载/编译引用文件

使用的工具

  • 真棒,打字稿装载机:3.1.2,
  • 的WebPack:2.3.3

webpack.js

var webpack = require('webpack'); 
module.exports = { 
    entry: { 
     'index': './Scripts/index.ts', 
    }, 

    output: { 
     path: helpers.root('dist'), 
     filename: '[name].js' 
    }, 

    resolve: { 
     extensions: ['.ts', '.js'] 
    }, 
    module: { 
     rules: [ 
      { 
       test: /.ts$/, 
       loader: 'awesome-typescript-loader' 
      } 
     ] 
    } 
} 

tsconfig

{ 
    "compilerOptions": { 
    "target": "es5", 
    "module": "commonjs", 
    "moduleResolution": "node", 
    "importHelpers": true, 
    "typeRoots": [ 
     "node_modules/@types" 
    ], 
    "sourceMap": true, 
    "lib": [ 
     "dom", 
     "es5", 
     "scripthost" 
    ], 
    "experimentalDecorators": true, 
    "emitDecoratorMetadata": true, 
    "noImplicitAny": false, 
    "suppressImplicitAnyIndexErrors": true 
    }, 
    "exclude": [ 
    "node_modules" 
    ] 
} 

错误消息(来自不由index.ts引用的文件)

[at-loader] Checking started in a separate process... 

[at-loader] Checking finished with 46 errors 
Hash: a2f6e7a56b152c02a325 
Version: webpack 2.3.3 
Time: 3554ms 
    Asset  Size Chunks    Chunk Names 
index.js 2.73 kB  0 [emitted] index 
    [0] ./Scripts/index.ts 79 bytes {0} [built] 

ERROR in [at-loader] ./Scripts/editor/foo.ts:47:61 
    TS2448: Block-scoped variable 'eventList' used before its declaration. 

... MORE ERRORS ... 

npm ERR! Windows_NT 10.0.14393 
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build" 
npm ERR! node v6.9.2 
npm ERR! npm v3.10.9 
npm ERR! code ELIFECYCLE 
npm ERR! [email protected] build: `webpack --config webpack.config.js` 
npm ERR! Exit status 2 
npm ERR! 
npm ERR! Failed at the [email protected] build script 'webpack --config webpack.config.js'. 
npm ERR! Make sure you have the latest version of node.js and npm installed. 
npm ERR! If you do, this is most likely a problem with the FooBar package, 
npm ERR! not with npm itself. 
npm ERR! Tell the author that this fails on your system: 
npm ERR!  webpack --config webpack.config.js 
npm ERR! You can get information on how to open an issue for this project with: 
npm ERR!  npm bugs FooBar 
npm ERR! Or if that isn't available, you can get their info via: 
npm ERR!  npm owner ls FooBar 
npm ERR! There is likely additional logging output above. 

npm ERR! Please include the following file with any support request: 
npm ERR!  D:\FooBar\npm-debug.log 
+0

Webpack只包括正在导入的文件(从入口点开始),因此只适用于那些额外匹配规则的文件。我在这里看不到你的问题。 –

+0

'[at-loader]在单独的进程中开始检查... [at-loader]在未引用的文件中检查已完成的49个错误。 –

回答

8

的WebPack本身不包括未正在导入的任何文件。但除非另行配置,否则TypeScript似乎会检查项目中的每个文件。从tsconfig.json docs

如果“文件”和“包括”都离开了指定,则编译器默认为包括所有的打字稿(.TS,.d.ts和.tsx)文件中包含的目录和子目录除了使用“排除”属性排除的那些。

您可以将files选项设置为仅包含入口点,并且它应该只检查要导入的文件。

"files": [ 
    "Scripts/index.ts" 
] 

据我测试过你也可以将它设置为空数组,这样就不会被默认选中所有文件和文件awesome-typescript-loader将如预期的文件传递给编译器,他们仍然会被检查。

"files": [] 
+1

“files”:[]似乎适用于awesome-typescript-loader,但打破ts-loader和tsc的TS18002错误:配置文件'tsconfig.json'中的'files'列表为空。 – ericsicons

+0

这种方法不适用于webpack 3.10.0和awesome-typescript-loader 3.4.1。由于某些原因,ATL包含所有文件而不是仅包含refferenced。 –