Skip to content

Commit c2e8d74

Browse files
author
Franck Freiburger
committed
feat(core): add devMode option
as far as possible, preserves original code formatting closes #123
1 parent deca103 commit c2e8d74

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/createVue2SFCModule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
6868

6969
const component = {};
7070

71-
const { delimiters, whitespace, moduleCache, compiledCache, getResource, addStyle, log, additionalBabelParserPlugins = [], additionalBabelPlugins = {}, customBlockHandler } = options;
71+
const { delimiters, whitespace, moduleCache, compiledCache, getResource, addStyle, log, additionalBabelParserPlugins = [], additionalBabelPlugins = {}, customBlockHandler, devMode = false } = options;
7272

7373
const descriptor = sfc_parse({
7474
source,
@@ -107,7 +107,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
107107
comments: true
108108
} as any,
109109
isProduction: isProd,
110-
prettify: false
110+
prettify: devMode,
111111
} : null;
112112

113113
// Vue2 doesn't support preprocessCustomRequire, so we have to preprocess manually
@@ -136,7 +136,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
136136

137137
const [ depsList, transformedScriptSource ] = await withCache(compiledCache, [ componentHash, src, additionalBabelParserPlugins, Object.keys(additionalBabelPlugins) ], async ({ preventCache }) => {
138138

139-
return await transformJSCode(src, true, strFilename, [ ...additionalBabelParserPlugins, 'jsx' ], { ...additionalBabelPlugins, jsx, babelSugarInjectH }, log);
139+
return await transformJSCode(src, true, strFilename, [ ...additionalBabelParserPlugins, 'jsx' ], { ...additionalBabelPlugins, jsx, babelSugarInjectH }, log, devMode);
140140
});
141141

142142
await loadDeps(filename, depsList, options);
@@ -180,7 +180,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
180180
log?.('info', 'SFC template', formatErrorStartEnd(err.msg, strFilename, source, err.start, err.end ));
181181
}
182182

183-
return await transformJSCode(template.code, true, filename, additionalBabelParserPlugins, additionalBabelPlugins, log);
183+
return await transformJSCode(template.code, true, filename, additionalBabelParserPlugins, additionalBabelPlugins, log, devMode);
184184
});
185185

186186
await loadDeps(filename, templateDepsList, options);

src/createVue3SFCModule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
6868

6969
const component : { [key: string]: any } = {};
7070

71-
const { delimiters, whitespace, moduleCache, compiledCache, getResource, addStyle, log, additionalBabelParserPlugins = [], additionalBabelPlugins = {}, customBlockHandler } = options;
71+
const { delimiters, whitespace, moduleCache, compiledCache, getResource, addStyle, log, additionalBabelParserPlugins = [], additionalBabelPlugins = {}, customBlockHandler, devMode = false } = options;
7272

7373
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
7474
const { descriptor, errors } = sfc_parse(source, {
@@ -145,7 +145,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
145145
if ( compileTemplateOptions !== null )
146146
compileTemplateOptions.compilerOptions.bindingMetadata = scriptBlock.bindings;
147147

148-
return await transformJSCode(scriptBlock.content, true, strFilename, [ ...additionalBabelParserPlugins, 'jsx' ], { ...additionalBabelPlugins, jsx }, log);
148+
return await transformJSCode(scriptBlock.content, true, strFilename, [ ...additionalBabelParserPlugins, 'jsx' ], { ...additionalBabelPlugins, jsx }, log, devMode);
149149

150150
});
151151

@@ -180,7 +180,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
180180
for ( const err of template.tips )
181181
log?.('info', 'SFC template', err);
182182

183-
return await transformJSCode(template.code, true, descriptor.filename, additionalBabelParserPlugins, additionalBabelPlugins, log);
183+
return await transformJSCode(template.code, true, descriptor.filename, additionalBabelParserPlugins, additionalBabelPlugins, log, devMode);
184184
});
185185

186186
await loadDeps(filename, templateDepsList, options);

src/tools.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const targetBrowserBabelPlugins = { ...(typeof ___targetBrowserBabelPlugins !==
212212
/**
213213
* @internal
214214
*/
215-
export async function transformJSCode(source : string, moduleSourceType : boolean, filename : AbstractPath, additionalBabelParserPlugins : Options['additionalBabelParserPlugins'], additionalBabelPlugins : Options['additionalBabelPlugins'], log : Options['log']) : Promise<[string[], string]> {
215+
export async function transformJSCode(source : string, moduleSourceType : boolean, filename : AbstractPath, additionalBabelParserPlugins : Options['additionalBabelParserPlugins'], additionalBabelPlugins : Options['additionalBabelPlugins'], log : Options['log'], devMode : boolean = false) : Promise<[string[], string]> {
216216

217217
let ast: t.File;
218218
try {
@@ -247,8 +247,13 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
247247
babelrc: false,
248248
configFile: false,
249249
highlightCode: false,
250-
compact: true, // doc: All optional newlines and whitespace will be omitted when generating code in compact mode.
251-
comments: false,
250+
compact: !devMode, // doc: All optional newlines and whitespace will be omitted when generating code in compact mode.
251+
comments: devMode,
252+
retainLines: devMode,
253+
//envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
254+
255+
//minified,
256+
sourceType: moduleSourceType ? 'module' : 'script',
252257
});
253258

254259
if ( transformedScript === null || transformedScript.code == null ) { // == null or undefined
@@ -357,7 +362,7 @@ export async function createJSModule(source : string, moduleSourceType : boolean
357362

358363
const [ depsList, transformedSource ] = await withCache(compiledCache, [ version, source, filename ], async () => {
359364

360-
return await transformJSCode(source, moduleSourceType, filename, additionalBabelParserPlugins, additionalBabelPlugins, log);
365+
return await transformJSCode(source, moduleSourceType, filename, additionalBabelParserPlugins, additionalBabelPlugins, log, options.devMode);
361366
});
362367

363368
await loadDeps(filename, depsList, options);

src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@ export type Options = {
399399
* ```
400400
*/
401401
customBlockHandler?(block : CustomBlock, filename : AbstractPath, options : Options) : Promise<CustomBlockCallback | undefined>,
402-
402+
403+
404+
/**
405+
* Set development mode
406+
* prevent minification, allow debugger statement,
407+
*/
408+
devMode?: boolean,
409+
403410
}
404411

405412

0 commit comments

Comments
 (0)