#use multiple files
8 messages · Page 1 of 1 (latest)
Hi @pallid junco. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can add global CSS styles by importing a .css file. However, any CSS or JavaScript that you write yourself should live in your src/ directory. If you place CSS and JavaScript in your public/ directory, those files will not be bundled or optimized in your final build. They will be copied into the build folder untouched source (https://docs.astro.build/en/core-concepts/project-structure#public).
If you want to separate JavaScript into its own file, you can modify the Astro configuration as shown in this example:
import { defineConfig } from 'astro/config';
export default defineConfig({
vite: {
build: {
assetsInlineLimit: '0',
},
},
});
This configuration will prevent Vite from inlining your assets, thus creating separate files for them source (https://github.com/withastro/astro/issues/5083).
However, it's important to note that adding type="module" or any other attribute to a <script> tag will disable Astro’s default bundling behavior, treating the tag as if it had an is:inline directive source (https://github.com/withastro/astro/issues/5083).
As for CSS, Astro currently bundles all CSS into a single file. There are discussions about implementing CSS analysis and code-splitting in Vite, but as of now, there's not much that can be done to split the CSS for each page separately source (https://github.com/withastro/astro/issues/5390).
If you want to control how CSS is inserted, you might need to create a Vite plugin. This would be a bit advanced but it's possible source (https://github.com/withastro/astro/issues/4521).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@pallid junco kapa.ai is still learning and improving, please let us know how it did by reacting below
@autumn wharf what about js file?
Let me check the knowledge base for you. One moment! :books:
In Astro, you can include JavaScript in your project in a few ways:
-
Client-side interactivity with JavaScript in a
<script>tag: You can add JavaScript directly in your Astro files within a<script>tag. This allows you to add client-side interactivity to your components source (https://docs.astro.build/en/tutorial/3-components/4#checklist). -
Import a
.jsfile into a<script>tag: If you have a separate.jsfile, you can import it into a<script>tag. This allows you to keep your JavaScript code separate from your HTML and CSS source (https://docs.astro.build/en/tutorial/3-components/4#checklist). -
Import local scripts: If your script lives inside of
src/, you can import it and Astro will build, optimize, and add these scripts to the page for you. Here's an example:
<!-- relative path to script at `src/scripts/local.js` -->
<script src="../scripts/local.js"></script>
<!-- also works for local TypeScript files -->
<script src="./script-with-types.ts"></script>
source (https://docs.astro.build/en/guides/client-side-scripts#include-javascript-files-on-your-page)
- Load external scripts: If your JavaScript file lives inside of
public/or on a CDN, you can load it using theis:inlinedirective. This approach skips the JavaScript processing, bundling, and optimizations that are provided by Astro. Here's an example:
<!-- absolute path to a script at `public/my-script.js` -->
<script is:inline src="/my-script.js"></script>
<!-- full URL to a script on a remote server -->
<script is:inline src="<https://my-analytics.com/script.js"></script>>
source (https://docs.astro.build/en/guides/client-side-scripts#include-javascript-files-on-your-page)