Skip to content

Commit

Permalink
fix: correct sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Feb 9, 2021
1 parent 5834b4f commit 2140987
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 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.1.2",
"version": "2.1.4",
"description": "A mock plugin for vite",
"main": "dist/index.js",
"files": [
Expand Down
1 change: 0 additions & 1 deletion src/createMockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import esbuildPlugin from 'rollup-plugin-esbuild';
import dayjs from 'dayjs';

import createServer, { NextHandleFunction } from 'connect';
import { Connect } from 'vite';

const pathResolve = require('@rollup/plugin-node-resolve');

Expand Down
31 changes: 16 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
(async () => {
try {
await import('mockjs');
} catch (e) {
throw new Error('vite-plugin-vue-mock requires mockjs to be present in the dependency tree.');
}
})();

import { ViteMockOptions } from './types';
import { Plugin, ResolvedConfig, normalizePath } from 'vite';
import { createMockServer, requestMiddle } from './createMockServer';
Expand All @@ -20,9 +28,7 @@ export function viteMockServe(opt: ViteMockOptions): Plugin {
configResolved(resolvedConfig) {
config = resolvedConfig;
isDev = config.command === 'serve' && !config.isProduction;
needSourcemap =
resolvedConfig.command === 'serve' ||
(resolvedConfig.isProduction && !!resolvedConfig.build.sourcemap);
needSourcemap = resolvedConfig.isProduction && !!resolvedConfig.build.sourcemap;
},

configureServer: async ({ middlewares }) => {
Expand All @@ -34,23 +40,18 @@ export function viteMockServe(opt: ViteMockOptions): Plugin {
middlewares.use(middleware);
},
async transform(code: string, id: string) {
const getResult = (content: string) => ({
map: needSourcemap ? this.getCombinedSourcemap() : null,
code: content,
});

if (isDev || !id.endsWith(injectFile)) {
return getResult(code);
return null;
}
const { prodEnabled = true, injectCode = '' } = opt;
if (!prodEnabled) {
return getResult(code);
return null;
}
return getResult(`
${code}
\n
${injectCode}
`);

return {
map: needSourcemap ? this.getCombinedSourcemap() : null,
code: `${code}\n${injectCode}`,
};
},
};
}
Expand Down

0 comments on commit 2140987

Please sign in to comment.