Last active
November 22, 2022 15:55
-
-
Save stepankuzmin/77f2e28195f000d1a63ac209d9087237 to your computer and use it in GitHub Desktop.
Nove v18 loader for `mapbox-gl`
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
diff --git a/index.js b/index.js | |
index f685aa3..7b7ad81 100644 | |
--- a/index.js | |
+++ b/index.js | |
@@ -49,7 +49,7 @@ module.exports = function flowRemoveTypes(source, options) { | |
allowReturnOutsideFunction: true, | |
allowSuperOutsideMethod: true, | |
sourceType: 'module', | |
- plugins: [ '*', 'jsx', 'flow', 'classProperties', 'importMeta' ], | |
+ plugins: ['*', 'jsx', 'flow', 'classProperties', 'importMeta', 'importAssertions' ], | |
tokens: true | |
}); |
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 flowRemoveTypes from '@mapbox/flow-remove-types'; | |
import {dataToEsm} from '@rollup/pluginutils'; | |
const glslRe = /\.glsl$/; | |
const jsonRe = /\.json$/; | |
export async function resolve(specifier, context, nextResolve) { | |
if (glslRe.test(specifier)) { | |
const url = new URL(specifier, context.parentURL).href; | |
return {url, shortCircuit: true}; | |
} | |
return nextResolve(specifier); | |
} | |
export async function load(url, context, nextLoad) { | |
if (context.format === 'module') { | |
const {source: rawSource} = await nextLoad(url, context); | |
const source = rawSource.toString(); | |
if (source.indexOf('@flow') >= 0) { | |
const transformedSource = flowRemoveTypes(source).toString(); | |
return {format: 'module', source: transformedSource, shortCircuit: true}; | |
} | |
} | |
if (glslRe.test(url)) { | |
const {source: rawSource} = await nextLoad(url, {...context, format: 'module'}); | |
const source = `export default \`${rawSource.toString()}\``; | |
return {format: 'module', source, shortCircuit: true}; | |
} | |
if (jsonRe.test(url)) { | |
const {source: rawSource} = await nextLoad(url, {...context, | |
// Force import assertions as "assert { type: 'json' }" | |
importAssertions: {type: 'json'} | |
}); | |
const source = dataToEsm(JSON.parse(rawSource.toString()), { | |
preferConst: true, | |
namedExports: true, | |
indent: ' ' | |
}); | |
return {format: 'module', source, shortCircuit: true}; | |
} | |
return nextLoad(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment