RDF/JS parsers (and serializers) are quite heavy, only work asynchronously, and depend on various node-native modules, such as stream
and crypto
. This can be a nuisance when static RDF is imported early in an application becasue it unnecessarily increases the size of the initial bundle and thus slows down the initial page load.
A webpack loader reserializes RDF into JS code so that it can be bundled along all other sources.
npm i -D webpack-loader-rdf
In webpack.config.js
module.exports = {
module: {
rules: [{
test: /\.(ttl|nt|nq|rdf|jsonld|trig)$/,
use: 'webpack-loader-rdf',
}],
}
}
In code:
- Import your favourire RDF/JS DataFatory
- Default-import RDF module
- Combine the two to get a
Quad[]
import * as $rdf from '@rdf-esm/data-model'
import getPersonQuads from './data/person.ttl`
const personQuads = getPersonQuads($rdf)
Declare this module, for example in a global.d.ts
, to get types for Turtle imports
declare module '*.ttl' {
import { Quad, DataFactory } from 'rdf-js'
export default function (factory: DataFactory): Quad[]
}