question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

process.env and webpack

See original GitHub issue

we have the same problem with the process.env and webpack. Webpack replaces process.env.VAR with value and can’t do that properly when it obfuscate to process[_0x3ab67a(0x74)][_0x3ab67a(0x6b)]);

Expected Behavior

process.env.VAR is not replaced

Current Behavior

process.env.VAR is replaced with process[_0x3ab67a(0x74)][_0x3ab67a(0x6b)])

Steps to Reproduce

  1. create a package with contains process.env.VAR
  2. obfuscate the new package
  3. include a new package in webpack project
  4. 😦

Temp solution

For now, we have to use

/* javascript-obfuscator:disable */
const VAR = process.env.VAR;
/* javascript-obfuscator:enable */

Proper solution

create an option to not obfuscate process.env

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
TrejGuncommented, Sep 22, 2022
import path from "path";
import { Configuration } from "webpack";
import DotEnvPlugin from "dotenv-webpack";
import CopyPlugin from "copy-webpack-plugin";
import ReactRefreshWebpackPlugin from "@pmmmwh/react-refresh-webpack-plugin";
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import ReactRefreshTypeScript from "react-refresh-typescript";
import "webpack-dev-server";

const config: Configuration = {
  mode: "development",
  devtool: "cheap-module-source-map",
  entry: {
    main: ["./src"],
  },
  output: {
    path: path.join(__dirname, "dist"),
    filename: "[name].js",
    sourceMapFilename: "[file].map",
    chunkFilename: "[id].js",
    publicPath: "/",
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".jsx", ".json"],
    modules: ["node_modules"],
    fallback: {
      path: require.resolve("path-browserify"),
    },
  },
  module: {
    rules: [
      {
        test: /\.(ttf|woff|woff2|eot|svg|gif|png|ico)(\?.+)?$/,
        use: [
          {
            loader: "file-loader?name=[name].[ext]?[hash]",
          },
        ],
      },
      {
        test: /\.[tj]sx?$/,
        exclude: [/node_modules/],
        use: [
          {
            loader: "ts-loader",
            options: {
              getCustomTransformers: () => ({
                before: [ReactRefreshTypeScript()],
              }),
              transpileOnly: true,
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new DotEnvPlugin({
      path: `.env.${process.env.NODE_ENV as string}`,
      systemvars: true,
    }),
    new CopyPlugin({
      patterns: [{ from: "./static", to: "./" }],
    }),
    new ReactRefreshWebpackPlugin(),
    new ForkTsCheckerWebpackPlugin(),
  ],
  performance: {
    hints: false,
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          name: "vendors",
          chunks: "all",
          enforce: true,
        },
      },
    },
  },
  watchOptions: {
    aggregateTimeout: 0,
  },
  devServer: {
    static: {
      directory: path.join(__dirname, "static"),
    },
    compress: true,
    port: 3002,
    historyApiFallback: {
      index: "index.html",
    },
  },
};

export default config;
0reactions
fanggangWebcommented, Oct 25, 2022

process.env.NODE_ENV confused, run with error

Read more comments on GitHub >

github_iconTop Results From Across the Web

EnvironmentPlugin - webpack
The EnvironmentPlugin is shorthand for using the DefinePlugin on process.env keys. Usage. The EnvironmentPlugin accepts either an array of keys or an object ......
Read more >
Using environment variables with Webpack - Prateek Surana
A guide for setting up and using environment variables with Webpack and handling different values for Production and Development ...
Read more >
Passing environment-dependent variables in webpack
I have 2 webpack configs currently: webpack.production.config.js new webpack.DefinePlugin({ 'process.env':{ 'NODE_ENV': ...
Read more >
A Dead Simple Guide to Using Environment Variables in Your ...
Using the node package dotenv-webpack we can access our .env file easily from ... 'process.env' variables which will trump anything local per dotenv...
Read more >
Using Environment Variables With React & WebPack
If you are using DotEnv there's a couple more steps you have to take to get it working unlike if your using DotEnv...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found