Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Babel transform plugin not working within NuxtJS #1776

Closed
babakness opened this issue Oct 3, 2017 · 34 comments
Closed

Babel transform plugin not working within NuxtJS #1776

babakness opened this issue Oct 3, 2017 · 34 comments

Comments

@babakness
Copy link

babakness commented Oct 3, 2017

Hello, I'm trying to use the newly approved to Stage-1 pipeline operator in a project.

https://github.com/SuperPaintman/babel-plugin-transform-pipeline

babel/proposals#29

I have the following

babel: {  
      "presets": [
        "es2015",
        "stage-2"
      ],
      "plugins": [
        "espower",
        "transform-runtime",
        "transform-pipeline"
      ]
    }

in nuxt.config.js, .babelrc (without the "babel" key), and package.json. Yet I'm still getting "Syntax Error: Unexpected Token"

Any advise on how to get this working with Nuxt would be appreciated. I've tried restarting the server.

This question is available on Nuxt.js community (#c1596)
@homerjam
Copy link

homerjam commented Oct 3, 2017

Have you tried .babelrc?

@babakness
Copy link
Author

Hi James, yes I've tried all of .babelrc, package.json, and nuxt.config.js

@homerjam
Copy link

homerjam commented Oct 3, 2017

Hmmm. Does nuxt work without the pipeline operator in the code?

@homerjam
Copy link

homerjam commented Oct 3, 2017

How about extend()ing the webpack config?

build: {
    extend(config, ctx) {
      if (ctx.dev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
        });
      }
}

@calebanthony
Copy link

@homerjam is right about the extend(). Just adding in the .babelrc isn't enough, you also have to tell Webpack to use it.

So first make sure your .babelrc is set up. Then, in your nuxt.config.js:

build: {
    extend (config, ctx) {
      if (ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        }, {
          test: /\.js$/,
          loader: 'babel-loader',
          exclude: /(node_modules)/
        })
      }
    }
}

@babakness
Copy link
Author

Yeah, still doesn't work. Tried this:

extend (config, ctx) {
        config.module.rules.push( {
          test: /\.js$/,
          loader: 'babel-loader',
          exclude: /(node_modules)/,
          options: {
            "presets": [
              "es2015",
              "stage-2"
            ],
            "plugins": [
              "espower",
              "transform-runtime",
              "transform-pipeline"
            ]
          }
        })
    }

No luck. The |> operator is still not being interpreted

@homerjam
Copy link

Have you managed to get this working outside of nuxt? Maybe you can pinpoint the problem more easily in isolation.

@christophwolff
Copy link

Any updates on this?

@benmccallum
Copy link

benmccallum commented Dec 25, 2017

Nuxt's webpack build, including babel-loader is configured here:
https://github.com/nuxt/nuxt.js/blob/40aa0ce41e6bae56939a9559aedbaa1ff7c3e879/lib/webpack/base.config.js#L70

It seems to indicate that it doesn't use the .babelrc config file, as it sets option babelrc: false. Instead it uses what's in nuxt.config.js > babel, merged with their default, prioritizing your setting obviously.

Furthermore, there is already a js file matching babel-loader configured. So I'm finding it, then adjusting the existing one based on what it looks like by default in the file I linked above.

Problem I'm having right now is if I include another directory (vue-bootstrap node_module I want to only import in pieces) it's failing on the spread operator in .nuxt/index.js, which is weird cuz that's not a new file I'm including, it's part of nuxt. Argh!

@mr-e-
Copy link

mr-e- commented Jan 25, 2018

I am having the same issue. I have included the babel spread operator in my nuxt.config:

build: { babel: { 'plugins': ['transform-object-rest-spread'] } }

and I still get a compile error Unexpected Token. I too have tried using a .babelrc and I also get errors regarding the spread operator on internal nuxt files. If nuxt is using spread internally then why can't we?

@lenvonsam
Copy link

any solution for babel plugin in nuxt.config.js

@clarkdo clarkdo closed this as completed in b138ffe Mar 9, 2018
@Merovex
Copy link

Merovex commented Mar 24, 2018

I see @clarkdo closed this by changing it to where .babelrc can now be used. However, is this actually fixed?

@mtnptrsn
Copy link

mtnptrsn commented Apr 7, 2018

Im trying to use transform-regenerator and I have the same issue.

@bitsmakerde
Copy link

Hi,

i can't use any babel plugin, in my project. I'm using nuxt-edge with a express server.

What have I do:

nuxt.config.js
`build: {
/*
** You can extend webpack config here
*/
babel: {
plugins: [
["babel-plugin-inline-import"]
],
babelrc: true
},

extend (config, { isDev }) {
if (isDev && process.client) {
config.module.rules.push({
enforce: 'pre',
test: /.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
},`

.babelrc
{ "passPerPreset": true, "presets": [ "react", "es2015", "stage-0" ], "plugins": [ ["babel-plugin-inline-import", { "extensions": [ ".gql", ".graphql" ] }] ] }

Who is my error.

Thank you

@usb248
Copy link

usb248 commented May 29, 2018

I have the same issue ... I don't understand why this issue is closed while nobody can use babel plugin in nuxt ...

@clarkdo
Copy link
Member

clarkdo commented May 29, 2018

Sorry for closing this issue, if you can still reproduce this issue, could you please provide a simple repo ?

And if you're using the custom server like express, nuxt won't transpile the server-side codes, you may need webpack or rollup to build your backend codes.

Another thing is that babelrc in nuxt is false by default, if you're using it, please config it as true

@benmccallum
Copy link

Can you explain what you mean by using the custom server? Are you saying if we pick any of the other server options when we setup with the cli that babel won't execute as part of the build/startup build?

Also, what's the benefits of using a custom server (like express) over the default? I'd definitely just use the default if I didn't need anything else and it was good enough.

@clarkdo
Copy link
Member

clarkdo commented May 29, 2018

I mean if you're using the custom server, the source codes belong to your server, such as router, api, model and so on, they won't be transpiled by nuxt, because these codes are outside the nuxt.

Hope you can understand my poor English, sorry for this.

The benefits of a customer is that you can make your project be a more full functionalities application.
Nuxt is more involved in frontend pages and rendering.
You can implement your complex backend logic (API, router, DB/Cache interaction) with custom server, it also can make you have an organized MVC architecture on the server side.

@janzheng
Copy link

janzheng commented Jun 9, 2018

@clarkdo is right, this is how I got object-spread to work:

First I made sure to install the babel plugins I needed, by running:
npm install --save-dev babel-cli babel-preset-es2015 and
npm install --save-dev babel-plugin-transform-object-rest-spread

Then I added babel to the build section, specifying what presets and plugins to use, and then I added babel-loader like mentioned above:

build: {
    babel: {
      presets: ['es2015', 'stage-2'],
      plugins: ["transform-vue-jsx", "transform-runtime", "transform-object-rest-spread"],
    },
    extend (config, { isDev, isClient }) {
      if (isDev && isClient) {
        config.module.rules.push(
        {
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/
        },{
          test: /\.js$/,
          loader: 'babel-loader',
          exclude: /(node_modules)/
        })
      }
    }
}

Then I also had to add the .babelrc file with all the same plugins specified in there.

Hope this helps!

@benmccallum
Copy link

Definitely good to see someone with success. I haven't really revisited this. But from your example and clarkdo's comments, it seems like that handles transpiration of files in your own app. For me I had an issue getting transpiration occurring on bootstrap JS files in it's node_modules folder, likely because it was being excluded by the default nuxt rule. Im sure it was just a matter of tweaking the extend like you did to setup your own exclude/include and make sure they transpire, I just never had enough time to get it working with what I tried.

@devunion
Copy link

@clarkdo Could you attach your .babelrc file also? So, we will have complete solution in one place.

@clarkdo
Copy link
Member

clarkdo commented Jun 18, 2018

@devunion
The babelrc depends on which feature you want to use in you server code, here is an example:

{
  "presets": ["es2015"],
  "plugins": ["transform-runtime",
    "transform-decorators-legacy",
    "transform-class-properties",
    "transform-object-rest-spread",
    "transform-async-to-generator", ["component", [{
      "libraryName": "element-ui",
      "styleLibraryName": "theme-default"
    }]]
  ],
  "comments": false,
  "env": {
    "development": {
      "retainLines": true,
      "sourceMaps": true,
      "comments": true
    }
  }
}

@rnenjoy
Copy link

rnenjoy commented Jun 23, 2018

Same here, tries to use:

{
  "presets": [
    ["env", {
      "targets": {
        "node": "9.3.0"
      }
    }]
  ],
  "plugins": [
    "babel-plugin-inline-import",
    ["transform-imports", {
      "vuetify": {
        "transform": "vuetify/es5/components/${member}",
        "preventFullImport": true
      }
    }]
  ]
}

with nuxt-edge, but its not respected.

@Atinux Atinux added this to Open in Bugs 🐞 Aug 8, 2018
@clarkdo
Copy link
Member

clarkdo commented Aug 21, 2018

@babakness Since this issue doesn't have any new information for a long time, I'll close it.

Nuxt 2 is going to release and upgrade to babel 7, you can have a glance at nuxt-edge 😸

@clarkdo clarkdo closed this as completed Aug 21, 2018
Bugs 🐞 automation moved this from Open to Fixed Aug 21, 2018
@Merovex
Copy link

Merovex commented Aug 21, 2018

This should not have been closed. The defect remains, and there is an example that appears testable.

@clarkdo clarkdo reopened this Aug 21, 2018
Bugs 🐞 automation moved this from Fixed to Open Aug 21, 2018
@clarkdo
Copy link
Member

clarkdo commented Aug 21, 2018

@menthol Could you provide a repo to reproduce the issue ?

@menthol
Copy link

menthol commented Aug 21, 2018

@clarkdo ???

@clarkdo
Copy link
Member

clarkdo commented Aug 21, 2018

Sorry @Merovex

@shirohana
Copy link

I'm launching a new project with Nuxt, I can use babel-plugin-transform-flow-strip-types in my case. Does it helps?

package.json

  ...
  "dependencies": {
    "nuxt": "^1.4.2"
  },
  "devDependencies": {
    "babel-plugin-transform-flow-strip-types": "^6.22.0",
    "flow-bin": "^0.79.1"
  }
  ...

nuxt.config.js

module.exports = {
  build: {
    babel: {
      plugins: ['transform-flow-strip-types']
    }
  }
}

above: w/o plugins: ['transform-flow-strip-types'] in nuxt.config.js
below: w/ plugins: ['transform-flow-strip-types']

2018-08-27 2 13 19

@clarkdo
Copy link
Member

clarkdo commented Sep 3, 2018

BTW, In Nuxt 1.0, if you want to use .babelrc, I think need to config build.babel.babelrc: true

Now babel-loader has changed the logic, we can also use extend for babelrc file
Some thing like : build.babel.extends: path.join(__dirname, "fixtures/babelrc"),

https://github.com/babel/babel-loader/blob/master/src/index.js#L70-L73

@JkmAS
Copy link

JkmAS commented Sep 18, 2018

I have the same issue. I want to use babel-plugin-root-import for server side in nuxt-edge to support ~/path/file imports. But it seems, that the babelrc is ignored.

@stale
Copy link

stale bot commented Oct 31, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Oct 31, 2018
@manniL
Copy link
Member

manniL commented Nov 2, 2018

An interactive CodeSandbox that shows how to use the pipeline-proposal babel plugin with Nuxt 2 can be found here.

@manniL manniL closed this as completed Nov 2, 2018
Bugs 🐞 automation moved this from Open to Fixed Nov 2, 2018
@lock
Copy link

lock bot commented Dec 12, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 12, 2018
@danielroe danielroe added the 2.x label Jan 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Bugs 🐞
  
Fixed
Development

No branches or pull requests