Skip to content

Commit fa6bd6b

Browse files
tucker87Jason Tucker
authored andcommitted
feat(core): Add inline source-maps
This provides error information in the browser. Including the original source Removed changes to genSourceMap as I realized it was unrelated Fixes: #198
1 parent bed17ac commit fa6bd6b

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/createVue3SFCModule.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type PreprocessLang = SFCAsyncStyleCompileOptions['preprocessLang'];
4444
* the version of the library
4545
*/
4646
import { version, vueVersion } from './index'
47+
import { RawSourceMap } from '@vue/component-compiler-utils/dist/types'
4748

4849
// @ts-ignore
4950
const targetBrowserBabelPluginsHash : string = hash(...Object.keys({ ...(typeof ___targetBrowserBabelPlugins !== 'undefined' ? ___targetBrowserBabelPlugins : {}) }));
@@ -55,7 +56,10 @@ const genSourcemap : boolean = !!process.env.GEN_SOURCEMAP;
5556
*/
5657
const isProd : boolean = process.env.NODE_ENV === 'production';
5758

58-
59+
/**
60+
* @internal
61+
*/
62+
const buildMapComment = (map : RawSourceMap) => `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(JSON.stringify(map))}`
5963

6064
/**
6165
* @internal
@@ -118,6 +122,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
118122
scoped: hasScoped,
119123
id: scopeId,
120124
slotted: descriptor.slotted,
125+
inMap: descriptor.template.map,
121126
compilerOptions: {
122127
isCustomElement,
123128
whitespace,
@@ -180,6 +185,9 @@ export async function createSFCModule(source : string, filename : AbstractPath,
180185
templateOptions: compileTemplateOptions,
181186
});
182187

188+
if(scriptBlock.map)
189+
scriptBlock.content += `\r\n${buildMapComment(scriptBlock.map)}`
190+
183191
// note:
184192
// scriptBlock.content is the script code after vue transformations
185193
// scriptBlock.scriptAst is the script AST before vue transformations
@@ -192,7 +200,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
192200
compileTemplateOptions.compilerOptions.bindingMetadata = bindingMetadata;
193201

194202
await loadDeps(filename, depsList, options);
195-
Object.assign(component, interopRequireDefault(createCJSModule(filename, transformedScriptSource, options).exports).default);
203+
Object.assign(component, interopRequireDefault((await createCJSModule(filename, transformedScriptSource, options)).exports).default);
196204
}
197205

198206

@@ -237,11 +245,14 @@ export async function createSFCModule(source : string, filename : AbstractPath,
237245
for ( const err of template.tips )
238246
log?.('info', 'SFC template', err);
239247

248+
if(template.map)
249+
template.code += `\r\n${buildMapComment(template.map)}`
250+
240251
return await transformJSCode(template.code, true, descriptor.filename, additionalBabelParserPlugins, additionalBabelPlugins, log, devMode);
241252
});
242253

243254
await loadDeps(filename, templateDepsList, options);
244-
Object.assign(component, createCJSModule(filename, templateTransformedSource, options).exports);
255+
Object.assign(component, (await createCJSModule(filename, templateTransformedSource, options)).exports);
245256
}
246257

247258

src/tools.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
253253
comments: devMode,
254254
retainLines: devMode,
255255
//envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),
256-
256+
filename: filename.toString(),
257257
//minified,
258258
sourceType: moduleSourceType ? 'module' : 'script',
259259
});
@@ -324,7 +324,7 @@ export async function loadModuleInternal(pathCx : PathContext, options : Options
324324
* Create a cjs module
325325
* @internal
326326
*/
327-
export function defaultCreateCJSModule(refPath : AbstractPath, source : string, options : Options) : Module {
327+
export async function defaultCreateCJSModule(refPath : AbstractPath, source : string, options : Options) : Promise<Module> {
328328

329329
const { moduleCache, pathResolve, getResource } = options;
330330

@@ -348,8 +348,31 @@ export function defaultCreateCJSModule(refPath : AbstractPath, source : string,
348348

349349
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L195-L198
350350
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L1102
351-
const moduleFunction = Function('exports', 'require', 'module', '__filename', '__dirname', '__vsfcl_import__', source);
352-
moduleFunction.call(module.exports, module.exports, require, module, refPath, pathResolve({ refPath, relPath: '.' }, options), importFunction);
351+
const wrappedSource = `(exports, require, module, __filename, __dirname, __vsfcl_import__) => { ${source} \r\n }`
352+
353+
let ast = babel_parse(wrappedSource, {
354+
sourceType: 'module',
355+
sourceFilename: refPath.toString(),
356+
});
357+
358+
const transformedScript = await babel_transformFromAstAsync(ast, wrappedSource, {
359+
sourceMaps: "inline",
360+
babelrc: false,
361+
configFile: false,
362+
highlightCode: false,
363+
filename: refPath.toString(),
364+
sourceType: 'module',
365+
});
366+
367+
if ( transformedScript === null || transformedScript.code == null ) { // == null or undefined
368+
369+
const msg = `unable to transform script "${refPath.toString()}"`;
370+
options.log?.('error', msg);
371+
throw new Error(msg)
372+
}
373+
374+
375+
eval(transformedScript.code)(module.exports, require, module, refPath, pathResolve({ refPath, relPath: '.' }, options), importFunction)
353376

354377
return module;
355378
}
@@ -379,7 +402,7 @@ export async function createJSModule(source : string, moduleSourceType : boolean
379402
});
380403

381404
await loadDeps(filename, depsList, options);
382-
return createCJSModule(filename, transformedSource, options).exports;
405+
return (await createCJSModule(filename, transformedSource, options)).exports;
383406
}
384407

385408

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ export type Options = {
377377
* creates a CommonJS module from JS source string.
378378
* *(optional)*
379379
*/
380-
createCJSModule(refPath : AbstractPath, source : string, options : Options) : Module,
380+
createCJSModule(refPath : AbstractPath, source : string, options : Options) : Promise<Module>,
381381

382382

383383

0 commit comments

Comments
 (0)