Skip to content

Commit a5bdd2d

Browse files
refactor(core)!: replace recurrent (absPath, relPath) with PathContext
1 parent b0fa669 commit a5bdd2d

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

src/createVue2SFCModule.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export async function createSFCModule(source : string, filename : string, option
9696

9797
// hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below
9898
if ( descriptor.template && descriptor.template.lang )
99-
await loadModuleInternal(filename, descriptor.template.lang, options);
99+
await loadModuleInternal({ refPath: filename, relPath: descriptor.template.lang }, options);
100100

101101

102102
const hasScoped = descriptor.styles.some(e => e.scoped);
@@ -108,7 +108,7 @@ export async function createSFCModule(source : string, filename : string, option
108108

109109
const compileTemplateOptions : TemplateCompileOptions = descriptor.template ? {
110110
// hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap
111-
source: descriptor.template.src ? (await getResource(filename, descriptor.template.src, options).getContent()).content.toString() : descriptor.template.content,
111+
source: descriptor.template.src ? (await getResource({ refPath: filename, relPath: descriptor.template.src }, options).getContent()).content.toString() : descriptor.template.content,
112112
filename,
113113
compiler: vueTemplateCompiler as VueTemplateCompiler,
114114
compilerOptions: {
@@ -143,7 +143,7 @@ export async function createSFCModule(source : string, filename : string, option
143143

144144
// eg: https://github.com/vuejs/vue-loader/blob/v15.9.6/lib/index.js
145145

146-
const src = descriptor.script.src ? (await getResource(filename, descriptor.script.src, options).getContent()).content.toString() : descriptor.script.content;
146+
const src = descriptor.script.src ? (await getResource({ refPath: filename, relPath: descriptor.script.src }, options).getContent()).content.toString() : descriptor.script.content;
147147

148148
const [ depsList, transformedScriptSource ] = await withCache(compiledCache, [ componentHash, src ], async ({ preventCache }) => {
149149

@@ -238,7 +238,7 @@ export async function createSFCModule(source : string, filename : string, option
238238

239239
for ( const descStyle of descriptor.styles ) {
240240

241-
const src = descStyle.src ? (await getResource(filename, descStyle.src, options).getContent()).content.toString() : descStyle.content;
241+
const src = descStyle.src ? (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).content.toString() : descStyle.content;
242242

243243
const style = await withCache(compiledCache, [ componentHash, src, descStyle.lang ], async ({ preventCache }) => {
244244

@@ -258,7 +258,7 @@ export async function createSFCModule(source : string, filename : string, option
258258

259259
// Vue2 doesn't support preprocessCustomRequire, so we have to preprocess manually
260260
if ( descStyle.lang && processors[descStyle.lang] === undefined )
261-
processors[descStyle.lang] = await loadModuleInternal(filename, descStyle.lang, options) as StylePreprocessor;
261+
processors[descStyle.lang] = await loadModuleInternal({ refPath: filename, relPath: descStyle.lang }, options) as StylePreprocessor;
262262

263263
const compiledStyle = await sfc_compileStyleAsync(compileStyleOptions);
264264
if ( compiledStyle.errors.length ) {

src/createVue3SFCModule.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,15 @@ export async function createSFCModule(source : string, filename : string, option
9797

9898
// hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below
9999
if ( descriptor.template && descriptor.template.lang )
100-
await loadModuleInternal(filename, descriptor.template.lang, options);
100+
await loadModuleInternal({ refPath: filename, relPath: descriptor.template.lang }, options);
101101

102102

103103
const hasScoped = descriptor.styles.some(e => e.scoped);
104104

105105
const compileTemplateOptions : SFCTemplateCompileOptions = descriptor.template ? {
106106
// hack, since sourceMap is not configurable an we want to get rid of source-map dependency. see genSourcemap
107107
compiler: { ...vue_CompilerDOM, compile: (template, options) => vue_CompilerDOM.compile(template, { ...options, sourceMap: genSourcemap }) },
108-
source: descriptor.template.src ? (await getResource(filename, descriptor.template.src, options).getContent()).content.toString() : descriptor.template.content,
108+
source: descriptor.template.src ? (await getResource({ refPath: filename, relPath: descriptor.template.src }, options).getContent()).content.toString() : descriptor.template.content,
109109
filename: descriptor.filename,
110110
isProd,
111111
scoped: hasScoped,
@@ -127,7 +127,7 @@ export async function createSFCModule(source : string, filename : string, option
127127
// doc: <script setup> cannot be used with the src attribute.
128128
// TBD: check if this is the right solution
129129
if ( descriptor.script?.src )
130-
descriptor.script.content = (await getResource(filename, descriptor.script.src, options).getContent()).content.toString();
130+
descriptor.script.content = (await getResource({ refPath: filename, relPath: descriptor.script.src }, options).getContent()).content.toString();
131131

132132
// TBD: handle <script setup src="...
133133

@@ -243,9 +243,9 @@ export async function createSFCModule(source : string, filename : string, option
243243

244244
// hack: asynchronously preloads the language processor before it is required by the synchronous preprocessCustomRequire() callback, see below
245245
if ( descStyle.lang )
246-
await loadModuleInternal(filename, descStyle.lang, options);
246+
await loadModuleInternal({ refPath: filename, relPath: descStyle.lang }, options);
247247

248-
const src = descStyle.src ? (await getResource(filename, descStyle.src, options).getContent()).content.toString() : descStyle.content;
248+
const src = descStyle.src ? (await getResource({ refPath: filename, relPath: descStyle.src }, options).getContent()).content.toString() : descStyle.content;
249249

250250
const style = await withCache(compiledCache, [ componentHash, src ], async ({ preventCache }) => {
251251

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { posix as Path } from 'path'
33
import { createJSModule, loadModuleInternal } from './tools'
44
import { createSFCModule } from './createSFCModule'
55

6-
import { ModuleExport, ModuleHandler, PathHandlers, Options, File, Resource } from './types'
6+
import { ModuleExport, ModuleHandler, PathHandlers, Options, File, Resource, PathContext } from './types'
77

88
/**
99
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
@@ -45,17 +45,17 @@ const defaultPathHandlers : PathHandlers = {
4545

4646
return Path.extname(filepath);
4747
},
48-
resolve(absoluteFilepath, dependencyPath) {
48+
resolve({ refPath, relPath } : PathContext) {
4949

50-
return dependencyPath[0] !== '.' ? dependencyPath : Path.normalize(Path.join(Path.dirname(absoluteFilepath), dependencyPath));
50+
return relPath[0] !== '.' ? relPath : Path.normalize(Path.join(Path.dirname(refPath), relPath));
5151
}
5252
}
5353

5454

55-
function defaultGetResource(currentResourcePath : string, depResourcePath : string, options : Options) : Resource {
55+
function defaultGetResource(pathCx : PathContext, options : Options) : Resource {
5656

5757
const { pathHandlers: { resolve }, getFile } = options;
58-
const path = resolve(currentResourcePath, depResourcePath);
58+
const path = resolve(pathCx);
5959
return {
6060
id: path,
6161
path: path,
@@ -134,5 +134,5 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
134134
moduleHandlers: { ...defaultModuleHandlers, ...moduleHandlers },
135135
};
136136

137-
return await loadModuleInternal('', path, options);
137+
return await loadModuleInternal( { refPath: '', relPath: path }, options);
138138
}

src/tools.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
ModuleExport,
3232
Module,
3333
LoadingType,
34+
PathContext,
3435
} from './types'
3536

3637
/**
@@ -239,11 +240,11 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
239240
// module tools
240241

241242

242-
export async function loadModuleInternal(currentPath : string, modulePath : string, options : Options) : Promise<ModuleExport> {
243+
export async function loadModuleInternal(pathCx : PathContext, options : Options) : Promise<ModuleExport> {
243244

244245
const { moduleCache, loadModule, moduleHandlers } = options;
245246

246-
const { id, path, getContent } = options.getResource(currentPath, modulePath, options);
247+
const { id, path, getContent } = options.getResource(pathCx, options);
247248

248249
if ( id in moduleCache ) {
249250

@@ -293,7 +294,7 @@ export function createModule(filename : string, source : string, options : Optio
293294

294295
const require = function(path : string) {
295296

296-
const { id } = getResource(filename, path, options);
297+
const { id } = getResource({ refPath: filename, relPath: path }, options);
297298
if ( id in moduleCache )
298299
return moduleCache[id];
299300

@@ -302,7 +303,7 @@ export function createModule(filename : string, source : string, options : Optio
302303

303304
const import_ = async function(path : string) {
304305

305-
return await loadModuleInternal(filename, path, options);
306+
return await loadModuleInternal({ refPath: filename, relPath: path }, options);
306307
}
307308

308309
const module = {
@@ -311,7 +312,7 @@ export function createModule(filename : string, source : string, options : Optio
311312

312313
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L195-L198
313314
// see https://github.com/nodejs/node/blob/a46b21f556a83e43965897088778ddc7d46019ae/lib/internal/modules/cjs/loader.js#L1102
314-
Function('exports', 'require', 'module', '__filename', '__dirname', 'import_', source).call(module.exports, module.exports, require, module, filename, resolve(filename, '.'), import_);
315+
Function('exports', 'require', 'module', '__filename', '__dirname', 'import_', source).call(module.exports, module.exports, require, module, filename, resolve({ refPath: filename, relPath: '.' }), import_);
315316

316317
return module;
317318
}
@@ -340,6 +341,6 @@ export async function createJSModule(source : string, moduleSourceType : boolean
340341
*/
341342
export async function loadDeps(filename : string, deps : string[], options : Options) : Promise<void> {
342343

343-
await Promise.all(deps.map(dep => loadModuleInternal(filename, dep, options)))
344+
await Promise.all(deps.map(dep => loadModuleInternal({ refPath: filename, relPath: dep }, options)))
344345
}
345346

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ export type Cache = {
1818
}
1919

2020

21+
export type PathContext = {
22+
/** reference path */
23+
refPath : string,
24+
/** relative to @refPath */
25+
relPath : string,
26+
}
27+
28+
2129
export type PathHandlers = {
2230
extname(filepath : string) : string,
2331
/*
2432
* relative to absolute module path resolution.
2533
*/
26-
resolve(absoluteFilepath : string, dependencyPath : string) : string,
34+
resolve(pathCx : PathContext) : string,
2735
}
2836

2937

@@ -326,7 +334,7 @@ export type Options = {
326334
* Abstact resource handling
327335
*
328336
*/
329-
getResource(currentResourcePath : string, depResourcePath : string, options : Options) : Resource,
337+
getResource(pathCx : PathContext, options : Options) : Resource,
330338

331339

332340

0 commit comments

Comments
 (0)