1- import { fileURLToPath , pathToFileURL } from 'node:url' ;
1+ import { createRequire } from 'node:module' ;
2+ import path from 'node:path' ;
3+ import { pathToFileURL } from 'node:url' ;
24
35import { createFilter , dataToEsm } from '@rollup/pluginutils' ;
46import type { CSpellConfigFileReaderWriter } from 'cspell-config-lib' ;
@@ -12,6 +14,7 @@ export function createPlugin(): UnpluginInstance<Options | undefined, false> {
1214 return createUnplugin ( ( rawOptions = { } ) => {
1315 const options = resolveOptions ( rawOptions ) ;
1416 const filter = createFilter ( options . include , options . exclude ) ;
17+ const consoleLog = options . debug ? console . log . bind ( console ) : ( ) => { } ;
1518
1619 let readerWriter : CSpellConfigFileReaderWriter | undefined = undefined ;
1720 let bundler : CSpellDictionaryBundler | undefined = undefined ;
@@ -22,13 +25,13 @@ export function createPlugin(): UnpluginInstance<Options | undefined, false> {
2225 enforce : options . enforce ,
2326
2427 transform : {
25- // filter: {
26- // id: { include: options.include, exclude: options.exclude },
27- // },
28+ filter : {
29+ id : { include : options . include , exclude : options . exclude } ,
30+ } ,
2831 async handler ( code , id ) {
29- console . log ( `Can transform ${ id } ? ${ filter ( id ) ? 'yes' : 'no' } ` ) ;
32+ consoleLog ( `Can transform ${ id } ? ${ filter ( id ) ? 'yes' : 'no' } ` ) ;
3033 if ( ! filter ( id ) ) return undefined ;
31- console . log ( `Transforming ${ id } with ${ code . length } characters` ) ;
34+ consoleLog ( `Transforming ${ id } with ${ code . length } characters` ) ;
3235 readerWriter ??= createReaderWriter ( ) ;
3336 bundler ??= new CSpellDictionaryBundler ( readerWriter ) ;
3437
@@ -49,15 +52,17 @@ export function createPlugin(): UnpluginInstance<Options | undefined, false> {
4952 } ,
5053 } ,
5154 resolveId : {
52- // filter: {
53- // id: { include: options.include, exclude: options.exclude },
54- // },
55+ filter : {
56+ id : { include : options . include , exclude : options . exclude } ,
57+ } ,
5558 handler ( id , importer ) {
56- console . log ( `Can Resolve ${ id } ? ${ filter ( id ) ? 'yes' : 'no' } %o` , { id, importer } ) ;
59+ consoleLog ( `Can Resolve ${ id } ? ${ filter ( id ) ? 'yes' : 'no' } %o` , { id, importer } ) ;
5760 if ( id . includes ( '\0' ) || ! filter ( id ) ) return undefined ;
58- const importedFromUrl = pathToFileURL ( importer || process . cwd ( ) + '/' ) ;
59- const resolvedId = fileURLToPath ( import . meta. resolve ( id , importedFromUrl ) ) ;
60- console . log ( `Resolving ${ id } : %o` , import . meta. resolve ( id ) ) ;
61+ const resolvedId = resolveId ( id , importer ) ;
62+ if ( ! resolvedId ) return undefined ;
63+ consoleLog ( `Resolving ${ id } : %o` , {
64+ resolvedId,
65+ } ) ;
6166 return {
6267 id : resolvedId ,
6368 external : false ,
@@ -69,6 +74,20 @@ export function createPlugin(): UnpluginInstance<Options | undefined, false> {
6974 } ) ;
7075}
7176
77+ function resolveId ( id : string , importer ?: string ) : string | undefined {
78+ if ( id . includes ( '\0' ) ) return undefined ;
79+ const dir = ( importer ? path . dirname ( importer ) : process . cwd ( ) ) + '/' ;
80+ if ( id . startsWith ( './' ) || id . startsWith ( '../' ) ) {
81+ return path . resolve ( dir , id ) ;
82+ }
83+ const require = createRequire ( pathToFileURL ( dir ) ) ;
84+ try {
85+ return require . resolve ( id , { paths : [ dir ] } ) ;
86+ } catch {
87+ return undefined ;
88+ }
89+ }
90+
7291type Overwrite < T , U > = Pick < T , Exclude < keyof T , keyof U > > & U ;
7392
7493export type OptionsResolved = Overwrite < Required < Options > , Pick < Options , 'enforce' | 'exclude' > > ;
@@ -78,5 +97,6 @@ export function resolveOptions(options: Options): OptionsResolved {
7897 include : options . include || [ / .* c s p e l l (?: [ - ] e x t ) ? ( \. .* ) ? \. (?: j s o n c ? | y a ? m l | t o m l ) $ / i] ,
7998 exclude : options . exclude || undefined ,
8099 enforce : 'enforce' in options ? options . enforce : 'pre' ,
100+ debug : ! ! options . debug ,
81101 } ;
82102}
0 commit comments