Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
fix: rework composition API registration (#484)
Browse files Browse the repository at this point in the history
* addresses issue with .mjs format (nuxt + jest)
* compile composables into single file
* registers composition API using webpack entrypoint (or, for vite, by overriding `./middleware.js` import)
* retains auto-registration for testing (in cjs export - if `process.env.NODE_ENV === 'test'`)

closes #476, closes #479 (by implementing)

Also should cover nuxt/vite#134 and nuxt/vite#123.
  • Loading branch information
danielroe authored May 21, 2021
1 parent 1d374fa commit 1e423c3
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 35 deletions.
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ module.exports = {
moduleNameMapper: {
'@nuxtjs/composition-api/dist/runtime/globals':
'<rootDir>/src/runtime/globals',
'@nuxtjs/composition-api/dist/runtime/register':
'<rootDir>/src/runtime/register',
},
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
"import": "./dist/runtime/index.mjs",
"require": "./dist/runtime/index.js"
},
"./module": "./dist/module.js",
"./module": "./dist/module/index.js",
"./package.json": "./package.json",
"./dist/babel-plugin": "./dist/babel-plugin.js",
"./dist/runtime/*": "./dist/runtime/*"
"./dist/babel-plugin": "./dist/babel-plugin/index.js",
"./dist/runtime/globals": "./dist/runtime/globals.js",
"./dist/runtime/templates/*": "./dist/runtime/templates/*"
},
"main": "./dist/runtime/index.js",
"module": "./dist/runtime/index.mjs",
"types": "./dist/index.d.ts",
"types": "./dist/runtime/index.d.ts",
"files": [
"dist",
"module.js"
Expand Down
5 changes: 1 addition & 4 deletions siroc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import { defineSirocConfig } from 'siroc'

export default defineSirocConfig({
rollup: {
externals: [
'@nuxtjs/composition-api/dist/runtime/globals',
'@nuxtjs/composition-api/dist/runtime/register',
],
externals: ['@nuxtjs/composition-api/dist/runtime/globals'],
},
})
2 changes: 0 additions & 2 deletions src/babel-plugin.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/babel-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line
module.exports = require('jiti')(__dirname)('./index.ts')
File renamed without changes.
2 changes: 0 additions & 2 deletions src/index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/babel-register.ts → src/module/babel-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function registerBabelPlugin(this: ModuleThis) {
'Unable to automatically add Babel plugin. Make sure your custom `build.babel.plugins` returns `@nuxtjs/composition-api/dist/babel-plugin`'
)
} else {
nuxtOptions.build.babel.plugins.push(resolveRelativePath('babel-plugin'))
nuxtOptions.build.babel.plugins.push(resolveRelativePath('../babel-plugin'))
}

/**
Expand Down
File renamed without changes.
13 changes: 12 additions & 1 deletion src/module.ts → src/module/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Module, NuxtOptions } from '@nuxt/types'
import { resolve } from 'upath'
import type { Compiler } from 'webpack'

import { name, version } from '../package.json'
import { name, version } from '../../package.json'

import { registerBabelPlugin } from './babel-register'
import { addGlobalsFile } from './globals-register'
Expand All @@ -26,6 +27,14 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
'@vue/composition-api/dist/vue-composition-api.esm.js'
)

// Register the Vue Composition API for webpack

const registration = addResolvedTemplate.call(this, 'register.mjs')
this.nuxt.hook('build:compile', ({ compiler }: { compiler: Compiler }) => {
const entry = compiler.options.entry as Record<string, string[]>
entry.app.unshift(registration)
})

// Turn off webpack4 module context for .mjs files (as it appears to have some issues)

this.extendBuild(config => {
Expand All @@ -40,9 +49,11 @@ const compositionApiModule: Module<never> = function compositionApiModule() {

// If we're using nuxt-vite, register vite plugin & inject configuration

const viteMiddleware = addResolvedTemplate.call(this, 'middleware.mjs')
this.nuxt.hook('vite:extend', async (ctx: any) => {
const { compositionApiPlugin } = await import('./vite-plugin')
ctx.config.plugins.push(compositionApiPlugin())
ctx.config.resolve.alias['./middleware.js'] = viteMiddleware
})

// If we're using Babel, register Babel plugin for injecting keys
Expand Down
File renamed without changes.
7 changes: 4 additions & 3 deletions src/utils.ts → src/module/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NuxtConfig, NuxtOptions } from '@nuxt/types'
import { ModuleThis } from '@nuxt/types/config/module'
import { basename, join, resolve } from 'upath'
import { join, resolve } from 'upath'

export function isFullStatic(options: NuxtConfig) {
return (
Expand All @@ -26,10 +26,11 @@ export function addResolvedTemplate(
) {
const nuxtOptions: NuxtOptions = this.nuxt.options

const src = resolveRelativePath(join('runtime/templates', template))
const src = resolveRelativePath(`../runtime/templates/${template}`)
const filename = template.replace('register.mjs', 'register.js')
const { dst } = this.addTemplate({
src,
fileName: join('composition-api', basename(src)),
fileName: join('composition-api', filename),
options,
})

Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions src/runtime/composables/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '../register'

export { useAsync } from './async'
export { defineComponent } from './component'
export { useContext, withContext } from './context'
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

if (process.env.NODE_ENV === 'test') {
Vue.use(CompositionApi)
}

export * from './composables'
4 changes: 4 additions & 0 deletions src/runtime/templates/middleware.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import './register.js'
import middleware from '../middleware.js'

export default middleware
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ import Vue from 'vue'
import CompositionApi from '@vue/composition-api'

Vue.use(CompositionApi)

export default () => {}
18 changes: 7 additions & 11 deletions test/fixture/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@ const isTesting = process.env.NODE_ENV !== 'development'
const rootDir = resolve(__dirname, '../..')

const inDevelopment = !process.env.TEST_BUILT_MODULE
const moduleSource = join(rootDir, inDevelopment ? 'src' : 'dist')

console.log('Testing', inDevelopment ? 'source' : 'built', 'module')

export default <NuxtConfig>{
alias: {
'@nuxtjs/composition-api/dist/runtime/register': join(
rootDir,
inDevelopment ? 'src' : 'dist',
'runtime/register'
),
'@nuxtjs/composition-api/dist/runtime/globals': join(
rootDir,
inDevelopment ? 'src' : 'dist',
moduleSource,
'runtime/globals'
),
'@nuxtjs/composition-api': join(
rootDir,
inDevelopment ? 'src/runtime/index.ts' : 'dist/runtime/index.js'
'@nuxtjs/composition-api/dist/babel-plugin': join(
moduleSource,
'babel-plugin'
),
'@nuxtjs/composition-api': join(moduleSource, 'runtime'),
},
target: isGenerated ? 'static' : 'server',
publicRuntimeConfig: {
Expand Down Expand Up @@ -76,7 +72,7 @@ export default <NuxtConfig>{
buildModules: [
'@nuxt/typescript-build',
'@nuxtjs/pwa',
join(rootDir, inDevelopment ? 'src' : 'dist', 'module'),
join(moduleSource, 'module'),
],
pwa: {
icon: false,
Expand Down
3 changes: 0 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
"@nuxtjs/composition-api/dist/runtime/globals": [
"./src/runtime/globals.ts"
],
"@nuxtjs/composition-api/dist/runtime/register": [
"./src/runtime/register.ts"
],
"@nuxtjs/composition-api": ["./src/index.ts"]
},
"types": ["node", "@nuxt/types", "jest"]
Expand Down

0 comments on commit 1e423c3

Please sign in to comment.