airbnb / hypernova

A service for server-side rendering your JavaScript views
MIT License
5.82k stars 212 forks source link

[Module] `require` will be compiled to `__webpack_require__` by webpack #55

Closed loatheb closed 7 years ago

loatheb commented 7 years ago

when I'm using webpack to bundle the project code into one file. I find the code require in src/module will be compiled into __webpack_require__. so that I can't load the native module from node in the vm context.

code in lib/Module.js

{
    key: 'loadFile',
    value: function () {
      function loadFile(file, parent) {
        var filename = _module2['default']._resolveFilename(file, parent);

        if (parent) {
          var cachedModule = parent.cache[filename];
          if (cachedModule) return cachedModule.exports;
        }

        if (isNativeModule(filename)) {
          // eslint-disable-next-line global-require
          return require(filename);
        }
}

my project code use createVM, some sample code like this:

import * as hypernova from 'hypernova/server'
const createVM = hypernova.createVM

const vm = createVM(Object.assign({}, {
  cacheSize: 100,
}, vmOptions))

vm.run(name, `my code here`)

As a result. the code in Module.js will be bundled by webpack like this

{
    key: 'loadFile',
        value: function () {
      function loadFile(file, parent) {
        var filename = _module2['default']._resolveFilename(file, parent);

        if (parent) {
          var cachedModule = parent.cache[filename];
          if (cachedModule) return cachedModule.exports;
        }

        if (isNativeModule(filename)) {
          // eslint-disable-next-line global-require
          return __webpack_require__(187)(filename);
        }
}

so that. if I require some native module in vm.run. It cannot resolved by webpack

if I change the code in the src/Module.js then build and bundle.The result workd well.

if (isNativeModule(filename)) {
  // eslint-disable-next-line global-requir
  return eval("require")(filename);
}

Any suggestion?

ljharb commented 7 years ago

I'm confused; the vm part of hypernova is server code; webpack is for browser code. Why are you bundling the server part?

loatheb commented 7 years ago

I just want to bundle both server code into one file.that makes me easier to deploy.

ljharb commented 7 years ago

Typically that's done by using tar and deploying an archive, and extracting it on the server. Bundling up server code is not what I'd consider a supported use case, and it's not the purpose of webpack.

loatheb commented 7 years ago

Thanks for replying.I think you're right.

But I want to deploy these code to the AWS Lambda Function. So that, If I don't bundle both file, I have to deploy the node_modules folder on the server.The file size is very large, It makes me uncomfortable every time I deploy it.

So that I use webpack to bundle it, to reduce the size of the file at each deploy.

ljharb commented 7 years ago

Run npm prune --production before deploying it; also, you could use rsync over scp so that you're only copying changes.

loatheb commented 7 years ago

Thank you. Much useful.