Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore non-js files with babel/register

When my Node app includes the routes from my App I want to ignore non-js files e.g.

import './scss/App.scss' // i.e via Router.js -> Routes.js -> App.js

At the moment Node is obviously throwing a parse error as it's trying to parse the scss as js. The babel hook currently looks like this:

require('babel/register')({
    stage: 0
});

Any ideas how I can make babel ignore them? Thanks

like image 789
Dominic Avatar asked Oct 24 '15 23:10

Dominic


People also ask

Why use Babel register?

The purpose of babel is to transpile your js current code to an understandable version of js for the given environment, tool, framework you're using.

Can I use require in Babel?

One of the ways you can use Babel is through the require hook. The require hook will bind itself to node's require and automatically compile files on the fly. This is equivalent to CoffeeScript's coffee-script/register.

What is Babel choose the correct option?

Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.

What is Babel RC?

The . babelrc file is your local configuration for your code in your project. Generally you would put it in the root of your application repo. It will affect all files that Babel processes that are in the same directory or in sibling directories of the . babelrc .


3 Answers

You can use babel-plugin-transform-import-ignore to ignore matched imports. All matched import statements will be removed after compilation.

{
  plugins: [
    [
      "babel-plugin-transform-import-ignore",
      {
        patterns: [".scss"]
      }
    ]
  ]
}

like image 189
hueyhe Avatar answered Oct 13 '22 03:10

hueyhe


You can create .babelrc file and set ignore rule:

{
  "stage": 0,
  "ignore": [
    "*.scss"
  ]
}

You can read more about this here - https://babeljs.io/docs/usage/babelrc/

like image 26
Eugene Obrezkov Avatar answered Oct 13 '22 02:10

Eugene Obrezkov


Turns out it was node that needed the tweak:

require.extensions['.scss'] = () => {};
like image 33
Dominic Avatar answered Oct 13 '22 01:10

Dominic