-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.config.js
More file actions
68 lines (63 loc) · 2.29 KB
/
webpack.config.js
File metadata and controls
68 lines (63 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const path = require('path');
const fs = require('fs');
const Dotenv = require('dotenv-webpack');
const { DefinePlugin } = require('webpack');
const ShebangPlugin = require('webpack-shebang-plugin');
module.exports = (env) => {
const environment = env.ENV || 'production'; // Use the environment passed during the build
// Determine the correct .env file to load in order of priority
const basePath = `.env.${environment}`;
const localPath = `${basePath}.local`;
const defaultPath = `.env`;
// Check which file exists with priority for `.env`
const envPath = fs.existsSync(defaultPath)
? defaultPath
: fs.existsSync(localPath)
? localPath
: fs.existsSync(basePath)
? basePath
: null;
console.log(`Building using environment file: ${envPath || 'No .env file found'}`);
return {
mode: environment === 'production' ? 'production' : 'development',
devtool: environment === 'production' ? 'source-map' : 'inline-source-map',
entry: {
'cli': './src/cli.ts',
'version-check': './src/version-check.ts'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
clean: true
},
resolve: {
extensions: ['.ts', '.js'], // Resolve these extensions
},
module: {
rules: [
{
test: /\.ts$/, // Match TypeScript files
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
optimization: {
minimize: false,
},
plugins: [
new ShebangPlugin(),
new Dotenv({
path: envPath || false,
systemvars: true, // Load system environment variables as well
safe: false, // Don't require an .env.example file
defaults: false, // Don't load .env.defaults
}),
new DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(environment),
'process.env.CLI_VERSION': JSON.stringify(require('./package.json').version),
}),
],
target: 'node', // Set for CLI/Node.js applications
};
};