简体   繁体   English

Nuxt 2.x:添加 babel 的转换插件,适用于导入的 node_module 文件

[英]Nuxt 2.x: Add babel's transform plugin, that work on imported node_module file

How to add @babel/plugin-proposal-optional-chaining plugin to .nuxt.config that work for a file imported from node_modules (a one that not pre-build )如何将@babel/plugin-proposal-optional-chaining插件添加到适用于从.nuxt.config导入的文件的node_modules (不是预构建的)

By this Example here I do:通过这个例子我这样做:

export default {
  plugins: [
        '~/plugins/accesso.js' // that one make the trouble
  ],
  build: {
   babel: {
     plugins: [
        ['@babel/plugin-proposal-optional-chaining', {loose: true}]
     ]
   }
 }
}
// ~/plugins/accesso.js
import Accesso from '@perymimon/accesso'

export default function (ctx, inject) {
    inject('Accesso', accesso)
}

And it works fine for some foo?.bar in .vue files but not for files that come from node_module .它适用于.vue文件中的某些foo?.bar ,但不适用于来自node_module的文件。

why?为什么?

The eror is reguar error from webpack错误是来自 webpack 的经常性错误

Module parse failed: Unexpected token (311:25)                                                                                                                                                 friendly-errors 14:39:06
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|
| function frame$extractPayload(frame, authSetting) {
>     let location = frame?.contentWindow?.location || frame.location;
|     const {protocol, host, pathname} = location;
|     const atHome = protocol + '//' + host + pathname === authSetting.redirect_uri;
                                                                                                                                                                                               friendly-errors 14:39:06
 @ ./node_modules/@perymimon/accesso/src/providers.js 1:0-37 3:0-29 3:0-29 8:22-29 34:23-30 51:22-29
 @ ./plugins/accesso.js
 @ ./.nuxt/index.js
 @ ./.nuxt/client.js
 @ multi eventsource-polyfill webpack-hot-middleware/client?reload=true&timeout=30000&ansiColors=&overlayStyles=&name=modern&path=/__webpack_hmr/modern ./.nuxt/client.js

I'm speculating that it may be that the dependencies in question are not already transpiled.我推测可能是相关依赖项尚未转译。

In vue.config.js you can specify an array of dependencies that should be transpiled by the project.vue.config.js ,您可以指定项目应转换的依赖项数组。

vue.config.js:

module.exports = {
  transpileDependencies: ['tomsNodeModule'],
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM