Giter Site home page Giter Site logo

import-export's People

Contributors

macmee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

import-export's Issues

Can produce hard-to-understand bugs when it transforms imported modules

Hi @Macmee,

I saw this on /r/node and thought it seems useful. Thanks for making it!

I wanted to suggest using a proper parser, rather than regex-based replacing, to rewrite the source modules as they are loaded. The reason is that the regexes will replace matching text even when not appropriate, such as inside strings, regexes, and comments. If you'd rather keep it simple, then perhaps put a warning in the README about this behaviour as it would be hard to debug for someone hit by it.

As an example of what I mean, if you use this library to import itself, it will make replacements in the index.js that mess up the regexes resulting in a mysteriously non-working module whose source becomes (note 4 out of 6 regexes are corrupted):

var hook = require('node-hook');

var hook = { hook(...args) { } };
hook.hook('.js', (src, name) => {
    src = src.replace(/import ([^{]*?) from '(.*?)'/g, 'const $1 = require("$2")');
    src = src.replace(/module.exports = ([^ ]*)/g, 'module.exports = $1');
    src = src.replace(/export (var|let|const) ([a-zA-Z0-9_$]*)/g, '$1 $2 = module.exports.$2');
    src = src.replace(/var  = module.exports. = function([a-zA-Z0-9_$]*)/g, 'var $1 = module.exports.$1 = function');
    src = src.replace(/var  = module.exports. = class([a-zA-Z0-9_$]*)/g, 'var $1 = module.exports.$1 = class');
    src = src.replace(/var (.*?)= require("(.*?)").(.*?);/g, (all, $1, $2) => {
        return $1.split(",")
            .map(part => 'var ' + part + '= require("' + $2 + '").' + part.trim() + ';')
            .join('');
    });
    return src;
});

Issue during transform

Howdy, thanks for this library. Is really great.

I ran into something the other day that i isolated related to the use of the apollo-link library along with import-export

Gist is that if someone has a comment or throws an error that includes a string with import X from y it will be transformed and it can lead to some relatively hard to track down issues.

On my end we have moved to commonjs, so no need to fix, more just wanted to let you know in case still actively looking to develop anything.

Full details
apollographql/apollo-link#614

Thanks!

Document and/or fix ES6 Module incompatibilities

Hi @Macmee,

I have another suggestion. Looking at the replacements the hook function makes, I see a few incompatibilities/omissions compared to how 'real' ES6 modules would behave. You've probably already come across the MDN import and export pages, and Axel Rauschmayer eBook. Perhaps you could list which syntax is supported/unsupported in your README.

  • export default X is not the same as module.exports = X. It actually creates an export named 'default'.
  • import X from './X' is not the same as const X = require('./X'), it's more like const X = require('./X').default (but see next point)
  • exported values can be modified by their exporting module, and all import sites will instantly see the updated value. E.g. if module A contains export let x = 1; setTimeout(() => ++x, 1000);, and module B has import {x} from './A', then the x variable in module B will change form 1 to 2 after a second. This allows ES6 modules to support circular references.
  • ES6 imports are hoisted.
  • various ES6 syntaxes appear not to be supported here, e.g.:
    • import {foo as myFoo} from './module'
    • import './module'
    • import * as fs from 'fs'
    • import main, {other, another} from './module'
    • export default (/* absolutely any expression */)
    • export {foo, bar}
    • export {x} from './module'
    • export {x as default} from './module'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.