Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
try swc to build
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Apr 23, 2024
1 parent 74e68ca commit b21b4d0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
26 changes: 18 additions & 8 deletions example/react-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import viteWranglerSpa from '../../src';

const pagesPlugins = [
const devPlugins = [
react(),
viteWranglerSpa({
functionEntrypoint: 'functions/index.tsx',
allowedApiPaths: ['/api/*', '/oauth/*'],
}),
];

const pagesBuildPlugins = [react()];
const functionBuildPlugins = [
viteWranglerSpa({
functionEntrypoint: 'functions/index.tsx',
allowedApiPaths: ['/api/*', '/oauth/*'],
}),
];

export default defineConfig(({ mode, command }) => ({
plugins:
mode === 'page-function' && command === 'build'
? functionBuildPlugins
: pagesPlugins,
}));
export default defineConfig(({ mode, command }) => {
let plugins = devPlugins;
if (command === 'build') {
plugins = pagesBuildPlugins;
if (mode === 'page-function') plugins = functionBuildPlugins;
}

return {
build: {
minify: true,
},
plugins,
};
});
4 changes: 2 additions & 2 deletions package-lock.json

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

36 changes: 27 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ import { CloudflareSpaConfig } from './CloudflareSpaConfig';
import { UnstableDevWorker, unstable_dev } from 'wrangler';
import { builtinModules } from 'node:module';
import { convertWranglerResponse, makeWranglerFetch } from './utils';
import { transform } from '@swc/core';
import { writeFileSync } from 'node:fs';
import type { Plugin, PluginOption } from 'vite';
import type { Options as SWCOptions } from '@swc/core';

let wranglerDevServer: UnstableDevWorker;

export default function viteWranglerSpa(
config?: CloudflareSpaConfig
): PluginOption {
const defaults: SWCOptions = {
jsc: {
target: 'esnext',
parser: {
syntax: 'typescript',
decorators: true,
},
transform: {
decoratorMetadata: true,
legacyDecorator: true,
},
loose: true,
},
};

export default function viteWranglerSpa(config?: CloudflareSpaConfig): PluginOption {
const functionEntrypoint = config?.functionEntrypoint || 'functions/index.ts';
const wranglerConfig = config?.wranglerConfig || {
logLevel: 'log',
Expand Down Expand Up @@ -49,22 +64,25 @@ export default function viteWranglerSpa(
};
},

transform(code) {
return transform(code, {
...defaults,
sourceMaps: true,
});
},

/** Start the wrangler miniflare server */
configureServer: async (devServer) => {
if (!wranglerDevServer) {
wranglerDevServer = await unstable_dev(
functionEntrypoint,
wranglerConfig
);
wranglerDevServer = await unstable_dev(functionEntrypoint, wranglerConfig);
}

//setup middleware to redirect requests to miniflare
devServer.middlewares.use(async (req, res, next) => {
const { url } = req;
if (url === undefined) throw new Error('url is undefined!');

if (excludedApiPaths.find((x) => new RegExp(x).test(url)))
return next();
if (excludedApiPaths.find((x) => new RegExp(x).test(url))) return next();

/** only direct specific requests to the miniflare server so the SPA still renders correctly */
if (allowedApiPaths.find((x) => new RegExp(x).test(url))) {
Expand Down

0 comments on commit b21b4d0

Please sign in to comment.