【问题标题】:Unable to compile TypeScript of symlinked package using babel-loader无法使用 babel-loader 编译符号链接包的 TypeScript
【发布时间】:2020-02-22 13:24:40
【问题描述】:

我有 2 个包:“共享”和“网络”。他们都使用 TypeScript。我在“web”中使用带有 babel-loader 的 webpack。当我符号链接“共享”时,它无法编译其 TypeScript。

此问题建议在 webpack.config.js 中使用 symlinks: false,但不起作用: https://github.com/webpack/webpack/issues/1643

文件夹结构:

root
│
└───shared
│   │   package.json
│   │   UseMe.ts
│   
└───web
    │   .babelrc
    │   index.ts
    │   package.json
    │   tsconfig.json
    │   webpack.config.js

shared\package.json

{
  "name": "shared",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^3.6.4"
  }
}

共享\UseMe.ts

class UseMe {
    prop: string
}

export default UseMe;

web\.babelrc

{
    "presets": [
        "@babel/preset-typescript"
    ]
}

web\index.ts

import UseMe from 'shared/UseMe';
const x = new UseMe();

web\package.json

{
  "name": "web",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "scripts": {
    "build": "webpack"
  },
  "dependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-typescript": "^7.1.0",
    "babel-loader": "^8.0.5",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "devDependencies": {
    "typescript": "^3.3.3"
  }
}

web\tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
  }
}

web\webpack.config.js

const path = require('path');

module.exports = {
    entry: './index.ts',
    output: {
        path: path.join(__dirname, './dist'),
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js'],
        symlinks: false
    },
    module: {
        rules: [
            {
                test: /\.(ts|js|tsx)?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                },
            }
        ]
    }
};

命令:

cd shared
npm install
npm link
cd ..\web
npm install
npm link shared
npm run build

错误:

ERROR in ./node_modules/shared/UseMe.ts 2:8
Module parse failed: Unexpected token (2:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| class UseMe {
>     prop: string
| }
|

【问题讨论】:

  • 安装@babel/plugin-proposal-class-properties并将其作为插件添加到.babelrc有帮助吗?
  • @ford04 好主意,我尝试了您的建议,但没有解决。即使我删除了该属性并将class 更改为interface,它仍然无法编译.ts 文件。所以看起来不像是由于财产。
  • 您还想看看babel mono repo configsbabel.config.js,因为.babelrc 的babel 转换在package.json 指示的包根处停止。当你有 symlinks: false 时,我希望 babel 忽略链接包的转换,因为你已经为 babel-loader 设置了 exclude: /node_modules/

标签: javascript typescript webpack babeljs babel-loader


【解决方案1】:

解决方案

  1. 删除root\web\.babelrc并替换为root\web\babel.config.js
module.exports = function (api) {
    api.cache(true);

    const presets = ["@babel/preset-typescript"];
    const plugins = [];

    return {
        presets,
        plugins
    };
}
  1. root\web\webpack.config.js 中删除行exclude: /node_modules/

更多信息

第一个问题是,在使用 Babel 时,.babelrc 将其范围限制为与package.json 相同的文件夹。因此,即使我手动将“共享”文件夹移动到 root\web,它仍然无法正常工作,除非我删除了 shared\package.json

第二个问题是,当一个包被符号链接时,它会在你的“node_module”中结束,因此exclude: /node_modules/ 会将其排除在转译之外。

有关更多信息,请参阅此页面:https://babeljs.io/docs/en/config-files

感谢@ford04 提供答案,我刚刚写了步骤。

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 2017-03-14
    • 2017-03-25
    • 1970-01-01
    • 2019-12-01
    • 2017-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多