Skip to content

Commit 8ee6bf1

Browse files
tucker87Jason Tucker
authored andcommitted
feat(core): Add inline source-maps
This provides error information in the browser. Including the original source Fixes: #198
1 parent bed17ac commit 8ee6bf1

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
lines changed

build/rollup.config-node.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url))
1010
import pkg from '../package.json' assert { type: 'json' };
1111

1212

13-
const genSourcemap = false;
13+
const genSourcemap = "inline";
1414

1515
async function configFactory({ name, vueTarget, libraryTargetModule }) {
1616

build/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const configure = ({name, vueTarget, libraryTargetModule}) => async (env = {}, {
8888
}
8989
}));
9090

91-
const genSourcemap = false;
91+
const genSourcemap = "inline";
9292

9393
let actualTargetsBrowsers = targetsBrowsers;
9494

src/createVue2SFCModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import { version, vueVersion } from './index'
5050
// @ts-ignore
5151
const targetBrowserBabelPluginsHash : string = hash(...Object.keys({ ...(typeof ___targetBrowserBabelPlugins !== 'undefined' ? ___targetBrowserBabelPlugins : {}) }));
5252

53-
const genSourcemap : boolean = !!process.env.GEN_SOURCEMAP;
53+
const genSourcemap : boolean | "inline" = process.env.GEN_SOURCEMAP;
5454

5555
/**
5656
* @internal
@@ -88,7 +88,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
8888
const descriptor = sfc_parse({
8989
source,
9090
filename: strFilename,
91-
needMap: genSourcemap,
91+
needMap: !!genSourcemap,
9292
compiler: vueTemplateCompiler as VueTemplateCompiler}
9393
);
9494

src/createVue3SFCModule.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,22 @@ 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 : {}) }));
5051

51-
const genSourcemap : boolean = !!process.env.GEN_SOURCEMAP;
52+
const genSourcemap : boolean | "inline" = process.env.GEN_SOURCEMAP;
5253

5354
/**
5455
* @internal
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
@@ -87,7 +91,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
8791
// vue-loader next: https://github.com/vuejs/vue-loader/blob/next/src/index.ts#L91
8892
const { descriptor, errors } = sfc_parse(source, {
8993
filename: strFilename,
90-
sourceMap: genSourcemap,
94+
sourceMap: !!genSourcemap,
9195
});
9296

9397

@@ -111,13 +115,14 @@ export async function createSFCModule(source : string, filename : AbstractPath,
111115

112116
const compileTemplateOptions : SFCTemplateCompileOptions|undefined = descriptor.template ? {
113117
// hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap
114-
compiler: { ...vue_CompilerDOM, compile: (template, opts) => vue_CompilerDOM.compile(template, { ...opts, sourceMap: genSourcemap }) },
118+
compiler: { ...vue_CompilerDOM, compile: (template, options) => vue_CompilerDOM.compile(template, { ...options, sourceMap: !!genSourcemap }) },
115119
source: descriptor.template.src ? (await (await getResource({ refPath: filename, relPath: descriptor.template.src }, options).getContent()).getContentData(false)) as string : descriptor.template.content,
116120
filename: descriptor.filename,
117121
isProd,
118122
scoped: hasScoped,
119123
id: scopeId,
120124
slotted: descriptor.slotted,
125+
inMap: descriptor.template.map,
121126
compilerOptions: {
122127
isCustomElement,
123128
whitespace,
@@ -170,7 +175,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
170175
// src: https://github.com/vuejs/vue-next/blob/15baaf14f025f6b1d46174c9713a2ec517741d0d/packages/compiler-sfc/src/compileScript.ts#L43
171176
const scriptBlock = sfc_compileScript(descriptor, {
172177
isProd,
173-
sourceMap: genSourcemap,
178+
sourceMap: !!genSourcemap,
174179
id: scopeId,
175180
// @ts-ignore (unstable resolution: node_modules/@babel/parser/typings/babel-parser vs node_modules/@types/babel__core/node_modules/@babel/parser/typings/babel-parser)
176181
babelParserPlugins: [ ...contextBabelParserPlugins, ...additionalBabelParserPlugins ], // [...babelParserDefaultPlugins, 'jsx'] + additionalBabelParserPlugins // babelParserDefaultPlugins = [ 'bigInt', 'optionalChaining', 'nullishCoalescingOperator' ]
@@ -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: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { createSFCModule } from './createSFCModule'
4444
/**
4545
* @internal
4646
*/
47-
const genSourcemap : boolean = !!process.env.GEN_SOURCEMAP;
47+
const genSourcemap : boolean | "inline" = process.env.GEN_SOURCEMAP;
4848

4949
const version : string = process.env.VERSION as string;
5050

@@ -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)