Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow svelte imports in optimizeDeps include #68

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-yaks-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

feat: Allow svelte imports to be added to optimizeDeps.include and don't exclude svelte from optimizeDeps then
2 changes: 2 additions & 0 deletions packages/playground/optimizedeps-include/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reproduction of failed dedupe for svelte
project setup from https://github.com/bluwy/vite-svelte-dedupe
12 changes: 12 additions & 0 deletions packages/playground/optimizedeps-include/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte + Vite App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions packages/playground/optimizedeps-include/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"compilerOptions": {
"moduleResolution": "node",
"target": "esnext",
"module": "esnext",
/**
* svelte-preprocess cannot figure out whether you have
* a value or a type, so tell TypeScript to enforce using
* `import type` instead of `import` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
* To have warnings / errors of the Svelte compiler at the
* correct position, enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
/**
* Typecheck JS in `.svelte` and `.js` files by default.
* Disable this if you'd like to use dynamic types.
*/
"checkJs": true
},
/**
* Use global.d.ts instead of compilerOptions.types
* to avoid limiting type declarations.
*/
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
16 changes: 16 additions & 0 deletions packages/playground/optimizedeps-include/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "playground-optimizedeps-include",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.38.1",
"vite": "^2.3.8",
"tinro": "^0.6.4"
}
}
16 changes: 16 additions & 0 deletions packages/playground/optimizedeps-include/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
import { Route, router } from 'tinro';
</script>

<nav>
<a href="/foo">To foo</a>
<a href="/bar">To bar</a>
<button on:click={() => router.goto('/bar')}>Go bar</button>
</nav>
<Route path="/foo"><h1>Foo route</h1></Route>
<Route path="/bar"><h1>Bar route</h1></Route>
<Route fallback><h1>Default route</h1></Route>

<pre>
$router: {JSON.stringify($router,null,2)}
</pre>
7 changes: 7 additions & 0 deletions packages/playground/optimizedeps-include/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import App from './App.svelte';

const app = new App({
target: document.getElementById('app')
});

export default app;
2 changes: 2 additions & 0 deletions packages/playground/optimizedeps-include/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions packages/playground/optimizedeps-include/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
const SVELTE_IMPORTS = [
'svelte/animate',
'svelte/easing',
'svelte/internal',
'svelte/motion',
'svelte/store',
'svelte/transition',
'svelte'
];
export default defineConfig(({ command, mode }) => {
const isProduction = mode === 'production';
return {
optimizeDeps: {
include: [...SVELTE_IMPORTS]
},
plugins: [svelte()],
build: {
minify: isProduction
}
};
});
27 changes: 2 additions & 25 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { log, logCompilerWarnings } from './utils/log';
import { CompileData, createCompileSvelte } from './utils/compile';
import { buildIdParser, IdParser, SvelteRequest } from './utils/id';
import {
buildExtraViteConfig,
validateInlineOptions,
Options,
ResolvedOptions,
Expand All @@ -12,7 +13,6 @@ import {
} from './utils/options';
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';

import { SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './utils/constants';
import { setupWatchers } from './utils/watch';
import { resolveViaPackageJsonSvelte } from './utils/resolve';
import { addExtraPreprocessors } from './utils/preprocess';
Expand Down Expand Up @@ -56,30 +56,7 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
}
options = await resolveOptions(inlineOptions, config, configEnv);
// extra vite config
const extraViteConfig: Partial<UserConfig> = {
optimizeDeps: {
exclude: [...SVELTE_IMPORTS]
},
resolve: {
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
dedupe: [...SVELTE_IMPORTS]
},
// this option is still awaiting a PR in vite to be supported
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60
// @ts-ignore
knownJsSrcExtensions: options.extensions
};
// needed to transform svelte files with component imports
// can cause issues with other typescript files, see https://github.com/sveltejs/vite-plugin-svelte/pull/20
if (options.useVitePreprocess) {
extraViteConfig.esbuild = {
tsconfigRaw: {
compilerOptions: {
importsNotUsedAsValues: 'preserve'
}
}
};
}
const extraViteConfig = buildExtraViteConfig(options, config);
log.debug('additional vite config', extraViteConfig);
return extraViteConfig as Partial<UserConfig>;
},
Expand Down
5 changes: 4 additions & 1 deletion packages/vite-plugin-svelte/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export const SVELTE_IMPORTS = [
'svelte/motion',
'svelte/store',
'svelte/transition',
'svelte',
'svelte'
];

export const SVELTE_HMR_IMPORTS = [
'svelte-hmr/runtime/hot-api-esm.js',
'svelte-hmr/runtime/proxy-adapter-dom.js',
'svelte-hmr'
Expand Down
40 changes: 40 additions & 0 deletions packages/vite-plugin-svelte/src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { ConfigEnv, UserConfig, ViteDevServer } from 'vite';
import { log } from './log';
import { loadSvelteConfig } from './load-svelte-config';
import { SVELTE_HMR_IMPORTS, SVELTE_IMPORTS, SVELTE_RESOLVE_MAIN_FIELDS } from './constants';

const knownOptions = new Set([
'configFile',
Expand Down Expand Up @@ -151,6 +152,45 @@ export async function resolveOptions(
return resolvedOptions;
}

export function buildExtraViteConfig(
options: ResolvedOptions,
config: UserConfig
): Partial<UserConfig> {
const allSvelteImports = [...SVELTE_IMPORTS, ...SVELTE_HMR_IMPORTS];

// exclude svelte imports from optimization unless explicitly included
const excludeFromOptimize = allSvelteImports.filter(
(x) => !config.optimizeDeps?.include?.includes(x)
);
dominikg marked this conversation as resolved.
Show resolved Hide resolved

const extraViteConfig: Partial<UserConfig> = {
optimizeDeps: {
exclude: excludeFromOptimize
},
resolve: {
mainFields: [...SVELTE_RESOLVE_MAIN_FIELDS],
dedupe: allSvelteImports
},
// this option is still awaiting a PR in vite to be supported
// see https://github.com/sveltejs/vite-plugin-svelte/issues/60
// @ts-ignore
knownJsSrcExtensions: options.extensions
};

if (options.useVitePreprocess) {
// needed to transform svelte files with component imports
// can cause issues with other typescript files, see https://github.com/sveltejs/vite-plugin-svelte/pull/20
extraViteConfig.esbuild = {
tsconfigRaw: {
compilerOptions: {
importsNotUsedAsValues: 'preserve'
}
}
};
}
return extraViteConfig;
}

export interface Options {
// eslint-disable no-unused-vars
/** path to svelte config file, either absolute or relative to vite root*/
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.