Skip to content

Commit 9c4c20e

Browse files
authored
refactor: split resolve options phase (#229)
1 parent 3a38b77 commit 9c4c20e

File tree

15 files changed

+224
-103
lines changed

15 files changed

+224
-103
lines changed

.changeset/khaki-kids-vanish.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': minor
3+
---
4+
5+
handle preprocess for prebundleSvelteLibraries

.changeset/olive-flowers-glow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': patch
3+
---
4+
5+
handle production builds for non "production" mode

packages/e2e-tests/env/.env.staging

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NODE_ENV=production

packages/e2e-tests/env/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# default svelte app template
2+
3+
Created with `npx degit sveltejs/template`
4+
5+
adapted to vite by moving index.html to root and replacing rollup config with vite
6+
7+
use pnpm
8+
9+
`pnpm dev` starts dev server
10+
`pnpm build` builds for production
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { findAssetFile, isBuild } from 'testUtils';
2+
3+
// can't have no tests for test:serve
4+
it('dummy', () => {});
5+
6+
if (isBuild) {
7+
it('custom production mode should build for production', () => {
8+
const indexBundle = findAssetFile(/index\..*\.js/);
9+
expect(indexBundle).not.toContain('SvelteComponentDev');
10+
});
11+
}

packages/e2e-tests/env/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width,initial-scale=1" />
6+
7+
<title>Svelte app</title>
8+
9+
<script type="module" src="/src/main.js"></script>
10+
</head>
11+
12+
<body></body>
13+
</html>

packages/e2e-tests/env/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "e2e-tests-env",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"preview": "vite preview"
9+
},
10+
"devDependencies": {
11+
"@sveltejs/vite-plugin-svelte": "workspace:*",
12+
"svelte": "^3.44.2",
13+
"vite": "^2.7.0"
14+
},
15+
"type": "module"
16+
}

packages/e2e-tests/env/src/App.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Hello world!</h1>
2+
3+
<style>
4+
h1 {
5+
color: #ff3e00;
6+
}
7+
</style>

packages/e2e-tests/env/src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.body
5+
});
6+
7+
export default app;

packages/e2e-tests/env/vite.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { svelte } from '@sveltejs/vite-plugin-svelte';
2+
import { defineConfig } from 'vite';
3+
4+
export default defineConfig({
5+
mode: 'staging',
6+
plugins: [svelte()],
7+
build: {
8+
// make build faster by skipping transforms and minification
9+
target: 'esnext',
10+
minify: false,
11+
commonjsOptions: {
12+
// pnpm only symlinks packages, and vite wont process cjs deps not in
13+
// node_modules, so we add the cjs dep here
14+
include: [/node_modules/, /cjs-only/]
15+
}
16+
},
17+
server: {
18+
watch: {
19+
// During tests we edit the files too fast and sometimes chokidar
20+
// misses change events, so enforce polling for consistency
21+
usePolling: true,
22+
interval: 100
23+
}
24+
}
25+
});

packages/vite-plugin-svelte/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import {
99
validateInlineOptions,
1010
Options,
1111
ResolvedOptions,
12-
resolveOptions
12+
resolveOptions,
13+
patchResolvedViteConfig,
14+
preResolveOptions
1315
} from './utils/options';
1416
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';
1517

1618
import { ensureWatchedFile, setupWatchers } from './utils/watch';
1719
import { resolveViaPackageJsonSvelte } from './utils/resolve';
18-
import { addExtraPreprocessors } from './utils/preprocess';
1920
import { PartialResolvedId } from 'rollup';
2021
import { toRollupError } from './utils/error';
2122

@@ -51,15 +52,17 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
5152
} else if (config.logLevel) {
5253
log.setLevel(config.logLevel);
5354
}
54-
options = await resolveOptions(inlineOptions, config, configEnv);
55+
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
56+
options = await preResolveOptions(inlineOptions, config, configEnv);
5557
// extra vite config
5658
const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
5759
log.debug('additional vite config', extraViteConfig);
58-
return extraViteConfig as Partial<UserConfig>;
60+
return extraViteConfig;
5961
},
6062

6163
async configResolved(config) {
62-
addExtraPreprocessors(options, config);
64+
options = resolveOptions(options, config);
65+
patchResolvedViteConfig(config, options);
6366
requestParser = buildIdParser(options);
6467
compileSvelte = createCompileSvelte(options);
6568
viteConfig = config;

packages/vite-plugin-svelte/src/utils/esbuild.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type EsbuildOptions = NonNullable<DepOptimizationOptions['esbuildOptions']>;
1010
type EsbuildPlugin = NonNullable<EsbuildOptions['plugins']>[number];
1111
type EsbuildPluginBuild = Parameters<EsbuildPlugin['setup']>[0];
1212

13+
export const facadeEsbuildSveltePluginName = 'vite-plugin-svelte:facade';
14+
1315
export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
1416
return {
1517
name: 'vite-plugin-svelte:optimize-svelte',
@@ -64,6 +66,7 @@ async function compileSvelte(
6466
...options.compilerOptions,
6567
css: true,
6668
filename,
69+
format: 'esm',
6770
generate: 'dom'
6871
};
6972

0 commit comments

Comments
 (0)