ricokahler / next-plugin-preval

Pre-evaluate async functions during builds and import them like JSON
MIT License
257 stars 13 forks source link

Cannot import modules prefixed with @ #103

Open tristan-kelley opened 2 years ago

tristan-kelley commented 2 years ago

Hi all -

Preval is great and fixes performance issues I was experiencing around constantly re-fetching data during a build. However, one minor problem I have is that Preval can't find modules whose imports are prefixed with @. If I convert the imports to absolute paths Preval works great. I assume there is something simple I'm missing. Any help would be appreciated. Thanks.

Error Message

Error: Failed to pre-evaluate "~/sites/rdc-nextjs-wp-poc/functions/wordpress/archives/getAndCacheAllPosts.preval.js". Error: Cannot find module '@/lib/wordpress/connector.js'

Imports from getAndCacheAllPosts.preval.js

import preval from 'next-plugin-preval'
import {initializeWpApollo} from '@/lib/wordpress/connector.js'
import {gql} from '@apollo/client'

I converted the imports to the below and Preval works

import preval from 'next-plugin-preval'
import {initializeWpApollo} from '../../../lib/wordpress/connector.js'
import {gql} from '../../../node_modules/@apollo/client'

next.config.js

const withPlugins = require('next-compose-plugins')
const withPreact = require('next-plugin-preact')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true'
})

const createNextPluginPreval = require('next-plugin-preval/config')
const withNextPluginPreval = createNextPluginPreval()

module.exports = withNextPluginPreval(
  withPlugins([
    withBundleAnalyzer,
    {
      productionBrowserSourceMaps: true,
      trailingSlash: true,
      swcMinify: true,
      withPreact
  ])
)