相关疑难解决方法(0)

如何在 next.js 中加载导出 Typescript 的库

当尝试从导出Typescript 的私有库导入组件时,我们收到以下错误消息:

Module parse failed: Unexpected token (82:7)
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
| 
| // Types
> export type {
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?我尝试在tsconfig文件中显式包含库节点模块:

  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "node_modules/@private-lib/*.*"
  ],
  "exclude": [""]
Run Code Online (Sandbox Code Playgroud)

但不幸的是,没有效果。似乎可以更改next.jswebpack配置,但不幸的是,尝试插入 Typescript 加载器并不起作用:

module.exports = {
  webpack: (config, options) => {
    config.module.rules.push({
      test: /\.(ts|js)x?$/,
      use: [
        options.defaultLoaders.babel,
        {
          loader: "ts-loader",
          options: {
            transpileOnly: true,
            experimentalWatchApi: …
Run Code Online (Sandbox Code Playgroud)

typescript webpack next.js webpack-4

21
推荐指数
3
解决办法
2万
查看次数

标签 统计

next.js ×1

typescript ×1

webpack ×1

webpack-4 ×1