Skip to content

Commit

Permalink
fix: bazel to work with optimizer (QwikDev#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery authored Nov 13, 2021
1 parent aa005b7 commit 7bab256
Show file tree
Hide file tree
Showing 10 changed files with 868 additions and 809 deletions.
6 changes: 3 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ js_library(
":package/core.cjs.map",
":package/core.mjs",
":package/core.mjs.map",
":package/jsx-runtime/index.cjs",
":package/jsx-runtime/index.d.ts",
":package/jsx-runtime/index.mjs",
":package/jsx-runtime.cjs",
":package/jsx-runtime.d.ts",
":package/jsx-runtime.mjs",
":package/optimizer.cjs",
":package/optimizer.d.ts",
":package/optimizer.mjs",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@microsoft/api-extractor": "^7.16.1",
"@napi-rs/cli": "^1.3.5",
"@node-rs/helper": "^1.2.1",
"@napi-rs/triples": "^1.0.3",
"@types/cross-spawn": "^6.0.2",
"@types/execa": "^2.0.0",
"@types/express": "^4.17.13",
Expand All @@ -57,6 +58,7 @@
"concurrently": "^6.2.0",
"conventional-changelog-cli": "^2.1.1",
"cypress": "^7.7.0",
"cross-spawn": "^7.0.3",
"domino": "^2.1.6",
"esbuild": "^0.12.13",
"esbuild-register": "^2.6.0",
Expand Down
3 changes: 3 additions & 0 deletions scripts/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ nodejs_binary(
"--dev",
"--tsc",
"--api",
# Dissable these flags, as we don't need them for now.
# "--platform-binding",
# "--wasm",
]
)

Expand Down
22 changes: 19 additions & 3 deletions scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,25 @@ function createTypesApi(
outFileName: string,
corePath: string
) {
const extractorConfig = ExtractorConfig.loadFileAndPrepare(
join(config.srcDir, submodule, 'api-extractor.json')
);
let extractorConfig;
if (config.bazelOutputDir) {
const configObjectFullPath = join(config.srcDir, submodule, 'api-extractor.json');
const configObject = ExtractorConfig.loadFile(configObjectFullPath);
configObject.mainEntryPointFilePath = configObject.mainEntryPointFilePath.replace(
'<projectFolder>',
config.bazelOutputDir
);
const packageJsonFullPath = join(config.srcDir, '..', 'package.json');
extractorConfig = ExtractorConfig.prepare({
configObject,
configObjectFullPath,
packageJsonFullPath,
});
} else {
extractorConfig = ExtractorConfig.loadFileAndPrepare(
join(config.srcDir, submodule, 'api-extractor.json')
);
}
const result = Extractor.invoke(extractorConfig, {
localBuild: !!config.dev,
showVerboseMessages: false,
Expand Down
6 changes: 4 additions & 2 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* to allow NodeJS to build .ts files on-demand.
*/

const { dirname, join } = require('path');
const { register } = require('esbuild-register/dist/node');

const esmNode = parseInt(process.version.substr(1).split('.')[0], 10) >= 14;
Expand All @@ -24,7 +23,10 @@ if (process.env.BAZEL_NODE_MODULES_ROOTS) {
// This is a signal that Bazel has started this script
// If Bazel is running this, then find out where it
// would like to see the build output to be written.
config.pkgDir = dirname(join(process.cwd(), args[args.length - 1]));
if (!config.bazelOutputDir) {
console.error("When running under bazel must use '--bazelOutputDir $(RULEDIR)'");
process.exit(-1);
}
}

// let's do this!
Expand Down
12 changes: 11 additions & 1 deletion scripts/package_build.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def package_build(
"@npm//source-map-support",
"@npm//terser",
"@npm//typescript",
"@npm//@types/cross-spawn",
"@npm//cross-spawn",
"@npm//gzip-size",
"@npm//execa",
"@npm//@napi-rs/triples",
"@npm//@types/semver",
"@npm//semver",
"@npm//path-browserify",
"@npm//@types/path-browserify",
"//scripts:all_build_source",
],
outs = [
Expand Down Expand Up @@ -54,7 +63,8 @@ def package_build(
name + "/testing/index.mjs",
],
args = [
"$(execpath %s/core.cjs)" % name,
"--bazelOutputDir",
"$(RULEDIR)"
],
visibility = None,
)
24 changes: 22 additions & 2 deletions scripts/submodule-optimizer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { build, BuildOptions } from 'esbuild';
import { BuildConfig, banner, nodeTarget, target, watcher, writeFile, readFile } from './util';
import {
BuildConfig,
banner,
nodeTarget,
target,
watcher,
writeFile,
readFile,
access,
} from './util';
import { join } from 'path';
import { minify } from 'terser';
import { platformArchTriples } from '@napi-rs/triples';
import { readPackageJson } from './package-json';
import { watch } from 'rollup';
import { constants } from 'fs';

/**
* Builds @builder.io/optimizer
Expand Down Expand Up @@ -156,5 +166,15 @@ async function generatePlatformBindingsData(config: BuildConfig) {
const code = c.join('\n') + '\n';

const platformBindingPath = join(config.srcDir, 'optimizer', 'src', 'qwik-binding-map.ts');
await writeFile(platformBindingPath, code);
let isWritable;
try {
await access(platformBindingPath, constants.W_OK);
isWritable = true;
} catch (e) {
// When running under bazel source files are read only.
isWritable = false;
}
if (isWritable) {
await writeFile(platformBindingPath, code);
}
}
12 changes: 9 additions & 3 deletions scripts/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
statSync,
unlinkSync,
writeFile as fsWriteFile,
mkdir as fsMkdir,
} from 'fs';
import { promisify } from 'util';
import gzipSize from 'gzip-size';
Expand All @@ -36,6 +37,7 @@ export interface BuildConfig {
esmNode: boolean;
distVersion: string;
platformTarget?: string;
bazelOutputDir?: string;

api?: boolean;
build?: boolean;
Expand All @@ -59,13 +61,16 @@ export interface BuildConfig {
*/
export function loadConfig(args: string[] = []) {
const config: BuildConfig = mri(args) as any;
config.bazelOutputDir = config.bazelOutputDir && join(process.cwd(), config.bazelOutputDir);

config.rootDir = join(__dirname, '..');
config.distDir = join(config.rootDir, 'dist-dev');
config.distDir = join(config.bazelOutputDir || config.rootDir, 'dist-dev');
config.srcDir = join(config.rootDir, 'src');
config.srcNapiDir = join(config.srcDir, 'napi');
config.scriptsDir = join(config.rootDir, 'scripts');
config.distPkgDir = join(config.distDir, '@builder.io-qwik');
config.distPkgDir = config.bazelOutputDir
? join(join(config.bazelOutputDir, 'package'))
: join(config.distDir, '@builder.io-qwik');
config.distBindingsDir = join(config.distPkgDir, 'bindings');
config.tscDir = join(config.distDir, 'tsc-out');
config.esmNode = parseInt(process.version.substr(1).split('.')[0], 10) >= 14;
Expand Down Expand Up @@ -211,6 +216,7 @@ export const copyFile = promisify(fsCopyFile);
export const readFile = promisify(fsReadFile);
export const stat = promisify(fsStat);
export const writeFile = promisify(fsWriteFile);
export const mkdir = promisify(fsMkdir);

export function emptyDir(dir: string) {
if (existsSync(dir)) {
Expand All @@ -236,7 +242,7 @@ export function ensureDir(dir: string) {
}

export function panic(msg: string) {
console.error(`\n❌ ${msg}\n`);
console.error(`\n❌ ${msg}\n`, new Error(msg).stack);
process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function setServerPlatform(document: any, opts: DocumentOptions) {
? url.pathname + '.js'
: url.pathname;
const filePath = join(opts.outDir, pathname);
return './' + filePath;
return '.' + (filePath.startsWith('/') ? '' : '/') + filePath;
},
queueRender: (renderMarked) => {
if (!queuePromise) {
Expand Down
Loading

0 comments on commit 7bab256

Please sign in to comment.