title: Using babel, how can I append some code to the top of every file?
body: <p>My goal is to fake out getting some requirejs code working via babel. I've found that if I add the following: <code>if (typeof define !== "function") { var define = require("amdefine")(module); }</code> to the top of every file while running in nodejs things seem to work out.</p>
  
  <p>Here is some code I wrote, which I thought would work or nearly work:</p>
  
  <pre><code>function injectDefine(babel) {
      var header = 'if (typeof define !== "function") { var define = require("amdefine")(module); }';
  
      return new babel.Plugin('amdefine', {
          visitor: {
              Program: {
                  enter: function(path, file) {
                      path.unshiftContainer(
                          'body',
                          babel.types.expressionStatement(
                              babel.types.stringLiteral(header)
                          )
                      );
                  },
              },
          },
      });
  }
  
  require('babel-core/register')({
      stage: 0,
      plugins: [{transformer: injectDefine}],
  });
  
  require('../components/button');
  </code></pre>
  
  <p>The <code>components/button</code> file is just me trying to test that some file can load.</p>
  
  <p>Other notes: I'm using babel 5, and I can't upgrade right now. I also can't use a <code>.babelrc</code> very easily right now.</p>