compile optimizes one module, or a bundled chunk that kept its JSX. The output is still JSX, so run Solid's JSX transform after it.
import { transform } from '@solidjs/compiler';
import { compile } from 'solid-optimizer';
const optimized = compile(code, { filename: 'app.tsx' });
const output = transform(optimized.code, {
filename: 'app.tsx',
moduleName: '@solidjs/web',
generate: 'dom',
});Steps#
Call
compilewith the code and afilename. The file name picks the parser and names the source in the source map.TypeScriptimport { compile } from 'solid-optimizer'; const result = compile(code, { filename: 'app.tsx' }); result.code; // JSX, ready for Solid's JSX transform result.map; // a source map, or null when nothing changed result.skipped; // what was left alone, when `report` is setLower the result with
@solidjs/compiler, as in the example at the top.Fold constants from other modules, if you want to. Read the constants of the imported module with
readModuleConstants, then pass them asimportedConstants, keyed by the import specifier as the code writes it.TypeScriptimport { compile, readModuleConstants } from 'solid-optimizer'; const config = readModuleConstants(configCode, { filename: 'config.ts' }); config.exports; // { SHOW_BANNER: true, THEME: 'light' } config.imports; // the modules it imports from compile(appCode, { filename: 'app.tsx', importedConstants: { './config': config.exports }, });A module that re-exports constants from another module takes that module's constants in its own
importedConstants.Fold global names, if you want to.
globalConstantstakes values of global names and paths, like a bundler'sdefineentries.TypeScriptcompile(code, { filename: 'app.tsx', globalConstants: { __DEV__: false, 'import.meta.env.MODE': 'production' }, });Ask for a report, if you want to. Set
report: trueand readresult.skipped. Each entry has apass, areason, and aname.TypeScriptconst result = compile(code, { filename: 'app.tsx', report: true }); for (const skip of result.skipped ?? []) { console.log(skip.pass, skip.reason, skip.name); }Compile a server build and its client build with the same code and options. See Hydration.
The Vite plugin does all of this for you.
Related#
- compile and readModuleConstants for every option and type.
- Constants across modules for what folds.
- Report reasons for the reason codes in
skipped. - Set up the Vite plugin to let the plugin do this.