loading a css file from 'assets' directory in a NUXT layout - node.js

in a nuxt layout (default.vue) I want to load an image and a css file from assets folder
the image loaded successfully, but the css file not, why?
/layouts/default.vue
<template>
<img src="~assets/photo.jpg" />
<!-- converted to /_nuxt/assets/photo.jpg and loaded successfully -->
</template>
<script>
export default{
head:{
link: [ { rel: 'stylesheet', href: '~assets/style.css' }]
}
}
</sript>
when I view the source code:
<link href="~assets/style.css" />
and it fails to be loaded
also navigating to http://localhost:3000/_nuxt/assets/style.css faild, but http://localhost:3000/_nuxt/assets/photo.jpg successed
note:
I don't want to put style.css in 'static' folder as I want to load it via webpack css-loader to add caching hashes

The image src is automatically compiled by Vue, you can see more at relative path import; From the docs:
all asset URLs such as <img src="...">, background: url(...) and CSS
#import are resolved as module dependencies.
For a custom path besides cases listed above, you need to explicitly require the path for it to be compiled as a path to static assets:
export default{
head:{
link: [{ rel: 'stylesheet', href: require('~assets/style.css') }]
}
}

Related

vite build always using static paths

I have a simple setup with an index.html, some js file and a sass file, building it with vite. I am using vite defaults without a config file.
After running a build the index.html in the dist folder references everything as static paths:
<head>
<script type="module" crossorigin src="/assets/index.b850bc1f.js"></script>
<link rel="stylesheet" href="/assets/index.04d1c13d.css">
</head>
The same happens to url() paths in css: They are turned into static paths as well.
My question is: Is there a configuration option to make vite output relative paths, so:
<head>
<script type="module" crossorigin src="assets/index.b850bc1f.js"></script>
<link rel="stylesheet" href="assets/index.04d1c13d.css">
</head>
This leading path is the base URL, configured by the base option, which is / by default.
You can remove the base URL by setting base to an empty string, making the path relative to its deployment directory:
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
base: '', 👈
})
demo

Webpack Encore didn't write css and js in folder "build" in production environment of Symfony 4

I'm working with Symfony 4 and my deployment platform is Microsoft Azure. This is my website :https://synergie-2i.azurewebsites.net/SynergieInformatique/public/index.php/.
My problem is when my environment is in production, my css and js aren't loaded. When inspect my page in services i see my folder SynergieInformatique/public and an other folder name build. Inside there is 3 css files but there is nothing inside. I think webpack didn't write my css in these files.
I have try to copy my css and put in the empty css file and it works but i don't know how make this with symfony.
When i'm in dev environement, the folder build is in SynergieInformatique/public and my css is loaded.
###webpack.config.js : ###
var Encore = require('#symfony/webpack-encore');
Encore
.setOutputPath('public/build/')
.setPublicPath('/build')
.addEntry('app', './assets/js/app.js')
.addEntry('owlcarouselcss','./node_modules/owl.carousel/dist/assets/owl.carousel.min.css')
.addEntry('owlcarouseljs','./node_modules/owl.carousel/dist/owl.carousel.min.js')
.splitEntryChunks()
.enableSingleRuntimeChunk()
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
.enableVersioning(Encore.isProduction())
.configureBabel(() => {}, {
useBuiltIns: 'usage',
corejs: 3
})
.enableSassLoader()
.autoProvideVariables({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
});
module.exports = Encore.getWebpackConfig();`
What are the step for deploy an symfony 4 application with Webpack and how i can have my css and js load on production environement ?
If you need more informations ask me :)
I have found, my links were bad. I use {{ encore_entry_link_tags('app') }} and this doesn't search at the good folder so i remplace this by <link rel="stylesheet" href="/SynergieInformatique/public//build/app.css"> and it works.

Webpack file loader configuration for static images

Hy,
i have /images folder with /static sub-directory with some images.
how i can config webpack to make public avaiable images from images/static on path dist/images/image-name.png, and preserve names?
thank you
You can use CopyWebpackPlugin to copy all the files to required folder of your build.
plugins: [
new CopyWebpackPlugin([
{ from: 'src/images/static', to: './dist/images' }
])
]
Later you you have to import them like this.
<img src="path/to/dist/image.png">
Otherwise you can use file-loader to make the file URL available for programmtic usage
{
test: /\.(jpe?g|png|gif|svg)$/i,
loader: "file-loader?name=/images/[name].[ext]"
}
and your images will be emitted to:
dist/images/
Later you can import images like this:
import url from './file.png'
<img src={url}>

bootstrap.css doesn't show up in the browser

I am setting up a project with angular 2 and gulp using this tutorial. http://blog.codeleak.pl/2016/03/quickstart-angular2-with-typescript-and.html
The only change I made is I added bootstrap to the package.json, but it doesn't show in the browser. It shows in the node_modules and the lib folder in my project in the IDE. But the bootstrap classes don't get applied if I use them. The bootstrap.min.css file doesn't show in the browser either.
I added the 2 lines about bootstrap to my gulp file
gulp.task("libs", () => {
return gulp.src([
'es6-shim/es6-shim.min.js',
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'reflect-metadata/Reflect.js',
'rxjs/**',
'zone.js/dist/**',
'#angular/**',
'bootstrap/dist/js/bootstrap.min.js',
'bootstrap/dist/css/bootstrap.min.css'
], {cwd: "node_modules/**"}) /* Glob required here. */
.pipe(gulp.dest("build/lib"));
});
I added this line in index.html to include it
<link rel="stylesheet" type="text/css" src="lib/bootstrap/dist/css/bootstrap.min.css">
Here is my project structure
EDIT: Just to clarify, I see the bootstrap.css files copied over in the lib folder in the IDE, but I don't see it in the browser. See screenshot below from Chrome dev tools
Any clue as to what I may be missing? Github repo - https://github.com/richa008/Angular-starter-app-with-gulp
This should import all relevant files and directories:
gulp.task("libs", () => {
return gulp.src([
'es6-shim/es6-shim.min.js',
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'reflect-metadata/Reflect.js',
'rxjs/**',
'zone.js/dist/**',
'#angular/**',
'bootstrap/dist/**/*.+(js|css.map|css|eot|svg|ttf|woff|woff2)'
], { cwd: "node_modules/**" }) /* Glob required here. */
.pipe(gulp.dest("build/lib"));
});

Require.js: "file not loading" in AMD process

I use the Require.js "AMN" to load my files, this is my HTML file.
<script data-main="js/main.js" href="js/require.js"></script>
when i load my main.js - i made this config to load my jquery file.
require.config({
baseUrl: 'js',
paths: {
"jQuery":'lib/jquery-1.9.1.min'
}
});
require(['jQuery'], function ($) {
console.log($);
})
But i am not getting any console. all my paths are correct.
my index.html file located at the parent of the js filder.
Help me to resolve this.
jQuery module for AMD should always be lowercase. Check what path is used when browser requesting files using developer tools in chrome or fiddler. This way you will know if you configured it right.
UPDATE: please correct the syntax for loading script. Must be src instead of href.
<script data-main="js/main.js" src="js/require.js"></script>

Resources