@joaovpmamede Using npm run build:production
worked for me.
Could you explain a bit better?
Did you had to import
or require
the images in main.js?
What about during development? How do you use them?
Because everytime you run npm run watch
the dist
folder is ârecreatedâ.
Edit:
Okay nevermind, I was doing something wrong.
I finally got it working, just need to figure how to âmoveâ the images to their folder, e.g. assets/images/foo/bar.png
to dist/images/foo/bar.png
This is not figured out yet with the current setup. This will most likely have to be separate from webpack.
Yup, currently Iâm having some issues with svgs.
Canât inline them.
I guess Iâll have to go back to Sage 8.x.
I just sent a PR (already merged!) to roots/sage sage-9
branch. The assets images and fonts will keep the same subfolder structure. It works fine with me when I import them in my main.js.
main.js :
import "../images/foo/bar.png";
when running npm run build
assets/images/foo/bar.png
-> dist/images/foo/bar.png
I hope that works for you.
The settings that makes it work is in webpack.config.js
. The file loader needs a context= assets/
and then we can use [path][name].[ext]
to respect the folder tree.
@patrickv Hi, Iâve just updated my version of sage to be inline with your contributions, but now if I run watch the site loads with no css and js. But is I run build it works fine as the dist/ directory is created and used.
Any ideas why this might be?
Thanks
Itâs because in watch mode, the assets (css/js) are not stored on the hard drive but served by webpack. Normally you can access you site at the browser-sync address localhost:3000.
Hi,
Sorry, I was not clear. I get the build process produces those in the dist and that watch doesnât, but the watch doesnât load any css/js when viewed at http://localhost:3000/my-site itâs like webpack isnât loading
Have you edited the assets/config.json
? output > publicPath
and devUrl
should be set to your theme folder and dev server.
Iâve just changed the public path as I think that might be set up for Trellis? Iâve just got it working in the âtraditionalâ setup. But this hasnât helped.
{
"context": "assets",
"entry": {
"main": [
"./scripts/main.js",
"./styles/main.scss"
],
"customizer": [
"./scripts/customizer.js"
]
},
"output": {
"path": "dist",
"publicPath": "/wp-content/themes/starter-2016/dist/"
},
"devUrl": "http://localhost/Starter-Website/",
"devPort": 3000
}
You could try "publicPath": "/Starter-Website/wp-content/themes/starter-2016/dist/"
. The watch script (watch.js) is probably setting the publicPath to http://localhost:3000/wp-content/themes/starter-2016/dist/ and your files are at http://localhost:3000/Starter-Website/wp-content/themes/starter-2016/dist/
YES! Thatâs it.
Thank you.
I have another question ( sorry & itâs open to anyone here and not directly aimed at @patrickv ), in the main.js file could you load up admin only js?
Like this âŚ
'wp-admin': {
init: function() {
alert();
},
finalize: function() {
// NOTE:
// JavaScript to be fired on the home page, after the init JS
}
},
and have webpack âwatchâ it also?
I think I would add en enrty to assets/config.json.
{
âcontextâ: âassetsâ,
âentryâ: {
âmainâ: [
â./scripts/main.jsâ,
â./styles/main.scssâ
],
âcustomizerâ: [
â./scripts/customizer.jsâ
],
âadminâ: [
â./scripts/admin.jsâ,
â./styles/admin.scssâ
]
âŚ
Then, in src/admin.php, I would enqueue both scripts and styles with the admin_enqueue_scripts
.
add_action(âadmin_enqueue_scriptsâ, function () {
wp_enqueue_style(âsage/admin.cssâ, asset_path(âstyles/admin.cssâ), false, null);
wp_enqueue_script(âsage/admin.jsâ, asset_path(âscripts/admin.jsâ), [âjqueryâ], null, true);
}, 100);
CSS is inlined with the JavaScript in âwatchâ, so you kind of need to load both js/css if you want to load styles.
I hope it helps! Maybe someone else would have a better way, but that seems to work well for me.
Any ideas on how to ignore third party scripts from being processed on build/watch ? I keep getting eslint errors.
I added this in config.json
"iconic.min": [
"./vendor/iconic/js/iconic.min.js"
]
And enqueueâd the file in setup.php
wp_enqueue_script('wplynx/iconic.min.js', asset_path('vendor/iconic/js/iconic.min.js'), null, null, true);
So to be clear, Sage 9 currently doesnât move (or optimize in the case of fonts) fonts or images to the dist
folder?
Not yet. This is probably going to be handled separately from Webpack.
https://webpack.github.io/docs/configuration.html#module-loaders
You can âexcludeâ the vendor dir from the .js loader
@austin, am I reading that correct? Youâre planning to not handle images and fonts with Webpack?
The current version of sage-9 optimize images with image-webpack-loader
and copy fonts to dist/fonts
folder.
https://github.com/roots/sage/blob/sage-9/webpack.config.js#L121-156