antfu / vite-node

Vite as Node.js runtime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[question] How to use vite to bundle node project to a file without clearing node build-in module?

radiorz opened this issue · comments

I can use webpack4 to pack the node project to one file like this :

'use strict';

const path = require('path');
let externals = _externals();

module.exports = {
  mode: 'production',
  entry: {
    app: './index.js'
  },
  target: 'node',
  output: {
    path: path.resolve('./build'),
    filename: '[name].cjs'
  },
  resolve: {
    extensions: ['.js']
  },
  externals: externals,
  node: {
    console: true,
    global: true,
    process: true,
    Buffer: true,
    __filename: true,
    __dirname: true,
    setImmediate: true
  },
  module: {},
  plugins: []
};

function _externals() {
  let manifest = require('./package.json');
  let dependencies = manifest.dependencies;
  let externals = {};
  for (let p in dependencies) {
    externals[p] = 'commonjs ' + p;
  }
  return externals;
}

How to build by vite or vite-node?

This is out-of-scope of this project.