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

Commit ddc9c0f

Browse files
danielroepi0
andauthored
refactor: significant packaging changes (#438)
BREAKING CHANGE: There is no longer a need to amend `moduleNameMapper` in your jest.config - you should remove any entry for `@nuxtjs/composition-api` there. BREAKING CHANGE: `defineNuxtConfig`, `defineNuxtModule` and `defineNuxtServerMiddleware` have been removed. BREAKING CHANGE: `@nuxtjs/composition-api/babel` has been renamed to `@nuxtjs/composition-api/dist/babel-plugin`. * This library is now located within `node_modules` rather than being templated, with a limited template for the config. * There is no longer a need to amend `moduleNameMapper` in your jest.config. When used outside a Nuxt context, the module will 'auto-mock' and use https://github.com/nuxt-community/composition-api/blob/be94d4f4e1321565864dd3d3d5e850f7cabf6ca9/src/globals.ts instead of live Nuxt configuration. * The ESM version of the library is now `.mjs` (but we polyfill support for this and other `.mjs` files in webpack 4). * `defineNuxtConfig`, `defineNuxtModule` and `defineNuxtServerMiddleware` have been removed from this library. You can create your own helper with the following code: ```ts import { Module, ServerMiddleware, NuxtConfig } from '@nuxt/types' export const defineNuxtModule = <T extends Record<string, unknown>>(module: Module<T>) => module export const defineNuxtServerMiddleware = (serverMiddleware: ServerMiddleware) => serverMiddleware export const defineNuxtConfig = (config: NuxtConfig) => config ``` Co-authored-by: Pooya Parsa <[email protected]>
1 parent 1594bc3 commit ddc9c0f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+356
-610
lines changed

babel.js

-2
This file was deleted.

docs/content/en/getting-started/setup.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Quick start
33
description: 'Getting started with the Nuxt Composition API.'
44
category: Getting started
55
position: 2
6-
version: 0.161
6+
version: 0.225
77
---
88

99
## Quick start
@@ -58,13 +58,3 @@ version: 0.161
5858
- For convenience, this package also exports the [`@vue/composition-api`](https://github.com/vuejs/composition-api) methods and hooks, so you can import directly from `@nuxtjs/composition-api`.
5959

6060
</alert>
61-
62-
## Testing with Jest
63-
64-
If you need to use jest tests with this module installed, just add the following lines to your `jest.config.js`:
65-
66-
```js{}[jest.config.js]
67-
moduleNameMapper: {
68-
'@nuxtjs/composition-api': '@nuxtjs/composition-api/entrypoint',
69-
},
70-
```

docs/content/en/helpers/ssrPromise.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default defineComponent({
3636

3737
<alert type="info">
3838

39-
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.
39+
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/dist/babel-plugin` to your Babel plugins.
4040

4141
</alert>
4242

docs/content/en/helpers/ssrRef.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const val2 = ssrRef(myExpensiveSetterFunction)
3030

3131
<alert type="info">
3232

33-
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.
33+
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/dist/babel-plugin` to your Babel plugins.
3434

3535
</alert>
3636

docs/content/en/typings/definitionHelpers.md

+1-39
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,13 @@ title: defineNuxt*
33
description: 'You can get automatic type-hinting for Nuxt configuration, plugins, middleware, modules and serverMiddleware.'
44
category: Typings
55
position: 10
6-
version: 0.201
6+
version: 0.225
77
---
88

99
There are some helpers to optimize developer experience when creating Nuxt plugins, middleware, server middleware and modules.
1010

1111
These helpers simply return the function or object passed into them, adding the correct typings.
1212

13-
## defineNuxtConfig
14-
15-
Create your `nuxt.config.js` with types with:
16-
17-
```ts
18-
import { defineNuxtConfig } from '@nuxtjs/composition-api'
19-
20-
export default defineNuxtConfig({
21-
// your nuxt config
22-
})
23-
```
24-
25-
<alert type="warning">Note that if you define your Nuxt config this way you will need to ensure that `@nuxtjs/composition-api` is present in your `dependencies` rather than `devDependencies`.</alert>
26-
2713
## defineNuxtPlugin
2814

2915
Create a plugin with types with:
@@ -47,27 +33,3 @@ export default defineNuxtMiddleware(ctx => {
4733
// do stuff
4834
})
4935
```
50-
51-
## defineNuxtModule
52-
53-
Create a Nuxt module with types with:
54-
55-
```ts
56-
import { defineNuxtModule } from '@nuxtjs/composition-api'
57-
58-
export default defineNuxtModule<{ myOption: boolean }>(moduleOptions => {
59-
// do stuff
60-
})
61-
```
62-
63-
## defineNuxtServerMiddleware
64-
65-
Create server middleware with types with:
66-
67-
```ts
68-
import { defineNuxtServerMiddleware } from '@nuxtjs/composition-api'
69-
70-
export default defineNuxtServerMiddleware((req, res, next) => {
71-
// do stuff
72-
})
73-
```

entrypoint.js

-2
This file was deleted.

jest.config.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
module.exports = {
2-
verbose: true,
32
testEnvironment: 'node',
4-
coveragePathIgnorePatterns: ['test', '.babelrc.js'],
3+
collectCoverage: true,
4+
verbose: true,
55
transform: {
66
'^.+\\.(js|ts)$': 'babel-jest',
77
},
88
setupFiles: ['<rootDir>/test/unit/setup'],
9+
moduleNameMapper: {
10+
'@nuxtjs/composition-api/dist/globals': '<rootDir>/src/globals',
11+
},
912
}

module.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// stub file for node versions without support for package exports
2-
module.exports = require('./lib/index')
1+
module.exports = require('./dist/module')

package.json

+20-19
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,28 @@
1818
"url": "https://roe.dev"
1919
},
2020
"exports": {
21-
"./babel": "./lib/babel.js",
22-
"./entrypoint": "./lib/entrypoint.js",
23-
"./module": "./lib/index.js",
24-
"./lib/entrypoint.es": "./lib/entrypoint.es.js",
25-
"./lib/templates/*": "./lib/templates/*",
26-
"./package.json": "./package.json"
21+
".": {
22+
"import": "./dist/index.mjs",
23+
"require": "./dist/index.js"
24+
},
25+
"./module": "./dist/module.js",
26+
"./package.json": "./package.json",
27+
"./dist/babel-plugin": "./dist/babel-plugin.js",
28+
"./dist/globals": "./dist/globals.js",
29+
"./dist/runtime/templates/*": "./dist/runtime/templates/*"
2730
},
28-
"types": "lib/entrypoint.d.ts",
31+
"main": "./dist/index.js",
32+
"module": "./dist/index.mjs",
33+
"types": "./dist/index.d.ts",
2934
"files": [
30-
"lib/**/*",
31-
"lib/index.d.ts",
32-
"babel.js",
33-
"entrypoint.js",
34-
"module.js",
35-
"!**/*.map"
35+
"dist",
36+
"module.js"
3637
],
3738
"scripts": {
38-
"build": "yarn clean && yarn compile",
39-
"clean": "rimraf lib/*",
40-
"clean:fixture": "rimraf dist/ test/fixture/.nuxt",
41-
"compile": "siroc build",
39+
"build": "siroc build",
40+
"clean:fixture": "rimraf test/fixture/dist/ test/fixture/.nuxt",
4241
"dev": "nuxt dev test/fixture",
43-
"fixture:generate": "yarn fixture:generate:export && yarn http-server -s -p 5000 dist",
42+
"fixture:generate": "yarn fixture:generate:export && yarn http-server -s -p 5000 test/fixture/dist",
4443
"fixture:generate:export": "yarn clean:fixture && cross-env GENERATE=true PORT=6000 CMD=generate yarn nuxt-run",
4544
"fixture:prod": "yarn clean:fixture && cross-env CMD=build yarn nuxt-run && cross-env CMD=start yarn nuxt-run",
4645
"lint": "run-s lint:all:*",
@@ -59,7 +58,7 @@
5958
"test:e2e-ssr": "cross-env PORT=4000 start-server-and-test fixture:prod 4000 \"testcafe firefox:headless test/e2e\"",
6059
"test:types": "tsd",
6160
"test:unit": "jest",
62-
"watch": "yarn compile -w"
61+
"watch": "yarn build -w"
6362
},
6463
"dependencies": {
6564
"@vue/composition-api": "1.0.0-rc.6",
@@ -73,7 +72,9 @@
7372
"@babel/plugin-transform-runtime": "^7.13.15",
7473
"@babel/preset-env": "^7.13.15",
7574
"@babel/preset-typescript": "^7.13.0",
75+
"@nuxt/test-utils": "^0.2.1",
7676
"@nuxt/types": "^2.15.4",
77+
"@nuxt/typescript-build": "^2.1.0",
7778
"@nuxtjs/module-test-utils": "^1.6.3",
7879
"@release-it/conventional-changelog": "^2.0.1",
7980
"@types/fs-extra": "^9.0.10",

siroc.config.js

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,7 @@
11
import { defineSirocConfig } from 'siroc'
2-
import replace from '@rollup/plugin-replace'
3-
import { resolve } from 'upath'
4-
5-
import jiti from 'jiti'
6-
7-
const getFile = jiti()
82

93
export default defineSirocConfig({
10-
hooks: {
11-
'build:extendRollup'(pkg, { rollupConfig }) {
12-
rollupConfig.forEach(config => {
13-
if (!config.plugins) return
14-
config.plugins.push(
15-
replace({
16-
preventAssignment: true,
17-
values: {
18-
__HELPER_FUNCTIONS__: JSON.stringify(
19-
Object.keys(getFile(resolve(__dirname, './src/entrypoint.ts')))
20-
),
21-
},
22-
})
23-
)
24-
})
25-
},
4+
rollup: {
5+
externals: ['@nuxtjs/composition-api/dist/globals'],
266
},
277
})

src/babel-plugin.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line
2+
module.exports = require('jiti')(__dirname)('./babel-plugin.ts')

src/babel.ts src/babel-plugin.ts

File renamed without changes.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export function registerBabelPlugin(this: ModuleThis) {
1414

1515
if (nuxtOptions.build.babel.plugins instanceof Function) {
1616
console.warn(
17-
'Unable to automatically add Babel plugin. Make sure your custom `build.babel.plugins` returns `@nuxtjs/composition-api/babel`'
17+
'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'))
20+
nuxtOptions.build.babel.plugins.push(resolveRelativePath('babel-plugin'))
2121
}
2222

2323
/**

src/composables/defineHelpers.ts

-17
This file was deleted.

src/composables/globals.ts

-11
This file was deleted.

src/entrypoint.ts

-2
This file was deleted.

src/globals-register.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { ModuleThis } from '@nuxt/types/config/module'
2+
import { withTrailingSlash } from 'ufo'
3+
4+
import { prepareUseStatic } from './static'
5+
import {
6+
addResolvedTemplate,
7+
getNuxtGlobals,
8+
isFullStatic,
9+
isUrl,
10+
} from './utils'
11+
12+
export function addGlobalsFile(this: ModuleThis) {
13+
const nuxtOptions = this.options
14+
15+
const { staticPath } = prepareUseStatic.call(this)
16+
const { globalContext, globalNuxt } = getNuxtGlobals.call(this)
17+
18+
const routerBase = withTrailingSlash(nuxtOptions.router.base)
19+
const publicPath = withTrailingSlash(nuxtOptions.build.publicPath)
20+
21+
const globals = {
22+
// useFetch
23+
isFullStatic: isFullStatic(nuxtOptions),
24+
// useStatic
25+
staticPath,
26+
publicPath: isUrl(publicPath) ? publicPath : routerBase,
27+
// Throughout
28+
globalContext,
29+
globalNuxt,
30+
}
31+
32+
const contents = Object.entries(globals)
33+
.map(([key, value]) => `export const ${key} = ${JSON.stringify(value)}`)
34+
.join('\n')
35+
36+
const globalsFile = addResolvedTemplate.call(this, 'globals.js', {
37+
contents,
38+
})
39+
40+
nuxtOptions.alias['@nuxtjs/composition-api/dist/globals'] = globalsFile
41+
}

src/globals.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This is a shim for runtime/templates/globals.js
2+
3+
export const globalNuxt = '$nuxt'
4+
5+
export const globalContext = '__NUXT__'
6+
7+
export const isFullStatic = false
8+
9+
export const staticPath = '/static-path'
10+
11+
export const publicPath = '/_nuxt/'

src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import compositionApiModule from './module'
2-
3-
export default compositionApiModule
1+
export * from './runtime/composables'

0 commit comments

Comments
 (0)