Skip to content

Commit

Permalink
feat(qwik): process qwik library by optimizer
Browse files Browse the repository at this point in the history
this enables various future features
  • Loading branch information
wmertens committed Oct 17, 2024
1 parent 1fa3cc3 commit ce16d3e
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 65 deletions.
12 changes: 6 additions & 6 deletions packages/docs/src/repl/bundled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import terserPkgJson from 'terser/package.json';
import terserJs from '../../node_modules/terser/dist/bundle.min.js?raw-source';

import qBuild from '../../node_modules/@builder.io/qwik/dist/build/index.d.ts?raw-source';
import qCoreCjs from '../../node_modules/@builder.io/qwik/dist/core.cjs?raw-source';
import qCoreCjs from '../../node_modules/@builder.io/qwik/dist/core.qwik.cjs?raw-source';
import qCoreDts from '../../node_modules/@builder.io/qwik/dist/core.d.ts?raw-source';
import qCoreMinMjs from '../../node_modules/@builder.io/qwik/dist/core.min.mjs?raw-source';
import qCoreMjs from '../../node_modules/@builder.io/qwik/dist/core.mjs?raw-source';
import qCoreMinMjs from '../../node_modules/@builder.io/qwik/dist/core.min.qwik.mjs?raw-source';
import qCoreMjs from '../../node_modules/@builder.io/qwik/dist/core.qwik.mjs?raw-source';
import qOptimizerCjs from '../../node_modules/@builder.io/qwik/dist/optimizer.cjs?raw-source';
import qServerCjs from '../../node_modules/@builder.io/qwik/dist/server.cjs?raw-source';
import qServerDts from '../../node_modules/@builder.io/qwik/dist/server.d.ts?raw-source';
Expand Down Expand Up @@ -48,10 +48,10 @@ export const bundled: PkgUrls = {
[QWIK_PKG_NAME]: {
version: qwikVersion,
'/dist/build/index.d.ts': qBuild,
'/dist/core.cjs': qCoreCjs,
'/dist/core.qwik.cjs': qCoreCjs,
'/dist/core.d.ts': qCoreDts,
'/dist/core.min.mjs': qCoreMinMjs,
'/dist/core.mjs': qCoreMjs,
'/dist/core.min.qwik.mjs': qCoreMinMjs,
'/dist/core.qwik.mjs': qCoreMjs,
'/dist/optimizer.cjs': qOptimizerCjs,
'/dist/server.cjs': qServerCjs,
'/dist/server.d.ts': qServerDts,
Expand Down
7 changes: 4 additions & 3 deletions packages/docs/src/repl/repl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ const getDependencies = (input: ReplAppInput) => {
if (input.version !== 'bundled') {
const [M, m, p] = input.version.split('-')[0].split('.').map(Number);
const prefix = M > 1 || (M == 1 && (m > 7 || (m == 7 && p >= 2))) ? '/dist/' : '/';
const qwik = M > 1 || (M == 1 && m >= 9) ? '.qwik' : '';
out[QWIK_PKG_NAME] = {
version: input.version,
};
for (const p of [
`${prefix}core.cjs`,
`${prefix}core.mjs`,
`${prefix}core.min.mjs`,
`${prefix}core${qwik}.cjs`,
`${prefix}core${qwik}.mjs`,
`${prefix}core.min${qwik}.mjs`,
`${prefix}optimizer.cjs`,
`${prefix}server.cjs`,
`/bindings/qwik.wasm.cjs`,
Expand Down
5 changes: 4 additions & 1 deletion packages/docs/src/repl/worker/repl-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const depResponse = async (pkgName: string, pkgPath: string) => {
if (M > 1 || (M == 1 && (m > 7 || (m == 7 && p >= 2)))) {
pkgPath = `/dist${pkgPath}`;
}
if (!(M > 2 || (M === 1 && (m > 8 || (m === 8 && version.includes('-dev')))))) {
pkgPath = pkgPath.replace('.qwik', '');
}
}
const url = options.deps[pkgName][pkgPath];
if (!url) {
Expand Down Expand Up @@ -71,7 +74,7 @@ const _loadDependencies = async (replOptions: ReplInputOptions) => {
}

if (!isSameQwikVersion(self.qwikCore?.version)) {
await exec(QWIK_PKG_NAME, '/core.cjs');
await exec(QWIK_PKG_NAME, '/core.qwik.cjs');
if (self.qwikCore) {
console.debug(`Loaded @builder.io/qwik: ${self.qwikCore.version}`);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/repl/worker/repl-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const replResolver = (options: ReplInputOptions, buildMode: 'client' | 's
}
}

const rsp = await depResponse('@builder.io/qwik', '/core.mjs');
const rsp = await depResponse('@builder.io/qwik', '/core.qwik.mjs');
if (rsp) {
return rsp.text();
}
Expand Down
42 changes: 21 additions & 21 deletions packages/qwik/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
".": {
"types": "./dist/core.d.ts",
"import": {
"development": "./dist/core.mjs",
"production": "./dist/core.prod.mjs",
"default": "./dist/core.prod.mjs",
"min": "./dist/core.min.mjs"
"development": "./dist/core.qwik.mjs",
"production": "./dist/core.prod.qwik.mjs",
"default": "./dist/core.prod.qwik.mjs",
"min": "./dist/core.min.qwik.mjs"
},
"require": {
"development": "./dist/core.cjs",
"production": "./dist/core.prod.cjs",
"default": "./dist/core.prod.cjs"
"development": "./dist/core.qwik.cjs",
"production": "./dist/core.prod.qwik.cjs",
"default": "./dist/core.prod.qwik.cjs"
}
},
"./cli": {
Expand All @@ -59,29 +59,29 @@
"./jsx-runtime": {
"types": "./dist/jsx-runtime.d.ts",
"import": {
"development": "./dist/core.mjs",
"production": "./dist/core.prod.mjs",
"default": "./dist/core.prod.mjs",
"min": "./dist/core.min.mjs"
"development": "./dist/core.qwik.mjs",
"production": "./dist/core.prod.qwik.mjs",
"default": "./dist/core.prod.qwik.mjs",
"min": "./dist/core.min.qwik.mjs"
},
"require": {
"development": "./dist/core.cjs",
"production": "./dist/core.prod.cjs",
"default": "./dist/core.prod.cjs"
"development": "./dist/core.qwik.cjs",
"production": "./dist/core.prod.qwik.cjs",
"default": "./dist/core.prod.qwik.cjs"
}
},
"./jsx-dev-runtime": {
"types": "./dist/jsx-runtime.d.ts",
"import": {
"development": "./dist/core.mjs",
"production": "./dist/core.prod.mjs",
"default": "./dist/core.mjs",
"min": "./dist/core.min.mjs"
"development": "./dist/core.qwik.mjs",
"production": "./dist/core.prod.qwik.mjs",
"default": "./dist/core.qwik.mjs",
"min": "./dist/core.min.qwik.mjs"
},
"require": {
"development": "./dist/core.cjs",
"production": "./dist/core.prod.cjs",
"default": "./dist/core.cjs"
"development": "./dist/core.qwik.cjs",
"production": "./dist/core.prod.qwik.cjs",
"default": "./dist/core.qwik.cjs"
}
},
"./build": {
Expand Down
12 changes: 6 additions & 6 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ export async function build(config: BuildConfig) {
[join(config.srcQwikDir, 'core')]: async () => {
await submoduleCore({ ...config, dev: true });
await copyFile(
join(config.srcQwikDir, '..', 'dist', 'core.cjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.cjs')
join(config.srcQwikDir, '..', 'dist', 'core.qwik.cjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.qwik.cjs')
);
await copyFile(
join(config.srcQwikDir, '..', 'dist', 'core.mjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.mjs')
join(config.srcQwikDir, '..', 'dist', 'core.qwik.mjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.qwik.mjs')
);
console.log(
join(config.srcQwikDir, '..', 'dist', 'core.cjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.cjs')
join(config.srcQwikDir, '..', 'dist', 'core.qwik.cjs'),
join(config.srcQwikDir, '..', 'dist', 'core.prod.qwik.cjs')
);
},
[join(config.srcQwikDir, 'optimizer')]: () => submoduleOptimizer(config),
Expand Down
49 changes: 29 additions & 20 deletions scripts/submodule-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function submoduleCoreProd(config: BuildConfig) {
const esmOutput: OutputOptions = {
dir: join(config.distQwikPkgDir),
format: 'es',
entryFileNames: 'core.mjs',
entryFileNames: 'core.qwik.mjs',
sourcemap: true,
banner: getBanner('@builder.io/qwik', config.distVersion),
};
Expand All @@ -53,7 +53,7 @@ async function submoduleCoreProd(config: BuildConfig) {
dir: join(config.distQwikPkgDir),
format: 'umd',
name: 'qwikCore',
entryFileNames: 'core.cjs',
entryFileNames: 'core.qwik.cjs',
sourcemap: true,
globals: {
'@builder.io/qwik/build': 'qwikBuild',
Expand All @@ -65,9 +65,9 @@ async function submoduleCoreProd(config: BuildConfig) {

await Promise.all([build.write(esmOutput), build.write(cjsOutput)]);

console.log('🦊 core.mjs:', await fileSize(join(config.distQwikPkgDir, 'core.mjs')));
console.log('🦊 core.qwik.mjs:', await fileSize(join(config.distQwikPkgDir, 'core.qwik.mjs')));

const inputCore = join(config.distQwikPkgDir, 'core.mjs');
const inputCore = join(config.distQwikPkgDir, 'core.qwik.mjs');
const inputMin: InputOptions = {
input: inputCore,
onwarn: rollupOnWarn,
Expand Down Expand Up @@ -98,7 +98,7 @@ async function submoduleCoreProd(config: BuildConfig) {
await buildMin.write({
dir: join(config.distQwikPkgDir),
format: 'es',
entryFileNames: 'core.min.mjs',
entryFileNames: 'core.min.qwik.mjs',
plugins: [
{
name: 'minify',
Expand Down Expand Up @@ -147,7 +147,7 @@ async function submoduleCoreProd(config: BuildConfig) {
const indx = Math.max(selfIdx);
if (indx !== -1) {
throw new Error(
`"core.min.mjs" should not have any global references, and should have been removed for a production minified build\n` +
`"core.min.qwik.mjs" should not have any global references, and should have been removed for a production minified build\n` +
esmCleanCode.substring(indx, indx + 10) +
'\n' +
esmCleanCode.substring(indx - 100, indx + 300)
Expand All @@ -161,16 +161,19 @@ async function submoduleCoreProd(config: BuildConfig) {
],
});

console.log('🐭 core.min.mjs:', await fileSize(join(config.distQwikPkgDir, 'core.min.mjs')));
console.log(
'🐭 core.min.qwik.mjs:',
await fileSize(join(config.distQwikPkgDir, 'core.min.qwik.mjs'))
);

let esmCode = await readFile(join(config.distQwikPkgDir, 'core.mjs'), 'utf-8');
let cjsCode = await readFile(join(config.distQwikPkgDir, 'core.cjs'), 'utf-8');
let esmCode = await readFile(join(config.distQwikPkgDir, 'core.qwik.mjs'), 'utf-8');
let cjsCode = await readFile(join(config.distQwikPkgDir, 'core.qwik.cjs'), 'utf-8');
// fixup the Vite base url
cjsCode = cjsCode.replaceAll('undefined.BASE_URL', 'globalThis.BASE_URL||"/"');
await writeFile(join(config.distQwikPkgDir, 'core.cjs'), cjsCode);
await writeFile(join(config.distQwikPkgDir, 'core.qwik.cjs'), cjsCode);

await submoduleCoreProduction(config, esmCode, join(config.distQwikPkgDir, 'core.prod.mjs'));
await submoduleCoreProduction(config, cjsCode, join(config.distQwikPkgDir, 'core.prod.cjs'));
await submoduleCoreProduction(config, esmCode, join(config.distQwikPkgDir, 'core.prod.qwik.mjs'));
await submoduleCoreProduction(config, cjsCode, join(config.distQwikPkgDir, 'core.prod.qwik.cjs'));
}

async function submoduleCoreProduction(config: BuildConfig, code: string, outPath: string) {
Expand Down Expand Up @@ -225,7 +228,7 @@ async function submoduleCoreDev(config: BuildConfig) {
...opts,
external: ['@builder.io/qwik/build'],
format: 'esm',
outExtension: { '.js': '.mjs' },
outExtension: { '.js': '.qwik.mjs' },
});

const cjs = build({
Expand All @@ -237,7 +240,7 @@ async function submoduleCoreDev(config: BuildConfig) {
'import.meta.env.BASE_URL': '"globalThis.BASE_URL||\'/\'"',
},
format: 'cjs',
outExtension: { '.js': '.cjs' },
outExtension: { '.js': '.qwik.cjs' },
banner: {
js: `globalThis.qwikCore = (function (module) {`,
},
Expand All @@ -249,15 +252,21 @@ async function submoduleCoreDev(config: BuildConfig) {
await Promise.all([esm, cjs]);

// Point the minified and prod versions to the dev versions
await writeFile(join(config.distQwikPkgDir, 'core.prod.mjs'), `export * from './core.mjs';\n`);
await writeFile(
join(config.distQwikPkgDir, 'core.prod.cjs'),
`module.exports = require('./core.cjs');\n`
join(config.distQwikPkgDir, 'core.prod.qwik.mjs'),
`export * from './core.qwik.mjs';\n`
);
await writeFile(
join(config.distQwikPkgDir, 'core.prod.qwik.cjs'),
`module.exports = require('./core.qwik.cjs');\n`
);
await writeFile(
join(config.distQwikPkgDir, 'core.min.qwik.mjs'),
`export * from './core.qwik.mjs';\n`
);
await writeFile(join(config.distQwikPkgDir, 'core.min.mjs'), `export * from './core.mjs';\n`);
await writeFile(
join(config.distQwikPkgDir, 'core.min.cjs'),
`module.exports = require('./core.cjs');\n`
join(config.distQwikPkgDir, 'core.min.qwik.cjs'),
`module.exports = require('./core.qwik.cjs');\n`
);

console.log('🐬', submodule, '(dev)');
Expand Down
4 changes: 2 additions & 2 deletions scripts/submodule-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ async function getQwikDomVersion(config: BuildConfig) {

const browserCjsRequireShim = `
if (typeof require !== 'function' && typeof location !== 'undefined' && typeof navigator !== 'undefined') {
// shim cjs require() for core.cjs within a browser
// shim cjs require() for core.qwik.cjs within a browser
globalThis.require = function(path) {
if (path === './core.cjs' || path === '@builder.io/qwik') {
if (path === './core.qwik.cjs' || path === '@builder.io/qwik') {
if (!self.qwikCore) {
throw new Error('Qwik Core global, "globalThis.qwikCore", must already be loaded for the Qwik Server to be used within a browser.');
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/submodule-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function submoduleTesting(config: BuildConfig) {
banner: { js: getBanner('@builder.io/qwik/testing', config.distVersion) },
outExtension: { '.js': '.mjs' },
plugins: [
importPath(/^@builder\.io\/qwik$/, '../core.mjs'),
importPath(/^@builder\.io\/qwik$/, '../core.qwik.mjs'),
importPath(/^@builder\.io\/qwik\/optimizer$/, '../optimizer.mjs'),
importPath(/^@builder\.io\/qwik\/server$/, '../server.mjs'),
],
Expand All @@ -44,7 +44,7 @@ export async function submoduleTesting(config: BuildConfig) {
js: getBanner('@builder.io/qwik/testing', config.distVersion),
},
plugins: [
importPath(/^@builder\.io\/qwik$/, '../core.cjs'),
importPath(/^@builder\.io\/qwik$/, '../core.qwik.cjs'),
importPath(/^@builder\.io\/qwik\/optimizer$/, '../optimizer.cjs'),
importPath(/^@builder\.io\/qwik\/server$/, '../server.cjs'),
],
Expand Down
6 changes: 3 additions & 3 deletions scripts/validate-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ export async function validateBuild(config: BuildConfig) {

await validatePackageJson(config, pkg, errors);
await Promise.all([
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.min.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.prod.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.min.qwik.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.prod.qwik.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'core.qwik.mjs')),
validateModuleTreeshake(config, join(config.distQwikPkgDir, 'server.mjs')),
]);
if (config.qwikcity) {
Expand Down

0 comments on commit ce16d3e

Please sign in to comment.