是否可以从node_module转换本地模块? [英] Is it possible to transpile local modules from node_module?

查看:416
本文介绍了是否可以从node_module转换本地模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个打字稿项目,其中包含几个类.我在package.json的Project2中添加了Project1的依赖项

I have 2 typescript projects which contain few classes. I have added Project1's dependency in the Project2 in package.json

{
  "name": "Project2",
  "dependencies": {
    "@Project1": "file:../Project1/dist"
  }
}

两个项目都是使用

目标":"es5",模块":"es2015",

"target": "es5", "module": "es2015",

我正在使用 Karma-Webpack 为项目设置测试环境.为了转译代码,我使用了 babel-loader (预设为es2015)而不是ts-装载机.它会编译来自Project2的代码,但不会编译位于node_modules中的Project1的代码.因此,在运行测试时会抛出以下错误

I am using Karma-Webpack for setting up test environment for the projects. To transpile the code I have used babel-loader (with preset: es2015) instead of ts-loader. It transpiles the code from the Project2 but the code from Project1 located in node_modules is not getting transpiled. Due to that it throws below error when the test is run

Chrome 55.0.2883(Windows 10 0.0.0)错误未捕获的语法错误: 在spec.bundle.js:80972

Chrome 55.0.2883 (Windows 10 0.0.0) ERROR Uncaught SyntaxError: Unexpected token export at spec.bundle.js:80972

我想知道是否可以使用webpack从node_modules转换本地模块?

I was wondering is it possible to transpile local modules from node_modules using webpack?

注意::如果我将模块类型更改为"commonjs",则可以使用,但这不是我正在寻找的解决方案.

Note: if I change the module type to "commonjs", it works but this not the solution I am looking for.

任何建议?

推荐答案

好吧,由于我使用es2015编译父模块,因此遇到了此错误.如果您使用commonjs,就不会遇到这个问题.

Well, I faced this error since I am using es2015 to compile the parent module. If you use commonjs, you wont face this issue.

您可以通过在karma.config.file中的webpack配置中添加以下内容来转换节点模块代码.

You can transpile the node module code by adding below in webpack configurations in karma.config.file.

webpack: {
            resolve: {
                extensions: ['', '.ts', '.js']
            },

            module: {
                loaders: [
                    {
                        // This will transpile Typescript files
                        test: /\.ts(x?)$/,
                        exclude: /node_modules/,
                        loader: "babel-loader" + "?presets[]=es2015" + "!ts-loader",
                    },
                    {
                        // This will transpile ES2015 javascript files
                        test: /\.js(x?)$/,
                        include: [
                            path.resolve(__dirname, "node_modules/<<YOUR MODULE NAME>>")
                        ],
                        loader: "babel-loader" + "?presets[]=es2015"
                    }
                ]
            }
        },

现在,Webpack将使用babel-loader转换两个JS/TS代码.

Now, Webpack will transpile both JS/TS code using babel-loader.

这篇关于是否可以从node_module转换本地模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆