语法错误node_modules / esnext和commonjs中意外的令牌导出不兼容

时间:2019-12-30 05:28:33

标签: javascript node.js typescript webpack ecmascript-6

在基于打字稿的项目A中,我使用以下命令导入项目B:

import { functionA, functionB } from '@something/projectB'

但是我得到了错误:

Path_To_Project_B\lib\index.js:1
(function (exports, require, module, __filename, __dirname) { export * from './core/index';
                                                              ^^^^^^
SyntaxError: Unexpected token export

我检查了两个项目的tsconfig.json,这是项目B的tsconfig.json:

{
  "compilerOptions": {
    "incremental": true,
    "target": "es5",
    "module": "esnext",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "noEmitOnError": true,
    "moduleResolution": "node",
    "strict": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true
  }
}

这是我的项目A的tsconfig.json:

{
    "compileOnSave": true,
    "compilerOptions": {
        "outDir": "../../dist/ProjectA/dts",
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "lib": [
            "dom",
            "es6",
            "scripthost",
            "es2016",
            "es2015",
            "es2015.promise",
            "esnext"
        ],
        "declaration": true,
        "sourceMap": true,
        "noEmitOnError": true,
        "noImplicitThis": true,
        "alwaysStrict": true,
        "strictBindCallApply": true,
        "strictNullChecks": true,
        "strictFunctionTypes": true,
        "strictPropertyInitialization": true,
        "experimentalDecorators": true,

    },
    "include": [
        "./scripts/Definitions/*.d.ts",
        "./scripts/Main.ts"
    ],
    "exclude": []
}

我尝试使用(遵循Babel官方网站的步骤)添加babel依赖项和.babelrc

{
    "plugins": ["preset-env"]
}

我注意到有一些与Jest相关的帖子,但是在我的项目A中我没有使用它,而在项目B中,Jest正在使用中。

我也尝试过在Webpack文件中添加“ babel-loder”:

module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                include: /node_modules/,
                use: [
                    'thread-loader',
                    {
                      loader: 'babel-loader',
                      options: {
                        cacheDirectory: true,
                        configFile: path.resolve(
                            __dirname,
                            "./.babelrc",
                        ),
                      },
                    },
                  ],
                enforce: 'pre',
                query: {
                    presets: ['preset-env']
                }
            },
            {
                test: /\.ts$/,
                loader: 'babel-loader',
                include: /node_modules/,
                use: [
                    'thread-loader',
                    {
                      loader: 'babel-loader',
                      options: {
                        cacheDirectory: true,
                        configFile: path.resolve(
                            __dirname,
                            "./.babelrc",
                        ),
                      },
                    },
                  ],
                query: {
                    presets: ['preset-env']
                },
                use: [
                    { loader: "ts-loader", options: tsLoaderOptions }, 
                    { loader: "ifdef-loader", options: ifDefLoaderOptions }
                 ]
            }
        ]
    },

但是问题仍然存在,如何将node_modules下的项目B进行编译并由我的项目A使用?谢谢!

0 个答案:

没有答案