镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Support for .jsx extensions #11

Open

Description

@neilpa-inv

Is there a way to make this work with .jsx extensions, or really any non-standard extension? Looks like the built-in loader is used for resolving source code before calling the babel transformers. Since the built-in loader is restricted to only a few file extensions it limits the utility of this loader (short of renaming a ton of JSX files)

Activity

  1. kireerik commented on Jan 27, 2024

    @kireerik

    This is how a more general loader looks like:

    loader.js

    import {cwd} from 'process'
    import {relative, sep} from 'path'
    import {fileURLToPath} from 'url'
    
    import {transformFileSync} from '@babel/core'
    
    const dot = '.'
    
    , endsWith = (value, names) =>
    	value.endsWith(
    		names.reduce((result, name) =>
    			result + dot + name
    		, '')
    	)
    
    var extensions, options
    
    export const initialize = data =>
    	[extensions, options] = data
    
    , load = (url, context, nextLoad) =>
    	endsWith(url, extensions) ? {
    		shortCircuit: true
    		, format: 'module'
    		, source:
    			transformFileSync(
    				relative(
    					cwd(), fileURLToPath(url)
    				)
    					.split(sep).join('/')
    
    				, options
    			)
    				.code
    	} :
    		nextLoad(url, context)

    usage:

    import {register} from 'module'
    
    register('./loader.js', import.meta.url, {data: [/*extensions array*//*, options object*/]})
    
    //import(/*...*/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions