Skip to content

Commit

Permalink
fix: dev sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Feb 7, 2021
1 parent d3ae41e commit 5834b4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 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.1",
"version": "2.1.2",
"description": "A mock plugin for vite",
"main": "dist/index.js",
"files": [
Expand Down
30 changes: 11 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,37 @@ export function viteMockServe(opt: ViteMockOptions): Plugin {
configResolved(resolvedConfig) {
config = resolvedConfig;
isDev = config.command === 'serve' && !config.isProduction;
needSourcemap = resolvedConfig.isProduction && !!resolvedConfig.build.sourcemap;
needSourcemap =
resolvedConfig.command === 'serve' ||
(resolvedConfig.isProduction && !!resolvedConfig.build.sourcemap);
},

configureServer: async ({ middlewares }) => {
const { localEnabled = isDev } = opt;
if (!localEnabled) return;
createMockServer(opt);

// parse application/x-www-form-urlencoded
// middlewares.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
// middlewares.use(bodyParser.json());
const middleware = await requestMiddle(opt);
middlewares.use(middleware);
},
async transform(code: string, id: string) {
const getMap = () => (needSourcemap ? this.getCombinedSourcemap() : null);
const getResult = (content: string) => ({
map: needSourcemap ? this.getCombinedSourcemap() : null,
code: content,
});

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

0 comments on commit 5834b4f

Please sign in to comment.