Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with Linking Library #141

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ If you want to make changes to the lens library while integrating those changes

To controll that the linking of the local version worked, run `npm ls | grep lens` in your applications repository and verify that it refers to your local lens repository.

#### Additional Note for Applications using Vite
When your depending application is built with vite, you need to ensure to add the following to your vite.config.ts:

``` javascript
export default defineConfig({
// ...
optimizeDeps: {
exclude: ['@samply/lens']
}
});
```

## Style Integration

To import the default stylings, use
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"scripts": {
"start": "npm run dev -s",
"dev": "vite --config vite.demo.config.ts",
"build": "vite build",
"build": "rimraf dist && vite build",
"build:demo": "node options_tester.cjs && vite build --config vite.demo.config.ts",
"preview": "vite preview --config vite.demo.config.ts",
"check": "node options_tester.cjs && svelte-check --tsconfig ./tsconfig.json",
"lint": "lint-staged",
"watch": "rimraf dist && vite build --watch",
"link": "wait-on dist/types.d.ts && cd dist/ && npm link",
"link": "wait-on dist/types.d.ts && npm link",
"prepublishOnly": "npm run build -s",
"security:check": "npm audit --omit=dev --audit-level high",
"prepare": "husky install",
Expand Down
50 changes: 3 additions & 47 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ export default defineConfig({
root: "./packages/lib/",
build: {
outDir: "../../dist",
emptyOutDir: true,
lib: {
entry: "./index.ts",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formats: bundleComponents ? (["es", "esm", "umd"] as any) : ["es"],
name: pkg.name.replace(/-./g, (char) => char[1].toUpperCase()),
fileName: (format) =>
({
es: `${pkg.name}.js`,
esm: `${pkg.name}.min.js`,
umd: `${pkg.name}.umd.js`,
es: `${pkg.name.replace("@samply/", "")}.js`,
esm: `${pkg.name.replace("@samply/", "")}.min.js`,
umd: `${pkg.name.replace("@samply/", "")}.umd.js`,
})[format],
},
rollupOptions: {
Expand All @@ -45,7 +44,6 @@ export default defineConfig({
}),
dts({
insertTypesEntry: true,
outDir: "../../dist",
include: ["**/types/*.ts"],
afterBuild: afterBuild,
}),
Expand Down Expand Up @@ -81,14 +79,6 @@ function minifyEs(): PluginOption {
*/
function afterBuild(): void {
concatenateDeclarationFiles("./dist/src/types/");

/**
* Building somehow adds another @samply folder to the dist folder so this workaround is needed
* to move the files to the root of the dist folder and delete the unnecessary folder
*/
if (fs.existsSync("./dist/@samply/")) {
restructureDirectory("./dist/@samply/");
}
}

/**
Expand All @@ -107,40 +97,6 @@ function concatenateDeclarationFiles(folderPath: string): void {
fs.rmSync("./dist/src", { recursive: true, force: true });
}

/**
* Restructure the directory to match the npm package structure
* this removes the @samply folder and moves the files to the root of the dist folder
* @param path the path where the files are located
*/
function restructureDirectory(path: string): void {
moveFile(`${path}lens.js`, "./dist/lens.js");
moveFile(`${path}lens.min.js`, "./dist/lens.min.js");
moveFile(`${path}lens.umd.js`, "./dist/lens.umd.js");
}

/**
* Moves a file from the oldFile to the target
* Removes the @samply folder if it is empty
* @param oldFile the path of the old file
* @param target the path of the target file
*/
function moveFile(oldFile: string, target: string): void {
let attempts = 0;
const interval = setInterval(() => {
attempts++;
if (fs.readFileSync(oldFile, "utf-8").length > 0) {
fs.renameSync(oldFile, target);
clearInterval(interval);
} else if (attempts > 10) {
clearInterval(interval);
throw new Error("File not found");
}
if (fs.readdirSync("./dist/@samply/").length === 0) {
fs.rmSync("./dist/@samply/", { recursive: true, force: true });
}
}, 1000);
}

/**
* removes all import statements from a file
* @param filePath the path of the file
Expand Down
Loading