Skip to content

Commit 05f0e9d

Browse files
chore: init
0 parents  commit 05f0e9d

18 files changed

+10861
-0
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_size = 2
6+
indent_style = space
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false

.eslintignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Common
2+
node_modules
3+
dist
4+
.nuxt
5+
coverage
6+
7+
# Plugin
8+
lib/plugin.js

.eslintrc.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
parser: 'babel-eslint',
5+
sourceType: 'module'
6+
},
7+
extends: [
8+
'@nuxtjs'
9+
]
10+
}

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
node_modules
2+
*.iml
3+
.idea
4+
*.log*
5+
.nuxt
6+
.vscode
7+
.DS_Store
8+
coverage
9+
dist

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Alexander Lichter <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# @nuxtjs/netlify-files-module
2+
3+
[![npm version][npm-version-src]][npm-version-href]
4+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
5+
[![Circle CI][circle-ci-src]][circle-ci-href]
6+
[![Codecov][codecov-src]][codecov-href]
7+
[![License][license-src]][license-href]
8+
9+
> Nuxt module to create new _headers and _redirects files for Netlify or to use existing ones
10+
11+
[📖 **Release Notes**](./CHANGELOG.md)
12+
13+
## Setup
14+
15+
1. Add `@nuxtjs/netlify-files-module` dependency to your project
16+
17+
```bash
18+
yarn add @nuxtjs/netlify-files-module # or npm install @nuxtjs/netlify-files-module
19+
```
20+
21+
2. Add `@nuxtjs/netlify-files-module` to the `modules` section of `nuxt.config.js`
22+
23+
```js
24+
{
25+
modules: [
26+
// Simple usage
27+
'@nuxtjs/netlify-files-module',
28+
29+
// With options
30+
['@nuxtjs/netlify-files-module', { /* module options */ }]
31+
]
32+
}
33+
```
34+
35+
## Development
36+
37+
1. Clone this repository
38+
2. Install dependencies using `yarn install` or `npm install`
39+
3. Start development server using `npm run dev`
40+
41+
## License
42+
43+
[MIT License](./LICENSE)
44+
45+
Copyright (c) Alexander Lichter <[email protected]>
46+
47+
<!-- Badges -->
48+
[npm-version-src]: https://img.shields.io/npm/v/@nuxtjs/netlify-files-module/latest.svg?style=flat-square
49+
[npm-version-href]: https://npmjs.com/package/@nuxtjs/netlify-files-module
50+
51+
[npm-downloads-src]: https://img.shields.io/npm/dt/@nuxtjs/netlify-files-module.svg?style=flat-square
52+
[npm-downloads-href]: https://npmjs.com/package/@nuxtjs/netlify-files-module
53+
54+
[circle-ci-src]: https://img.shields.io/circleci/project/github/nuxt-community/netlify-files-module.svg?style=flat-square
55+
[circle-ci-href]: https://circleci.com/gh/nuxt-community/netlify-files-module
56+
57+
[codecov-src]: https://img.shields.io/codecov/c/github/nuxt-community/netlify-files-module.svg?style=flat-square
58+
[codecov-href]: https://codecov.io/gh/nuxt-community/netlify-files-module
59+
60+
[license-src]: https://img.shields.io/npm/l/@nuxtjs/netlify-files-module.svg?style=flat-square
61+
[license-href]: https://npmjs.com/package/@nuxtjs/netlify-files-module

babel.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env', {
5+
targets: {
6+
esmodules: true
7+
}
8+
}
9+
]
10+
]
11+
}

commitlint.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
extends: [
3+
'@commitlint/config-conventional'
4+
]
5+
}

example/_headers

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foobar

example/_redirects

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
barfoo

example/nuxt.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { resolve } = require('path')
2+
3+
module.exports = {
4+
rootDir: __dirname,
5+
buildDir: resolve(__dirname, '.nuxt'),
6+
srcDir: __dirname,
7+
render: {
8+
resourceHints: false
9+
},
10+
modules: [
11+
{ handler: require('../') }
12+
]
13+
}

husky.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
hooks: {
3+
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
4+
'pre-commit': 'yarn lint',
5+
'pre-push': 'yarn lint'
6+
}
7+
}

jest.config.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
collectCoverage: true,
4+
collectCoverageFrom: [
5+
'lib/**/*.js',
6+
'!lib/plugin.js'
7+
],
8+
moduleNameMapper: {
9+
'^~/(.*)$': '<rootDir>/lib/$1',
10+
'^~~$': '<rootDir>',
11+
'^@@$': '<rootDir>',
12+
'^@/(.*)$': '<rootDir>/lib/$1'
13+
},
14+
transform: {
15+
'^.+\\.js$': 'babel-jest'
16+
}
17+
}

lib/module.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { resolve } = require('path')
2+
const fs = require('fs-extra')
3+
const consola = require('consola')
4+
const logger = consola.withScope('nuxt-netlify-files')
5+
6+
const HEADERS_FILE_NAME = '_headers'
7+
const REDIRECTS_FILE_NAME = '_redirects'
8+
const FILE_NAMES = [HEADERS_FILE_NAME, REDIRECTS_FILE_NAME]
9+
10+
module.exports = function (moduleOptions) {
11+
logger.log('hi')
12+
const { srcDir, build } = this.options
13+
const DEFAULT_OPTIONS = {
14+
copyExistingFiles: true,
15+
existingFilesDirectory: srcDir,
16+
publicPath: build.publicPath
17+
}
18+
const options = {
19+
...DEFAULT_OPTIONS,
20+
...this.options.netlifyFiles,
21+
...moduleOptions
22+
}
23+
24+
if (options.copyExistingFiles) {
25+
copyExistingNetlifyFiles.bind(this)(options)
26+
}
27+
}
28+
29+
function copyExistingNetlifyFiles ({ existingFilesDirectory }) {
30+
this.nuxt.hook('generate:done', async () => {
31+
await Promise.all(FILE_NAMES.map(async (name) => {
32+
const origin = resolve(existingFilesDirectory, name)
33+
const isAvailable = await fs.pathExists(origin)
34+
if (!isAvailable) {
35+
return
36+
}
37+
const destination = resolve(this.options.rootDir, this.options.generate.dir, name)
38+
return fs.copy(origin, destination)
39+
}))
40+
})
41+
}
42+
43+
module.exports.meta = require('../package.json')

package.json

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@nuxtjs/netlify-files-module",
3+
"version": "0.0.0",
4+
"description": "Nuxt module to create new _headers and _redirects files for Netlify or to use existing ones",
5+
"license": "MIT",
6+
"contributors": [
7+
{
8+
"name": "Alexander Lichter <[email protected]>"
9+
}
10+
],
11+
"main": "lib/module.js",
12+
"repository": "nuxt-community/netlify-files-module",
13+
"publishConfig": {
14+
"access": "public"
15+
},
16+
"scripts": {
17+
"dev": "nuxt example",
18+
"lint": "eslint --ext .js,.vue example lib test",
19+
"test": "yarn lint && jest",
20+
"release": "yarn test && standard-version && git push --follow-tags && npm publish"
21+
},
22+
"files": [
23+
"lib"
24+
],
25+
"dependencies": {
26+
"fs-extra": "^8.1.0"
27+
},
28+
"devDependencies": {
29+
"@babel/core": "latest",
30+
"@babel/preset-env": "latest",
31+
"@commitlint/cli": "latest",
32+
"@commitlint/config-conventional": "latest",
33+
"@nuxtjs/eslint-config": "latest",
34+
"babel-eslint": "latest",
35+
"babel-jest": "latest",
36+
"codecov": "latest",
37+
"eslint": "latest",
38+
"get-port": "latest",
39+
"husky": "latest",
40+
"jest": "latest",
41+
"nuxt-edge": "latest",
42+
"request": "latest",
43+
"request-promise-native": "latest",
44+
"standard-version": "latest"
45+
}
46+
}

renovate.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"@nuxtjs"
4+
]
5+
}

test/module.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
jest.setTimeout(60000)
2+
3+
const { resolve, join } = require('path')
4+
const { readFileSync } = require('fs')
5+
const { Nuxt, Builder, Generator } = require('nuxt-edge')
6+
const config = require('../example/nuxt.config')
7+
config.dev = false
8+
9+
let nuxt
10+
11+
describe('basic', () => {
12+
beforeAll(async () => {
13+
nuxt = new Nuxt(config)
14+
await nuxt.ready()
15+
const builder = await new Builder(nuxt)
16+
const generator = new Generator(nuxt, builder)
17+
await generator.generate()
18+
})
19+
20+
afterAll(async () => {
21+
await nuxt.close()
22+
})
23+
24+
test('it copies netlify files file', () => {
25+
const distBasePath = resolve(nuxt.options.rootDir, join(nuxt.options.generate.dir))
26+
const headersPath = join(distBasePath, '_headers')
27+
const redirectsPath = join(distBasePath, '_redirects')
28+
29+
expect(readFileSync(headersPath, { encoding: 'utf8' })).toMatchSnapshot()
30+
expect(readFileSync(redirectsPath, { encoding: 'utf8' })).toMatchSnapshot()
31+
})
32+
})

0 commit comments

Comments
 (0)