Skip to content

Instantly share code, notes, and snippets.

@jgcmarins
Forked from sibelius/webpack.config.js
Created April 28, 2020 13:20
Show Gist options
  • Save jgcmarins/2aec03846d6896e0e4f85d15cb04b202 to your computer and use it in GitHub Desktop.
Save jgcmarins/2aec03846d6896e0e4f85d15cb04b202 to your computer and use it in GitHub Desktop.
run node files using webpack
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const cwd = process.cwd();
export const outputPath = path.join(cwd, '.webpack');
export const outputFilename = 'bundle.js';
export default {
context: cwd,
mode: 'development',
devtool: false,
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json', '.mjs'],
},
output: {
libraryTarget: 'commonjs2',
path: outputPath,
filename: outputFilename,
futureEmitAssets: true,
},
target: 'node',
externals: [
nodeExternals(),
nodeExternals({
modulesDir: path.resolve(__dirname, '../node_modules'),
whitelist: [/@feedback/],
}),
],
module: {
rules: [
{
test: /\.mjs$/,
type: 'javascript/auto',
},
{
test: /\.(js|jsx|ts|tsx)?$/,
use: {
loader: 'babel-loader?cacheDirectory',
},
exclude: [/node_modules/, path.resolve(__dirname, '.serverless'), path.resolve(__dirname, '.webpack')],
},
],
},
plugins: [],
node: {
__dirname: false,
__filename: false,
fs: 'empty',
},
};
import path from 'path';
import { spawn } from 'child_process';
import webpack from 'webpack';
import config, { outputPath, outputFilename } from './webpack/webpack.config';
const compilerRunPromise = (compiler) => new Promise((resolve, reject) => {
compiler.run((err, stats) => {
if (err) {
return reject(err);
}
if (stats && stats.hasErrors()) {
reject(err || stats.toString());
}
resolve(stats);
});
});
const runProgram = () => {
const outputFile = path.join(outputPath, outputFilename);
const program = spawn(process.execPath, [outputFile], {
std: 'inherit',
shell: true,
});
program.stdout.on('data', (data) => {
// eslint-disable-next-line
console.log(data.toString());
});
program.stderr.on('data', (data) => {
// eslint-disable-next-line
console.log(data.toString());
});
program.on('exit', (code) => {
process.exit(code);
});
};
(async () => {
try {
const wpConfig = {
...config,
entry: path.join(__dirname, process.argv[2]),
};
const compiler = webpack(wpConfig);
await compilerRunPromise(compiler);
runProgram();
} catch (err) {
// eslint-disable-next-line
console.log('err: ', err);
process.exit(1);
}
process.exit(0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment