11import { pathToFileURL } from 'node:url'
2- import { createUnplugin } from 'unplugin'
2+ import { UnpluginOptions , createUnplugin } from 'unplugin'
33import { parseURL , parseQuery } from 'ufo'
44import MagicString from 'magic-string'
55import { addFlytrapInit , addMissingFlytrapImports } from './transform/imports'
@@ -11,66 +11,77 @@ import { loadConfig } from './transform/config'
1111import { setFlytrapConfig } from './core/config'
1212import { log } from './core/logging'
1313
14- export const FlytrapTransformPlugin = createUnplugin ( ( ) => {
15- return {
16- name : 'FlytrapTransformPlugin' ,
17- enforce : 'pre' ,
18- transformInclude ( id ) {
19- const { pathname, search } = parseURL ( decodeURIComponent ( pathToFileURL ( id ) . href ) )
20- const { type } = parseQuery ( search )
14+ export const unpluginOptions : UnpluginOptions = {
15+ name : 'FlytrapTransformPlugin' ,
16+ enforce : 'pre' ,
17+ transformInclude ( id ) {
18+ const { pathname, search } = parseURL ( decodeURIComponent ( pathToFileURL ( id ) . href ) )
19+ const { type } = parseQuery ( search )
2120
22- // vue files
23- if ( pathname . endsWith ( '.vue' ) && ( type === 'script' || ! search ) ) {
24- return true
25- }
21+ // vue files
22+ if ( pathname . endsWith ( '.vue' ) && ( type === 'script' || ! search ) ) {
23+ return true
24+ }
2625
27- // svelte files
28- if ( pathname . endsWith ( '.svelte' ) && ( type === 'script' || ! search ) ) {
29- return true
30- }
26+ // svelte files
27+ if ( pathname . endsWith ( '.svelte' ) && ( type === 'script' || ! search ) ) {
28+ return true
29+ }
3130
32- // js files
33- if ( pathname . match ( / \. ( ( c | m ) ? j | t ) s x ? $ / g) ) {
34- return true
35- }
31+ // js files
32+ if ( pathname . match ( / \. ( ( c | m ) ? j | t ) s x ? $ / g) ) {
33+ return true
34+ }
3635
37- return false
38- } ,
39- async transform ( code , id ) {
40- if ( code . includes ( '@flytrap-ignore' ) || id . includes ( '/node_modules/' ) ) {
41- return
42- }
36+ return false
37+ } ,
38+ async transform ( code , id ) {
39+ if ( code . includes ( '@flytrap-ignore' ) || id . includes ( '/node_modules/' ) ) {
40+ return
41+ }
4342
44- // Logging config
45- const config = await loadConfig ( )
46- if ( config ) setFlytrapConfig ( config )
47- log . info ( 'transform' , `Transforming file ${ id } ` )
43+ // Logging config
44+ const config = await loadConfig ( )
45+ if ( config ) setFlytrapConfig ( config )
46+ log . info ( 'transform' , `Transforming file ${ id } ` )
4847
49- const ss = new MagicString ( code )
50- // add missing Flytrap imports
51- addMissingFlytrapImports ( ss )
48+ const ss = new MagicString ( code )
49+ // add missing Flytrap imports
50+ addMissingFlytrapImports ( ss )
5251
53- // add Flytrap init
52+ // add Flytrap init
53+ if ( process . env . NODE_ENV !== 'test' ) {
5454 await addFlytrapInit ( ss )
55+ }
5556
56- // Find package root
57- const pkgDirPath = packageDirectorySync ( )
58- if ( ! pkgDirPath ) {
59- throw createHumanLog ( {
60- event : 'transform_failed' ,
61- explanation : 'transform_pkg_not_found'
62- } ) . toString ( )
63- }
57+ // Find package root
58+ const pkgDirPath = packageDirectorySync ( )
59+ if ( ! pkgDirPath ) {
60+ throw createHumanLog ( {
61+ event : 'transform_failed' ,
62+ explanation : 'transform_pkg_not_found'
63+ } ) . toString ( )
64+ }
6465
65- try {
66- return flytrapTransform ( ss . toString ( ) , id . replace ( pkgDirPath , '' ) )
67- } catch ( e ) {
68- console . warn ( `Oops! Something went wrong while transforming file ${ id } . Error:` )
69- console . warn ( e )
66+ try {
67+ return flytrapTransform ( ss . toString ( ) , id . replace ( pkgDirPath , '' ) )
68+ } catch ( e ) {
69+ console . log ( 'TRANSFORMED FILE CODE: ' )
70+ console . log ( ss . toString ( ) )
71+ console . log (
72+ ' errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- errorr -- '
73+ )
74+ console . log ( 'filepath ' , id )
75+ console . log ( JSON . stringify ( e ) )
76+ console . log ( e )
77+ console . log ( 'FILE ' , id )
78+ if ( process . env . NODE_ENV === 'test' ) {
79+ throw e
7080 }
81+ console . warn ( `Oops! Something went wrong while transforming file ${ id } . Error:` )
82+ console . warn ( e )
7183 }
7284 }
73- } )
85+ }
7486
75- // Export code transform
76- export * from './transform/index'
87+ export const FlytrapTransformPlugin = createUnplugin ( ( ) => unpluginOptions )
0 commit comments