How can I convert a multi-file node.js app to a single file?

后端 未结 4 1685
醉酒成梦
醉酒成梦 2020-12-31 00:16

If I have a node.js application that is filled with many require statements, how can I compile this into a single .js file? I\'d have to manually r

4条回答
  •  暖寄归人
    2020-12-31 01:05

    If you want to send common code to the browser I would personally recommend something like brequire or requireJS which can "compile" your nodeJS source into asynchronously loading code whilst maintaining the order.

    For an actual compiler into a single file you might get away with one for requireJS but I would not trust it with large projects with high complexity and edge-cases.

    It shouldn't be too hard to write a file like package.json that npm uses to state in which order the files should occur in your packaging. This way it's your responsibility to make sure everything is compacted in the correct order, you can then write a simplistic node application to reads your package.json file and uses file IO to create your compiled script.

    Automatically generating the order in which files should be packaged requires building up a dependency tree and doing lots of file parsing. It should be possible but it will probably crash on circular dependencies. I don't know of any libraries out there to do this for you.

提交回复
热议问题