Vue How to implement webpack and service-workers - vue.js

currently, my vue app has a package.json like this:
{
"name": "App",
"version": "0.1.2",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
}
}
In my main.js I have this:
if(process.env.NODE_ENV == 'development')
Vue.prototype.$http.defaults.baseURL = 'http://127.0.0.1:8000/api'
else
Vue.prototype.$http.defaults.baseURL = 'https://api.example.net/api'
I would now like to use webpack to build my app and manage certain things.
What is the best way to integrate webpack into my project now and optimize it?
I would like to use a service-worker to inform the users about a new version every time and ask them to reload the page.

You don't need to integrate Webpack into a Vue CLI scaffolded project. Vue CLI uses Webpack under the hood.
To configure the underlying Webpack, create vue.config.js with a chainWebpack or configureWebpack property:
// vue.config.js
module.exports = {
configureWebpack: {
//...
},
chainWebpack: config => {
//...
}
}
Regarding the Service Worker, you could use the #vue/cli-plugin-pwa plugin, which can be added to a Vue CLI project with this command:
$ vue add pwa

Related

How to add a typeorm in nuxt 3

I create an typeorm ESM project by running the command
npx typeorm init --name MyProject --database sqlite--module esm
as explained on https://typeorm.io. Running the project, everything works fine.
Then I create a nuxt 3 project: "npx nuxi init nuxt-project". Then I supplement the contents of the package.json and tsconfig.json files in the nuxt project with the appropriate typeorm values.
package.json:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"start": "node --loader ts-node/esm src/index.ts",
"typeorm": "typeorm-ts-node-esm"
},
"devDependencies": {
"#types/node": "^18.11.17",
"nuxt": "3.0.0",
"ts-node": "10.9.1",
"typescript": "4.9.4"
},
"dependencies": {
"#npmcli/fs": "^3.1.0",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.1.4",
"typeorm": "^0.3.11"
}
}
tsconfig.json:
{
// https://nuxt.com/docs/guide/concepts/typescript
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
"lib": [
"es2021"
],
"target": "es2021",
"module": "es2022",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"outDir": "./build",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true
}
}
I copy the entity, the configured data-source, the base and add code to the App.vue.
<script setup lang="ts">
import "reflect-metadata"
import { User } from "./db/entity/User.js"
import { AppDataSource } from "./db/data-source";
AppDataSource.initialize().then(async () => {
const user = new User()
user.firstName = "Timber"
user.lastName = "Saw"
user.age = 25
await AppDataSource.manager.save(user)
const users = await AppDataSource.manager.find(User)
console.log("Loaded users: ", users)
}).catch(error => console.log(error))
</script>
<template>
<div>
<NuxtWelcome />
</div>
</template>
I run a nuxt project and get an error:
[nuxt] [request error] [unhandled] [500] Column type for
User#firstName is not defined and cannot be guessed. Make sure you
have turned on an "emitDecoratorMetadata": true option in
tsconfig.json. Also make sure you have imported "reflect-metadata" on
top of the main entry file in your application (before any entity
imported).If you are using JavaScript instead of TypeScript you must
explicitly provide a column type.
Just in case, I add import reflect-metadata before the entity, but the error doesn't go away. I wonder if there is a good wizard who will guide me to the right path?
By the way, before that I tried to work with Sequelize, and also failed. It didn't fail on reflect-metadata, but:
Could not resolve "pg-hstore": const hstore = require("pg-hstore")
Any orm will work for me (that allows working with oracle, so Prisma is unfortunately out of the question). Any advice or an example?
Add to the User entity: #Column('text',{nullable:true}).

Tailwindcss does not automatically reflect the changes in the browser

Every time that I add a class, to see the changes I have to stop running nuxt, reload the vs code window and run "npm run dev" again. Then I can see the changes
My tailwind.config.js :
/** #type {import('tailwindcss').Config} */
module.exports = {
content: [
'./components/**/*.{js,vue,ts}',
'./layouts/**/*.vue',
'./pages/**/*.vue',
'./plugins/**/*.{js,ts}',
'./nuxt.config.{js,ts}'
],
theme: {
extend: {}
},
plugins: [require('daisyui')]
}
I place the tailwind.css file inside assets/css/tailwind.css
And import it inside my layout component: layouts/default.vue
My nuxt.config.ts:
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
css: ['~/assets/css/tailwind.css'],
build: {
postcss: {
postcssOptions: {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
}
}
}
})
My package.json:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare"
},
"devDependencies": {
"nuxt": "3.0.0-rc.11",
"tailwindcss": "^3.1.8"
},
"dependencies": {
"daisyui": "^2.31.0",
"firebase": "^9.10.0"
}
}
I think Nuxt.js is a framework that makes server-side rendering like NextJs for React.
In that way, all of the data and HTMLis rendered by the Nuxt server, which sends a generated "html/css" package to the client with only the css class that you used in your code.
So I would say it's normal to rebuild everytime you want to see your change for the css class you just add.
If you want to see directly the changes without rebuilding each time (like in the browser dev tool in order to write your css class easily), I would advise you to link in the HTML root file (index.html), the complete tailwind css sheet.
You can find a version on the tailwind doc page like this one : https://tailwindcss.com/_next/static/css/b606771d290f9908.css
Then you can remove the link at the end of your dev work.

How do i add vite.config.js file to my project?

I added vite to my project to run TailwindCSS. And now I am done and I wanted to build the project. But it only builds my index.html, not all my other pages (just using vanilla html and js). I know the line "npx tailwindcss init -p" to add the tailwind.config.js. Is there a command like this for Vite so i can say to build ./*.html?
{
"name": "tirisi",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "npm run build && vite preview"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"autoprefixer": "^10.4.2",
"postcss": "^8.4.8",
"tailwindcss": "^3.0.23",
"vite": "^2.8.6"
},
"dependencies": {
"#tailwindcss/line-clamp": "^0.3.1",
"daisyui": "^2.11.1"
}
}
From https://vitejs.dev/config/
The most basic config file looks like this:
// vite.config.js
export default {
// config options
}
Just create that file and paste that in. There's no vite equiv to npm init or tailwind init

Environment variables are getting fetched after build deploy to S3 is Vuejs

I have used Github Actions to build and deploy the Vue app to the S3 bucket. The problem is, the build script cannot fetch ENV from the .env or .env.production files, as those are not added in the commit.
I'm getting undefined when I log the ENV variables in my created() hook. As there are only two that are available, but the ones I wanted:
.env & .env.production as of now exactly the same:
VUE_APP_RZ_KEY=<Secret_Key>
VUE_APP_API_URL=<API_URL>
package.json
{
"name": "my_app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "npm run lint && vue-cli-service serve",
"serve:prod": "npm run lint && vue-cli-service serve --production",
"build": "vue-cli-service build --mode production",
"test:e2e": "vue-cli-service test:e2e",
"lint": "vue-cli-service lint --fix"
},
"dependencies": {
"axios": "^0.21.1",
"vue": "^2.6.11",
"vue-axios": "^3.2.5",
"vue-router": "^3.2.0"
},
...
}
Github Action .yaml file:
How do I make my ENVs make it to the built?
I propose to add the end data to GitHub Secrets.

vite.config.js => Uncaught SyntaxError: Cannot use import statement outside a module

I created a new repo with $ npm install vue-app and created some test components.
When i run the command $ npm run dev the application starts in localhost port 3000.
I try to add my component in a WP website. When i build the files and add to my theme, i receive the message:
Uncaught SyntaxError: Cannot use import statement outside a module
I found some documentation for webpack but not for vite.config.js.
Anyone can help?
vite.config.js
// vite.config.js
import vue from '#vitejs/plugin-vue'
export default {
plugins: [vue()]
}
package.json
{
"name": "vite-app",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"dependencies": {
"vue": "^3.0.5"
},
"devDependencies": {
"#vitejs/plugin-vue": "^1.2.3",
"#vue/compiler-sfc": "^3.0.5",
"vite": "^2.3.5"
}
}