Skip to content

Commit

Permalink
resolve lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 2, 2019
1 parent 50e1c03 commit c58ee9a
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 111 deletions.
3 changes: 2 additions & 1 deletion other-packages/eslint-config-saber/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
requireLast: false
}
}
]
],
'@typescript-eslint/prefer-includes': 'off'
}
}
1 change: 1 addition & 0 deletions packages/saber/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class Compiler extends EventEmitter {

injectToWebpack(config: WebpackChain) {
const ID = `compiler-${this.type}`
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this
config.plugin(ID).use(
class {
Expand Down
2 changes: 1 addition & 1 deletion packages/saber/src/plugins/config-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ exports.apply = api => {
name: 'styles',
// necessary to ensure async chunks are also extracted
test: m => {
return /css\/mini-extract/.test(m.type)
return m.type && m.type.includes('css/mini-extract')
},
chunks: 'all',
enforce: true
Expand Down
1 change: 1 addition & 0 deletions packages/saber/src/plugins/emit-runtime-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.apply = api => {
polyfills,
'utf8'
)
// eslint-disable-next-line require-atomic-updates
previousPolyfills = polyfills
}
})
Expand Down
18 changes: 12 additions & 6 deletions packages/saber/src/plugins/extend-browser-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ exports.apply = api => {
cwd: api.resolveCwd(),
ignoreInitial: true
})
const onAdd = async filename => {
api.browserApi.add(api.resolveCwd(filename))
await api.browserApi.reload()
}
const onRemove = async filename => {
api.browserApi.delete(api.resolveCwd(filename))
await api.browserApi.reload()
}
watcher
.on('add', async filename => {
api.browserApi.add(api.resolveCwd(filename))
await api.browserApi.reload()
.on('add', filename => {
onAdd(filename)
})
.on('unlink', async filename => {
api.browserApi.delete(api.resolveCwd(filename))
await api.browserApi.reload()
.on('unlink', filename => {
onRemove(filename)
})
}
})
Expand Down
61 changes: 32 additions & 29 deletions packages/saber/src/plugins/extend-node-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { log, colors } = require('saber-log')

const ID = 'builtin:extend-node-api'

function __noopHandler__(arg) {
return arg
}

exports.name = ID

exports.apply = api => {
Expand Down Expand Up @@ -58,43 +62,42 @@ exports.apply = api => {
}
})

if (api.dev && !/node_modules/.test(nodeApiFile)) {
if (api.dev && !nodeApiFile.includes('node_modules')) {
const onChange = async action => {
updateNodeApi()
// Remove all child pages
api.pages.removeWhere(page => page.internal.parent)
await Promise.all(
[...api.pages.values()].map(async page => {
// Recreate the page
api.pages.createPage(page)
// A page has been created
await api.hooks.onCreatePage.promise(page)
})
)
// All pages are created
await api.hooks.onCreatePages.promise()
// Emit pages
await api.hooks.emitPages.promise()
// Emit route file
await api.hooks.emitRoutes.promise()
log.warn(
`${action[0].toUpperCase()}${action.substring(1)} ${nodeApiFile}`
)
// Because you might also update webpack config in saber-node.js
// Which we can't (?) automatically reload
log.warn(`You probably need to restart the server.`)
}
require('chokidar')
.watch(nodeApiFile, {
ignoreInitial: true
})
.on('all', async action => {
await updateNodeApi()
// Remove all child pages
api.pages.removeWhere(page => page.internal.parent)
await Promise.all(
[...api.pages.values()].map(async page => {
// Recreate the page
api.pages.createPage(page)
// A page has been created
await api.hooks.onCreatePage.promise(page)
})
)
// All pages are created
await api.hooks.onCreatePages.promise()
// Emit pages
await api.hooks.emitPages.promise()
// Emit route file
await api.hooks.emitRoutes.promise()
log.warn(
`${action[0].toUpperCase()}${action.substring(1)} ${nodeApiFile}`
)
// Because you might also update webpack config in saber-node.js
// Which we can't (?) automatically reload
log.warn(`You probably need to restart the server.`)
.on('all', action => {
onChange(action)
})
}
}

handleNodeApiFile(path.join(api.theme, 'saber-node.js'), 'theme-node-api')
handleNodeApiFile(api.resolveCwd('saber-node.js'), 'user-node-api')
}

function __noopHandler__(arg) {
return arg
}
40 changes: 26 additions & 14 deletions packages/saber/src/plugins/layouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ exports.apply = api => {
const watchLayouts = (dir, layouts) => {
const chokidar = require('chokidar')

const onRemoveDir = async dir => {
if (!dir) {
Object.keys(layouts).forEach(name => {
delete layouts[name]
})
await writeLayouts(themeLayouts, userLayouts)
}
}

const onAddLayout = async file => {
setLayout(layouts, path.join(dir, file))
await writeLayouts(themeLayouts, userLayouts)
}

const onRemoveLayout = async file => {
setLayout(layouts, path.join(dir, file), true)
await writeLayouts(themeLayouts, userLayouts)
}

// Clear the layouts object when the layouts directory is removed
chokidar
.watch('.', {
Expand All @@ -68,30 +87,23 @@ exports.apply = api => {
},
ignoreInitial: true
})
.on('unlinkDir', async dir => {
if (!dir) {
Object.keys(layouts).forEach(name => {
delete layouts[name]
})
await writeLayouts(themeLayouts, userLayouts)
}
.on('unlinkDir', dir => {
onRemoveDir(dir)
})

// Add/Remove layout components
chokidar
.watch('*.{vue,js}', { cwd: dir, ignoreInitial: true })
.on('add', async file => {
setLayout(layouts, path.join(dir, file))
await writeLayouts(themeLayouts, userLayouts)
.on('add', file => {
onAddLayout(file)
})
.on('unlink', async file => {
setLayout(layouts, path.join(dir, file), true)
await writeLayouts(themeLayouts, userLayouts)
.on('unlink', file => {
onRemoveLayout(file)
})
}

// No need to watch theme layouts if it's from an npm package
if (!/node_modules/.test(themeLayoutsDir)) {
if (!themeLayoutsDir.includes('node_modules')) {
watchLayouts(themeLayoutsDir, themeLayouts)
}

Expand Down
14 changes: 11 additions & 3 deletions packages/saber/src/plugins/source-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ exports.apply = api => {
.map(async file => {
file.relative = file.path
file.absolute = path.join(pagesDir, file.relative)
// eslint-disable-next-line require-atomic-updates
file.content = await fs.readFile(file.absolute, 'utf8')
log.verbose(`Found page`, colors.dim(file.absolute))
return file
Expand Down Expand Up @@ -81,6 +82,7 @@ exports.apply = api => {

log.verbose(`Emitting page ${outPath}`)
await fs.outputFile(outPath, newContentHash, 'utf8')
// eslint-disable-next-line require-atomic-updates
page.internal.saved = true
})
)
Expand Down Expand Up @@ -126,9 +128,15 @@ exports.apply = api => {
await api.hooks.emitRoutes.promise()
}

watcher.on('add', handler('add'))
watcher.on('unlink', handler('remove'))
watcher.on('change', handler('change'))
watcher.on('add', () => {
handler('add')
})
watcher.on('unlink', () => {
handler('remove')
})
watcher.on('change', () => {
handler('change')
})
}
})
}
52 changes: 26 additions & 26 deletions packages/saber/src/plugins/transformer-markdown.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,6 @@
const ConfigChain = require('../config-chain')
const resolvePackage = require('../utils/resolvePackage')

exports.name = 'builtin:transformer-markdown'

exports.apply = api => {
api.transformers.add('markdown', {
extensions: ['md'],
transform(page) {
const { frontmatter, body } = require('../utils/parseFrontmatter')(
page.content,
page.internal.absolute
)
Object.assign(page, frontmatter)
page.content = body
page.content = renderMarkdown(api, page)
},
getPageComponent(page) {
return `
<template>
<layout-manager>
${page.content || ''}
</layout-manager>
</template>
`
}
})
}

function renderMarkdown(api, page) {
const { configDir } = api
const { markdown = {} } = api.config
Expand Down Expand Up @@ -113,3 +87,29 @@ function renderMarkdown(api, page) {

return md.render(page.content, env)
}

exports.name = 'builtin:transformer-markdown'

exports.apply = api => {
api.transformers.add('markdown', {
extensions: ['md'],
transform(page) {
const { frontmatter, body } = require('../utils/parseFrontmatter')(
page.content,
page.internal.absolute
)
Object.assign(page, frontmatter)
page.content = body
page.content = renderMarkdown(api, page)
},
getPageComponent(page) {
return `
<template>
<layout-manager>
${page.content || ''}
</layout-manager>
</template>
`
}
})
}
7 changes: 2 additions & 5 deletions packages/saber/src/utils/assetsAttribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { slash, isAbsoluteUrl } from 'saber-utils'
* When it's an absolute url or starting with `/`
* `/path` is used to reference files in static folder
*/
const isExternal = (str: string) => isAbsoluteUrl(str) || /^\//.test(str)
const isExternal = (str: string) => isAbsoluteUrl(str) || str.startsWith('/')

const MARK = '@@!!SABER_ASSET_MARK_e5968b9a!!@@'

Expand Down Expand Up @@ -42,7 +42,4 @@ const requireAssets = (str: string) =>
return `require("${p1}")`
})

export {
prefixAssets,
requireAssets
}
export { prefixAssets, requireAssets }
16 changes: 8 additions & 8 deletions packages/saber/src/utils/getPermalink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// @ts-check

/**
* Left-pad '0' to number that is smaller than 10
* @param {number} input
*/
function padZero(input) {
return input < 10 ? `0${input}` : input
}

// Default:
// about.md => /about.html
// about/index.md => /about
Expand Down Expand Up @@ -54,11 +62,3 @@ module.exports = (localeNames, page, permalinks) => {
.replace(/^\/index(\.html)?$/, '/')
.replace(/\/index(\.html)?$/, '')
}

/**
* Left-pad '0' to number that is smaller than 10
* @param {number} input
*/
function padZero(input) {
return input < 10 ? `0${input}` : input
}
2 changes: 1 addition & 1 deletion packages/saber/src/utils/serveDir.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const serveStatic = require('serve-static')
module.exports = function({ dir, host, port } = {}) {
const server = polka()
server.use(serveStatic(dir))
server.use(async (req, res, next) => {
server.use((req, res, next) => {
if (req.method !== 'GET') return next()
createReadStream(path.join(dir, '404.html')).pipe(res)
})
Expand Down
Loading

0 comments on commit c58ee9a

Please sign in to comment.