Hi @tonyng137
When you import both ./style.scss and a third-party CSS file like leaflet/dist/leaflet.css inside your index.js (which is the editor script), they are treated as part of the editor-only bundle and compiled into index.css, not style-index.css, because any CSS imported from a JavaScript file is bundled with the corresponding JS entry. To ensure that third-party styles like Leaflet’s CSS are included in style-index.css—which is loaded on both the frontend and editor—you should import them directly inside style.scss, or alternatively, set up a dedicated frontend JS entry file (e.g., frontend.js) and import both style.scss and the third-party CSS there.
Checkout this https://developer.ww.wp.xz.cn/block-editor/reference-guides/block-api/block-metadata/ for more details.
Regards
@sandeepdahiya
Hi again :-),
I used the @wordpress/create-block tool to scaffold the initial files. By default:
style.scss is already imported in index.js
editor.scss is already imported in editor.js
As shown in my screenshots, those files are correctly imported by default.
The @wordpress/scripts (included by @wordpress/create-block) build process then compiles:
style.scss (from index.js) → into style-index.css (frontend and editor)
editor.scss (from editor.js) → into index.css (editor only)
So logically, if you import any CSS/SCSS files into editor.js, they get compiled into index.css (the editor-only file) — and they do, I tested it!
Therefore, importing a CSS/SCSS file into index.js should compile it into style-index.css, right?
But that’s not what happens — it still ends up in index.css, the editor-only file. 🤯
Long story short. I tried your first suggestion and imported Leaflet into style.scss, but the result was the same: the Leaflet CSS still ended up in index.css (editor-only file).
Also, I’m not sure importing third-party S/CSS directly into style.scss or editor.scss is the right approach — especially since the comment in index.js says:
“Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.”
Your second suggestion worked better. I created a new frontend.js and imported Leaflet there. It generated a new CSS bundle (frontend.css) with Leaflet styles inside. Then I just adjusted the path in block.json to reference it, like:
"style": "file:./frontend.css"
That works fine — so I’ll stick with that approach for now. But to be honest, it still feels a bit hacky or unintuitive 😅
Thanks again for the support — really appreciate it!
See you around 😄