在节点模块中导入Typescript和Js文件时出现问题

时间:2018-08-20 08:27:52

标签: javascript typescript node-modules ts-loader

我正在导入一个包含Typescript和JS文件的节点模块文件夹。我将ts-loader用于打字稿,但由于某种原因,它会创建一长串错误,而且我认为这是因为两个文件都在一起。我自己无法触摸这些文件,所以也许有更好的方法来配置ts-loader?

这是我的tsconfig.json

{
"compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "declaration": false,
    "outDir": "lib",
    "strict": false,
    "allowJs": true,
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "noResolve": false
},
"include": [
    "./standalone/**/*",
    "./node_modules/**/*"
],
}

一些错误是:

ERROR in ./node_modules/project/frontend/react/shared/inputs/smallcolorinput.tsx Module build failed (from ./node_modules/ts-loader/index.js): Error: Typescript emitted no output for ../FrontEnd/ProjectFolder/node_modules/project/frontend/react/shared/inputs/smallcolorinput.tsx. at successLoader (../FrontEnd/ProjectFolder/node_modules/ts-loader/dist/index.js:41:15) at Object.loader (../FrontEnd/ProjectFolder/node_modules/ts-loader/dist/index.js:21:12) @ ./node_modules/project/frontend/react/shared/views/FontSettings.jsx 13:23-59 @ ./node_modules/project/frontend/react/shared/views/ThemeEditor.jsx @ ./node_modules/project/frontend/react/editor/editor.jsx @ ./electron/views/Edit.tsx

这是Web Pack配置:

return {
        devtool: devMode ? 'eval' : 'hidden-source-map',
        module: {
            rules: [
                {
                    test: /\.(ts|tsx)?$/,
                    use: [babelLoader, {
                        loader: 'ts-loader',
                        options: {
                            onlyCompileBundledFiles: true,
                            allowTsInNodeModules: true
                        }
                    }],
                    exclude: /node_modules\/(?!(project)\/).*/

                },
                {
                    test: /\.(css|scss)?$/,
                    use:  ['style-loader', MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader', 'sass-loader']
                },
                {
                    test: /.jsx?$/,
                    use: babelLoader,
                    exclude: /node_modules\/(?!(project)\/).*/
                },
},resolve: {
            symlinks: false,
            modules: [
                path.resolve(basePath, "node_modules"),

            ],
            extensions: ['.js', '.jsx', '.ts', '.tsx'],
            alias: {

                'classnames': path.join(basePath, 'node_modules/project/lib/utils/classnames.js')
            }
        },

1 个答案:

答案 0 :(得分:1)

经过大量的尝试并尝试了不同的答案后,我发现Typescript在编译来自node_modules的ts文件时遇到问题。 在ts-loader现在正式支持之前,它不受支持。

我发现删除ts-loader的onlyCompileBundledFiles可解决这些问题。