Skip to content

Commit

Permalink
fix: fix js parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Jul 6, 2021
1 parent 784f3f8 commit d2746b6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-mock",
"version": "2.9.0",
"version": "2.9.1",
"description": "A mock plugin for vite",
"main": "dist/index.js",
"files": [
Expand Down
35 changes: 21 additions & 14 deletions src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,17 @@ function createWatch(opt: ViteMockOptions) {
}

// clear cache
// function cleanRequireCache(opt: ViteMockOptions) {
// const { absConfigPath, absMockPath } = getPath(opt);
// Object.keys(require.cache).forEach((file) => {
// if (file === absConfigPath || file.indexOf(absMockPath) > -1) {
// delete require.cache[file];
// }
// });
// }
function cleanRequireCache(opt: ViteMockOptions) {
if (!require || !require.cache) {
return;
}
const { absConfigPath, absMockPath } = getPath(opt);
Object.keys(require.cache).forEach((file) => {
if (file === absConfigPath || file.indexOf(absMockPath) > -1) {
delete require.cache[file];
}
});
}

function parseJson(req: IncomingMessage): Promise<Recordable> {
return new Promise((resolve) => {
Expand All @@ -161,7 +164,7 @@ function parseJson(req: IncomingMessage): Promise<Recordable> {

// load mock .ts files and watch
async function getMockConfig(opt: ViteMockOptions) {
// cleanRequireCache(opt);
cleanRequireCache(opt);
const { absConfigPath, absMockPath } = getPath(opt);
const { ignore, configPath, logger } = opt;

Expand Down Expand Up @@ -254,31 +257,35 @@ function loggerOutput(title: string, msg: string, type: 'info' | 'error' = 'info
// Parse file content
export async function loadConfigFromBundledFile(fileName: string, bundledCode: string) {
const extension = path.extname(fileName);

// @ts-expect-error
const extensions = module.Module._extensions;
let defaultLoader: any;
const isJs = extension === '.js';
if (isJs) {
defaultLoader = require.extensions[extension]!;
defaultLoader = extensions[extension]!;
}

extensions[extension] = (module: NodeModule, filename: string) => {
if (filename === fileName) {
(module as NodeModuleWithCompile)._compile(bundledCode, filename);
} else {
require.extensions[extension]!(module, filename);
defaultLoader(module, filename);
if (!isJs) {
extensions[extension]!(module, filename);
} else {
defaultLoader(module, filename);
}
}
};
let config;
try {
if (isJs) {
if (isJs && require && require.cache) {
delete require.cache[fileName];
}
const raw = require(fileName);
config = raw.__esModule ? raw.default : raw;
if (defaultLoader && isJs) {
require.extensions[extension] = defaultLoader;
extensions[extension] = defaultLoader;
}
} catch (error) {
console.error(error);
Expand Down

0 comments on commit d2746b6

Please sign in to comment.