vite-typescript-plugin
Version:
A Vite plugin for seamless integration between Vite and Typescript.
92 lines (91 loc) • 3.86 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = exports.createTsPlugin = exports.createPlugin = void 0;
const typescript_1 = __importDefault(require("typescript"));
const host_1 = require("./host");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const normalizePath_1 = require("./host/utils/normalizePath");
const createTsPlugin = (options = {}) => {
const name = options.name || "typescript";
const testFileName = options.test || /\.(((t|j)sx?)|json)$/i;
const emitOuterFiles = {};
let workerHost = new host_1.CustomCompilerHost(options);
const logDiagnose = (vitePluginContext, diagnostics) => {
if (diagnostics.length !== 0) {
const error = {
message: workerHost.newLine + typescript_1.default.formatDiagnosticsWithColorAndContext(diagnostics, workerHost),
pluginCode: "KIX"
};
vitePluginContext.error(error);
}
};
return {
name: name,
enforce: "pre",
configureServer(server) {
if (fs_1.default.existsSync(workerHost.tsConfigPath)) {
fs_1.default.watch(workerHost.tsConfigPath, () => {
workerHost.fileCache.clear();
workerHost.tsConfigPath = workerHost.getTsConfigFilePath();
workerHost.configFileOptions = workerHost.getCompilerOptions();
});
}
server.watcher.on('unlink', (path) => {
workerHost.fileCache.delete((0, normalizePath_1.normalizePath)(path));
});
},
transform(code, id, options) {
const fileName = (0, normalizePath_1.normalizePath)(id);
if (testFileName.test(fileName)) {
const output = workerHost.emitFileIfChanged(fileName, code);
if (output.diagnostics.length !== 0) {
logDiagnose(this, output.diagnostics);
}
if (!this.meta.watchMode) {
Object.assign(emitOuterFiles, output.emitFiles);
}
return output;
}
return {
code: code
};
},
generateBundle(outputOptions) {
if (outputOptions.dir) {
for (const emitFilePath in emitOuterFiles) {
this.emitFile({
fileName: path_1.default.relative(outputOptions.dir, emitFilePath),
source: emitOuterFiles[emitFilePath],
type: 'asset'
});
}
}
}
};
};
exports.createTsPlugin = createTsPlugin;
exports.default = createTsPlugin;
/**
@deprecated use createTsPlugin()
*/
const createPlugin = createTsPlugin;
exports.createPlugin = createPlugin;
__exportStar(require("./host"), exports);