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: plugin public files (close: #1491) #1494

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
TODOs.md
packages/@vuepress/shared-utils/lib/
types
**/.vuepress/dist
1 change: 1 addition & 0 deletions packages/@vuepress/core/lib/node/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ module.exports = class App {
after: '</div>'
})
.use('@vuepress/last-updated', !!shouldUseLastUpdated)
.use('@vuepress/public-files', ['.vuepress/public'])
.use('@vuepress/register-components', {
componentsDir: [
path.resolve(this.sourceDir, '.vuepress/components'),
Expand Down
14 changes: 1 addition & 13 deletions packages/@vuepress/core/lib/node/webpack/createServerConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
*/

module.exports = function createServerConfig (ctx) {
const fs = require('fs')
const { path, env } = require('@vuepress/shared-utils')
const { env } = require('@vuepress/shared-utils')
const createBaseConfig = require('./createBaseConfig')
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
const CopyPlugin = require('copy-webpack-plugin')

const config = createBaseConfig(ctx, true /* isServer */)
const { sourceDir, outDir } = ctx

config
.target('node')
Expand All @@ -36,15 +33,6 @@ module.exports = function createServerConfig (ctx) {
filename: 'manifest/server.json'
}])

const publicDir = path.resolve(sourceDir, '.vuepress/public')
if (fs.existsSync(publicDir)) {
config
.plugin('copy')
.use(CopyPlugin, [[
{ from: publicDir, to: outDir }
]])
}

if (!env.isDebug) {
const WebpackBar = require('webpackbar')
config
Expand Down
1 change: 0 additions & 1 deletion packages/@vuepress/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"cache-loader": "^3.0.0",
"chokidar": "^2.0.3",
"connect-history-api-fallback": "^1.5.0",
"copy-webpack-plugin": "^5.0.2",
"cross-spawn": "^6.0.5",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/@vuepress/plugin-public-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @vuepress/plugin-public-files

> plugin for register public files

See [documentation](https://v1.vuepress.vuejs.org/plugin/official/plugin-public-files.html).
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.dotfolder/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
plugins: [
[require('../../..')]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.dotfolder/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<Content />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar/baz.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
plugins: [
[require('../../..'), {
from: '_assets',
to: 'assets',
ignore: ['*.ignore'],
}]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<Content />
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dotfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.dotfolder/foo.txt
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
readme

![bar](/bar.png)
54 changes: 54 additions & 0 deletions packages/@vuepress/plugin-public-files/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @jest-environment node
*/

const { createApp } = require('@vuepress/core')
const { path: { resolve }, fs } = require('@vuepress/shared-utils')

function testForExistence (app, name, existence = true) {
test(`${existence ? '' : 'in'}existence: ${name}`, () => {
expect(fs.existsSync(resolve(app.outDir, name))).toBe(existence)
})
}

describe('plugin-public-files: source folder', () => {
const app = createApp({
sourceDir: resolve(__dirname, 'docs-1')
})

beforeAll(async () => {
await app.process()
await app.build()
}, 60000)

testForExistence(app, 'foo.txt')
testForExistence(app, 'baz.txt')
testForExistence(app, 'bar/foo.txt')
testForExistence(app, 'bar/baz.txt')
testForExistence(app, 'index.html')
testForExistence(app, 'readme.md', false)
testForExistence(app, '.dotfile', false)
testForExistence(app, '.dotfolder', false)
testForExistence(app, 'bar/readme.md', false)
})

describe('plugin-public-files: assets folder', () => {
const app = createApp({
sourceDir: resolve(__dirname, 'docs-2')
})

beforeAll(async () => {
await app.process()
await app.build()
}, 60000)

testForExistence(app, 'foo.txt')
testForExistence(app, 'assets/bar.png')
testForExistence(app, 'assets/.dotfile')
testForExistence(app, 'assets/.dotfolder/foo.txt')
testForExistence(app, 'bar.png', false)
testForExistence(app, 'baz.ignore', false)
testForExistence(app, '.dotfile', false)
testForExistence(app, '.dotfolder', false)
testForExistence(app, 'assets/baz.ignore', false)
})
34 changes: 34 additions & 0 deletions packages/@vuepress/plugin-public-files/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const {
fs: { existsSync },
path: { resolve }
} = require('@vuepress/shared-utils')
const CopyPlugin = require('copy-webpack-plugin')
const mergeable = require('vuepress-mergeable')

module.exports = mergeable((patterns, context) => ({
name: `@vuepress/plugin-public-files`,

chainWebpack (config) {
if (!Array.isArray(patterns)) patterns = [patterns]

config
.plugin('copy')
.use(CopyPlugin, [patterns.map((pattern) => {
pattern = typeof pattern === 'string'
? { from: pattern }
: { ...pattern }
flozero marked this conversation as resolved.
Show resolved Hide resolved

// `from` will be resolved based on `sourceDir`
pattern.from = resolve(context.sourceDir, pattern.from || '')
if (!existsSync(pattern.from)) return

// `to` will be resolved based on `outDir`
pattern.to = resolve(context.outDir, pattern.to || '')

// ignore dotfiles and markdown by default
pattern.ignore = pattern.ignore || ['.*', '.*/**', '*.md']

return pattern
}).filter(p => p)])
}
}), 'flat')
36 changes: 36 additions & 0 deletions packages/@vuepress/plugin-public-files/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@vuepress/plugin-public-files",
"version": "1.0.0-alpha.47",
"description": "register-global-components plugin for vuepress",
"main": "index.js",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuejs/vuepress.git",
"directory": "packages/@vuepress/plugin-public-files"
},
"scripts": {
"build": "vuepress build __tests__/docs-1",
"jest": "jest .*\\.spec\\.js$"
},
"keywords": [
"documentation",
"vue",
"vuepress",
"generator"
],
"author": "Shigma <1700011071@pku.edu.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/vuejs/vuepress/issues"
},
"homepage": "https://github.com/vuejs/vuepress/packages/@vuepress/plugin-public-files#readme",
"dependencies": {
"@vuepress/core": "^1.0.0-alpha.47",
"@vuepress/shared-utils": "^1.0.0-alpha.47",
"copy-webpack-plugin": "^5.0.3",
"vuepress-mergeable": "^1.0.1"
}
}
81 changes: 81 additions & 0 deletions packages/docs/docs/plugin/official/plugin-public-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: public-files
metaTitle: public files plugin | VuePress
---

# [@vuepress/plugin-public-files](https://github.com/vuejs/vuepress/tree/master/packages/@vuepress/plugin-public-files)

> public files plugin

## Installation

```bash
yarn add -D @vuepress/plugin-public-files
# OR npm install -D @vuepress/plugin-public-files
```

## Usage

### Default Usage

``` js
module.exports = {
plugins: [
// includes all files in `sourceDir`
// ignore dotfiles and markdown
'@vuepress/public-files'
]
}
```

### An Array of Patterns

``` js
module.exports = {
plugins: [
['@vuepress/public-files', [
'.vuepress/another-public',
'.vuepress/one-more-public',
'.vuepress/public-file.txt'
]]
]
}
```

### object style of pattern

See: [https://github.com/webpack-contrib/copy-webpack-plugin#patterns](https://github.com/webpack-contrib/copy-webpack-plugin#patterns)

``` js
module.exports = {
plugins: [
['@vuepress/public-files', {
from: '_assets', // resolved based on sourceDir
to: 'assets', // resolved based on outDir
ignore: [/* glob patterns to be ignored */],
}],
],
}
```

::: TIP
In fact we support specifying a list of strings or objects.
:::

### Use Multiple Times

``` js
module.exports = {
plugins: [
['@vuepress/public-files', [
'.vuepress/another-public',
'.vuepress/one-more-public',
]],
['@vuepress/public-files', {
from: '_assets',
to: 'assets',
ignore: [/* globs */],
}],
],
}
```
Loading