带有多个条目的捆绑汇总打字稿

时间:2019-05-12 14:09:54

标签: reactjs typescript rollup

我正在建立一个项目,以便将我的react-app的业务逻辑提取到子模块中。 我可以将汇总与打字稿和多个条目捆绑在一起,并且效果很好。当我尝试从逻辑模块中的子文件夹导入函数时,就会出现问题。 构建文件夹为dist /,模块名称为core。

因此,我要通过import {configureStore} from 'core'导入函数。 一切正常,但是要在子文件夹中导入函数,打字稿告诉我它不存在: import {myFunc} from 'core/utils'。 在两者之间,我可以这样写:import {myFunc} from 'core/dist/utils,而且效果很好...

tsconfig.js:

{
  "compilerOptions": {
    "outDir": "dist/",
    "module": "esnext",
    "target": "es5",
    "lib": ["es6", "dom", "es2016", "es2017"],
    "sourceMap": true,
    "allowJs": false,
    "jsx": "react",
    "rootDir": "./",
    "declaration": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "build", "dist", "example", "rollup.config.js"]
}

rollup.config.js:

import typescript from 'rollup-plugin-typescript2';
import external from 'rollup-plugin-peer-deps-external';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';

export default {
  input: {
    index: 'src/index.ts',
    'translation/i18n': 'src/translation/i18n.ts',
  },
  output: [
    {
      dir: 'dist',
      format: 'es',
      sourcemap: true,
      exports: 'named',
    },
  ],
  external: [
    ...Object.keys(pkg.dependencies || {}),
    ...Object.keys(pkg.peerDependencies || {}),
  ],
  plugins: [
    external(),
    resolve({ dedupe: ['react', 'react-dom'] }),
    typescript({}),
    commonjs(),
  ],
};

package.json(核心):

  {
    "name": "core",
    "version": "1.0.0",
    "main": "dist/index.js",
    "module": "dist/index.js",
    "files": [
      "dist/*"
    ],
    ...
  }

链接核心软件包:

{
  ...
  "core": "link:../core",
}

在导入路径中不使用dist的情况下无法从子文件夹导入函数/类,我会错过什么?

这是仓库:https://github.com/Jm-Zion/react-moduled

0 个答案:

没有答案