Skip to content

feat(core): Add inline source-maps #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/createVue3SFCModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type PreprocessLang = SFCAsyncStyleCompileOptions['preprocessLang'];
* the version of the library
*/
import { version, vueVersion } from './index'
import { RawSourceMap } from '@vue/component-compiler-utils/dist/types'

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


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

/**
* @internal
Expand Down Expand Up @@ -118,6 +122,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
scoped: hasScoped,
id: scopeId,
slotted: descriptor.slotted,
inMap: descriptor.template.map,
compilerOptions: {
isCustomElement,
whitespace,
Expand Down Expand Up @@ -180,6 +185,9 @@ export async function createSFCModule(source : string, filename : AbstractPath,
templateOptions: compileTemplateOptions,
});

if(scriptBlock.map)
scriptBlock.content += `\r\n${buildMapComment(scriptBlock.map)}`

// note:
// scriptBlock.content is the script code after vue transformations
// scriptBlock.scriptAst is the script AST before vue transformations
Expand All @@ -192,7 +200,7 @@ export async function createSFCModule(source : string, filename : AbstractPath,
compileTemplateOptions.compilerOptions.bindingMetadata = bindingMetadata;

await loadDeps(filename, depsList, options);
Object.assign(component, interopRequireDefault(createCJSModule(filename, transformedScriptSource, options).exports).default);
Object.assign(component, interopRequireDefault((await createCJSModule(filename, transformedScriptSource, options)).exports).default);
}


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

if(template.map)
template.code += `\r\n${buildMapComment(template.map)}`

return await transformJSCode(template.code, true, descriptor.filename, additionalBabelParserPlugins, additionalBabelPlugins, log, devMode);
});

await loadDeps(filename, templateDepsList, options);
Object.assign(component, createCJSModule(filename, templateTransformedSource, options).exports);
Object.assign(component, (await createCJSModule(filename, templateTransformedSource, options)).exports);
}


Expand Down
33 changes: 28 additions & 5 deletions src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
comments: devMode,
retainLines: devMode,
//envName: devMode ? 'development' : 'production', see 'process.env.BABEL_ENV': JSON.stringify(mode),

filename: filename.toString(),
//minified,
sourceType: moduleSourceType ? 'module' : 'script',
});
Expand Down Expand Up @@ -324,7 +324,7 @@ export async function loadModuleInternal(pathCx : PathContext, options : Options
* Create a cjs module
* @internal
*/
export function defaultCreateCJSModule(refPath : AbstractPath, source : string, options : Options) : Module {
export async function defaultCreateCJSModule(refPath : AbstractPath, source : string, options : Options) : Promise<Module> {

const { moduleCache, pathResolve, getResource } = options;

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

// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L195-L198
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L1102
const moduleFunction = Function('exports', 'require', 'module', '__filename', '__dirname', '__vsfcl_import__', source);
moduleFunction.call(module.exports, module.exports, require, module, refPath, pathResolve({ refPath, relPath: '.' }, options), importFunction);
const wrappedSource = `(exports, require, module, __filename, __dirname, __vsfcl_import__) => { ${source} \r\n }`

let ast = babel_parse(wrappedSource, {
sourceType: 'module',
sourceFilename: refPath.toString(),
});

const transformedScript = await babel_transformFromAstAsync(ast, wrappedSource, {
sourceMaps: "inline",
babelrc: false,
configFile: false,
highlightCode: false,
filename: refPath.toString(),
sourceType: 'module',
});

if ( transformedScript === null || transformedScript.code == null ) { // == null or undefined

const msg = `unable to transform script "${refPath.toString()}"`;
options.log?.('error', msg);
throw new Error(msg)
}


eval(transformedScript.code)(module.exports, require, module, refPath, pathResolve({ refPath, relPath: '.' }, options), importFunction)

return module;
}
Expand Down Expand Up @@ -379,7 +402,7 @@ export async function createJSModule(source : string, moduleSourceType : boolean
});

await loadDeps(filename, depsList, options);
return createCJSModule(filename, transformedSource, options).exports;
return (await createCJSModule(filename, transformedSource, options)).exports;
}


Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export type Options = {
* creates a CommonJS module from JS source string.
* *(optional)*
*/
createCJSModule(refPath : AbstractPath, source : string, options : Options) : Module,
createCJSModule(refPath : AbstractPath, source : string, options : Options) : Promise<Module>,



Expand Down