Import and use NPM package (Sluggo) in Astro - npm-package

I want to use this NPM package, Sluggo (https://www.npmjs.com/package/sluggo), in Astro, in the frontmatter of an Astro component.
After installation, I import it following Astro docs:
import { sluggo } from 'sluggo';
When I try to use it following Sluggo docs:
var sluggedString = sluggo('# monkey\'s are elab؉؉orate fools##');
I get this error: vite_ssr_import_1.sluggo is not a function
I can understand that "sluggo" is not a function, but how can I access the function inside the package?

It looks like sluggo is not a named export, so you need to do:
import sluggo from 'sluggo';

Related

How to use Hook inside Nextjs middleware?

I have a middleware like this
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { useCheckIsAdminQuery } from "../../generated/graphql";
export function middleware(req: NextRequest, ev: NextFetchEvent) {
const {data,loading,error} = useCheckIsAdminQuery()
if(data?.checkIsAdmin){
return NextResponse.next()
}else{
const url = req.nextUrl.clone()
url.pathname = '/404'
return NextResponse.redirect(url)
}
}
useCheckIsQuery() just a hook was generated by codegen package, but I cannot call this hook inside middleware.
The error display
Invalid hook call. Hooks can only be called inside of the body of a function component.
How can i fix it?
According to React docs: "Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns.".
Nextjs' middleware is not a React function, so you cannot use hooks inside it. The solution to your problem is to create a hook with your custom logic, and manually calling it in the page you'd like to take effect.

Load function from support folder cypress

In my support folder I have a folder called action. In there is a file calles login-action.js. The code in this file looks like this:
class LoginActions{
login(usernameField, passwordField, username, password) {
cy.get(usernameField).first().type(username);
cy.get(passwordField).last().type(password);
}
}
export default new LoginActions();
Now I want to use this login function in a test
loginActions.login('[data-test=username]', '[data-test=password]', this.user.username, this.user.password);
This is how I call it in the test. But somehow this is not working. It says that login is undefined.
I don't think that you can export new object like you are doing, I usually export only the class like this:
export default LoginActions();
Do you import and create new object loginActions like this:
import LoginActions from '../../../support/pageObjects/{loginActionsPage}'
const loginActions = new LoginActions();
And after this you can:
loginActions.login('[data-test=username]', '[data-test=password]', this.user.username, this.user.password);

Go import local package

I have a main.go
package main
import (
"context"
"fmt"
"log"
model "model"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(...){
}
I try to import model which is in the directory model and the file is called model.go
It just contains:
package model
type xxx struct {
xxx
}
I try to import this in the main but I have the error:
build: cannot load model: cannot find module providing package model
If your module model is not local then you can use Tonys answer and it will work fine but if you are using this module locally then you will need to add the paths in your go.mod file.
So for example, Local module model contains only model.go which has the following content
package model
type Example struct {
Name string
}
func (e *Example) Foo() string {
return e.Name
}
For this local module must have have to init the module with the command go mod init model and the content of the ./model/go.mod will be
module model
go 1.13
In the main module in which you are importing this module you need to add the following line
require model v1.0.0
replace model v1.0.0 => {Absolute or relative path to the model module}
So, your main testing module's go.mod file will look like this
module main
require model v1.0.0
replace model v1.0.0 => ./model
go 1.13
By setting this up you can use this module in this test module with just import "model"
Hence when testing the module with the main method
package main
import (
model "model"
)
func main() {
example := model.Example{
Name: "Hello World",
}
println(example.Foo())
}
The output will be
Hello World
If your go.mod looks like this:
module github.com/meakesbia/myproject
go 1.14
then you need to import the module package using the full module reference:
import "github.com/meakesbia/myproject/model"
If it's an entirely local project then replace github.com/meakesbia with the model name from go.mod e.g.:
module meakesbia/myproject
go 1.14
import "meakesbia/myproject/model"
You don't need to add a replace directive to the go.mod file unless you're making local changes to a module that is imported from e.g. github.

Registering vue components in Laravel package

I have been trying very hard to register vue component in my own test package developed for laravel. I did even check laravel\horizon and some other packages but I can't figure out how to do it. I'm following Laravel Package Development
so my package is outside my Laravel app. And my package structure is like below:
vendor
packagename
resources
js
components
examplecomponent.vue
app.js
AppServiceProvide.php
package.json
webpack.mix.js
For the vue component it's just the Laravel's example, and for the app.js too, basically didn't change anything because it's just for testing purpose.
I tried to copy vue components to the resources\js\compnents with "vendor:publish" command it's copying it but still not registered in app.js.
So the question is what is the best way of registering vue components in Laravel package or vendor? How Laravel Horizon package register it's components, I did check all the source of code of Laravel/horizon there's no command like "npm run" or copying the components and adding it in some place in the main app, it lloks like the npm is checking vendor files searching for package.json file in laravel/horizon and then register the componenets. if it's so why my vue compnents is not registered.
Laravel Horizon Way:
So after reading the entire code of Laravel Horizon I could understand there way which it's like below:
1- First thing you register your vue components like Laravel do for example-component in app.js. so you have to make your own app.js for your Laravel package inside resourses\js:
Vue.component('example-component', require('./components/ExampleComponent.vue').default);
2- second make webpack.mix.js file in the root of your package and copy your app.js into public\vendor\packagename\ like below:
mix
.options({
terser: {
terserOptions: {
compress: {
drop_console: true
}
}
}
})
.setPublicPath("public")
.js("resources/js/app.js", "public")
.version()
.webpackConfig({
resolve: {
symlinks: false,
alias: {
"#": path.resolve(__dirname, "resources/js/")
}
},
plugins: [new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)]
});
3- run "npm run dev" in your package to compile your app.js in public folder inside your package.
4- publish your public\app.js file in appserviceprovider.php of your package:
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/xoadmin'),
], 'views');
5- now in your resource view files you can add the app.js file like below:
<script src="{{asset(mix('app.js', 'vendor/packagename'))}}"></script>
6- make a command to publish the app.js file from your package public folder to laravel's public\vendor\package folder.
<?php
namespace Vendor\PackageName\Console;
use Illuminate\Console\Command;
class AssetsCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'packagename:assets';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Re-publish the packagename assets';
/**
* Execute the console command.
*
* #return void
*/
public function handle()
{
$this->call('vendor:publish', [
'--tag' => 'packagename-assets',
'--force' => true,
]);
$this->call('vendor:publish', [
'--tag' => 'views',
'--force' => true,
]);
}
}
All of the codes above can be found in Laravel Horizon Github repository. I'm just explaining the way to make it easier for people to understand.
There is no best practice, but if you want to do it the easiest way possible, you can do it like this:
const components = require.context('./components', true, /\.vue$/i);
components.keys().map((key) => {
return Vue.component(`${key.split('/').pop().split('.')[0]}-component`, components(key).default)
});
Any file found deeply (2nd argument to require.context) within your components directory under resources/js will automagically be added as a Vue component with -component added to the end.
So you can just run vendor:publish and your components will appear there automagically. If you're running yarn watch or yarn hot then they will be automagically registered on publish, as well.
The reason this works is because we're telling webpack to create a context that matches our expression within a given directory. You can read more about require.context here.

Ember 2.16.x: Accessing Ember.Handlebars.Utils.escapeExpression with new import syntax

How does one access the Ember.Handlebars.Utils.escapeExpression function using the new import syntax in Ember 2.16.x and above?
The following code snippet comes from the Writing Helpers section of the Ember docs. (FYI, there are a couple of unrelated errors in the original, which I have cleaned up in the code below.)
import { helper } from "#ember/component/helper";
import Handlebars from "handlebars";
import { htmlSafe } from "#ember/string";
export function makeBold(param /*, ...rest*/ ) {
let value = Handlebars.Utils.escapeExpression(param);
return htmlSafe(`<b>${value}</b>`);
}
export default helper(makeBold);
If I use the code above, I get the following error:
Could not find module 'handlebars' imported from 'ember-app/helpers/make-bold'
As of right now the Handlebars.Utils.escapeExpression function is not yet exported by the New Module Imports (aka. RFC 176). You should keep using it from the Ember import for now:
import Ember from 'ember';
Ember.Handlebars.Utils.escapeExpression(...)
An open GitHub issue for this exists at https://github.com/ember-cli/ember-rfc176-data/issues/12
The guides page that you linked appears to be mistaken and we need to fix that particular snippet. Sorry about that!

Resources