Created
May 31, 2022 22:05
-
-
Save mischnic/32ebf448f2c14dd24756a11499f02db2 to your computer and use it in GitHub Desktop.
Elm module groups resolver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"extends": "@parcel/config-default", | |
"resolvers": ["parcel-resolver-elm", "..."] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Elm} from 'elm:widget-a'; | |
console.log(Elm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "elmexample", | |
"version": "0.0.0", | |
"devDependencies": { | |
"@babel/core": "^7.0.0", | |
"@parcel/babel-register": "2.6.0", | |
"parcel-resolver-elm": "*", | |
"parcel": "2.6.0" | |
}, | |
"parcel-resolver-elm": { | |
"widget-a": [ | |
"./src/Main.elm", | |
"./src/MainB.elm", | |
"./src/MainC.elm" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
const {Resolver} = require('@parcel/plugin'); | |
const {loadConfig} = require('@parcel/utils'); | |
const {md} = require('@parcel/diagnostic'); | |
const nullthrows = require('nullthrows'); | |
const path = require('path'); | |
module.exports = (new Resolver({ | |
async resolve({dependency, options, specifier}) { | |
let parent = dependency.sourcePath; | |
if (specifier.startsWith('elm:') && parent != null) { | |
let name = specifier.slice('elm:'.length); | |
let pkg = await loadConfig( | |
options.inputFS, | |
parent, | |
['package.json'], | |
options.projectRoot, | |
); | |
let invalidateOnFileChange = pkg?.files.map(f => f.filePath) ?? []; | |
let invalidateOnFileCreate = [ | |
{fileName: 'package.json', aboveFilePath: parent}, | |
]; | |
let modules = pkg?.config['parcel-resolver-elm']; | |
if (modules) { | |
let pkgFile = nullthrows(pkg?.files)[0].filePath; | |
let pkgDir = path.dirname(pkgFile); | |
let mod = modules[name]; | |
if (mod != null && mod.length > 0) { | |
let mainPath = path.resolve(pkgDir, mod[0]); | |
let mainDir = path.dirname(mainPath); | |
let newSpecifier = `${mod[0]}?${mod | |
.slice(1) | |
.map(m => path.relative(mainDir, path.resolve(pkgDir, m))) | |
.join('&')}`; | |
return { | |
invalidateOnFileChange, | |
invalidateOnFileCreate, | |
filePath: path.join(pkgDir, 'index.js'), | |
code: `export * from "${newSpecifier}";`, | |
}; | |
} else { | |
return { | |
invalidateOnFileChange, | |
invalidateOnFileCreate, | |
diagnostics: { | |
message: md`No module defined with name "${name}" in ${pkgFile}`, | |
}, | |
}; | |
} | |
} | |
return { | |
invalidateOnFileChange, | |
invalidateOnFileCreate, | |
}; | |
} | |
}, | |
}) /*: Resolver */); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment