Skip to content

Commit

Permalink
feat(cli): allow providing custom configuration (#1055)
Browse files Browse the repository at this point in the history
* allow specifying mode when creating config
* load config file from cwd
* add cli options for custom config
* update documentation for CLI
  • Loading branch information
odinr committed Jul 20, 2023
1 parent e65c671 commit 6c2fd59
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
10 changes: 10 additions & 0 deletions .changeset/heavy-rivers-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@equinor/fusion-framework-cli': minor
---

__Allow loading of custom vite config__

When running the CLI, allow the user to provide custom [Vite config](https://vitejs.dev/config/).
The provided config is merged with the built-in config (default generated by the CLI).

updated [documentation](https://equinor.github.io/fusion-framework/guide/app/cli.html#config)
22 changes: 13 additions & 9 deletions packages/cli/src/scripts/create-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path';

import { defineConfig, UserConfig, createLogger } from 'vite';
import { defineConfig, createLogger, type UserConfig } from 'vite';

import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
Expand All @@ -9,6 +9,8 @@ import { resolvePackage } from './app-config.js';

import EnvironmentPlugin from 'vite-plugin-environment';

type CreateConfigOptions = { mode: 'production' | 'development' };

const createCustomLogger = () => {
const logger = createLogger();
const originalWarning = logger.warn;
Expand All @@ -24,33 +26,35 @@ const createCustomLogger = () => {
return logger;
};

export const createConfig = (): UserConfig => {
export const loadCustomConfig = async (file: string) => {
const filePath = resolve(process.cwd(), file);
return (await import(filePath)).default;
};


export const createConfig = (opt?: CreateConfigOptions): UserConfig => {
const { mode } = opt ?? { mode: 'development' };
const { root, pkg } = resolvePackage();
return defineConfig({
plugins: [
react(),
tsconfigPaths(),
EnvironmentPlugin({
NODE_ENV: 'production',
NODE_ENV: mode,
}),
],
root: root,
server: {
middlewareMode: true,
},
mode: 'development',
appType: 'custom',
build: {
outDir: resolve(root, 'dist'),
lib: {
entry: resolve(root, pkg.main),
fileName: 'app-bundle',
formats: ['es'],
},
// minify: false,
// terserOptions: {
// mangle: false,
// },
}
},
customLogger: createCustomLogger(),
}) as unknown as UserConfig;
Expand Down
32 changes: 26 additions & 6 deletions packages/cli/src/scripts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ora from 'ora';
import { build, mergeConfig, UserConfig } from 'vite';

import startDevServer from './serve.js';
import createConfig from './create-config.js';
import { createConfig, loadCustomConfig } from './create-config.js';
import { resolveAppConfig } from './app-config.js';

const program = new Command();
Expand All @@ -25,9 +25,17 @@ app.command('dev')

.option('-p, --port <number>', 'dev-server port', '3000')
.option('--portal <string>', 'fusion portal host')
.action(async ({ port, portal }) => {
.option(
'-c, --config <file>',
'Use specified config file, see https://vitejs.dev/guide/cli.html#build'
)
.action(async ({ port, portal, config }) => {
const spinner = ora('Loading configuration').start();
const viteConfig = mergeConfig(await createConfig(), { server: { port } }) as UserConfig;
const customConfig = config ? await loadCustomConfig(config) : {};
const viteConfig = mergeConfig(await createConfig(), {
...customConfig,
server: { port },
}) as UserConfig;
const appConfig = await resolveAppConfig();

appConfig.portalHost = portal
Expand All @@ -38,8 +46,20 @@ app.command('dev')
startDevServer({ viteConfig, appConfig });
});

app.command('build').action(async () => {
build(mergeConfig(await createConfig(), { build: { emptyOutDir: true } }));
});
app.command('build')
.option(
'-c, --config <file>',
'Use specified config file, see https://vitejs.dev/guide/cli.html#build'
)
.action(async ({ config }) => {
const spinner = ora('Loading configuration').start();
const customConfig = config ? await loadCustomConfig(config) : {};
const viteConfig = mergeConfig(await createConfig({ mode: 'production' }), {
build: { emptyOutDir: true },
...customConfig,
});
spinner.succeed('Configuration loaded');
build(viteConfig);
});

program.parse();
25 changes: 25 additions & 0 deletions vue-press/src/guide/app/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ export default () => ({
});
```

### Vite

by default the CLI will generate configuration required, but can be overridden by providing custom [Vite config](https://vitejs.dev/config/).

```sh
fusion-framework-cli app dev -c vite.config.js
fusion-framework-cli app build -c vite.config.js
fusion-framework-cli app build --config vite.config.js
```

__example__
```js
/** @type {import('vite').UserConfig} */
export default {
build: {
minify: false,
terserOptions: {
mangle: false
},
sourcemap: true
}
}
```

> only static `UserConfig` is allowed
## Dev

Expand Down

0 comments on commit 6c2fd59

Please sign in to comment.