Closed George713 closed 3 years ago
@sveltejs/vite-plugin-svelte
).Hello @George713 , try excluding dayjs
from the dependencies for vite to optimize. I'm assuming you're using a SvelteKit set-up:
// svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
optimizeDeps: {
exclude: ['dayjs']
}
}
}
};
export default config;
Hope this helps.
Hi @metonym , commenting due to same issue here with SvelteKit. optimizeDeps
did not fix it so far. Using the latest Node v16.3.0 btw.
To further clarify: Using svelte-time on its own works! Importing dayjs for further i18n modules then fails.
import 'dayjs/esm/locale/de'
import dayjs from 'dayjs/esm'
dayjs.locale('de')
=> import * as C from './constant';
^^^^^^
SyntaxError: Cannot use import statement outside a module
@rvrm Interesting. Would you mind sharing your svelte.config.js?
This is my set-up:
package.json
@sveltejs/kit@next
resolved to version 1.0.0-next.113
.
{
"private": true,
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
"preview": "svelte-kit preview"
},
"devDependencies": {
"@sveltejs/kit": "next",
"svelte": "^3.34.0",
"svelte-time": "^0.3.0"
},
"type": "module"
}
svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: "#svelte",
vite: {
optimizeDeps: {
exclude: ["dayjs"],
},
},
},
};
export default config;
src/routes/index.svelte
<script context="module">
import "dayjs/esm/locale/de";
import dayjs from "dayjs/esm";
dayjs.locale("de");
</script>
<script>
import Time from "svelte-time";
</script>
<Time />
Output
In my browser, "Juni 07, 2021" is rendered.
I also cleared the cache and produced the same result.
rm -rf .svelte-kit node_modules/.vite
yarn && yarn dev
I mistakenly npm installed dayjs on its own. Now realized this step is not necessary. Uninstalling the package and simply import the plugins now worked as intended. Thank you!
Hey Metonym,
one sweet package you wrote there! I'm tinkering with it but for some reason I always end up having issues with the dayjs module. I'm importing within the module context ala
but always get the following error, when I first navigate to a page using it:
Any idea?
Best, George