我把一个汇总构建系统放在一起,该系统需要一个文件夹的Typescript组件将它们导出为模块。
如果组件文件位于文件夹结构中,则构建系统将完美运行:
src/components/Button
但是当我尝试从baseUrl
含义之外导入它们时,问题就出现了:
../../javascript/components/Button
一旦我尝试这样做,我就会收到以下错误:
[!] Error: The keyword 'interface' is reserved
这是我的tsconfig
文件的样子:
{
"compilerOptions": {
"jsx": "react",
"module": "es2015",
"target": "es2017",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"strictNullChecks": true,
"sourceMap": true,
"baseUrl": ".",
"preserveSymlinks": true,
"esModuleInterop": true,
"paths": {
"ui": ["../../components"]
}
},
"include": ["src/**/*.tsx"],
"exclude": ["node_modules"]
}
和我的 rollup.config
import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import less from 'rollup-plugin-less';
import alias from 'rollup-plugin-alias';
const plugins = [
alias({
ui: '../../components',
resolve: ['.tsx', '.ts', '.less']
}),
resolve(),
commonjs({
// All of our own sources will be ES6 modules, so only node_modules need to be resolved with cjs
include: 'node_modules/**',
namedExports: {
'node_modules/react/index.js': [
'Component',
'PropTypes',
'createElement'
],
'node_modules/lodash/lodash.js': ['isEmpty', 'isString']
}
}),
less({ insert: 'true' }),
typescript()
];
export default {
plugins,
external: ['react'],
input: './src/main.js',
output: {
sourcemap: true,
file: './lib/index.js',
format: 'cjs'
}
};
提前非常感谢!!
调查了一段时间后,我发现默认情况下@rollup/plugin-typescript
只转换项目根目录内的文件。要覆盖该行为,您需要在插件的include
选项中指定您计划使用的文件的 glob,包括项目中的文件。
import ...
const plugins = [
/* ... */
typescript({
include: [
// Project files
'./**/*.ts+(|x)',
// Files from outside of the project
'../../javascript/**/*.ts+(|x)'
]
})
];
export default {
/* ... */
};
您还需要在"tsconfig.json"中执行此操作,因为插件会抱怨外部文件未在tsconfig中列出。