Skip to content

Commit

Permalink
fix: use es module import as much as possible
Browse files Browse the repository at this point in the history
* causes slight dx regression with unregistered module (`This dependency was not found: fs`) - see #334
  • Loading branch information
danielroe committed Mar 2, 2021
1 parent 3df48e1 commit f7269b5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
27 changes: 27 additions & 0 deletions siroc.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineSirocConfig } from 'siroc'
import replace from '@rollup/plugin-replace'
import { resolve } from 'upath'

import jiti from 'jiti'

const getFile = jiti()

export default defineSirocConfig({
hooks: {
'build:extendRollup'(pkg, { rollupConfig }) {
rollupConfig.forEach(config => {
if (!config.plugins) return
config.plugins.push(
replace({
preventAssignment: true,
values: {
__HELPER_FUNCTIONS__: JSON.stringify(
Object.keys(getFile(resolve(__dirname, './src/entrypoint.ts')))
),
},
})
)
})
},
},
})
57 changes: 25 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
import { resolve, join } from 'upath'
import { withTrailingSlash } from 'ufo'
import { readdirSync, copyFileSync, existsSync, mkdirpSync } from 'fs-extra'

import type { Module, NuxtConfig } from '@nuxt/types'
import { name, version } from '../package.json'

const foolWebpack = (id: string) => require(id)
import type { Module, NuxtConfig } from '@nuxt/types'

const loadUtils = () => {
try {
// Try to load nuxt edge utils first
return foolWebpack('@nuxt/utils-edge')
} catch {
// if it fails, fall back to normal nuxt utils
return foolWebpack('@nuxt/utils')
}
function isFullStatic(options: NuxtConfig) {
return (
!options.dev &&
!options._legacyGenerate &&
options.target === 'static' &&
options.render?.ssr
)
}

const isUrl = function isUrl(url: string) {
function isUrl(url: string) {
return ['http', '//'].some(str => url.startsWith(str))
}

const compositionApiModule: Module<any> = function compositionApiModule() {
const utils = loadUtils()
const { readdirSync, copyFileSync, existsSync, mkdirpSync } = foolWebpack(
'fs-extra'
) as typeof import('fs-extra')

let corejsPolyfill = this.nuxt.options.build.corejs
? String(this.nuxt.options.build.corejs)
: undefined
try {
if (!['2', '3'].includes(corejsPolyfill || '')) {
// eslint-disable-next-line
const corejsPkg = require('core-js/package.json')
const corejsPkg = this.nuxt.resolver.requireModule('core-js/package.json')
corejsPolyfill = corejsPkg.version.slice(0, 1)
}
} catch {
Expand Down Expand Up @@ -92,8 +87,7 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
src: resolve(libRoot, 'lib', 'entrypoint.es.js'),
fileName: join('composition-api', 'index.js'),
options: {
isFullStatic:
'isFullStatic' in utils && utils.isFullStatic(this.nuxt.options),
isFullStatic: isFullStatic(this.nuxt.options),
staticPath: staticPath,
publicPath: isUrl(publicPath) ? publicPath : routerBase,
globalContext,
Expand Down Expand Up @@ -137,14 +131,10 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
return [[defaultPreset, newOptions]]
}

this.extendBuild(config => {
config.resolve = config.resolve || {}
config.resolve.alias = config.resolve.alias || {}
config.resolve.alias['@nuxtjs/composition-api'] = resolve(
this.options.buildDir || '',
entryDst
)
})
this.options.alias['@nuxtjs/composition-api'] = resolve(
this.options.buildDir || '',
entryDst
)

this.options.plugins = this.options.plugins || []
this.options.plugins.unshift(resolve(this.options.buildDir || '', pluginDst))
Expand All @@ -162,11 +152,12 @@ const compositionApiModule: Module<any> = function compositionApiModule() {
}
}

export default compositionApiModule

// eslint-disable-next-line
// @ts-ignore
compositionApiModule.meta = require('../package.json')
compositionApiModule.meta = {
name,
version,
}

const warnToAddModule = () => {
console.error(
Expand All @@ -177,8 +168,8 @@ const warnToAddModule = () => {
)
}

// eslint-disable-next-line
Object.keys(require('./entrypoint')).forEach(helper => {
const helperFunctions: string[] = JSON.parse(`__HELPER_FUNCTIONS__`)
helperFunctions.forEach(helper => {
// eslint-disable-next-line
// @ts-ignore
compositionApiModule[helper] = warnToAddModule
Expand All @@ -187,3 +178,5 @@ Object.keys(require('./entrypoint')).forEach(helper => {
// eslint-disable-next-line
// @ts-ignore
compositionApiModule.defineNuxtConfig = (config: NuxtConfig) => config

export default compositionApiModule
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"esModuleInterop": true,
"moduleResolution": "node",
"rootDir": "src",
"resolveJsonModule": true,
"outDir": "lib",
"declaration": false,
"skipLibCheck": true,
Expand Down

1 comment on commit f7269b5

@vercel
Copy link

@vercel vercel bot commented on f7269b5 Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.