@vitjs/core
Version:
@vitjs/core
73 lines (72 loc) • 2.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
const utils_1 = require("@vitjs/utils");
const mkdirp_1 = __importDefault(require("mkdirp"));
const Route_1 = __importDefault(require("../Route"));
const utils_2 = require("./utils");
class Service {
constructor(options) {
this._customApps = ['_app.tsx', '_app.jsx', '_app.js'];
this.paths = {};
this.debug = options.debug;
this.initPaths(options);
this.route = new Route_1.default({
routes: options.routes,
dynamicImport: options.dynamicImport,
service: this,
});
}
initPaths(options) {
const absSrcPath = (0, utils_1.winPath)((0, path_1.resolve)(options.cwd, './src'));
const absPagesPath = (0, utils_1.winPath)((0, path_1.resolve)(options.cwd, './src/pages'));
const absOutputPath = (0, utils_1.winPath)((0, path_1.resolve)(options.outDir));
const absTmpPath = (0, utils_1.winPath)((0, path_1.resolve)(options.cwd, './src/.vit'));
this.paths = {
cwd: options.cwd,
absSrcPath,
absPagesPath,
absOutputPath,
absTmpPath,
};
}
getCustomApp() {
return this._customApps.find((item) => {
return (0, fs_1.existsSync)((0, utils_1.winPath)((0, path_1.resolve)(this.paths.absSrcPath, item)));
});
}
getReactVersion() {
const packageJsonPath = (0, utils_1.winPath)((0, path_1.resolve)(this.paths.cwd, 'package.json'));
const packageJson = JSON.parse((0, fs_1.readFileSync)(packageJsonPath, 'utf-8'));
return packageJson.dependencies.react || packageJson.devDependencies.react;
}
writeFile({ path, content }) {
mkdirp_1.default.sync((0, path_1.dirname)(path));
if (!(0, fs_1.existsSync)(path) || (0, fs_1.readFileSync)(path, 'utf-8') !== content) {
(0, fs_1.writeFileSync)(path, content, 'utf-8');
}
}
writeTmpFile({ path, content, skipTSCheck = true }) {
const absPath = (0, path_1.join)(this.paths.absTmpPath, path);
let contentResult = content;
if ((0, utils_2.isTSFile)(path) && skipTSCheck) {
// write @ts-nocheck into first line
contentResult = `// @ts-nocheck${os_1.EOL}${contentResult}`;
}
this.writeFile({
path: absPath,
content: contentResult,
});
}
dumpGlobalImports(files) {
return `${(0, utils_2.getGlobalFiles)({ absSrcPath: this.paths.absSrcPath, files })
.map((file) => `import '../${(0, path_1.relative)(this.paths.absSrcPath, file)}';`)
.join('\n')}`;
}
}
exports.default = Service;