brfs icon indicating copy to clipboard operation
brfs copied to clipboard

Error combining with 6to5ify

Open akulapid opened this issue 10 years ago • 12 comments

I'm trying to do a brfs transform after 6to5ify, but get the following error.

browserify()
    .require("main", { entry: true })
    .transform(6to5ify)
    .transform(brfs)
    .bundle()
    .pipe(fs.createWriteStream(path.join('app.js')));
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: tried to statically call { readFile: [Function: readFile], readFileSync: [Function: readFileSync], readdir: [Function: readdir], readdirSync: [Function: readdirSync] } as a function while parsing file: /Users/akula/foo/main.js while parsing file: /Users/akula/foo/main.js
    at error (/Users/akula/foo/node_modules/brfs/node_modules/static-module/index.js:71:49)
    at traverse (/Users/akula/foo/node_modules/brfs/node_modules/static-module/index.js:247:24)
    at walk (/Users/akula/foo/node_modules/brfs/node_modules/static-module/index.js:216:13)
    at walk (/Users/akula/foo/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:60:9)
    at /Users/akula/foo/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:51:25
    at Array.forEach (native)
    at forEach (/Users/akula/foo/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:8:31)
    at /Users/akula/foo/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:49:17
    at Array.forEach (native)
    at forEach (/Users/akula/foo/node_modules/brfs/node_modules/static-module/node_modules/falafel/index.js:8:31)

btw, there's a double "while parsing file" in the message.

My main.js looks like this.

import fs from 'fs';

var foo = fs.readFileSync(__dirname + '/foo.html');
console.log(foo.toString());

If I change the import to a require and don't do 6to5ify it works fine, but together it gives the error.

akulapid avatar Feb 01 '15 15:02 akulapid

Ack! The same problem with [email protected] and [email protected] on [email protected] for Linux.

erykpiast avatar Feb 05 '15 22:02 erykpiast

But it works perfectly fine when used like

var fs = require('fs');
var content = fs.readFileSync(__dirname + '/file', 'utf8');

I don't have to disable 6to5.

erykpiast avatar Feb 05 '15 22:02 erykpiast

Faced this problem as well.

vegetableman avatar Feb 28 '15 01:02 vegetableman

I'm guessing this is down to how Babel (was: 6to5) transpiles ES6 imports. This:

import fs from 'fs'

Becomes this:

var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; };

var fs = _interopRequire(require("fs"));

If you want to go this route, resorting to a regular require call works fine:

var fs = require('fs');

danharper avatar Mar 05 '15 14:03 danharper

Just hit that, too.

nicroto avatar Mar 18 '15 09:03 nicroto

agh

lambdabaa avatar Apr 08 '15 19:04 lambdabaa

:+1:

dustinmoorenet avatar Apr 08 '15 21:04 dustinmoorenet

I think this could be handled upstream in static-module adding support for ES6 imports (not as much of a problem adding them here than to browserify itself since the node semantics haven't been hammered out). But then brfs should come before 6to5ify/babelify in the transform list.

ghost avatar Apr 09 '15 09:04 ghost

looks like static-module has been updated with ES6 support ? https://github.com/substack/static-module/commit/2eb7ce0d8d42176e1537fd034be2946b9db0920c

revolunet avatar May 28 '15 15:05 revolunet

I'm using brfs 1.4.1 which pulls in static-module 1.1.3 and still can't get brfs to work happily with ES6/babelify. Using same workaround as others for the moment, var fs = require('fs').

jedrichards avatar Sep 16 '15 13:09 jedrichards

@jedrichards after some time using import for importing CommonJS modules I realized it’s really a kind of hack. fs is a CommonJS module and const fs = require('fs') seems the right thing to do to me.

import is intended for real modules which use export for exporting things. And fs is not one of these yet.

tomek-he-him avatar Sep 16 '15 14:09 tomek-he-him

Tou say "import is intended for real modules which use export for exporting things. And fs is not one of these yet."

Over the last few days, I have created and imported modules that use both the module.exports and the plain export idiom. I have working modules that import one of each.

Mixed_Imports.zip contains GridProperties.js, which imports both CommonConstants.js, which uses the plain export idiom, and PropertyDisplayHelpers.js, which uses the module.exports idiom.

txwizard avatar Apr 24 '18 05:04 txwizard