Skip to content

refactor: split resolve options phase #229

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

Merged
merged 11 commits into from
Dec 19, 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/khaki-kids-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': minor
---

handle preprocess for prebundleSvelteLibraries
5 changes: 5 additions & 0 deletions .changeset/olive-flowers-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': patch
---

handle production builds for non "production" mode
1 change: 1 addition & 0 deletions packages/e2e-tests/env/.env.staging
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=production
10 changes: 10 additions & 0 deletions packages/e2e-tests/env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# default svelte app template

Created with `npx degit sveltejs/template`

adapted to vite by moving index.html to root and replacing rollup config with vite

use pnpm

`pnpm dev` starts dev server
`pnpm build` builds for production
11 changes: 11 additions & 0 deletions packages/e2e-tests/env/__tests__/env.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { findAssetFile, isBuild } from 'testUtils';

// can't have no tests for test:serve
it('dummy', () => {});

if (isBuild) {
it('custom production mode should build for production', () => {
const indexBundle = findAssetFile(/index\..*\.js/);
expect(indexBundle).not.toContain('SvelteComponentDev');
});
}
13 changes: 13 additions & 0 deletions packages/e2e-tests/env/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />

<title>Svelte app</title>

<script type="module" src="/src/main.js"></script>
</head>

<body></body>
</html>
16 changes: 16 additions & 0 deletions packages/e2e-tests/env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "e2e-tests-env",
"private": true,
"version": "1.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "workspace:*",
"svelte": "^3.44.2",
"vite": "^2.7.0"
},
"type": "module"
}
7 changes: 7 additions & 0 deletions packages/e2e-tests/env/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Hello world!</h1>

<style>
h1 {
color: #ff3e00;
}
</style>
7 changes: 7 additions & 0 deletions packages/e2e-tests/env/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.body
});

export default app;
25 changes: 25 additions & 0 deletions packages/e2e-tests/env/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vite';

export default defineConfig({
mode: 'staging',
plugins: [svelte()],
build: {
// make build faster by skipping transforms and minification
target: 'esnext',
minify: false,
commonjsOptions: {
// pnpm only symlinks packages, and vite wont process cjs deps not in
// node_modules, so we add the cjs dep here
include: [/node_modules/, /cjs-only/]
}
},
server: {
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100
}
}
});
13 changes: 8 additions & 5 deletions packages/vite-plugin-svelte/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
validateInlineOptions,
Options,
ResolvedOptions,
resolveOptions
resolveOptions,
patchResolvedViteConfig,
preResolveOptions
} from './utils/options';
import { VitePluginSvelteCache } from './utils/vite-plugin-svelte-cache';

import { ensureWatchedFile, setupWatchers } from './utils/watch';
import { resolveViaPackageJsonSvelte } from './utils/resolve';
import { addExtraPreprocessors } from './utils/preprocess';
import { PartialResolvedId } from 'rollup';
import { toRollupError } from './utils/error';

Expand Down Expand Up @@ -51,15 +52,17 @@ export function svelte(inlineOptions?: Partial<Options>): Plugin {
} else if (config.logLevel) {
log.setLevel(config.logLevel);
}
options = await resolveOptions(inlineOptions, config, configEnv);
// @ts-expect-error temporarily lend the options variable until fixed in configResolved
options = await preResolveOptions(inlineOptions, config, configEnv);
// extra vite config
const extraViteConfig = buildExtraViteConfig(options, config, configEnv);
log.debug('additional vite config', extraViteConfig);
return extraViteConfig as Partial<UserConfig>;
return extraViteConfig;
},

async configResolved(config) {
addExtraPreprocessors(options, config);
options = resolveOptions(options, config);
patchResolvedViteConfig(config, options);
requestParser = buildIdParser(options);
compileSvelte = createCompileSvelte(options);
viteConfig = config;
Expand Down
3 changes: 3 additions & 0 deletions packages/vite-plugin-svelte/src/utils/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type EsbuildOptions = NonNullable<DepOptimizationOptions['esbuildOptions']>;
type EsbuildPlugin = NonNullable<EsbuildOptions['plugins']>[number];
type EsbuildPluginBuild = Parameters<EsbuildPlugin['setup']>[0];

export const facadeEsbuildSveltePluginName = 'vite-plugin-svelte:facade';

export function esbuildSveltePlugin(options: ResolvedOptions): EsbuildPlugin {
return {
name: 'vite-plugin-svelte:optimize-svelte',
Expand Down Expand Up @@ -64,6 +66,7 @@ async function compileSvelte(
...options.compilerOptions,
css: true,
filename,
format: 'esm',
generate: 'dom'
};

Expand Down
Loading