linkeddata / rdflib.js

Linked Data API for JavaScript
http://linkeddata.github.io/rdflib.js/doc/
Other
564 stars 143 forks source link

Vite: Uncaught SyntaxError: import not found: default N3Lexer.js:3:7 #515

Open KonradHoeffner opened 2 years ago

KonradHoeffner commented 2 years ago

Has anyone successfully used this library in the browser with Vite? In my own attempt using vite 2.6.4, there was first an "global is not defined" error but after fixing that, using the following vite.config.js, I now get the error Uncaught SyntaxError: import not found: default N3Lexer.js:3:7.

vite.config.js

import nodeGlobals from 'rollup-plugin-node-globals'

export default {
    sourcemap: true,
    plugins: [nodeGlobals(),],
    optimizeDeps: {exclude: ["rdflib"]},
    build: {
        target: "esnext",
        rollupOptions: {
            output: {
                assetFileNames: "assets/[name][extname]",
            },
        },
    },
};

Code

import * as rdf from "rdflib";
const RDF = rdf.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");

Error from npm run build

vite v2.6.4 building for production...
✓ 315 modules transformed.
'default' is not exported by node_modules/queue-microtask/index.js, imported by node_modules/n3/src/N3Lexer.js
file: /home/konrad/projekte/snik/cytoscape/node_modules/n3/src/N3Lexer.js:3:7
1: // **N3Lexer** tokenizes N3 documents.
2: import namespaces from './IRIs';
3: import queueMicrotask from 'queue-microtask';
          ^
4: 
5: const { xsd } = namespaces;
error during build:
Error: 'default' is not exported by node_modules/queue-microtask/index.js, imported by node_modules/n3/src/N3Lexer.js
    at error (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:158:30)
    at Module.error (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:12339:16)
    at Module.traceVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:12724:29)
    at ModuleScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:11517:39)
    at ChildScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
    at ClassBodyScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
    at ChildScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
    at FunctionScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
    at ChildScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
    at TrackingScope.findVariable (/home/konrad/projekte/snik/cytoscape/node_modules/rollup/dist/shared/rollup.js:5837:38)
KonradHoeffner commented 2 years ago

When using import { Namespace, graph, sym, parse, SPARQLToQuery } from "rdflib"; instead of import * as rdf from "rdflib", it still doesn't work with npm run dev, however it works with npm run build && npm run preview on Vite! They scripts are defined in package.json as:

    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
KonradHoeffner commented 2 years ago

The problem still occurs with Vite v2.9.5 and rdflib 2.2.19.

MWE

$ npm create vite@latest
$ cd vite-project
$ npm install rdflib rollup-plugin-node-globals

Then create vite.config.js as defined in the original post and add the following to main.js:

import * as rdf from "rdflib";
const RDF = rdf.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#");

Then run npm run build:

> vite-project@0.0.0 build
> vite build

vite v2.9.5 building for production...
✓ 195 modules transformed.
'default' is not exported by node_modules/queue-microtask/index.js, imported by node_modules/n3/src/N3Lexer.js
file: /tmp/vite-project/node_modules/n3/src/N3Lexer.js:3:7
1: // **N3Lexer** tokenizes N3 documents.
2: import namespaces from './IRIs';
3: import queueMicrotask from 'queue-microtask';
          ^
4: 
5: const { xsd } = namespaces;
error during build:
Error: 'default' is not exported by node_modules/queue-microtask/index.js, imported by node_modules/n3/src/N3Lexer.js
    at error (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:198:30)
    at Module.error (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:12521:16)
    at Module.traceVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:12879:29)
    at ModuleScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:11509:39)
    at ChildScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)
    at ClassBodyScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)
    at ChildScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)
    at FunctionScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)
    at ChildScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)
    at TrackingScope.findVariable (/tmp/vite-project/node_modules/rollup/dist/shared/rollup.js:6477:38)