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

Commit

Permalink
refactor: change how library is imported (#418)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `@nuxtjs/composition-api/module` is now how the module should be imported in `nuxt.config`

* module exports are now identical in node versions with/without support for package.json exports. There are three public entrypoints:
  - `@nuxtjs/composition-api/babel` (for babel plugin which normally shouldn't be invoked by user)
  - `@nuxtjs/composition-api/entrypoint` (for jest config)
  - `@nuxtjs/composition-api/module` (the new module path, which MUST be updated in your `nuxt.config`)

This also solves a regression with using this module with `nuxt-vite`.
  • Loading branch information
danielroe authored Apr 2, 2021
1 parent 8cc21ce commit 5bb1a72
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 41 deletions.
2 changes: 2 additions & 0 deletions babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// stub file for node versions without support for package exports
module.exports = require('./lib/babel')
6 changes: 2 additions & 4 deletions docs/content/en/getting-started/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ version: 0.161
```js[nuxt.config.js]
{
buildModules: [
'@nuxtjs/composition-api'
'@nuxtjs/composition-api/module'
]
}
```
Expand Down Expand Up @@ -65,8 +65,6 @@ If you need to use jest tests with this module installed, just add the following

```js{}[jest.config.js]
moduleNameMapper: {
'@nuxtjs/composition-api': '@nuxtjs/composition-api/lib/entrypoint.js',
// alternatively, depending on your node version
// '@nuxtjs/composition-api': '@nuxtjs/composition-api/entrypoint',
'@nuxtjs/composition-api': '@nuxtjs/composition-api/entrypoint',
},
```
2 changes: 1 addition & 1 deletion docs/content/en/helpers/ssrPromise.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default defineComponent({

<alert type="info">

Under the hood, `ssrPromise` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
Under the hood, `ssrPromise` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.

</alert>

Expand Down
2 changes: 1 addition & 1 deletion docs/content/en/helpers/ssrRef.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const val2 = ssrRef(myExpensiveSetterFunction)

<alert type="info">

Under the hood, `ssrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
Under the hood, `ssrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.

</alert>

Expand Down
2 changes: 2 additions & 0 deletions entrypoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// stub file for node versions without support for package exports
module.exports = require('./lib/entrypoint')
2 changes: 1 addition & 1 deletion example/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
publicPath: 'example',
},
} : {}),
buildModules: ['@nuxtjs/composition-api'],
buildModules: ['@nuxtjs/composition-api/module'],
generate: {
interval: 2000,
async routes() {
Expand Down
2 changes: 2 additions & 0 deletions module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// stub file for node versions without support for package exports
module.exports = require('./lib/index')
22 changes: 9 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@
"url": "https://roe.dev"
},
"exports": {
".": "./lib/index.js",
"./babel": {
"require": "./lib/babel.js",
"import": "./lib/babel.es.js"
},
"./entrypoint": {
"require": "./lib/entrypoint.js",
"import": "./lib/entrypoint.es.js"
},
"./templates/*": "./lib/templates/*",
"./": "./"
"./babel": "./lib/babel.js",
"./entrypoint": "./lib/entrypoint.js",
"./module": "./lib/index.js",
"./lib/entrypoint.es": "./lib/entrypoint.es.js",
"./lib/templates/*": "./lib/templates/*",
"./package.json": "./package.json"
},
"main": "lib/index.js",
"types": "lib/entrypoint.d.ts",
"files": [
"lib/**/*",
"lib/index.d.ts",
"templates",
"babel.js",
"entrypoint.js",
"module.js",
"!**/*.map"
],
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/composables/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { validateKey } from './utils'
*
* **At the moment, `useAsync` is only suitable for one-offs, unless you provide your own unique key.**
* @param cb The async function that will populate the ref this function returns.
* @param key Under the hood, `useAsync` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @param key Under the hood, `useAsync` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
*
* @example
```ts
Expand Down
6 changes: 3 additions & 3 deletions src/composables/ssr-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const ssrValue = <T>(
/**
* `ssrRef` will automatically add ref values to `window.__NUXT__` on SSR if they have been changed from their initial value. It can be used outside of components, such as in shared utility functions, and it supports passing a factory function that will generate the initial value of the ref. **At the moment, an `ssrRef` is only suitable for one-offs, unless you provide your own unique key.**
* @param value This can be an initial value or a factory function that will be executed on server-side to get the initial value.
* @param key Under the hood, `ssrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @param key Under the hood, `ssrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @example
```ts
import { ssrRef } from '@nuxtjs/composition-api'
Expand Down Expand Up @@ -141,7 +141,7 @@ export const ssrRef = <T>(value: T | (() => T), key?: string): Ref<T> => {
/**
* This helper creates a [`shallowRef`](https://vue-composition-api-rfc.netlify.app/api.html#shallowref) (a ref that tracks its own .value mutation but doesn't make its value reactive) that is synced between client & server.
* @param value This can be an initial value or a factory function that will be executed on server-side to get the initial value.
* @param key Under the hood, `shallowSsrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @param key Under the hood, `shallowSsrRef` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @example
```ts
Expand Down Expand Up @@ -188,7 +188,7 @@ export const shallowSsrRef = <T>(
/**
* `ssrPromise` runs a promise on the server and serialises the result as a resolved promise for the client. It needs to be run within the `setup()` function but note that it returns a promise which will require special handling. (For example, you cannot just return a promise from setup and use it in the template.)
* @param value This can be an initial value or a factory function that will be executed on server-side to get the initial value.
* @param key Under the hood, `ssrPromise` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @param key Under the hood, `ssrPromise` requires a key to ensure that the ref values match between client and server. If you have added `@nuxtjs/composition-api/module` to your `buildModules`, this will be done automagically by an injected Babel plugin. If you need to do things differently, you can specify a key manually or add `@nuxtjs/composition-api/babel` to your Babel plugins.
* @example
```ts
Expand Down
14 changes: 7 additions & 7 deletions src/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
const routerBase = withTrailingSlash(nuxtOptions.router.base)
const publicPath = withTrailingSlash(nuxtOptions.build.publicPath)

const entryFile = addResolvedTemplate.call(this, 'entrypoint', {
const entryFile = addResolvedTemplate.call(this, 'lib/entrypoint.es', {
// useFetch
isFullStatic: isFullStatic(nuxtOptions),
// useStatic
Expand All @@ -59,7 +59,7 @@ const compositionApiModule: Module<never> = function compositionApiModule() {

// Register the Vue Composition API before any other layouts

this.addLayout(resolveRelativePath('./templates/layout'), '0')
this.addLayout(resolveRelativePath('lib/templates/layout.js'), '0')

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

Expand All @@ -80,12 +80,12 @@ const compositionApiModule: Module<never> = function compositionApiModule() {

// Add appropriate corejs polyfill for IE support

addResolvedTemplate.call(this, 'templates/polyfill.client.js', {
addResolvedTemplate.call(this, 'lib/templates/polyfill.client.js', {
corejsPolyfill: resolveCoreJsVersion.call(this),
})

// Plugin to allow running onGlobalSetup
const globalPlugin = addResolvedTemplate.call(this, 'templates/plugin.js')
const globalPlugin = addResolvedTemplate.call(this, 'lib/templates/plugin.js')

this.nuxt.hook('modules:done', () => {
nuxtOptions.plugins.unshift(globalPlugin)
Expand All @@ -99,7 +99,7 @@ const compositionApiModule: Module<never> = function compositionApiModule() {
!nuxtOptions.modules.includes('@nuxtjs/pwa')
) {
nuxtOptions.plugins.push(
addResolvedTemplate.call(this, 'templates/meta.js')
addResolvedTemplate.call(this, 'lib/templates/meta.js')
)
} else if (nuxtOptions.dev) {
console.warn(
Expand All @@ -120,10 +120,10 @@ compositionApiModule.meta = {

const warnToAddModule = () => {
console.error(
'You need to add `@nuxtjs/composition-api` to your buildModules in order to use it. See https://composition-api.nuxtjs.org/getting-started/setup.'
'You need to add `@nuxtjs/composition-api/module` to your buildModules in order to use it. See https://composition-api.nuxtjs.org/getting-started/setup.'
)
throw new Error(
'You need to add `@nuxtjs/composition-api` to your buildModules in order to use it. See https://composition-api.nuxtjs.org/getting-started/setup.'
'You need to add `@nuxtjs/composition-api/module` to your buildModules in order to use it. See https://composition-api.nuxtjs.org/getting-started/setup.'
)
}

Expand Down
10 changes: 1 addition & 9 deletions src/module/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ export function isUrl(url: string) {
}

export function resolveRelativePath(id: string) {
let src

try {
src = require.resolve(`@nuxtjs/composition-api/${id}`)
} catch {
src = require.resolve(join(__dirname, `./${id}`))
}

return src
return require.resolve(`@nuxtjs/composition-api/${id}`)
}

export function addResolvedTemplate(
Expand Down
6 changes: 5 additions & 1 deletion test/fixture/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export default <NuxtConfig>{
build: {
publicPath: isPublic ? 'fixture' : undefined,
},
buildModules: [process.env.NODE_ENV === 'test' ? require('../..') : rootDir],
buildModules: [
process.env.NODE_ENV === 'test'
? require('../../module')
: resolve(rootDir, './module.js'),
],
pwa: {
icon: false,
manifest: false,
Expand Down

0 comments on commit 5bb1a72

Please sign in to comment.