Skip to content

Commit

Permalink
Merge pull request #313 from finn-no/build_and_copy_fixes
Browse files Browse the repository at this point in the history
Build and copy fixes
  • Loading branch information
digitalsadhu authored Aug 20, 2024
2 parents 20b7b8a + 39dfa96 commit 1aeeff8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@eik/cli": "3.1.3",
"browserslist": "4.23.3",
"browserslist-to-esbuild": "2.1.1",
"findup-sync": "5.0.0",
"semver": "7.6.3"
}
}
8 changes: 6 additions & 2 deletions packages/lit-labs-ssr-client/esbuild.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { join, dirname } from "node:path";
import { createRequire } from "module";
import { build } from "../../esbuild.js";

const { resolve } = createRequire(import.meta.url);

const renderLightDirectivePath = resolve("@lit-labs/ssr-client/directives/render-light.js");
const litElementHydrateSupportPath = join(dirname(renderLightDirectivePath), '..', 'lit-element-hydrate-support.js');

await Promise.all([
build({
entryPoints: [resolve("@lit-labs/ssr-client/lit-element-hydrate-support.js")],
entryPoints: [litElementHydrateSupportPath],
outfile: "./dist/lit-element-hydrate-support.js",
}),
build({
entryPoints: [resolve("@lit-labs/ssr-client/directives/render-light.js")],
entryPoints: [renderLightDirectivePath],
outfile: "./dist/directives/render-light.js",
}),
]);
29 changes: 14 additions & 15 deletions packages/vue-3/esbuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@ const dir = await fs.readdir(path.resolve(import.meta.dirname, "dist"));
for (const file of dir) {
if (file.endsWith(".js")) {
const filePath = path.resolve(import.meta.dirname, "dist", file);
let buildFile = filePath;
if (filePath.includes(".cjs")) {
// These shouldn't be used by any of us, but they've been included in the Eik asset
await build({
allowOverwrite: true,
sourcemap: false,
format: "cjs",
entryPoints: [filePath],
outfile: filePath,
});
} else {
await build({
allowOverwrite: true,
sourcemap: false,
entryPoints: [filePath],
outfile: filePath,
});
// Rename .cjs.js to a .tmp.cjs file to prevent Esbuild choking
buildFile = filePath.replace(".cjs", ".tmp").replace(".js", ".cjs");
await fs.rename(filePath, buildFile);
}
await build({
allowOverwrite: true,
sourcemap: false,
entryPoints: [buildFile],
outfile: filePath,
});
if (filePath.includes(".cjs")) {
// cleanup by removing the .tmp.cjs file
await fs.unlink(buildFile);
}
}
}
44 changes: 34 additions & 10 deletions scripts/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,44 @@
// 2. dependency name eg. @fabric-ds/css
// 3. dependency file or dir path eg. dist/fabric.min.css eg. dist/*

import { join } from 'path';
import findup from 'findup-sync';
import { execSync } from 'child_process';
import { join } from "path";
import { execSync } from "child_process";

const dirname = new URL('./', import.meta.url).pathname;
const dirname = new URL("./", import.meta.url).pathname;
const [, , packageNameArg, dependencyNameArg, dependencyFilePathArg] = process.argv;
const cwd = join(dirname, '../packages', packageNameArg);
const packageDir = join(dirname, "../packages", packageNameArg);
const rootDir = join(dirname, "../");

// find closest node_modules folder. This might be in the package folder or at the monorepo root.
const nodeModulesPath = findup(`node_modules`, { cwd });
// build the dependency path from the closest node_modules folder
const dependencyPath = join(nodeModulesPath || '', dependencyNameArg);
const localDependencyPath = join(packageDir, "node_modules", dependencyNameArg);
// navigate to the root node_modules folder as a backup
const rootDependencyPath = join(rootDir, "node_modules", dependencyNameArg);

// make directory if it doesn't already exist
execSync(`mkdir -p ${join(cwd, 'dist')}`);
console.log(` ==> Making directory ${join(packageDir, "dist")}`);
execSync(`mkdir -p ${join(packageDir, "dist")}`);
// copy the dependency files to the packages dist folder ready for uploading
execSync(`cp -R ${join(dependencyPath, dependencyFilePathArg)} ${join(cwd, 'dist/')}`);
try {
// try to copy from package node_modules folder
console.log(` ==> Copying file ${dependencyFilePathArg}`);
console.log(` From ${localDependencyPath}`);
console.log(` To ${join(packageDir, "dist/")}`);
execSync(
`cp -R ${join(localDependencyPath, dependencyFilePathArg)} ${join(packageDir, "dist/")}`
);
} catch (e) {
// fallback to root node_modules folder
console.log(` ==> Copying failed`);
console.log(` ==> Retrying copy from root node_modules folder...`);
console.log(` ==> Copying file ${dependencyFilePathArg}`);
console.log(` From ${rootDependencyPath}`);
console.log(` To ${join(packageDir, "dist/")}`);
try {
execSync(
`cp -R ${join(rootDependencyPath, dependencyFilePathArg)} ${join(packageDir, "dist/")}`
);
console.log(` ==> Copying successful`);
} catch (e) {
console.log(` ==> Copying failed, aborting`);
}
}

0 comments on commit 1aeeff8

Please sign in to comment.