Skip to content
This repository was archived by the owner on Jul 15, 2022. It is now read-only.

feat: template improvements #74

Open
wants to merge 23 commits into
base: template
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5952733
chore: remove `yarn.lock` to ensure latest dependencies are installed
danielroe Mar 8, 2021
f0be496
refactor: various module template improvements
danielroe Mar 8, 2021
4e7d31a
refactor: use `src/templates` folder for compiled templates with live…
danielroe Mar 9, 2021
8030480
test: fix test suite
danielroe Mar 9, 2021
9be885b
ci: test on node 12/14
danielroe Mar 9, 2021
034d079
ci: add branch name
danielroe Mar 9, 2021
e9932ad
style: fix out-of-the-box lint errors
danielroe Mar 9, 2021
5302f94
ci: restore lockfile for cacheable runs and update scripts
danielroe Mar 9, 2021
5976480
test: fix race condition
danielroe Mar 9, 2021
ec4e0c9
test: update test for window support
danielroe Mar 9, 2021
dabc36a
ci: temporarily run tests against PR
danielroe Mar 9, 2021
9e0c159
test: normalise setup
danielroe Mar 9, 2021
8febc52
ci: cache playwright
danielroe Mar 9, 2021
5a97ec8
test: fix
danielroe Mar 9, 2021
bd3dc68
test: simplify
danielroe Mar 9, 2021
02dd68b
ci: use recommended approach for cache and test fewer targets
danielroe Mar 9, 2021
6f89ac1
test: collect coverage from templates
danielroe Mar 9, 2021
69ddb3d
test: remove src/templates from coverage
danielroe Mar 9, 2021
0659223
ci: add node_modules to cache folder
danielroe Mar 9, 2021
a95c652
ci: always install dependencies
danielroe Mar 9, 2021
b34a8dd
fix: insert typescript plugins in fixture
danielroe Mar 9, 2021
4f4408d
chore: remove explicit `typescript` dep
danielroe Mar 9, 2021
ea7efa3
ci: remove temporary branch target
danielroe Mar 9, 2021
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
23 changes: 14 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ name: ci
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

jobs:
ci:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node: [10, 12]
os: [ubuntu-latest]
node: [14]

steps:
- uses: actions/setup-node@v1
Expand All @@ -25,14 +25,19 @@ jobs:
- name: checkout
uses: actions/checkout@master

- name: cache node_modules
uses: actions/cache@v1
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v2
id: yarn-cache
with:
path: node_modules
key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles(format('{0}{1}', github.workspace, '/yarn.lock')) }}
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ matrix.os }}-node-v${{ matrix.node }}-deps-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ matrix.os }}-yarn-

- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn

- name: Lint
Expand Down
1 change: 1 addition & 0 deletions example/nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default {
modules: [
'@nuxt/typescript-build',
'../src/module.ts'
]
}
5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
}
]
},
moduleNameMapper: {
npm_package: '<rootDir>/src'
},
Comment on lines +11 to +13
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
moduleNameMapper: {
npm_package: '<rootDir>/src'
},

Looks like this causes issues with importing of other npm packages.

collectCoverage: true,
collectCoverageFrom: ['src/**', '!templates/**']
collectCoverageFrom: ['src/**']
}
27 changes: 18 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@
],
"repository": "github_repo",
"license": "MIT",
"main": "dist/module.js",
"types": "dist/module.d.ts",
"exports": {
".": {
"require": "./dist/module.js",
"import": "./dist/module.es.js"
},
"./templates/*": "./dist/templates/*",
"./package.json": "./package.json"
},
"main": "./dist/module.js",
"types": "./dist/module.d.ts",
"files": [
"dist",
"templates"
"dist"
],
"scripts": {
"build": "siroc build",
"dev": "nuxt-ts example",
"dev": "nuxt example",
"lint": "eslint --ext .js,.ts,.vue .",
"release": "yarn build && yarn test && standard-version && git push --follow-tags && npm publish",
"test": "yarn lint && jest"
"release": "yarn build && yarn lint && yarn test && standard-version && git push --follow-tags && npm publish",
"test": "jest"
},
"dependencies": {
"defu": "^2.0.4"
"defu": "latest",
"upath": "latest"
},
"devDependencies": {
"@babel/plugin-transform-runtime": "latest",
"@babel/preset-env": "latest",
"@babel/preset-typescript": "latest",
"@nuxt/test-utils": "latest",
"@nuxt/types": "latest",
"@nuxt/typescript-runtime": "latest",
"@nuxt/typescript-build": "latest",
"@nuxtjs/eslint-config-typescript": "latest",
"@siroc/jest-preset": "latest",
"@types/jest": "latest",
Expand All @@ -40,6 +48,7 @@
"eslint": "latest",
"jest": "latest",
"nuxt-edge": "latest",
"playwright": "latest",
"siroc": "latest",
"standard-version": "latest"
},
Expand Down
24 changes: 15 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { resolve } from 'path'
import defu from 'defu'
import { Module } from '@nuxt/types'
import { extname } from 'upath'

import { Module, NuxtOptions } from '@nuxt/types'

import { name, version } from '../package.json'

export interface ModuleOptions {}
const DEFAULTS: ModuleOptions = {}
const CONFIG_KEY = 'myModule'

const nuxtModule: Module<ModuleOptions> = /* async */ function (moduleOptions) {
const options = defu<ModuleOptions>(this.options[CONFIG_KEY], moduleOptions, DEFAULTS)
// const { nuxt } = this
const options = defu(this.options[CONFIG_KEY] /* istanbul ignore next */ || {}, moduleOptions, DEFAULTS)

const nuxtOptions: NuxtOptions = this.nuxt.options
console.log('Normalized & typed Nuxt options available, e.g.', { rootDir: nuxtOptions.rootDir })

const src = require.resolve('./templates/plugin')
this.addPlugin({
src: resolve(__dirname, '../templates/plugin.js'),
fileName: 'myPlugin.js',
src,
fileName: 'myPlugin' + extname(src),
options
})
}

;(nuxtModule as any).meta = require('../package.json')
;(nuxtModule as any).meta = { name, version }

declare module '@nuxt/types' {
interface NuxtConfig { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.14+
interface Configuration { [CONFIG_KEY]?: ModuleOptions } // Nuxt 2.9 - 2.13
interface NuxtConfig { [CONFIG_KEY]?: Partial<ModuleOptions> } // Nuxt 2.14+
interface Configuration { [CONFIG_KEY]?: Partial<ModuleOptions> } // Nuxt 2.9 - 2.13
}

export default nuxtModule
8 changes: 8 additions & 0 deletions src/templates/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Plugin } from '@nuxt/types'

console.log('imported')

export default <Plugin> /* async */ function (ctx, inject) {
// Fully typed plugin
console.log('loaded', ctx && 'with context', !!inject && 'and with inject')
}
6 changes: 0 additions & 6 deletions templates/plugin.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('module', () => {

test('browser', async () => {
const page = await createPage('/')
const html = await page.getHtml()
const html = await page.content()

expect(html).toContain('Works!')
})
Expand Down
4 changes: 2 additions & 2 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('module', () => {

test('should inject plugin', () => {
expectModuleToBeCalledWith('addPlugin', {
src: expect.stringContaining('templates/plugin.js'),
fileName: 'myPlugin.js',
src: expect.stringMatching(/templates[\\/]plugin/),
fileName: expect.stringContaining('myPlugin'),
options: getNuxt().options.myModule
})
})
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"strict": true,
"target": "es2018",
"allowJs": true,
"module": "esnext",
"lib": [
"esnext",
Expand Down
Loading