pacvue-cli
Version:
136 lines • 6 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const vite_1 = require("vite");
const path = require("path");
const vite_config_1 = require("./vite.config");
const fs = require("fs");
const esbuild_1 = require("esbuild");
function default_1(optionMode) {
try {
fs.statSync(path.resolve(process.cwd(), './index.html'));
}
catch (e) {
fs.writeFileSync(path.resolve(process.cwd(), './index.html'), `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>pacvue App</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>`, 'utf8');
}
function loadConfigFromBundledFile(fileName, bundledCode) {
return __awaiter(this, void 0, void 0, function* () {
const extension = path.extname(fileName);
const defaultLoader = require.extensions[extension];
require.extensions[extension] = (module, filename) => {
if (filename === fileName) {
module._compile(bundledCode, filename);
}
else {
defaultLoader(module, filename);
}
};
// clear cache in case of server restart
delete require.cache[require.resolve(fileName)];
const raw = require(fileName);
const config = raw.__esModule ? raw.default : raw;
require.extensions[extension] = defaultLoader;
return config;
});
}
function bundleConfigFile(fileName, mjs = false) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield esbuild_1.build({
entryPoints: [fileName],
outfile: 'out.js',
write: false,
platform: 'node',
bundle: true,
format: mjs ? 'esm' : 'cjs',
metafile: true,
plugins: [
{
name: 'externalize-deps',
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
const id = args.path;
if (id[0] !== '.' && !path.isAbsolute(id)) {
return {
external: true
};
}
});
}
},
{
name: 'replace-import-meta',
setup(build) {
build.onLoad({ filter: /\.[jt]s$/ }, (args) => __awaiter(this, void 0, void 0, function* () {
const contents = yield fs.promises.readFile(args.path, 'utf8');
return {
loader: args.path.endsWith('.ts') ? 'ts' : 'js',
contents: contents
.replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`))
.replace(/\b__dirname\b/g, JSON.stringify(path.dirname(args.path)))
.replace(/\b__filename\b/g, JSON.stringify(args.path))
};
}));
}
}
]
});
const { text } = result.outputFiles[0];
return {
code: text,
dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
};
});
}
(() => __awaiter(this, void 0, void 0, function* () {
const congfigString = yield bundleConfigFile(path.resolve(process.cwd(), './vite.config.js'));
const config = yield loadConfigFromBundledFile(path.resolve(process.cwd(), './vite.config.js'), congfigString.code);
global.config = config;
const defaultConfig = vite_config_1.default(global.config);
const server = yield vite_1.createServer({
mode: optionMode || 'development',
// 任何合法的用户配置选项,加上 `mode` 和 `configFile`
configFile: path.resolve(process.cwd(), './vite.config.js'),
plugins: defaultConfig.plugins,
css: defaultConfig.css,
resolve: defaultConfig.resolve
});
console.log(`/**
*
*
* — — — __ _ _ _ _ _ _ _
* | ——— \\ / \\ / _ _ \\| | | _ _|
* | ( ) |/ \\ / / \\_| | | |_ _
* | ——— / -- | | | | | _ _|
* | ——— / / \\ \\ \\ _ _ | \\ _ | |_ _
* |_| /_/ \\_\\ ___ / \\ _ _/|_ _ _|
*
* ======================1.0.42=================
*
*
*/`);
yield server.listen();
server.printUrls();
}))();
}
exports.default = default_1;
//# sourceMappingURL=viteServe.js.map