forked from icd/rentgen
		
	Test code in background.ts and test-content-script.js now only builds when ENABLE_TESTS=true is set. Production builds (default) exclude test code completely via esbuild's define feature. Changes: - esbuild.config.js: conditionally add test entrypoints and define ENABLE_TESTS - background.ts: wrap test message listener in if (ENABLE_TESTS) block - Dockerfile: add test_builder stage that builds with ENABLE_TESTS=true - package-lock.json: updated from npm install Verified with typecheck and lint. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
		
			
				
	
	
		
			78 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import esbuild from 'esbuild';
 | |
| import scss from 'esbuild-plugin-sass';
 | |
| 
 | |
| const watch = process.argv.includes('--watch') && {
 | |
|     onRebuild(error) {
 | |
|         if (error) console.error('[watch] build failed', error);
 | |
|         else console.log('[watch] build finished');
 | |
|     },
 | |
| };
 | |
| 
 | |
| const ENABLE_TESTS = process.env.ENABLE_TESTS === 'true';
 | |
| 
 | |
| // see https://github.com/evanw/esbuild/issues/806#issuecomment-779138268
 | |
| let skipReactImports = {
 | |
|     name: 'skipReactImports',
 | |
|     setup(build) {
 | |
|         build.onResolve({ filter: /^(react(-dom)?|survey-react)$/ }, (args) => {
 | |
|             return {
 | |
|                 path: args.path,
 | |
|                 namespace: `globalExternal_${args.path}`,
 | |
|             };
 | |
|         });
 | |
| 
 | |
|         build.onLoad({ filter: /.*/, namespace: 'globalExternal_react' }, () => {
 | |
|             return {
 | |
|                 contents: `module.exports = globalThis.React`,
 | |
|                 loader: 'js',
 | |
|             };
 | |
|         });
 | |
| 
 | |
|         build.onLoad({ filter: /.*/, namespace: 'globalExternal_react-dom' }, () => {
 | |
|             return {
 | |
|                 contents: `module.exports = globalThis.ReactDOM`,
 | |
|                 loader: 'js',
 | |
|             };
 | |
|         });
 | |
|         build.onLoad({ filter: /.*/, namespace: 'globalExternal_survey-react' }, () => {
 | |
|             return {
 | |
|                 contents: `module.exports = globalThis.Survey`,
 | |
|                 loader: 'js',
 | |
|             };
 | |
|         });
 | |
|     },
 | |
| };
 | |
| 
 | |
| const entryPoints = [
 | |
|     'components/toolbar/toolbar.tsx',
 | |
|     'components/sidebar/sidebar.tsx',
 | |
|     'components/report-window/report-window.tsx',
 | |
|     'background.ts',
 | |
|     'diag.tsx',
 | |
|     'styles/global.scss',
 | |
|     'styles/fonts.scss',
 | |
| ];
 | |
| 
 | |
| if (ENABLE_TESTS) {
 | |
|     entryPoints.push('tests/test-content-script.js');
 | |
| }
 | |
| 
 | |
| esbuild
 | |
|     .build({
 | |
|         entryPoints,
 | |
|         bundle: true,
 | |
|         // minify: true,
 | |
|         outdir: './lib',
 | |
|         loader: { '.woff': 'file', '.woff2': 'file' },
 | |
|         plugins: [scss(), skipReactImports],
 | |
|         define: {
 | |
|             PLUGIN_NAME: '"Rentgen"',
 | |
|             PLUGIN_URL: '"https://addons.mozilla.org/pl/firefox/addon/rentgen/"',
 | |
|             ENABLE_TESTS: String(ENABLE_TESTS),
 | |
|         },
 | |
|         external: ['react', 'react-dom', 'survey-react'],
 | |
|         watch,
 | |
|     })
 | |
|     .then(() => console.log('Add-on was built'))
 | |
|     .catch(() => process.exit(1));
 |