Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/nuxt next targets #2

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const generator = require('./generator');
const generator = require('./src/generator');
const path = require('path');
const emitter = require('./src/emitter');

module.exports = (nextConfig) => ({
...nextConfig,
Expand All @@ -10,17 +11,24 @@ module.exports = (nextConfig) => ({
config: { sitemap = {} }
} = options;


if (isServer) {
console.log('[Sitemap Generator]: Generating Sitemap');

if (typeof sitemap.publicPath === 'undefined') {
sitemap.publicPath = path.join(options.dir, 'public');
}

generator(sitemap);
sitemap.emitter = emitter.init({
fileName: sitemap.fileName,
})

let sitemapData = generator(sitemap);

return config;
try {
sitemap.emitter.emit(sitemapData)(sitemap.publicPath);
} catch (error) {
console.error("[Sitemap Generator] Unable to write sitemap to storage. Build will continue")
}
}

if (typeof nextConfig.webpack === 'function') {
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"keywords": [
"javascript",
"nextjs",
"nuxtjs",
"prismic.io",
"sitemap",
"sitemap generator"
Expand All @@ -13,6 +14,11 @@
"url": "https://github.com/reecem/prismic-sitemap"
},
"main": "index.js",
"exports": {
".": "./index.js",
"./next": "./src/next",
"./nuxt": "./src/nuxt"
},
"dependencies": {},
"devDependencies": {
"cross-spawn": "^7.0.3",
Expand Down
65 changes: 65 additions & 0 deletions src/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const path = require("path");
const fs = require("fs");

let emitter = {
data: Buffer.from('<error>Empty Sitemap</error>', 'utf-8'),
fileName: 'sitemap.xml',

init({
fileName, emitter = 'file',
}) {
this.fileName = fileName;
this.emitter = emitter;
return this;
},

fileWriter(publicPath) {
let basePath = resolvePublicPath(publicPath);

if (!fs.existsSync(basePath)) {
fs.mkdirSync(basePath, { recursive: true });
}

return fs.writeFileSync(
path.join(basePath, this.fileName),
this.data,
"utf-8"
);
},

webpackEmitter(compilation, cb) {
// This will generate `.nuxt/dist/filename.ext' with contents of source variable.

compilation.assets[this.fileName] = {
source: () => this.data,
size: () => this.data.length
}

cb()
},

emit(data) {
this.data = data;

switch (this.emitter) {
case 'file':
return this.fileWriter.bind(this)
case 'nuxt':
return this.webpackEmitter.bind(this)

default:
console.error(`Emitter [${this.emitter}] is not supported`)
return null;
}
}
}

function resolvePublicPath(dir) {
if (dir === 'public') {
dir = path.join(__dirname, dir);
}

return dir;
}

module.exports = emitter;
24 changes: 1 addition & 23 deletions generator.js → src/generator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require("path");
const fs = require("fs");
const Prismic = require("prismic-javascript");
const { SitemapStream, streamToPromise } = require('sitemap');

Expand All @@ -21,8 +19,6 @@ const generator = async (sitemap) => {
hostname = '',
optionsMapPerDocumentType = {},
documentTypes = [],
fileName = 'sitemap.xml',
publicPath = 'public',
sitemapConfig
} = sitemap;

Expand Down Expand Up @@ -56,25 +52,7 @@ const generator = async (sitemap) => {

sitemapStream.end();

const sitemapData = await streamToPromise(sitemapStream);

let basePath = resolvePublicPath(publicPath)

if (!fs.existsSync(path.join(basePath))) {
fs.mkdirSync(path.join(basePath), { recursive: true });
}

fs.writeFileSync(path.join(basePath, fileName), sitemapData, "utf-8");

return sitemapData;
}

function resolvePublicPath(dir) {
if (dir === 'public') {
dir = path.join(__dirname, dir);
}

return dir;
return await streamToPromise(sitemapStream);
}

module.exports = generator;
34 changes: 34 additions & 0 deletions src/nuxt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import generator from '../generator'
import emitter from '../emitter'

/**
* The main Nuxt module for generating the site map
*
* @author ReeceM
* @since 0.2.0
* @param {*} moduleOptions
*/

export default async function PrismicSitemap(moduleOptions) {
const options = Object.assign({}, this.options.sitemap, moduleOptions)

this.nuxt.hook('ready', async nuxt => {
console.log('Nuxt is ready')
})

emitter.init({
fileName: options.fileName,
emitter: 'nuxt',
});

let sitemapData = await generator(options);

this.options.build.plugins.push({
apply(compiler) {
compiler.plugin('emit', emitter.emit(sitemapData))
}
})
}

// REQUIRED if publishing the module as npm package
module.exports.meta = require('../../package.json')
58 changes: 58 additions & 0 deletions tests/emitter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Test File for the generator
*/
let fs = require('fs');
let path = require('path');

jest.mock('fs');
let generator = require('../src/generator');
let emitter = require('../src/emitter');

let _emitter = emitter.init({
fileName: 'sitemap.xml',
})
let sitemapData = null;

const sitemapConfig = {
linkResolver: doc => {
return doc.type == 'post'
? `blog/${doc.uid}`
: `page/${doc.uid}`;
},
apiEndpoint: process.env.TEST_REPOSITORY,
hostname: 'https://example.com',
documentTypes: ['post', 'page'],
optionsMapPerDocumentType: {
post: { changefreq: "weekly", priority: 0.8 },
},
};

describe('tests for generator.js', () => {
beforeAll(async () => {
// clear any previous calls
jest.restoreAllMocks();
sitemapData = await generator(sitemapConfig);
_emitter.emit(sitemapData)(path.join(__dirname, "public"));
});

it('should check if exists', async () => {

expect(jest.spyOn(fs, "existsSync"))
.toHaveBeenCalledWith(path.join(__dirname, "public"));
});

it('should have called 1 time', () => {

expect(jest.spyOn(fs, 'writeFileSync'))
.toHaveBeenCalledTimes(1);
});

it('file write should have been called with ...', () => {

expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith(
path.join(__dirname, "public/sitemap.xml"),
require('./buffer-example'),
"utf-8"
);
});
});
39 changes: 23 additions & 16 deletions tests/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let fs = require('fs');
let path = require('path');

jest.mock('fs');
let generator = require('../generator');
let generator = require('../src/generator');

const sitemapConfig = {
linkResolver: doc => {
Expand All @@ -29,26 +29,33 @@ describe('tests for generator.js', () => {
await generator(sitemapConfig);
});

it('should check if exists', async () => {
it('should return a sitemap with valid data', async () => {
let sitemapData = await generator(sitemapConfig);

expect(jest.spyOn(fs, "existsSync"))
.toHaveBeenCalledWith(path.join(__dirname, "../public"));
});
expect(sitemapData)
.toEqual(require('./buffer-example'))
})

it('should have called 1 time', () => {
// it('should check if exists', async () => {

expect(jest.spyOn(fs, 'writeFileSync'))
.toHaveBeenCalledTimes(1);
});
// expect(jest.spyOn(fs, "existsSync"))
// .toHaveBeenCalledWith(path.join(__dirname, "../public"));
// });

it('file write should have been called with ...', () => {
// it('should have called 1 time', () => {

expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith(
path.join(__dirname, "../public/sitemap.xml"),
require('./buffer-example'),
"utf-8"
);
});
// expect(jest.spyOn(fs, 'writeFileSync'))
// .toHaveBeenCalledTimes(1);
// });

// it('file write should have been called with ...', () => {

// expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith(
// path.join(__dirname, "../public/sitemap.xml"),
// require('./buffer-example'),
// "utf-8"
// );
// });
});

describe('generator errors', () => {
Expand Down