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

Commit 1e423c3

Browse files
authored
fix: rework composition API registration (#484)
* 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.
1 parent 1d374fa commit 1e423c3

File tree

19 files changed

+45
-35
lines changed

19 files changed

+45
-35
lines changed

jest.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ module.exports = {
99
moduleNameMapper: {
1010
'@nuxtjs/composition-api/dist/runtime/globals':
1111
'<rootDir>/src/runtime/globals',
12-
'@nuxtjs/composition-api/dist/runtime/register':
13-
'<rootDir>/src/runtime/register',
1412
},
1513
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
"import": "./dist/runtime/index.mjs",
2424
"require": "./dist/runtime/index.js"
2525
},
26-
"./module": "./dist/module.js",
26+
"./module": "./dist/module/index.js",
2727
"./package.json": "./package.json",
28-
"./dist/babel-plugin": "./dist/babel-plugin.js",
29-
"./dist/runtime/*": "./dist/runtime/*"
28+
"./dist/babel-plugin": "./dist/babel-plugin/index.js",
29+
"./dist/runtime/globals": "./dist/runtime/globals.js",
30+
"./dist/runtime/templates/*": "./dist/runtime/templates/*"
3031
},
3132
"main": "./dist/runtime/index.js",
3233
"module": "./dist/runtime/index.mjs",
33-
"types": "./dist/index.d.ts",
34+
"types": "./dist/runtime/index.d.ts",
3435
"files": [
3536
"dist",
3637
"module.js"

siroc.config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { defineSirocConfig } from 'siroc'
22

33
export default defineSirocConfig({
44
rollup: {
5-
externals: [
6-
'@nuxtjs/composition-api/dist/runtime/globals',
7-
'@nuxtjs/composition-api/dist/runtime/register',
8-
],
5+
externals: ['@nuxtjs/composition-api/dist/runtime/globals'],
96
},
107
})

src/babel-plugin.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/babel-plugin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line
2+
module.exports = require('jiti')(__dirname)('./index.ts')
File renamed without changes.

src/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/babel-register.ts renamed to src/module/babel-register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function registerBabelPlugin(this: ModuleThis) {
1717
'Unable to automatically add Babel plugin. Make sure your custom `build.babel.plugins` returns `@nuxtjs/composition-api/dist/babel-plugin`'
1818
)
1919
} else {
20-
nuxtOptions.build.babel.plugins.push(resolveRelativePath('babel-plugin'))
20+
nuxtOptions.build.babel.plugins.push(resolveRelativePath('../babel-plugin'))
2121
}
2222

2323
/**
File renamed without changes.

src/module.ts renamed to src/module/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Module, NuxtOptions } from '@nuxt/types'
22
import { resolve } from 'upath'
3+
import type { Compiler } from 'webpack'
34

4-
import { name, version } from '../package.json'
5+
import { name, version } from '../../package.json'
56

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

30+
// Register the Vue Composition API for webpack
31+
32+
const registration = addResolvedTemplate.call(this, 'register.mjs')
33+
this.nuxt.hook('build:compile', ({ compiler }: { compiler: Compiler }) => {
34+
const entry = compiler.options.entry as Record<string, string[]>
35+
entry.app.unshift(registration)
36+
})
37+
2938
// Turn off webpack4 module context for .mjs files (as it appears to have some issues)
3039

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

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

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

4859
// If we're using Babel, register Babel plugin for injecting keys

0 commit comments

Comments
 (0)