rollup / rollup-plugin-babel

This package has moved and is now available at @rollup/plugin-babel / https://github.com/rollup/plugins/tree/master/packages/babel

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Plugin breaks if file does not end with ".js"

felix-roehrich opened this issue · comments

As the title says this plugin breaks if the input file of rollup does not end with ".js".
(At least I think the issue is within the plugin, as other plugins seem to work fine. )
This issue mainly affects people who use Typescript.
Minimal example:

// test.ts & test.js
console.log([1,2,3].map(n => n**2));
// rollup.js
let rollup = require("rollup");
let babel = require('rollup-plugin-babel');
let typescript = require('rollup-plugin-typescript');
rollup.rollup({
    input: "test.ts",
    plugins: [
        typescript({ abcdef: "abc", }),
    ],
}).then(bundle => bundle.write({
    file: "test.min.js",
    format: "iife",
}));

What should happen is that "babel" throws no error, but "typescript" does.
This shows that the error is exclusive to this plugin.

I also tried it directly with Babel and it will work as expected:

let B = require('@babel/core');
B.transformFile("test.ts", { presets: [["@babel/preset-env", { debug: true, } ]] }, function(err, res) {
   console.log(err || res.code);
});

Rather than breaking the plugin, after some more testing, it seems that the plugin simply skips/ignores ".ts" files / files not ending with ".js".

Yes - this is how Babel works. It only handles certain extensions by default but you can opt-in to more with extensions option described here https://github.com/rollup/rollup-plugin-babel#usage

I know, but that does not seem to work for me. I will upload a minimal package later, when I am back home (I used include: ["*.ts", "*.js"] and it still does not work ".ts" files).
As a side note, what I intended to tell you was that if you pass ".ts" files to Babel directly it will not ignore them, but I do not know how you call Babel internally.

I used include: [".ts", ".js"] and it still does not work ".ts" files

You actually have to use extensions option to opt into other extensions - include rule is not enough.

As a side note, what I intended to tell you was that if you pass ".ts" files to Babel directly it will not ignore them, but I do not know how you call Babel internally.

Interesting. Would love to see a repro - to check out how do you call Babel etc. AFAIR it shouldn't compile it - unless you maybe pass a single file directly to it.

You actually have to use extensions option to opt into other extensions - include rule is not enough.

Thank you. I wonder how I managed to overlook this option.

Interesting. Would love to see a repro - to check out how do you call Babel etc. AFAIR it shouldn't compile it - unless you maybe pass a single file directly to it.

From https://babeljs.io/docs/en/babel-preset-typescript:

You will need to specify --extensions ".ts" for @babel/cli & @babel/node cli's to handle .ts files.

and from https://babeljs.io/docs/en/babel-core#default_extensions:

A list of default extensions supported by babel (".js", ".jsx", ".es6", ".es", ".mjs"). This list is used by @babel/register and @babel/cli to determine which files need transpiling. Extending this list isn't possible, however @babel/cli does provide ways to support other extensions with --extensions.

But there is no mention of @babel/core and transformFile(Sync|Async) are the only functions that take an input file.

Here is the demo repo.

From looking at the source code I understand that you use DEFAULT_EXTENSIONS to generate an extension filter. I think it would be helpful to generate a warning if files get skipped by the plugin (only when the extensions option is not used).
Even if people do not overlook extensions options (like me), by silently skipping files there is a high chance that forgetting to set the option will not be noticed.

Problem is that skipping files is not necessarily a problem and it's hard for this plugin to know that. We don't want to touch asset files or files written entirely with a different language (like reasonml) - arguably we could skip non-JS (and TS extensions to achieve that). The bigger problem is that there are setups with both JS and TS files and not everyone wants to compile TS with Babel - it's perfectly valid to handle TS with typescript compiler and JS files with Babel, or even preprocess TS with typescript and only then handle the output to Babel (latter would require typescript plugin to be put before babel anyway - so not that of a problem, but the first scenario is valid)

I don´t use Babel to compile TS. My setup looks like this:
TS plugin, node-resolve plugin, babel plugin, CJS plugin, terser plugin.
But since the original files have the .ts extension they get ignored.

I understand what you mean and that´s why I said this plugin could generate a warning (not an error) and only if extensions option is not used.
So specifying the extensions is like saying, I know what I am doing and am ignoring the other extensions on purpose.
To reduce unnecessary warnings, you could only issue them for files which can be compiled to plain JS.

I'll give this a thought - but for now, it doesn't seem to be crucial. Going to close this now, because I'm cleaning the issues to migrate this plugin to Rollup plugins monorepo ( https://github.com/rollup/plugins ). Once that happens - this can be raised once again there. Hope you understand.