Skip to content

Commit

Permalink
feat:修改viter打包配置,加入内置alias
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryYuX committed Jul 26, 2021
1 parent 6d990c6 commit 70d826c
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 69 deletions.
3 changes: 1 addition & 2 deletions packages/create-app/template-react-admin-ts/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
</head>
<body>
<div id="root"></div>
{# 入口路径,中后台项目非必要不修改 #}
<script type="module" src="./src/.viter/entry.tsx"></script>
<script type="module" src="./.viter/entry.tsx"></script>
</body>
</html>
11 changes: 5 additions & 6 deletions packages/create-app/template-react-admin-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "viter-template",
"name": "viter-project",
"version": "0.0.0",
"scripts": {
"dev": "vite --config viter.config.ts",
"build": "tsc && vite build",
"serve": "vite preview"
"dev": "viter",
"build": "tsc && viter build",
"serve": "viter preview"
},
"dependencies": {
"react": "^17.0.0",
Expand All @@ -15,7 +15,6 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@vitejs/plugin-react-refresh": "^1.3.1",
"typescript": "^4.1.2",
"vite": "^2.1.3"
"typescript": "^4.1.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from "react";
import { Outlet } from "viter";

function Layout(props) {
return <div>{props.children}</div>;
function Layout() {
return (
<div style={{ backgroundColor: "GrayText", height: "400px" }}>
<Outlet></Outlet>
</div>
);
}

export default Layout;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import React from "react";

function Loading() {
return <div>Loading</div>;
return <div style={{ color: "#fff" }}>Loading</div>;
}

export default Loading;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

function About() {
return <div>about us</div>;
}

export default About;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

button {
font-size: calc(10px + 2vmin);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState } from "react";
import styles from "./Home.module.css";

function Home() {
const [count, setCount] = useState(0);

return (
<div className="App">
<header className={styles["App-header"]}>
<p>Hello Viter + React!</p>
<p>
<button onClick={() => setCount((count) => count + 1)}>
count is: {count}
</button>
</p>
<p>
Edit <code>Home.tsx</code> and save to test HMR updates.
</p>
<p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
{" | "}
<a
className="App-link"
href="https://vitejs.dev/guide/features.html"
target="_blank"
rel="noopener noreferrer"
>
Vite Docs
</a>
</p>
</header>
</div>
);
}

export default Home;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

function NotFind() {
return <div>404 NotFind</div>;
}

export default NotFind;

This file was deleted.

This file was deleted.

21 changes: 18 additions & 3 deletions packages/create-app/template-react-admin-ts/viter.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { defineConfig } from 'vite';
import reactRefresh from '@vitejs/plugin-react-refresh';
const { resolve } = require('path');
import { defineConfig } from "viter";
import reactRefresh from "@vitejs/plugin-react-refresh";
const { resolve } = require("path");

// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefresh()],
routes: [
{
path: "/",
component: "@/layout",
routes: [
{ path: "/home", component: "./Home" },
{ path: "/about", component: "./About" },
{ path: "/404", component: "./NotFind" },
{ path: "/*", redirect: "/404" },
],
},
],
dynamicImport: {
loading: "@/loading",
},
});
4 changes: 2 additions & 2 deletions packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
"url": "https://github.com/ykfe/viter/issues"
},
"dependencies": {
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"art-template": "^4.13.2",
"lodash-es": "^4.17.21",
"mkdirp": "^1.0.4",
"react-router-dom": "^6.0.0-beta.0"
},
"devDependencies": {
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"@types/lodash": "^4.14.170",
"@types/lodash-es": "^4.17.4",
"@types/mkdirp": "^1.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/renderer/src/generateInitFiles/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IRoute {
path?: string;
routes?: IRoute[];
title?: string;
redirect?: string;
[key: string]: any;
}
export interface IDynamicImport {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { BrowserRouter } from '@viter/runtime';
import { BrowserRouter } from 'viter';
import Routes from './routes'
export default function renderRouter() {
return (
Expand Down
4 changes: 2 additions & 2 deletions packages/renderer/src/generateInitFiles/tplFiles/routes.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{if dynamic}}
import { dynamic } from '@viter/runtime';
import { dynamic } from 'viter';
{{/if}}
import { Navigate, useRoutes } from '@viter/runtime';
import { Navigate, useRoutes } from 'viter';
import React from 'react';
{{each modules}}
import {{$value.name}} from '{{$value.path}}';
Expand Down
6 changes: 3 additions & 3 deletions packages/viter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"homepage": "https://github.com/ykfe/viter",
"license": "MIT",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"module": "dist/esm/runtime.js",
"types": "dist/src/index.d.ts",
"directories": {
"dist": "dist"
Expand Down Expand Up @@ -46,6 +46,7 @@
"@types/react": "^17.0.13",
"cac": "^6.7.3",
"chokidar": "^3.5.2",
"lodash": "^4.17.21",
"react": "^17.0.2",
"rollup": "^2.53.3",
"source-map-support": "^0.5.19"
Expand All @@ -58,7 +59,6 @@
"peerDependencies": {
"@viter/renderer": "^0.2.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"vite": "^2.4.3"
"react-dom": "^17.0.2"
}
}
62 changes: 40 additions & 22 deletions packages/viter/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,64 @@
import path from 'path';
import rollupBaseConfig from '../../rollup.config';
import pkg from './package.json';

import { cloneDeep, merge } from 'lodash';
const external = [
'fsevents',
'anymatch',
'is-binary-path',
'object-assign',
'vite',
...Object.keys(pkg.peerDependencies),
];

export default Object.assign(rollupBaseConfig, {
input: {
index: './src/index.ts',
cli: './src/cli.ts',
},
output: [
{
export default [
merge(cloneDeep(rollupBaseConfig), {
input: {
index: './src/index.ts',
cli: './src/cli.ts',
},
output: {
dir: path.resolve(__dirname, 'dist/cjs'),
entryFileNames: `[name].js`,
chunkFileNames: 'chunks/dep-[hash].js',
exports: 'named',
format: 'cjs',
sourcemap: true,
},
{

external,
onwarn(warning, warn) {
// vite use the eval('require') trick to deal with optional deps
if (warning.message.includes('Use of eval')) {
return;
}
if (warning.message.includes('Circular dependency')) {
return;
}
warn(warning);
},
}),
merge(cloneDeep(rollupBaseConfig), {
input: {
runtime: './src/runtime.ts',
},
output: {
dir: path.resolve(__dirname, 'dist/esm'),
entryFileNames: `[name].js`,
chunkFileNames: 'chunks/dep-[hash].js',
format: 'es',
sourcemap: true,
},
],
external,
onwarn(warning, warn) {
// vite use the eval('require') trick to deal with optional deps
if (warning.message.includes('Use of eval')) {
return;
}
if (warning.message.includes('Circular dependency')) {
return;
}
warn(warning);
},
});
external,
onwarn(warning, warn) {
// vite use the eval('require') trick to deal with optional deps
if (warning.message.includes('Use of eval')) {
return;
}
if (warning.message.includes('Circular dependency')) {
return;
}
warn(warning);
},
}),
];
5 changes: 5 additions & 0 deletions packages/viter/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export function convertConfig(config: ResolvedConfig): ViteUserConfig {
ignored: [configFile],
},
},
resolve: {
alias: {
'@': path.resolve(process.cwd(), 'src'),
},
},
};
const viteConfig = mergeConfig(config, innerConfig);

Expand Down
2 changes: 0 additions & 2 deletions packages/viter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from '@viter/runtime';

export * from './config';
1 change: 1 addition & 0 deletions packages/viter/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@viter/runtime';

0 comments on commit 70d826c

Please sign in to comment.