next-transpile-modules
非常适合 Next 项目,但是如何为原始 SWC 构建转换模块???我难住了
回购:https ://github.com/deltaepsilon/script-kitty
我从 Turborepo 基础开始,导出了两个包,ui
然后command-k
导入到名为web
. 一旦我添加到文件中ui
,一切都很好,如下所示:command-k
next.config.js
const withTM = require('next-transpile-modules')(['command-k', 'ui']);
module.exports = withTM({
reactStrictMode: true,
});
现在我有了一个名为的新应用程序external
,它将是包的独立版本command-k
。这将发布到 npm。
我正在使用swc-loader
以下配置对其进行转换:
const path = require('path');
// See https://github.com/iykekings/react-swc-loader-template
const config = {
mode: 'development',
entry: './index.tsx',
module: {
rules: [
{
test: /\.(ts|tsx)$/,
loader: 'swc-loader',
include: [
path.resolve(__dirname),
path.resolve(__dirname, '../../packages/command-k'),
path.resolve(__dirname, '../../packages/ui'),
],
exclude: /node_modules/,
},
],
},
};
module.exports = config;
构建时我不断收到以下错误yarn dev
:
ERROR in ../../packages/command-k/index.tsx 2:0-50
Module not found: Error: Can't resolve './command-k' in '/kitty/packages/command-k'
resolve './command-k' in '/kitty/packages/command-k'
using description file: /kitty/packages/command-k/package.json (relative path: .)
// /packages/command-k/index.tsx
import * as React from 'react';
export { default as CommandK } from './command-k';
看起来好像swc-loader
无法从 Turborepo 包内部导入。如果我内联./command-k
into的内容,它工作正常/packages/command-k/index.tsx
,但swc-loader
拒绝遵循导入。