|  | 
| 1 | 1 | import { InputOptions, OutputOptions, rollup } from 'rollup'; | 
| 2 |  | -import { BuildConfig, fileSize, rollupOnWarn, terser } from './util'; | 
|  | 2 | +import { | 
|  | 3 | +  BuildConfig, | 
|  | 4 | +  ensureDir, | 
|  | 5 | +  fileSize, | 
|  | 6 | +  PackageJSON, | 
|  | 7 | +  readFile, | 
|  | 8 | +  rollupOnWarn, | 
|  | 9 | +  terser, | 
|  | 10 | +  writeFile, | 
|  | 11 | +} from './util'; | 
| 3 | 12 | import { join } from 'path'; | 
| 4 | 13 | import { transform } from 'esbuild'; | 
|  | 14 | +import { writePackageJson } from './package-json'; | 
| 5 | 15 | 
 | 
| 6 | 16 | /** | 
| 7 | 17 |  * Builds the qwikloader javascript files. These files can be used | 
| @@ -151,6 +161,73 @@ export async function submoduleQwikLoader(config: BuildConfig) { | 
| 151 | 161 |     build.write(optimizeDebug), | 
| 152 | 162 |   ]); | 
| 153 | 163 | 
 | 
|  | 164 | +  await generateLoaderSubmodule(config); | 
|  | 165 | + | 
| 154 | 166 |   const optimizeFileSize = await fileSize(join(config.distPkgDir, 'qwikloader.optimize.js')); | 
| 155 | 167 |   console.log('🐸 qwikloader:', optimizeFileSize); | 
| 156 | 168 | } | 
|  | 169 | + | 
|  | 170 | +/** | 
|  | 171 | + * Load each of the qwik scripts to be inlined with esbuild "define" as const varialbles. | 
|  | 172 | + */ | 
|  | 173 | +export async function inlineQwikScriptsEsBuild(config: BuildConfig) { | 
|  | 174 | +  const variableToFileMap = [ | 
|  | 175 | +    ['QWIK_LOADER_DEFAULT_MINIFIED', 'qwikloader.js'], | 
|  | 176 | +    ['QWIK_LOADER_DEFAULT_DEBUG', 'qwikloader.debug.js'], | 
|  | 177 | +    ['QWIK_LOADER_OPTIMIZE_MINIFIED', 'qwikloader.optimize.js'], | 
|  | 178 | +    ['QWIK_LOADER_OPTIMIZE_DEBUG', 'qwikloader.optimize.debug.js'], | 
|  | 179 | +    ['QWIK_PREFETCH_MINIFIED', 'prefetch.js'], | 
|  | 180 | +    ['QWIK_PREFETCH_DEBUG', 'prefetch.debug.js'], | 
|  | 181 | +  ]; | 
|  | 182 | + | 
|  | 183 | +  const define: { [varName: string]: string } = {}; | 
|  | 184 | + | 
|  | 185 | +  await Promise.all( | 
|  | 186 | +    variableToFileMap.map(async (varToFile) => { | 
|  | 187 | +      const varName = `global.${varToFile[0]}`; | 
|  | 188 | +      const filePath = join(config.distPkgDir, varToFile[1]); | 
|  | 189 | +      const content = await readFile(filePath, 'utf-8'); | 
|  | 190 | +      define[varName] = JSON.stringify(content.trim()); | 
|  | 191 | +    }) | 
|  | 192 | +  ); | 
|  | 193 | + | 
|  | 194 | +  return define; | 
|  | 195 | +} | 
|  | 196 | + | 
|  | 197 | +async function generateLoaderSubmodule(config: BuildConfig) { | 
|  | 198 | +  const loaderDistDir = join(config.distPkgDir, 'loader'); | 
|  | 199 | + | 
|  | 200 | +  const loaderCode = await readFile(join(config.distPkgDir, 'qwikloader.js'), 'utf-8'); | 
|  | 201 | +  const loaderDebugCode = await readFile(join(config.distPkgDir, 'qwikloader.debug.js'), 'utf-8'); | 
|  | 202 | + | 
|  | 203 | +  const code = [ | 
|  | 204 | +    `const QWIK_LOADER = ${JSON.stringify(loaderCode.trim())};`, | 
|  | 205 | +    `const QWIK_LOADER_DEBUG = ${JSON.stringify(loaderDebugCode.trim())};`, | 
|  | 206 | +  ]; | 
|  | 207 | + | 
|  | 208 | +  const esmCode = [...code, `export { QWIK_LOADER, QWIK_LOADER_DEBUG };`]; | 
|  | 209 | +  const cjsCode = [ | 
|  | 210 | +    ...code, | 
|  | 211 | +    `exports.QWIK_LOADER = QWIK_LOADER;`, | 
|  | 212 | +    `exports.QWIK_LOADER_DEBUG = QWIK_LOADER_DEBUG;`, | 
|  | 213 | +  ]; | 
|  | 214 | +  const dtsCode = [ | 
|  | 215 | +    `export declare const QWIK_LOADER: string;`, | 
|  | 216 | +    `export declare const QWIK_LOADER_DEBUG: string;`, | 
|  | 217 | +  ]; | 
|  | 218 | + | 
|  | 219 | +  ensureDir(loaderDistDir); | 
|  | 220 | +  await writeFile(join(loaderDistDir, 'index.mjs'), esmCode.join('\n') + '\n'); | 
|  | 221 | +  await writeFile(join(loaderDistDir, 'index.cjs'), cjsCode.join('\n') + '\n'); | 
|  | 222 | +  await writeFile(join(loaderDistDir, 'index.d.ts'), dtsCode.join('\n') + '\n'); | 
|  | 223 | + | 
|  | 224 | +  const loaderPkg: PackageJSON = { | 
|  | 225 | +    name: `@builder.io/qwik/loader`, | 
|  | 226 | +    version: config.distVersion, | 
|  | 227 | +    main: `index.cjs`, | 
|  | 228 | +    module: `index.mjs`, | 
|  | 229 | +    types: `index.d.ts`, | 
|  | 230 | +    private: true, | 
|  | 231 | +  }; | 
|  | 232 | +  await writePackageJson(loaderDistDir, loaderPkg); | 
|  | 233 | +} | 
0 commit comments