|
| 1 | +import path from 'node:path' |
| 2 | +import * as glob from 'glob' |
| 3 | + |
| 4 | +const shorthandRoot = path.join(process.cwd(), 'app/javascript') |
| 5 | +const config = { |
| 6 | + shorthandRoot |
| 7 | +} |
| 8 | + |
| 9 | +export function resolve(specifier, context, nextResolve) { |
| 10 | + let finalSpecifier = specifier |
| 11 | + if (specifier.startsWith('#')) { |
| 12 | + finalSpecifier = resolveShorthandPath(finalSpecifier, context, nextResolve) |
| 13 | + } |
| 14 | + |
| 15 | + if (specifier.includes('*')) { |
| 16 | + return resolveGlobPath(finalSpecifier, context, nextResolve) |
| 17 | + } |
| 18 | + |
| 19 | + return nextResolve(finalSpecifier, context) |
| 20 | +} |
| 21 | + |
| 22 | +export function load(specifier, context, nextLoad) { |
| 23 | + if (!specifier.includes('*')) return nextLoad(specifier, context) |
| 24 | + |
| 25 | + const pattern = specifier.replace('file://', '') |
| 26 | + const paths = glob.globSync(pattern, { nodir: true }) |
| 27 | + |
| 28 | + const pathParts = pattern.split('/') |
| 29 | + const basePathParts = pathParts.slice(0, pathParts.findIndex(part => part.includes('*'))) |
| 30 | + // we need to determine how many times we have a "*" in a folder level. The imports have to go back that far in order to work properly. |
| 31 | + const folderLevels = pathParts.length - basePathParts.length - 1 // minus one for the file |
| 32 | + const basePath = basePathParts.join('/') |
| 33 | + const baseImportPath = `${Array(folderLevels).fill('../').join('')}` |
| 34 | + |
| 35 | + const getModuleIdentifier = index => `module${index}` |
| 36 | + const getImportPath = file => path.join(baseImportPath, path.relative(basePath, file)) |
| 37 | + const importStatements = paths.map((file, index) => { |
| 38 | + return `import * as ${getModuleIdentifier(index)} from './${getImportPath(file)}'` |
| 39 | + }) |
| 40 | + const exportStatement = `export default [${paths.map((_s, index) => getModuleIdentifier(index)).join(', ') }]` |
| 41 | + |
| 42 | + const content = [...importStatements, exportStatement].join('\n') |
| 43 | + const source = Buffer.from(content) |
| 44 | + |
| 45 | + return { |
| 46 | + format: "module", |
| 47 | + source, |
| 48 | + shortCircuit: true |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function resolveShorthandPath(specifier, _context, _nextResolve) { |
| 53 | + return path.join(config.shorthandRoot, specifier.slice(1)) |
| 54 | +} |
| 55 | + |
| 56 | +function resolveGlobPath(specifier, context, _nextResolve) { |
| 57 | + // We have to manually resolve the path so that we can load all the files that match the pattern later |
| 58 | + let finalPath |
| 59 | + if (specifier.startsWith('.')) { // Relative path to parent |
| 60 | + const basePath = context.parentURL.replace('file://', '').split('/').slice(0, -1).join('/') |
| 61 | + finalPath = path.join(basePath, specifier) |
| 62 | + } else if (specifier.startsWith('/')) { // Absolute path |
| 63 | + finalPath = specifier |
| 64 | + } else { // node modules |
| 65 | + finalPath = path.join(process.cwd(), 'node_modules', specifier) |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + url: `file://${finalPath}`, |
| 70 | + type: "module", |
| 71 | + shortCircuit: true |
| 72 | + } |
| 73 | +} |
0 commit comments