getkey / rollup-plugin-obfuscator

The most powerful rollup plugin for javascript-obfuscator.
Mozilla Public License 2.0
112 stars 9 forks source link

not working with vite and import.meta.glob #11

Closed simonbuehler closed 1 year ago

simonbuehler commented 1 year ago

Describe the bug hi, thanks for the pulgin!

when i use the plugin in vite and register the components with Glob imports i get (intermediate value)[z(...)] is not a function in the source it shows

... const c0=import.meta[z(226,188,253,225)]("./componen"+B(-113,-71,-76,-53),j); ...

To Reproduce

  1. import components using vite glob.meta
  2. use in vite.conf like
    build: {
    rollupOptions: {
      plugins: [
        obfuscator({
          include :['**/*.js', '**/*.ts', '**/*.vue'],
          exclude : ['node_modules/**'],
          fileOptions: {
  3. find error

Package versions:

fisherXYZ commented 1 year ago

Try add "enforce: 'post'" in your Plugins like this:

plugins: [
  vue(),
  {
    ...obfuscator({}),
    enforce: 'post'
  }
]

https://vitejs.dev/guide/using-plugins.html#finding-plugins

simonbuehler commented 1 year ago

unfortunately this doesn't work and post only applies to globalOptions and not fileoptions. globalOptions doesn't allow to exclude chunks and my aim is to just obfuscate app.js and leave the vendor chunk as is

my config is

build: {
    rollupOptions: {
      plugins: [
        obfuscator({
          include :['**/*.js', '**/*.ts', '**/*.vue'],
          exclude : ['node_modules/**'],
          fileOptions: {           
            log: true,            
          },
          globalOptions: false,
        })
      ]
    },
  },
plugins: [
    splitVendorChunkPlugin(),
    vue(),
    {
      ...obfuscator({}),
      enforce: 'post'
    },
    laravel({
      input: [
        'resources/sass/app.scss',
        'resources/js/app.js',
        'resources/js/dashboard.js'], // add scss file
      refresh: true,
    }),
doroved commented 1 year ago

Hi, I have this error TypeError: obfuscator is not a function

I'm using code like this.

import { fileURLToPath, URL } from 'node:url'
import obfuscator from 'rollup-plugin-obfuscator'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  base: '',
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  build: {
    outDir: 'chrome/store/app',
     rollupOptions: {
       output: {
         plugins: [obfuscator({})],
       },
     },
  },
})

Can someone tell me how to solve this problem? I only want to obfuscate the JS file of the application, not the dependencies.

getkey commented 1 year ago

@doroved please open another issue, your problem has nothing to do with this issue.

doroved commented 1 year ago

@doroved please open another issue, your problem has nothing to do with this issue.

I created a new issue, but unfortunately there is no reaction

getkey commented 1 year ago

Thanks. Indeed. That's because I haven't had time to look at either of them.

doroved commented 1 year ago

I understand you. I hope you will have time to check the issue soon, because I have not found any working alternatives to your solution

getkey commented 1 year ago

So, the problem here is that javascript-obfuscator is treating import.meta.glob() as any other function, and obfuscating it. It's unfortunate that you can't use enforce: 'post' to run the obfuscation after Vite does its replacement. But there is another trick that I have used (not with Vite though):

/* javascript-obfuscator:disable */
const modules = import.meta.glob('./dir/*.js')
/* javascript-obfuscator:enable */

If that doesn't work, reopen the issue!