How do I create an npm package that I can import into sveltekit? - svelte-3

I would like to create a simple npm package and import that into svelte components, however, I cannot seem to use index files to import deeply nested files, e.g.
// routes/test.svelte
<script lang="ts">
import { Test } from '#my-co/my-lib/dist/folder1/folder2/test';
const test = new Test('foo', 'bar');
</script>
works, but
// routes/test.svelte
<script lang="ts">
import { Test } from '#my-co/my-lib';
const test = new Test('foo', 'bar');
</script>
does not. I have the following in the index.ts file in my npm module:
export { Test } from './folder1/folder2/test';
This strangely also does seem to work in ssr (dev server output in console seems to pick the import {Test} from '#my-co/my-lib' correctly), but not in the browser, where I get the error that Test is not a constructor...
Npm library package.json:
{
"name": "#myco/my-lib",
"version": "0.0.2",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"prepare": "npm run build"
},
"author": "redacted",
"license": "ISC",
"dependencies": {
"#types/rosie": "0.0.40",
"#types/slug": "^5.0.3",
"rosie": "^2.1.0",
"slug": "^5.1.1"
}
}
Npm library tsconfig
{
"compilerOptions": {
"declaration": true,
"strictNullChecks": true,
"outDir": "dist",
"moduleResolution": "node",
"module": "es2015",
"target": "es5",
"sourceMap": true,
"lib": ["es2015", "dom"],
"rootDir": "src"
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
Lib structure
my-lib/
| dist/
| node_modules/
| src/
| | folder1/
| | | folder2/
| | | | test.ts
test.ts
export class Test {
foo: string;
bar: string;
constructor(foo: string, bar: string) {
this.foo = foo;
this.bar = bar;
}
testMe() {
console.log("foobar", this.foo, this.bar);
}
}

The svelte-kit package command should automatically do everything for you (docs).
This Youtube video should explain everything.
The steps it provides to publish are:
npm init svelte#next project-name
cd project-name
Create component
npx svelte-kit package
cd package
Login to npm / create an account
npm publish --access public

While Caleb's answer did not really help in my case, I think it was a bit of a pointer as to what was going wrong. I did not intend to write a svelte-focused Component library, but rather a general model/factory library to be reused between the front- and backend.
I did a couple of things since this issue occurred and here is a list of things I think went wrong:
Local linking of the npm package into my project caused issues (npm link). I have very little experience with all the build tools in sveltekit but figured out where those stale/non-functional libs are referenced from. I deleted the node-modules/.vite folder which sometimes resolved my issues (I greped for seemingly cached values which led me to this directory. In my dev experience quite uncommon to have found build artifacts in node-modules, so that was a bit tedious to find in that place).
I converted my npm package to a hybrid ESM/CJS module (previously CommonJS only). I'm unsure whether that resolved the initial issues, but might be worth a try.
I think the main issue was the local linking and some sort of build caching going on in vite.
That being said, I haven't encountered any issues with the library ever since.

Related

Electron-forge package generates an empty out folder

I am working on app with electron and electron-forge, this app is being built on a virtual machine with no internet connection, so I got electron binaries files, and set the electron_config_cache to the path where I located the new binaries as well as for cacheRoot for packagerConfig in the package.json file, the problem is that:
When I run yarn package (electron-forge package) I am getting Done with green check next to each step which had been called by yarn package .. but the out folder is empty, while it should has appName-win32-x64 folder which contains .exe.
so does anyone have an idea regarding this?
Be sure that your package.json has the correct settings, including the "main" attribute as well as build directories. It seems electron forge uses electron-packager as its packaging platform so:
Following these instructions:
From Electron forge,Electron forge - packager options, Electron-packager options
Without a forge.config.js file:
Add this to your package.json:
"config": {
"forge": {
"packagerConfig": {dir:"./path/to/your/dir"},
"makers": [
{
"name": "#electron-forge/maker-zip"
}
]
}
Or with a forge.config.js file:
In your package.json, make sure to have:
{
"name": "my-app",
"version": "0.0.1",
"config": {
"forge": "./forge.config.js"
}
}
In your forge.config.js:
{
packagerConfig: {dir:"./path/to/your/dir"},
electronRebuildConfig: { ... },
makers: [ ... ],
publishers: [ ... ],
plugins: [ ... ],
hooks: { ... },
buildIdentifier: 'my-build'
}
I think it is most probable that forge is just not pointed to the correct dir, add the dir setting shown above.
The problem was a virtual machine problem, nothing related to electron forge itself, the VM was not able to copy the content from the temp folder to the out folder, this can be solved by changing the temp folder

npm run make is not working in electron-forge

I have created and electron application and trying to use electron forge for building purpose.
Please find below command which i have run it for creating the electron application:
#npm i -g create-react-app
#npm i -g #electron-forge/cli
#npx create-electron-app my-ele-app
The above last command created a project my-ele-app. and now i am able to start the application as well.
#npm start.
content of package.json file is:
{
"name": "my-ele-app",
"productName": "my-ele-app",
"version": "1.0.0",
"description": "My Electron application description",
"main": "src/index.js",
"scripts": {
"start": "electron-forge start",
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "echo \"No linting configured\""
},
"keywords": [],
"author": {
"name": "rohit",
"email": "rohit#xyz.com"
},
"license": "MIT",
"config": {
"forge": {
"packagerConfig": {},
"makers": [
{
"name": "#electron-forge/maker-squirrel",
"config": {
"name": "my_ele_app"
}
},
{
"name": "#electron-forge/maker-zip",
"platforms": [
"darwin"
]
},
{
"name": "#electron-forge/maker-deb",
"config": {}
},
{
"name": "#electron-forge/maker-rpm",
"config": {}
}
]
}
},
"dependencies": {
"electron-squirrel-startup": "^1.0.0"
},
"devDependencies": {
"#electron-forge/cli": "^6.0.0-beta.55",
"#electron-forge/maker-deb": "^6.0.0-beta.55",
"#electron-forge/maker-rpm": "^6.0.0-beta.55",
"#electron-forge/maker-squirrel": "^6.0.0-beta.55",
"#electron-forge/maker-zip": "^6.0.0-beta.55",
"electron": "12.0.9"
}
}
Now when i am running below command, it is throwing error:
#npm run make
Error is:
> my-ele-app#1.0.0 make
> electron-forge make
√ Checking your system
√ Resolving Forge Config
An unhandled rejection has occurred inside Forge:
Error: Could not find module with name: #electron-forge/maker-squirrel. Make sure it's listed in the devDependencies of your package.json
at _default (C:\Users\212807091\Desktop\Rohit\Office Note\RBAC\Electron_project\npx_electrong\my-ele-app\node_modules\#electron-forge\core\src\api\make.ts:125:15)
at C:\Users\212807091\Desktop\Rohit\Office Note\RBAC\Electron_project\npx_electrong\my-ele-app\node_modules\#electron-forge\cli\src\electron-forge-make.ts:44:5
Electron Forge was terminated. Location:
{}
NOTE: i can see there is module available in node_modules folder:
my-ele-app\node_modules#electron-forge\maker-squirrel
If anyone here has any idea how to fix this issue. please provide the answer. Thanks!
In my cases, it makes error when the description or author is empty in package.json file.
I got the same error after following the "Getting Started" instructions. Nothing resolved it until I ran npm install -g #electron-forge/cli#beta -- after that finished I was able to successfully run electron-forge make.
Also running npm run make did the same as electron-forge make as I didn't appear to have it installed. Another thing I noticed is that if I run npm run make with maker-squirrel at version 6.0.0-beta.55 then I get this error: Could not find module with name: #electron-forge/maker-squirrel. However, if I re-install maker-squirrel as npm install --save-dev #electron-forge/maker-squirrel#6.0.0-beta.33 and re-run npm run make then I get an out folder with an exe.
you have installed all the required dependencies so just run this command
npm run package
And after this a folder with name out is generated and inside that your .exe file will be present
I had a similar issue but I followed the instructions from here: https://www.electronjs.org/docs/latest/tutorial/quick-start#package-and-distribute-your-application
I missed calling npx electron-forge import. So the full pipeline looks like this:
npm install --save-dev #electron-forge/cli
npx electron-forge import
npm run make
Check the documentation. i solve same problem with this
If you are using Windows, this works for me:
Run npm i electron-winstaller --save-dev --ignore-scripts
Install 7zip x86 in your computer. After installed, go to C:\Program Files (x86)\7-Zip and copy both 7z.dll and 7z.exe files to /path/to/your_project/node_modules/electron-winstaller/vendor if they are not still there.
Run npm electron-forge import again
Then try run npm run make again. I hope it works.

exclude files from build in electron via package.json

I am trying to build an electron app and during build creation exclude some files and folders.
I read similar topics and also bug thread on github but still cannot make it properly.
Final goal is to exclude all files with extension .py and folder named tests and all its subfolders.
However even simple example with one file with explicit name does not work. Can you please point me to my mistake?
Here is package.json
{
"name": "Build-Downloader",
"version": "0.1.0",
"main": "electron_main_win.js",
"scripts": {
"start": "electron ."
},
"dependencies": {
"axios": "^0.19.2",
"python-shell": "^1.0.8"
},
"build": {
"files": [
"!electron_backend.py"
]
}
}
and the command line I use to compile my package:
electron-packager ./ --platform=win32 --arch=x64 --electron-version=8.2.3 --out=electron_build --overwrite
Finally I found a solution on my own. I have already thrown a big stone to myself due to my stupidity :)
However I would like to share my knowledge in case somebody will need this info.
electron-packager supports only --ignore command line arguments with RegEx
Note: you may use as many ignores as you want
Note2: do not mix names electron-packager and electron-builder, builder supports package.json :)
So finally ultimate solution was:
electron-packager ./ --platform=win32 --arch=x64 --electron-version=8.2.3 --out=electron_build --overwrite --ignore="^.*\.py" --ignore="\/node_modules" --ignore="\/tests"

VS Code tasks 2.0 troubles

I use:
VS Code 1.14.1
Tasks 2.0
node 6.11.1
My problem is that some tasks are working, and some are not.
Here my tasks.json file.
{
"version": "2.0.0",
"tasks": [
{ // This task work fine.
"label": "tsc",
"type": "typescript",
"tsconfig": "tsconfig.json",
"group": {
"kind": "build",
"isDefault": true
}
},
{ // This task work fine.
"taskName": "css",
"type": "shell",
"command": "./scss.sh",
"windows": {
"command": "./scss.cmd"
},
"presentation": {
"reveal": "always",
"panel": "new"
}
},
{ // This task not work. (See output #1 below)
"taskName": "npm-install",
"type": "shell",
"command": "npm install",
"problemMatcher": [],
"presentation": {
"reveal": "always",
}
},
{ // This task not work. (See output #2 below)
"type": "gulp",
"task": "clean",
"problemMatcher": []
}
]
}
Output #1 - When I try to run npm task with type shell\npm, I get the same result:
> Executing task: npm install <
Usage: npm <command>
where <command> is one of:
access, adduser, bin, bugs, c, cache, completion, config,
ddp, dedupe, deprecate, dist-tag, docs, edit, explore, get,
help, help-search, i, init, install, install-test, it, link,
list, ln, login, logout, ls, outdated, owner, pack, ping,
prefix, prune, publish, rb, rebuild, repo, restart, root,
run, run-script, s, se, search, set, shrinkwrap, star,
stars, start, stop, t, tag, team, test, tst, un, uninstall,
unpublish, unstar, up, update, v, version, view, whoami
npm <cmd> -h quick help on <cmd>
npm -l display full usage info
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
C:\Users\usr\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm#3.10.10 C:\Program Files\nodejs\node_modules\npm
Output #2 - When I try to run gulp task I get next (gulp package installed as global (npm install gulp -g),(I have gulpfile.js with correct content)):
> Executing task: node_modules\.bin\gulp.cmd clean <
clean: node_modules.bingulp.cmd: command not found
I can use gulp, npm, node and other tools from cmd as usually, and it's works fine, but not in the VS Code.
Current solution of this thing:
Create batch files for each stuff.
Create shell task for each batch file (as scss.cmd)
Run those tasks.
Yep, it works well, but you need to do too much action for simple tasks.
What can you advise in this regard?
I found a solution to this problem, the problem was in the default terminal application.
By default on Windows OS visual studio code use powershell as terminal app, in my case, I changed it to the git-bash app and got this problem. After that I changed it back to powershell and all works fine.
I did not bother about why gitbash does not work, it is possible that additional configuration is required in the configuration in the vs code.
This is an issue in VS Code. A < gets added at the end of commands, making them fail. There doesn't seem to be a direct fix at the moment.

ASP.NET 5 Client Side Depdency Management - Bower

I'm trying out the new ASP.NET 5 with MVC 6, and I'm using bower to manage all my client-side dependencies. Everything is working fine.
But I have a question: When I add a dependency (let's say jQuery). It adds both the /dist and /src along with bower configuration files to the /lib folder of wwwroot. How do I make it include just the compiled source for usage? (So I can reference it in my pages via /lib/jquery/jquery.js?
I have recently been playing in this space and following is something that I have tried:
Deleted the .bowerrrc file to enable installing in the default bower_components folder under the project folder rather than under wwwroor\lib as anything under wwwroot tends to get published.
Added "main-bower-files": "2.9.0" to package.json. This package gets all the files mentioned in the main property of each installed package's bower.json files.
Created a gulp task using the above package
gulp.task('copyMainFiles', function () {
return gulp.src(mainBowerFiles(), { base: 'bower_components' })
.pipe(gulp.dest('wwwroot/lib'));
});
Added a postrestore step to your application's project.json file
"scripts": {
"postrestore": "gulp copyMainFiles",
"prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
}
Updated my application's bower.json to copy files which are not listed in main (like some packages do not have min files as main files..ex: jQuery). The following settings are read by main-bower-files:
"overrides": {
"jquery": {
"main": [ "dist/jquery.js", "dist/jquery.min.js" ]
},
"hammer.js": {
"main": [ "hammer.js", "hammer.min.js" ]
},
"bootstrap": {
"main": [
"./dist/js/bootstrap.js",
"./dist/js/bootstrap.min.js",
"./dist/css/bootstrap.css",
"./dist/css/bootstrap.min.css"
]
}
}
Finally had to update the jquery-validation package to use 1.14.0 instead of 1.11.1 as the previous version does not dist folder and indeed no bower.json...

Resources