Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Last active November 22, 2022 15:55
Show Gist options
  • Save stepankuzmin/77f2e28195f000d1a63ac209d9087237 to your computer and use it in GitHub Desktop.
Save stepankuzmin/77f2e28195f000d1a63ac209d9087237 to your computer and use it in GitHub Desktop.
Nove v18 loader for `mapbox-gl`
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
});
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