-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Build for production? #20
Comments
if possible, I wish the vite could build nodejs to one file like webpack do. '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;
} |
import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';
import { resolve } from 'path';
export default defineConfig({
root: 'src',
server: {
port: 3000,
},
build: {
ssr: 'index.ts',
outDir: '../api',
emptyOutDir: true
},
plugins: [
...VitePluginNode({
adapter: 'express',
appPath: './src/index.ts',
tsCompiler: 'esbuild',
}),
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
},
}); {
...
"scripts": {
"dev": "vite",
"build": "vite build"
},
...
}
yarn build or npm run build |
I just released 0.0.17 to npm, please check it out. Thanks @taka1156 for providing this solution. I added this ssr config to the plugin default, the plugin will just use the simply add a build script to your package.json, it should just work! |
@axe-me [bug] |
import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';
import { resolve } from 'path';
export default defineConfig({
root: 'src',
server: {
port: 3000,
},
build: {
ssr: true,
outDir: '../api',
emptyOutDir: true
},
plugins: [
...VitePluginNode({
adapter: 'express',
appPath: process.env.NODE_ENV ==='production' ? 'index.ts' : 'src/index.ts',
tsCompiler: 'esbuild',
}),
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
},
}); https://github.com/taka1156/vite-express-sample/tree/update-vite-plugin-node |
thanks, this is a way to solve my problem. |
I think we need to update the README |
import { Plugin, UserConfig, ConfigEnv } from 'vite';
import { PLUGIN_NAME, ViteConfig, VitePluginNodeConfig } from '.';
import { RollupPluginSwc } from './rollup-plugin-swc';
import { createMiddleware } from './server';
export function VitePluginNode(cfg: VitePluginNodeConfig): Plugin[] {
const config: VitePluginNodeConfig = {
appPath: cfg.appPath,
adapter: cfg.adapter,
tsCompiler: cfg.tsCompiler ?? 'esbuild',
exportName: cfg.exportName ?? 'viteNodeApp'
};
const resolvePath = (root: string = '', config: VitePluginNodeConfig): VitePluginNodeConfig => {
if (root !== '') {
config.appPath = `${root}/${config.appPath}`;
}
return config;
};
const resolveConfig = (viteConfig: UserConfig, env: ConfigEnv ): ViteConfig => {
const { root, build } = viteConfig;
const { appPath } = config;
if (env.mode === 'serve') {
// serve
return {
// https://vitejs.dev/config/#root
root: root || process.cwd(),
server: {
hmr: false
},
esbuild: config.tsCompiler === 'esbuild' ? {} : false,
VitePluginNodeConfig: resolvePath(root, config),
};
} else {
// build
return {
// https://vitejs.dev/config/#root
root: root || process.cwd(),
build: {
ssr: appPath || 'index.js', // production entry point
outDir: build?.outDir || '../dist',
emptyOutDir: build?.emptyOutDir|| true,
},
esbuild: config.tsCompiler === 'esbuild' ? {} : false,
VitePluginNodeConfig: resolvePath(root, config)
};
}
};
const plugins: Plugin[] = [
{
name: PLUGIN_NAME,
config: (viteConfig, env) => resolveConfig(viteConfig, env),
configureServer: (server) => {
server.middlewares.use(createMiddleware(server));
},
apply: 'serve'
},
{
name: PLUGIN_NAME,
config: (viteConfig, env) => resolveConfig(viteConfig, env),
apply: 'build'
}
];
if (config.tsCompiler === 'swc') {
plugins.push({
...RollupPluginSwc({
jsc: {
target: 'es2019',
parser: {
syntax: 'typescript',
decorators: true
},
transform: {
legacyDecorator: true,
decoratorMetadata: true
}
}
})
});
}
return plugins;
} @axe-me |
Hi. I've tried your plugin, as it seems my last chance of making my setup working. It seems really great, but I am not sure to understand how to use it for production.
I have a fairly sophisticated express.js-based backend, and I try to build into a custom folder
dist-prod
that I deploy to heroku. But I can't find how to runvite build
and end up with the correct output.As far as I understand, your plugin isn't ready to build for production, right? Anything I can do to help? Thanks a lot.
The text was updated successfully, but these errors were encountered: