无法使用 babel-loader 编译符号链接包的 TypeScript

Nin*_*ine 2 javascript typescript webpack babeljs babel-loader

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

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

文件夹结构:

root
?
????shared
?   ?   package.json
?   ?   UseMe.ts
?   
????web
    ?   .babelrc
    ?   index.ts
    ?   package.json
    ?   tsconfig.json
    ?   webpack.config.js
Run Code Online (Sandbox Code Playgroud)

共享\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"
  }
}

Run Code Online (Sandbox Code Playgroud)

共享\UseMe.ts

class UseMe {
    prop: string
}

export default UseMe;
Run Code Online (Sandbox Code Playgroud)

网页\.babelrc

{
    "presets": [
        "@babel/preset-typescript"
    ]
}
Run Code Online (Sandbox Code Playgroud)

网页\index.ts

import UseMe from 'shared/UseMe';
const x = new UseMe();
Run Code Online (Sandbox Code Playgroud)

网页\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"
  }
}

Run Code Online (Sandbox Code Playgroud)

网页\tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
  }
}
Run Code Online (Sandbox Code Playgroud)

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'
                },
            }
        ]
    }
};
Run Code Online (Sandbox Code Playgroud)

命令:

cd shared
npm install
npm link
cd ..\web
npm install
npm link shared
npm run build
Run Code Online (Sandbox Code Playgroud)

错误:

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
| }
|
Run Code Online (Sandbox Code Playgroud)

Nin*_*ine 5

解决方案

  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
    };
}
Run Code Online (Sandbox Code Playgroud)
  1. 删除线exclude: /node_modules/root\web\webpack.config.js

更多信息

第一个问题是,在使用 Babel 时,.babelrc将其范围限制在与package.json. 因此,即使我手动将“共享”文件夹移入root\web其中仍然无效,除非我删除了shared\package.json.

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

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

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