Skip to content

Commit d7d5669

Browse files
wip(core)!: replace Options.moduleHandler object with Options.handleModule()
replace ModuleHandler : Record<string, ModuleHandler> with ModuleHandler = (extname : string, source : FileContent, path : string, options : Options) => Promise<ModuleExport | null>
1 parent 4163c12 commit d7d5669

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
lines changed

docs/examples.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@ node -e "require('express')().use(require('express').static(__dirname, {index:'i
161161
},
162162
},
163163
164-
additionalModuleHandlers: {
165-
'.json': (source, path, options) => JSON.parse(source),
164+
handleModule(extname, source, path, options) {
165+
166+
if ( extname === '.json' )
167+
return JSON.parse(source);
166168
}
167169
}
168170

src/index.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { posix as Path } from 'path'
22

3-
import { createJSModule, loadModuleInternal } from './tools'
4-
import { createSFCModule } from './createSFCModule'
3+
import { loadModuleInternal } from './tools'
54

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

87
/**
98
* the version of the library (process.env.VERSION is set by webpack, at compile-time)
@@ -27,16 +26,6 @@ function throwNotDefined(details : string) : never {
2726
}
2827

2928

30-
/**
31-
* @internal
32-
*/
33-
const defaultModuleHandlers : Record<string, ModuleHandler> = {
34-
'.vue': (source, path, options) => createSFCModule(source, path, options),
35-
'.js': (source, path, options) => createJSModule(source, false, path, options),
36-
'.mjs': (source, path, options) => createJSModule(source, true, path, options),
37-
};
38-
39-
4029
/**
4130
* Default implementation of PathHandlers
4231
*/
@@ -114,7 +103,6 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
114103
moduleCache = Object.create(null),
115104
getFile = throwNotDefined('options.getFile()'),
116105
addStyle = throwNotDefined('options.addStyle()'),
117-
moduleHandlers = null,
118106
pathHandlers = defaultPathHandlers,
119107
getResource = defaultGetResource,
120108
} = options_;
@@ -133,7 +121,6 @@ export async function loadModule(path : string, options_ : Options = throwNotDef
133121
getResource,
134122
...options_,
135123
getFile: normalizedGetFile,
136-
moduleHandlers: { ...defaultModuleHandlers, ...moduleHandlers },
137124
};
138125

139126
return await loadModuleInternal( { refPath: '/', relPath: path }, options);

src/tools.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import {
3434
PathContext,
3535
} from './types'
3636

37+
import { createSFCModule } from './createSFCModule'
38+
39+
3740
/**
3841
* @internal
3942
*/
@@ -241,7 +244,7 @@ export async function transformJSCode(source : string, moduleSourceType : boolea
241244

242245
export async function loadModuleInternal(pathCx : PathContext, options : Options) : Promise<ModuleExport> {
243246

244-
const { moduleCache, loadModule, moduleHandlers } = options;
247+
const { moduleCache, loadModule, handleModule } = options;
245248

246249
const { id, path, getContent } = options.getResource(pathCx, options);
247250

@@ -265,13 +268,20 @@ export async function loadModuleInternal(pathCx : PathContext, options : Options
265268

266269
const { content, extname } = await getContent();
267270

268-
if ( !(extname in moduleHandlers) )
269-
throw new TypeError(`Unable to handle ${ extname } files (${ path }), see moduleHandlers`);
270-
271271
if ( typeof content !== 'string' )
272272
throw new TypeError(`Invalid module content (${ path }): ${ content }`);
273273

274-
const module = await moduleHandlers[extname](content, path, options);
274+
// note: null module is accepted
275+
let module : ModuleExport | undefined | null = undefined;
276+
277+
if ( handleModule !== undefined )
278+
module = await handleModule(extname, content, path, options);
279+
280+
if ( module === undefined )
281+
module = await defaultHandleModule(extname, content, path, options);
282+
283+
if ( module === undefined )
284+
throw new TypeError(`Unable to handle ${ extname } files (${ path })`);
275285

276286
return moduleCache[id] = module;
277287

@@ -343,3 +353,17 @@ export async function loadDeps(filename : string, deps : string[], options : Opt
343353
await Promise.all(deps.map(dep => loadModuleInternal({ refPath: filename, relPath: dep }, options)))
344354
}
345355

356+
357+
/**
358+
* Default implementation of handleModule
359+
*/
360+
async function defaultHandleModule(extname : string, source : string, path : string, options : Options) : Promise<ModuleExport | null> {
361+
362+
switch (extname) {
363+
case '.vue': return createSFCModule(source.toString(), path, options);
364+
case '.js': return createJSModule(source.toString(), false, path, options);
365+
case '.mjs': return createJSModule(source.toString(), true, path, options);
366+
}
367+
368+
return null;
369+
}

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type PathHandlers = {
5757
* ...
5858
* ```
5959
*/
60-
export type ModuleHandler = (source : string, path : string, options : Options) => Promise<ModuleExport>;
60+
export type ModuleHandler = (extname : string, source : string, path : string, options : Options) => Promise<ModuleExport | null>;
6161

6262

6363
/**
@@ -241,10 +241,10 @@ export type Options = {
241241

242242

243243
/**
244-
* Additional module type handlers. see [[ModuleHandler]]
244+
* Handle additional module types (eg. '.svg', '.json' ). see [[ModuleHandler]]
245245
*
246246
*/
247-
moduleHandlers?: Record<string, ModuleHandler>,
247+
handleModule?: ModuleHandler,
248248

249249

250250
/**

tests/basic.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,11 @@ const { defaultFilesFactory, createPage } = require('./testsTools.js');
705705
'/optionsOverride.js': `
706706
export default (options) => {
707707
708-
options.moduleHandlers = {
709-
'.svg': (source, path, options) => 'data:image/svg+xml,' + source,
708+
options.handleModule = (extname, source, path, options) => {
709+
710+
switch (extname) {
711+
case '.svg': return 'data:image/svg+xml,' + source.toString();
712+
}
710713
};
711714
};
712715
`,

0 commit comments

Comments
 (0)