Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import JSON as module #386

Closed
akaFTS opened this issue Sep 11, 2021 · 10 comments
Closed

Import JSON as module #386

akaFTS opened this issue Sep 11, 2021 · 10 comments

Comments

@akaFTS
Copy link

akaFTS commented Sep 11, 2021

Importing JSON files as modules causes a deno-ts error:

import classes from "~/data/classes/classes.json";
Module '~/data/classes/classes.json' was resolved to 'file:///workspaces/bcc-data/data/classes/classes.json', but '--resolveJsonModule' is not used.

Seems to be just a matter of adding the above flag to Aleph's tsconfig.

@ije
Copy link
Member

ije commented Sep 18, 2021

because deno doesn't support json as module ye, check denoland/deno#7623

@ije
Copy link
Member

ije commented Sep 18, 2021

but in your case i guess you can add a json file loader like:

import type { Plugin } from 'aleph/types.d.ts'

export default <Plugin>{
  name: 'json-loader',
  setup: aleph => {
    aleph.onLoad(/\.json$/i, async ({ specifier }) => {
      const { content } = await aleph.fetchModule(specifier)
      return {
        code: `export default ` + new TextDecoder().decode(content)
      }
    })
  }
}

@ije
Copy link
Member

ije commented Sep 18, 2021

anyway, i added a json-loader here: https://github.com/alephjs/aleph.js/blob/master/plugins/json.ts

@akaFTS
Copy link
Author

akaFTS commented Sep 18, 2021

Thanks. Would you be interested in a YAML loader as well? I can send a PR.

@ije
Copy link
Member

ije commented Sep 18, 2021

cool, can you please create a deno module? like https://github.com/ije/aleph-plugin-windicss, then add it to https://github.com/alephjs/alephjs.org/blob/master/pages/docs/plugins/community-plugins.md, if you're interested, thanks @akaFTS

@akaFTS akaFTS closed this as completed Sep 19, 2021
@akaFTS
Copy link
Author

akaFTS commented Sep 25, 2021

In fact, deno-ts still complains when importing a JSON module using the plugin. Any way to fix that?

@akaFTS akaFTS reopened this Sep 25, 2021
@hazae41
Copy link
Member

hazae41 commented Oct 28, 2021

That's expected because Deno doesn't have a builtin JSON importer

You can use @ts-ignore to ignore this warning

index.tsx

// @ts-ignore
import json from "~/assets/test.json"

export default function Home() {
  const [count, isSyncing, increase, decrease] = useCounter()
  const version = useDeno(() => Deno.version.deno)

  console.log(json)

  ...

Don't forget to install the plugin

aleph.config.ts

import type { Config } from 'aleph/types'
import json from "aleph/plugins/json.ts"

export default <Config>{
  plugins: [json()]
}

@hazae41 hazae41 closed this as completed Oct 28, 2021
@akaFTS
Copy link
Author

akaFTS commented Oct 30, 2021

But I assume Deno also does not have a built-in CSS importer and still deno-ts does not complain when you import those in Aleph. Is there no way to customize it and whitelist some extensions?

Also as Aleph evolves and other third-party loaders are added (i.e. GraphQL or YAML), they will also face the same issue and adding @ts-ignore to everything is very cumbersome.

@hazae41
Copy link
Member

hazae41 commented Oct 30, 2021

I think that may be because the CSS import doesn't have a name:

import "myfile.css"

While the JSON does have a name:

import myfile from "myfile.json"

@gurk
Copy link

gurk commented Jan 5, 2022

Deno now supports importing JSON as module.

https://deno.com/blog/v1.17#import-assertions-and-json-modules

Dynamic importing works with Aleph, but local and remote seem to not.

TypeError: Expected a "JavaScript" module but loaded a "JSON" module. at async Aleph.getAPIRoute (https://deno.land/x/aleph@v0.3.0-beta.19/server/aleph.ts:486:29) at async Server.handle (https://deno.land/x/aleph@v0.3.0-beta.19/server/server.ts:240:23) at async https://deno.land/x/aleph@v0.3.0-beta.19/server/server.ts:382:15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants