How to use webpack bundle only node_modules?

Viewed 3256

I want to bundle my vue ssr server.js, include all node_modules into one file

I have already build my server and client codes into dist/ folder

Now, I want to build codes in docker images

I don't want to include node_modules folder in final images(too big)

So I decide to bundle my server.js

After bundled, I will remove node_modules in final images.

But in my server.js

const express = require('express')
const bundle = require('./dist/vue-ssr-server-bundle.json')

It require my dist/ js , I don't want webpack bundle dist/ folder content only node_modules like express

only bundle node_modules

after a long time wait

I received

Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Here are my webpack config

const path = require('path')

const resolve = (...dir) => path.resolve(...dir)

module.exports = {
    entry: resolve(__dirname, '../server.js'),
    output: {
        filename: 'bundled-server.js',
        path: resolve(__dirname, '../')
    },
    mode: 'production',
    target: 'node',
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
        alias: {
        }
    },
    module: {
        rules: [
            {
                use: 'babel-loader',
                test: /\.js$/,
                exclude: /node_module/
            },
        ]
    },
}

My folder structure

enter image description here

My question

How to let webpack only bundle node_modules and server.js, except my dist/ folder?

Sorry for my bad English

1 Answers

Webpack bundles all your source code and puts it into the dist folder. When you build a final Docker image out of it, you want to ignore the node_modules and other unnecessary directories. This is where the .dockerignore file comes to your aid. Just like .gitignore, .dockerignore ignores files during build process. Mention the files you want to ignore in the .dockerignore file.

Related