Skip to content

Commit

Permalink
fix: nuxt 2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dword-design committed Jun 15, 2024
1 parent e0c9b31 commit a8fc44e
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 26 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ Nuxt pages have a `meta` property that allows to define meta data. These can be

ℹ️ **Note that this module can only extract static data from the pages at build time. It will not work with dynamic data depending on `this`. In case you have an idea how to improve that, feel free to open up an issue or pull request.**

## Compatibility

| nuxt-route-meta | Nuxt |
|-----------------|------|
| <= 5 | 2 |
| >= 6 | 3 |

<!-- INSTALL/ -->
## Install

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"@babel/core": "^7.11.1",
"@babel/traverse": "^7.13.13",
"@dword-design/functions": "^6.0.0",
"@nuxt/kit": "^3.12.1",
"@vue/compiler-sfc": "^3.3.4",
"ast-to-literal": "^0.0.5",
"deepmerge": "^4.3.1",
Expand All @@ -60,6 +60,7 @@
"@babel/plugin-proposal-pipeline-operator": "^7.22.5",
"@dword-design/base": "^11.0.2",
"@dword-design/base-config-nuxt-module": "^1.0.0",
"@dword-design/functions": "^6.0.0",
"@dword-design/tester": "^2.0.19",
"@dword-design/tester-plugin-tmp-dir": "^2.1.26",
"execa": "^9.0.0",
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNuxt3 as isNuxt3Try } from '@nuxt/kit'
import { parse as parseVue } from '@vue/compiler-sfc'
import deepmerge from 'deepmerge'
import fs from 'fs-extra'
Expand All @@ -6,7 +7,15 @@ import P from 'path'
import parseBabel from './parse-babel.js'
import parseTypescript from './parse-typescript.js'

export default (options, nuxt) => {
export default function (options, nuxt) {
nuxt = nuxt || this
let isNuxt3 = true
try {
isNuxt3 = isNuxt3Try()
} catch {
isNuxt3 = false
}

const extractMeta = async filename => {
const fileContent = fs.readFileSync(filename, 'utf8')

Expand Down Expand Up @@ -45,5 +54,7 @@ export default (options, nuxt) => {
}
}),
)
nuxt.hook('pages:extend', parseRoutes)

const hookObject = isNuxt3 ? nuxt : nuxt.nuxt
hookObject.hook('pages:extend', parseRoutes)
}
53 changes: 51 additions & 2 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { endent } from '@dword-design/functions'
import tester from '@dword-design/tester'
import testerPluginTmpDir from '@dword-design/tester-plugin-tmp-dir'
import packageName from 'depcheck-package-name'
import { execaCommand } from 'execa'
import { execa, execaCommand } from 'execa'
import fs from 'fs-extra'
import outputFiles from 'output-files'
import P from 'path'

export default tester(
{
Expand Down Expand Up @@ -227,6 +229,38 @@ export default tester(
})
await execaCommand('nuxt build')
},
nuxt2: async () => {
await outputFiles({
'nuxt.config.js': endent`
import expect from '${packageName`expect`}'
export default {
modules: [
'~/../src/index.js',
function () {
this.nuxt.hook('pages.extend', routes => expect(routes[0].meta).toEqual({ foo: false }));
},
],
}
`,
'pages/index.vue': endent`
<template>
<div />
</template>
<script>
export default {
foo: true,
}
</script>
`,
})
await fs.symlink(
P.join('..', 'node_modules', '.cache', 'nuxt2', 'node_modules'),
'node_modules',
)
await execa(P.join('node_modules', '.bin', 'nuxt'), ['build'])
},
'predefined properties': async () => {
await outputFiles({
'nuxt.config.js': endent`
Expand Down Expand Up @@ -475,5 +509,20 @@ export default tester(
await execaCommand('nuxt build')
},
},
[testerPluginTmpDir()],
[
testerPluginTmpDir(),
{
before: async () => {
console.log('Installing Nuxt 2 …')
await fs.outputFile(
P.join('node_modules', '.cache', 'nuxt2', 'package.json'),
JSON.stringify({}),
)
await execaCommand('yarn add nuxt@^2', {
cwd: P.join('node_modules', '.cache', 'nuxt2'),
stdio: 'inherit',
})
},
},
],
)
6 changes: 2 additions & 4 deletions src/parse-typescript.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { property } from '@dword-design/functions'
import deepmerge from 'deepmerge'
import packageName from 'depcheck-package-name'
import { keys, omit } from 'lodash-es'

import predefinedProperties from './predefined-properties.js'

export default async script => {
const ts = import('typescript') |> await |> property('default')
const ts = (await import('typescript')).default

const tsAstToLiteral =
import(packageName`ts-ast-to-literal`) |> await |> property('default')
const tsAstToLiteral = (await import(packageName`ts-ast-to-literal`)).default

const rootNode = ts.createSourceFile(
'x.ts',
Expand Down
67 changes: 57 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2424,6 +2424,32 @@
unimport "^3.7.1"
untyped "^1.4.2"

"@nuxt/kit@^3.12.1":
version "3.12.1"
resolved "https://registry.yarnpkg.com/@nuxt/kit/-/kit-3.12.1.tgz#cdd083850da676cf90ff1629f253ed00198e5449"
integrity sha512-PHONuNCMqi3FYp0abgkhF3iH1j6CznJLMLpa8qxDGH532ALDcR1ThxbxytTA3fPiYulG2KenK8jloDfNdXOfCA==
dependencies:
"@nuxt/schema" "3.12.1"
c12 "^1.10.0"
consola "^3.2.3"
defu "^6.1.4"
destr "^2.0.3"
globby "^14.0.1"
hash-sum "^2.0.0"
ignore "^5.3.1"
jiti "^1.21.6"
klona "^2.0.6"
knitwork "^1.1.0"
mlly "^1.7.1"
pathe "^1.1.2"
pkg-types "^1.1.1"
scule "^1.3.0"
semver "^7.6.2"
ufo "^1.5.3"
unctx "^2.3.1"
unimport "^3.7.2"
untyped "^1.4.2"

"@nuxt/[email protected]", "@nuxt/schema@^3.11.2":
version "3.11.2"
resolved "https://registry.yarnpkg.com/@nuxt/schema/-/schema-3.11.2.tgz#530c7b4efd24c24523d8fd2d83dd66f44474d434"
Expand All @@ -2441,6 +2467,24 @@
unimport "^3.7.1"
untyped "^1.4.2"

"@nuxt/[email protected]":
version "3.12.1"
resolved "https://registry.yarnpkg.com/@nuxt/schema/-/schema-3.12.1.tgz#27a05e9413771465e62600c9cc69495375285fa4"
integrity sha512-yPgZVczd0vKhG73E7N61+EZHZTjtCvh2LKVhvT7c69zLBnPPqJNK1oJfqsKUCOOHSm1o1rTG8Xaibp91q2I49w==
dependencies:
compatx "^0.1.3"
consola "^3.2.3"
defu "^6.1.4"
hookable "^5.5.3"
pathe "^1.1.2"
pkg-types "^1.1.1"
scule "^1.3.0"
std-env "^3.7.0"
ufo "^1.5.3"
uncrypto "^0.1.3"
unimport "^3.7.2"
untyped "^1.4.2"

"@nuxt/telemetry@^2.5.3":
version "2.5.4"
resolved "https://registry.yarnpkg.com/@nuxt/telemetry/-/telemetry-2.5.4.tgz#a395be090cef23de14204cd7b4e1a9faf81e1838"
Expand Down Expand Up @@ -5235,6 +5279,11 @@ compare-func@^2.0.0:
array-ify "^1.0.0"
dot-prop "^5.1.0"

compatx@^0.1.3:
version "0.1.8"
resolved "https://registry.yarnpkg.com/compatx/-/compatx-0.1.8.tgz#af6f61910ade6ce1073c0fdff23c786bcd75c026"
integrity sha512-jcbsEAR81Bt5s1qOFymBufmCbXCXbk0Ql+K5ouj6gCyx2yHlu6AgmGIi9HxfKixpUDO5bCFJUHQ5uM6ecbTebw==

component-emitter@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.1.tgz#ef1d5796f7d93f135ee6fb684340b26403c97d17"
Expand Down Expand Up @@ -9882,6 +9931,11 @@ jiti@^1.16.0, jiti@^1.18.2, jiti@^1.19.1, jiti@^1.21.0:
resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.21.3.tgz#b2adb07489d7629b344d59082bbedb8c21c5f755"
integrity sha512-uy2bNX5zQ+tESe+TiC7ilGRz8AtRGmnJH55NC5S0nSUjvvvM2hJHmefHErugGXN4pNv4Qx7vLsnNw9qJ9mtIsw==

jiti@^1.21.6:
version "1.21.6"
resolved "https://registry.yarnpkg.com/jiti/-/jiti-1.21.6.tgz#6c7f7398dd4b3142767f9a168af2f317a428d268"
integrity sha512-2yTgeWTWzMWkHu6Jp9NKgePDaYHbntiwvYuuJLbbN9vl7DC9DvXKOB2BC3ZZ92D3cvV/aflH0osDfwpHepQ53w==

js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down Expand Up @@ -10336,14 +10390,7 @@ load-json-file@^4.0.0:
pify "^3.0.0"
strip-bom "^3.0.0"

load-pkg@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/load-pkg/-/load-pkg-4.0.0.tgz#f80a29aec2f6d02aadde714099fda4eea1b611f1"
integrity sha512-xS6uYdbUzHbwO5bExhtM8RsU6Z/fVEfZE4uZpWQOwVygLSUfXz7J97nGzJfZ+FFCZc0Weabn/wA3y1k9Q7Y4mw==
dependencies:
find-pkg "^2.0.0"

"load-pkg@npm:@dword-design/load-pkg":
load-pkg@^4.0.0, "load-pkg@npm:@dword-design/load-pkg":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@dword-design/load-pkg/-/load-pkg-4.0.0.tgz#201f76adeb44c64ed22d789591ab740573fe0a5b"
integrity sha512-lZxKRusgIqSg2y4jLzZzMPWyhrGPGke3YYmAxj4SoJeUGV4CmvYyhJkJnfA7Yaov7GMXZi64nlhx8D809VDazg==
Expand Down Expand Up @@ -10979,7 +11026,7 @@ mkdirp@^1.0.3:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mlly@^1.3.0, mlly@^1.4.2, mlly@^1.6.1, mlly@^1.7.0:
mlly@^1.3.0, mlly@^1.4.2, mlly@^1.6.1, mlly@^1.7.0, mlly@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/mlly/-/mlly-1.7.1.tgz#e0336429bb0731b6a8e887b438cbdae522c8f32f"
integrity sha512-rrVRZRELyQzrIUAVMHxP97kv+G786pHmOKzuFII8zDYahFBS7qnHh2AlYSl1GAHhaMPCz6/oHjVMcfFYgFYHgA==
Expand Down Expand Up @@ -14951,7 +14998,7 @@ unicorn-magic@^0.1.0:
resolved "https://registry.yarnpkg.com/unicorn-magic/-/unicorn-magic-0.1.0.tgz#1bb9a51c823aaf9d73a8bfcd3d1a23dde94b0ce4"
integrity sha512-lRfVq8fE8gz6QMBuDM6a+LO3IAzTi05H6gCVaUpir2E1Rwpo4ZUog45KpNXKC/Mn3Yb9UDuHumeFTo9iV/D9FQ==

unimport@^3.7.1:
unimport@^3.7.1, unimport@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/unimport/-/unimport-3.7.2.tgz#36fead8bdeb2695b6026861318bf111ad0596a5b"
integrity sha512-91mxcZTadgXyj3lFWmrGT8GyoRHWuE5fqPOjg5RVtF6vj+OfM5G6WCzXjuYtSgELE5ggB34RY4oiCSEP8I3AHw==
Expand Down

0 comments on commit a8fc44e

Please sign in to comment.