Skip to content

Commit

Permalink
Merge pull request #1921 from pi0/feat/nuxt-components
Browse files Browse the repository at this point in the history
feat: detect tags from @nuxt/components
  • Loading branch information
octref authored Jun 9, 2020
2 parents c9b1a9d + fef5898 commit 5f9974f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 39 deletions.
8 changes: 1 addition & 7 deletions server/src/modes/template/tagProviders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,7 @@ export function getTagProviderSettings(workspacePath: string | null | undefined)
// and enable Quasar later below in the for()
dependencies['quasar-framework'] = '^0.0.17';
}
if (
dependencies['nuxt'] ||
dependencies['nuxt-legacy'] ||
dependencies['nuxt-edge'] ||
dependencies['nuxt-ts'] ||
dependencies['nuxt-ts-edge']
) {
if (dependencies['nuxt'] || dependencies['nuxt-edge'] || devDependencies['nuxt'] || devDependencies['nuxt-edge']) {
const nuxtTagProvider = getNuxtTagProvider(workspacePath);
if (nuxtTagProvider) {
settings['nuxt'] = true;
Expand Down
62 changes: 30 additions & 32 deletions server/src/modes/template/tagProviders/nuxtTags.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
import * as fs from 'fs';
import * as path from 'path';
import { join } from 'path';
import { getExternalTagProvider } from './externalTagProviders';

const NUXT_VUE_APP_PATH = 'node_modules/@nuxt/vue-app';
const NUXT_EDGE_VUE_APP_PATH = 'node_modules/@nuxt/vue-app-edge';
const NUXT_JSON_SOURCES = ['@nuxt/vue-app-edge', '@nuxt/vue-app', 'nuxt-helper-json'];

export function getNuxtTagProvider(workspacePath: string) {
if (fs.existsSync(path.resolve(workspacePath, NUXT_VUE_APP_PATH, 'package.json'))) {
const { nuxtTags, nuxtAttributes } = getNuxtTagsAndAttributes(NUXT_VUE_APP_PATH);
return getExternalTagProvider('nuxt', nuxtTags, nuxtAttributes);
let nuxtTags, nuxtAttributes;
for (const source of NUXT_JSON_SOURCES) {
if (tryResolve(join(source, 'package.json'), workspacePath)) {
nuxtTags = tryRequire(join(source, 'vetur/nuxt-tags.json'), workspacePath);
nuxtAttributes = tryRequire(join(source, 'vetur/nuxt-attributes.json'), workspacePath);
if (nuxtTags) {
break;
}
}
}

if (fs.existsSync(path.resolve(workspacePath, NUXT_EDGE_VUE_APP_PATH, 'package.json'))) {
const { nuxtTags, nuxtAttributes } = getNuxtTagsAndAttributes(NUXT_EDGE_VUE_APP_PATH);
return getExternalTagProvider('nuxt', nuxtTags, nuxtAttributes);
}
const componentsTags = tryRequire(join(workspacePath, '.nuxt/vetur/tags.json'), workspacePath);
const componentsAttributes = tryRequire(join(workspacePath, '.nuxt/vetur/attributes.json'), workspacePath);

return getExternalTagProvider(
'nuxt',
{ ...nuxtTags, ...componentsTags },
{ ...nuxtAttributes, ...componentsAttributes }
);
}

function getNuxtTagsAndAttributes(nuxtVueAppPath: string) {
let nuxtVer = '0.0.0';
function tryRequire(modulePath: string, workspacePath: string) {
try {
nuxtVer = require(path.resolve(nuxtVueAppPath, 'package.json')).version;
} catch (err) {}

if (nuxtVer < '2.4.0') {
const nuxtTags = require('nuxt-helper-json/nuxt-tags.json');
const nuxtAttributes = require('nuxt-helper-json/nuxt-attributes.json');

return {
nuxtTags,
nuxtAttributes
};
} else {
const nuxtTags = require(path.resolve(nuxtVueAppPath, 'vetur/nuxt-tags.json'));
const nuxtAttributes = require(path.resolve(nuxtVueAppPath, 'vetur/nuxt-attributes.json'));
const resolved = tryResolve(modulePath, workspacePath);
return resolved ? require(resolved) : undefined;
} catch (_err) {}
}

return {
nuxtTags,
nuxtAttributes
};
}
function tryResolve(modulePath: string, workspacePath: string) {
try {
return require.resolve(modulePath, {
paths: [workspacePath, __dirname]
});
} catch (_err) {}
}

0 comments on commit 5f9974f

Please sign in to comment.