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

[feat] include gasket-nextjs-plugin in monorepo #12

Merged
merged 5 commits into from
Jul 18, 2019
Merged
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
3 changes: 3 additions & 0 deletions packages/gasket-nextjs-plugin/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "godaddy"
}
1 change: 1 addition & 0 deletions packages/gasket-nextjs-plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json binary
69 changes: 69 additions & 0 deletions packages/gasket-nextjs-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Editor files
.idea
*.iml

### ▲ .gitignore contents above
### ▼ additional ignore files for publishing

test/*
.eslintrc
.gitattributes
11 changes: 11 additions & 0 deletions packages/gasket-nextjs-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# CHANGELOG

### 1.1.1

- Fix for missing dependency during create command

### 1.1.0

- Renaming hooks and removing the `webpack` hook
- Separate next plugins from core
- Initial release.
21 changes: 21 additions & 0 deletions packages/gasket-nextjs-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 GoDaddy Operating Company, LLC.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
129 changes: 129 additions & 0 deletions packages/gasket-nextjs-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# @gasket/nextjs-plugin

The `nextjs-plugin` adds `next` to your application.

## Installation

```
npm install --save @gasket/nextjs-plugin
```

## Configuration

The nextjs plugin is configured using the `gasket.config.js` file.

- First, add it to the `plugins` section of your `gasket.config.js`:

```js
{
"plugins": [
"add": ["nextjs"]
]
}
```

- Instead of adding a dedicated `next.config.js`, the `next` property within `gasket.config.js` is used. Everything you can configure in the [next.config][next.config] can be added here.

#### Example configuration

```js
module.exports = {
plugins: {
add: ['nextjs']
},
next: {
poweredByHeader: false,
useFileSystemPublicRoutes: false,
generateBuildId: () => Date.now()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: could be just Date.now

}
}
```

## Hooks

`nextjs` hooks into the following lifecycles in order to work.

#### nextCreate

```js
module.exports = {
name: 'nextjs',
hooks: {
/**
* Creates the next app
* @param {Gasket} gasket The Gasket API
* @param {Servers} devServer true or false
* @return {Promise<Object>} next app
*/
'nextCreate': async function createNext(gasket, devServer) {
return nextApp;
}
}
};
```

#### nextBuild

```js
module.exports = {
name: 'nextjs',
hooks: {
/**
* Builds next app
* @param {Gasket} gasket The Gasket API
* @return {Promise<Object>} next build
*/
'nextBuild': async function createBuild(gasket) {
return build;
}
}
};
```

## Lifecycles

#### next

Executed when the `next` server has been created. It will receive a reference to
the created `next` instance.

```js
module.exports = {
hooks: {
/**
* Modify the Next app instance
* @param {Gasket} gasket The Gasket API
* @param {Next} next Next app instance
*/
next: function next(gasket, next) {
next.setAssetPrefix('https://my.cdn.com/dir/');
}
}
}
```

#### nextConfig

Executed before the `next` server has been created. It will receive a reference to the `next` config.
This will allow you to modify the `next` config before the `next` server is created.

```js
module.exports = {
hooks: {
/**
* Modify the Next config
* @param {Gasket} gasket The Gasket API
* @param {Object} config Next config
* @returns {Object} config
*/
nextConfig(gasket, config) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you did with the hooks, add jsdoc to this lifecycle and the one above so it's clear what the extra parameters are.

return {
...config,
modification: 'newValue'
};
}
}
}
```

[next.config]: https://nextjs.org/docs#custom-configuration
34 changes: 34 additions & 0 deletions packages/gasket-nextjs-plugin/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { initWebpack } = require('@gasket/webpack-plugin');

/**
* Small helper function that creates nextjs configuration from the gasket
* configuration.
*
* @param {Gasket} gasket The gasket API.
* @param {Boolean} includeWebpackConfig `true` to generate webpack config
* @returns {Promise<Object>} The configuration data for Nextjs
* @private
*/
function createConfig(gasket, includeWebpackConfig = true) {
const { config } = gasket;

let nextConfig;
if (!includeWebpackConfig) {
nextConfig = config.next || {};
} else {
nextConfig = {
...config.next,
webpack: (webpackConfig, data) => {
return initWebpack(gasket, webpackConfig, data);
}
};
}

nextConfig.poweredByHeader = false;

return gasket.execWaterfall('nextConfig', nextConfig);
}

module.exports = {
createConfig
};
44 changes: 44 additions & 0 deletions packages/gasket-nextjs-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { resolve } = require('path');
const { createConfig } = require('./config');

module.exports = {
dependencies: ['webpack'],
name: 'nextjs',
hooks: {
nextCreate: async function createNext(gasket, devServer) {
const { exec } = gasket;
const nextApp = require('next');

const app = nextApp({
dev: devServer,
conf: await createConfig(gasket, devServer)
});

//
// We need to call the `next` lifecycle before we prepare the application
// as the prepare step initializes all the routes that a next app can have.
// If we wait later, it's possible that our added routes/pages are not
// recognized.
//
await exec('next', app);
await app.prepare();

return app;
},
nextBuild: async function createBuild(gasket) {
//
// Different versions of Nextjs, have different ways of exporting the builder.
// In order to support canary, and other versions of next we need to detect
// the different locations.
//
let builder;
try {
builder = require('next/dist/server/build').default;
} catch (e) {
builder = require('next/dist/build').default;
}

return await builder(resolve('.'), await createConfig(gasket, true));
}
}
};
51 changes: 51 additions & 0 deletions packages/gasket-nextjs-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@gasket/nextjs-plugin",
"version": "1.1.0",
"description": "Adds Next support to your application",
"author": "GoDaddy Operating Company, LLC",
"homepage": "https://github.com/godaddy/gasket/tree/master/packages/gasket-nextjs-plugin",
"license": "MIT",
"main": "index.js",
"scripts": {
"lint": "eslint './**/*.js' 'test/**/*.js'",
"lint:fix": "npm run lint -- --fix",
"report": "nyc report --reporter=lcov",
"pretest": "npm run lint",
"test": "nyc --reporter=text --reporter=json-summary npm run test:runner",
"test:runner": "mocha --require ./setup.js 'test/**/*.test.js'"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/godaddy/gasket"
},
"keywords": [
"next",
"nextjs",
"gasket",
"plugin"
],
"dependencies": {
"@gasket/webpack-plugin": "^1.0.1"
},
"peerDependencies": {
"next": "^7.0.0 || ^8.0.0",
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"devDependencies": {
"@gasket/plugin-engine": "^1.0.0",
"assume": "^2.1.0",
"assume-sinon": "^1.0.0",
"eslint": "^5.13.0",
"eslint-config-godaddy": "^3.0.0",
"eslint-plugin-json": "^1.3.2",
"eslint-plugin-mocha": "^5.2.1",
"mocha": "^5.2.0",
"next": "^8.1.0",
"nyc": "^13.1.0",
"proxyquire": "^2.1.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"sinon": "^7.2.3"
}
}
4 changes: 4 additions & 0 deletions packages/gasket-nextjs-plugin/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const assumeSinonPlugin = require('assume-sinon');
const assume = require('assume');

assume.use(assumeSinonPlugin);
Loading