Created
March 8, 2024 00:18
-
-
Save dwelch2344/26687e7c2053c4611a816bb1906ac215 to your computer and use it in GitHub Desktop.
Obfuscate via vite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from 'vite'; | |
import path from 'path'; | |
import glob from 'glob'; | |
import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'; | |
// Function to generate a virtual entry file content | |
const generateVirtualEntry = (srcFiles, moduleFiles) => { | |
let content = ''; | |
// Import specific modules first | |
content += moduleFiles.map(file => `import "${file}";`).join('\n'); | |
// Then import all src files | |
content += '\n' + srcFiles.map(file => `import "${file}";`).join('\n'); | |
return content; | |
}; | |
// Find all JS files in src | |
const srcFiles = glob.sync(path.resolve(__dirname, 'src/**/*.js')).map(file => | |
path.relative(__dirname, file).replace(/\\/g, '/') | |
); | |
// Specify module files to include explicitly and first | |
const moduleFiles = [ | |
'node_modules/example1/a.js', | |
'node_modules/angular/angular.js', // prefer NON-minified files to avoid issues | |
'node_modules/example2/b.js' | |
].map(file => path.relative(__dirname, file).replace(/\\/g, '/')); | |
export default defineConfig({ | |
plugins: [ | |
// If you have CommonJS dependencies, consider using this plugin | |
esbuildCommonjs(), | |
{ | |
name: 'configure-input', | |
config: () => ({ | |
build: { | |
rollupOptions: { | |
input: 'virtual-entry', | |
}, | |
}, | |
}), | |
resolveId(source) { | |
if (source === 'virtual-entry') { | |
return source; // this signals that rollup should not try to resolve the import | |
} | |
return null; | |
}, | |
load(id) { | |
if (id === 'virtual-entry') { | |
return generateVirtualEntry(srcFiles, moduleFiles); | |
} | |
return null; | |
}, | |
}, | |
], | |
build: { | |
rollupOptions: { | |
// This will create a single bundle file | |
output: { | |
format: 'iife', // Wrap your code in an immediately invoked function expression (IIFE) | |
}, | |
}, | |
minify: 'terser', // Use terser for minification | |
terserOptions: { | |
compress: { | |
drop_console: true, // Remove console.log statements | |
drop_debugger: true, // Remove debugger statements | |
}, | |
mangle: true, // Mangle names for obfuscation | |
}, | |
sourcemap: false, // Set to true if you need a source map | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup:
Build: