title: With webpack, can I put Node.js and browser code in the same file, but prevent the Node.js code making it to the browser?
body: <p>I have a file that I want to have both Node.js and browser code.</p>
  
  <p>Node.js code should not be visible in the browser.</p>
  
  <p>Can webpack exclude code from a file depending on the environment?</p>
  
  <p>Kind of like C preprocessing directives. <code>#if #else, etc</code>.</p>
  
  <p>I know that Webpack parses the code quite intensively which makes me think it can.</p>
  
  <p><strong>Constraints</strong></p>
  
  <ul>
  <li><p>The Node.js code cannot make it to the browser because it might contain sensitive config options.</p></li>
  <li><p>I want the browser and Node code to be in the same file. I know that I could use the <code>webpack.IgnorePlugin</code> to ignore requires on a file basis, or put the shared code in the browser file and require that from the server (which I am doing now), but I want to know if its possible to have the code side by side in the same file.</p></li>
  </ul>
  
  <hr>
  
  <p>EDIT: Here is an example of what I'm trying to do.</p>
  
  <pre><code>export class FooPlugin {
  
    $name = 'foo-plugin'
  
    register(app) {
  
      const app = koa()
      app.get('/foo', () =&gt; { this.send('foo') })
      app.set('SECRET STUFF!')
      app.start()
      this.shared()
  
    }
  
    shared(app) {
  
        // browser things - isomorphic react router routes, etc.
  
    }
  
    registerBrowser(app) {
  
        this.shared()
  
    }
  
  }
  
  // This is called in another file depending on the environment.
  
  if (!isBrowser) {
    (new FooPlugin).register(app)
  } else {
    (new FooPlugin).registerBrowser(app)
  }
  </code></pre>