最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack Obfuscator Issue: Cannot read property 'tap' of undefined - Stack Overflow

programmeradmin0浏览0评论

I need help with obfuscating my typescript code. I'm using this Electron template though the issue seems particular to webpack and the obfuscator.

I'm using the following versions:

"webpack": "4.46.0",
"webpack-obfuscator": "^3.3.0"

My configurations are:

webpack.main.js

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: ['./src/main.ts'],
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
    alias: require('./webpack.aliases'),
  },
};

webpack.rules.js

const { inDev } = require('./webpack.helpers');
const path = require('path');
const WebpackObfuscator = require('webpack-obfuscator');

module.exports = [
  {
    // Add support for native node modules
    test: /\.node$/,
    use: 'node-loader',
  },
  {
    // Webpack asset relocator loader
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
  {
    // Typescript loader
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
      },
    },
  },
  {
    // CSS Loader
    test: /\.css$/,
    use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
  },
  {
    // Less loader
    test: /\.less$/,
    use: [
      { loader: 'style-loader' },
      { loader: 'css-loader' },
      { loader: 'less-loader' },
    ],
  },
  {
    // Images Loader
    test: /\.(gif|jpe?g|tiff|png|webp|bmp|svg)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          publicPath: 'assets/images',
          outputPath: inDev() ? 'assets/images' : './main_window/assets/images',
        },
      },
    ],
  },
  {
    // Font & SVG loader
    test: /\.(woff(2)?|ttf|otf|eot)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          publicPath: 'assets/fonts',
          outputPath: inDev() ? 'assets/fonts' : './main_window/assets/fonts',
        },
      },
    ],
  },
  {
    test: /\.tsx?$/,
    exclude: /node_modules/,
    enforce: 'post',
    use: {
      loader: WebpackObfuscator.loader,
      options: {
        rotateStringArray: true,
      },
    },
  },
];

webpack.plugin.js

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const WebpackObfuscator = require('webpack-obfuscator');

module.exports = [
  new ForkTsCheckerWebpackPlugin(),
  new WebpackObfuscator(
    {
      rotateStringArray: true,
    },
    ['excluded_bundle_name.js'],
  ),
];

The error is:

An unhandled error has occurred inside Forge:
Cannot read property 'tap' of undefined
TypeError: Cannot read property 'tap' of undefined
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack-obfuscator/dist/plugin/index.js:25:45
    at SyncHook.eval (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:13:1)
    at SyncHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.newCompilation (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:631:26)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:667:29
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Compilerpile (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:662:28)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:77:18
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Watching._go (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:41:32)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:33:9
    at Compiler.readRecords (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:529:11)
    at new Watching (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:30:17)
    at Compiler.watch (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:244:10)

Thanks in advance.

I need help with obfuscating my typescript code. I'm using this Electron template though the issue seems particular to webpack and the obfuscator.

https://github./codesbiome/electron-react-webpack-typescript-2021

I'm using the following versions:

"webpack": "4.46.0",
"webpack-obfuscator": "^3.3.0"

My configurations are:

webpack.main.js

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: ['./src/main.ts'],
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
    alias: require('./webpack.aliases'),
  },
};

webpack.rules.js

const { inDev } = require('./webpack.helpers');
const path = require('path');
const WebpackObfuscator = require('webpack-obfuscator');

module.exports = [
  {
    // Add support for native node modules
    test: /\.node$/,
    use: 'node-loader',
  },
  {
    // Webpack asset relocator loader
    test: /\.(m?js|node)$/,
    parser: { amd: false },
    use: {
      loader: '@marshallofsound/webpack-asset-relocator-loader',
      options: {
        outputAssetBase: 'native_modules',
      },
    },
  },
  {
    // Typescript loader
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: 'ts-loader',
      options: {
        transpileOnly: true,
      },
    },
  },
  {
    // CSS Loader
    test: /\.css$/,
    use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
  },
  {
    // Less loader
    test: /\.less$/,
    use: [
      { loader: 'style-loader' },
      { loader: 'css-loader' },
      { loader: 'less-loader' },
    ],
  },
  {
    // Images Loader
    test: /\.(gif|jpe?g|tiff|png|webp|bmp|svg)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          publicPath: 'assets/images',
          outputPath: inDev() ? 'assets/images' : './main_window/assets/images',
        },
      },
    ],
  },
  {
    // Font & SVG loader
    test: /\.(woff(2)?|ttf|otf|eot)$/,
    use: [
      {
        loader: 'file-loader',
        options: {
          publicPath: 'assets/fonts',
          outputPath: inDev() ? 'assets/fonts' : './main_window/assets/fonts',
        },
      },
    ],
  },
  {
    test: /\.tsx?$/,
    exclude: /node_modules/,
    enforce: 'post',
    use: {
      loader: WebpackObfuscator.loader,
      options: {
        rotateStringArray: true,
      },
    },
  },
];

webpack.plugin.js

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const WebpackObfuscator = require('webpack-obfuscator');

module.exports = [
  new ForkTsCheckerWebpackPlugin(),
  new WebpackObfuscator(
    {
      rotateStringArray: true,
    },
    ['excluded_bundle_name.js'],
  ),
];

The error is:

An unhandled error has occurred inside Forge:
Cannot read property 'tap' of undefined
TypeError: Cannot read property 'tap' of undefined
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack-obfuscator/dist/plugin/index.js:25:45
    at SyncHook.eval (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:13:1)
    at SyncHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.newCompilation (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:631:26)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:667:29
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.pile (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:662:28)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:77:18
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/mt/Desktop/ca/ca-app/node_modules/tapable/lib/Hook.js:154:20)
    at Watching._go (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:41:32)
    at /Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:33:9
    at Compiler.readRecords (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:529:11)
    at new Watching (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Watching.js:30:17)
    at Compiler.watch (/Users/mt/Desktop/ca/ca-app/node_modules/webpack/lib/Compiler.js:244:10)

Thanks in advance.

Share Improve this question asked Apr 18, 2021 at 14:26 OasaOasa 511 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

"webpack-obfuscator": "^3.3.0"

With webpack 4 you will need webpack-obfuscator 2.6.0

npm install --save-dev [email protected]

I got it working for webpack 5 by using [email protected]

发布评论

评论列表(0)

  1. 暂无评论