@vituum/vite-plugin-pug 0.1.4 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +97 -86
  2. package/package.json +21 -8
  3. package/types/index.d.ts +37 -0
package/index.js CHANGED
@@ -1,136 +1,147 @@
1
- import { dirname, resolve, relative } from 'path'
1
+ import { resolve, relative } from 'path'
2
2
  import fs from 'fs'
3
3
  import process from 'node:process'
4
- import FastGlob from 'fast-glob'
5
4
  import lodash from 'lodash'
6
5
  import pug from 'pug'
7
- import chalk from 'chalk'
8
- import { fileURLToPath } from 'url'
6
+ import { getPackageInfo, merge, pluginBundle, pluginError, pluginReload, processData } from 'vituum/utils/common.js'
7
+ import { renameBuildEnd, renameBuildStart } from 'vituum/utils/build.js'
8
+ import { minimatch } from 'minimatch'
9
9
 
10
- const { name } = JSON.parse(fs.readFileSync(resolve(dirname((fileURLToPath(import.meta.url))), 'package.json')).toString())
10
+ const { name } = getPackageInfo(import.meta.url)
11
+
12
+ /**
13
+ * @type {import('@vituum/vite-plugin-pug/types').PluginUserConfig}
14
+ */
11
15
  const defaultOptions = {
12
16
  reload: true,
13
17
  root: null,
14
18
  filters: {},
15
- globals: {},
16
- data: '',
17
- filetypes: {
18
- html: /.(json.html|pug.json.html|pug.html)$/,
19
- json: /.(json.pug.html)$/
19
+ globals: {
20
+ format: 'pug'
20
21
  },
21
- pug: {}
22
+ data: ['src/data/**/*.json'],
23
+ formats: ['pug', 'json.pug', 'json'],
24
+ options: {}
22
25
  }
23
26
 
24
- function processData(paths, data = {}) {
25
- let context = {}
26
-
27
- lodash.merge(context, data)
28
-
29
- FastGlob.sync(paths).forEach(entry => {
30
- const path = resolve(process.cwd(), entry)
31
-
32
- context = lodash.merge(context, JSON.parse(fs.readFileSync(path).toString()))
33
- })
34
-
35
- return context
36
- }
37
-
38
- const renderTemplate = async(filename, content, options) => {
27
+ const renderTemplate = async ({ filename, server, root }, content, options) => {
28
+ const initialFilename = filename.replace('.html', '')
39
29
  const output = {}
40
- const context = options.data ? processData(options.data, options.globals) : options.globals
41
-
42
- const isJson = filename.endsWith('.json.html') || filename.endsWith('.json')
43
- const isHtml = filename.endsWith('.html') && !options.filetypes.html.test(filename) && !options.filetypes.json.test(filename) && !isJson
30
+ const context = options.data
31
+ ? processData({
32
+ paths: options.data,
33
+ root
34
+ }, options.globals)
35
+ : options.globals
44
36
 
45
- if (isJson || isHtml) {
46
- lodash.merge(context, isHtml ? content : JSON.parse(fs.readFileSync(filename).toString()))
37
+ if (initialFilename.endsWith('.json')) {
38
+ lodash.merge(context, JSON.parse(content))
47
39
 
48
40
  output.template = true
49
41
 
50
42
  if (typeof context.template === 'undefined') {
51
- console.error(chalk.red(name + ' template must be defined - ' + filename))
43
+ const error = `${name}: template must be defined for file ${initialFilename}`
44
+
45
+ return new Promise((resolve) => {
46
+ output.error = error
47
+ resolve(output)
48
+ })
52
49
  }
53
50
 
54
51
  context.template = relative(process.cwd(), context.template).startsWith(relative(process.cwd(), options.root)) ? resolve(process.cwd(), context.template) : resolve(options.root, context.template)
55
- } else if (fs.existsSync(filename + '.json')) {
56
- lodash.merge(context, JSON.parse(fs.readFileSync(filename + '.json').toString()))
52
+ } else if (fs.existsSync(`${initialFilename}.json`)) {
53
+ lodash.merge(context, JSON.parse(fs.readFileSync(`${initialFilename}.json`).toString()))
57
54
  }
58
55
 
59
- try {
60
- const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.pug, {
61
- basedir: options.root,
62
- filters: options.filters
63
- }))
56
+ return new Promise((resolve) => {
57
+ try {
58
+ const template = pug.compileFile(output.template ? context.template : filename, Object.assign(options.options, {
59
+ basedir: options.root,
60
+ filters: options.filters
61
+ }))
64
62
 
65
- output.content = template(context)
66
- } catch (error) {
67
- output.error = error
68
- }
63
+ output.content = template(context)
69
64
 
70
- return output
65
+ resolve(output)
66
+ } catch (error) {
67
+ output.error = error
68
+
69
+ resolve(output)
70
+ }
71
+ })
71
72
  }
72
73
 
74
+ /**
75
+ * @param {import('@vituum/vite-plugin-pug/types').PluginUserConfig} options
76
+ * @returns [import('vite').Plugin]
77
+ */
73
78
  const plugin = (options = {}) => {
74
- options = lodash.merge(defaultOptions, options)
79
+ let resolvedConfig
80
+ let userEnv
81
+
82
+ options = merge(defaultOptions, options)
75
83
 
76
- return {
84
+ return [{
77
85
  name,
78
- config: ({ root }) => {
86
+ config (userConfig, env) {
87
+ userEnv = env
88
+ },
89
+ configResolved (config) {
90
+ resolvedConfig = config
91
+
79
92
  if (!options.root) {
80
- options.root = root
93
+ options.root = config.root
81
94
  }
82
95
  },
83
- transformIndexHtml: {
84
- enforce: 'pre',
85
- async transform(content, { path, filename, server }) {
86
- path = path.replace('?raw', '')
87
- filename = filename.replace('?raw', '')
96
+ buildStart: async () => {
97
+ if (userEnv.command !== 'build') {
98
+ return
99
+ }
100
+
101
+ await renameBuildStart(resolvedConfig.build.rollupOptions.input, options.formats)
102
+ },
103
+ buildEnd: async () => {
104
+ if (userEnv.command !== 'build') {
105
+ return
106
+ }
88
107
 
108
+ await renameBuildEnd(resolvedConfig.build.rollupOptions.input, options.formats)
109
+ },
110
+ transformIndexHtml: {
111
+ order: 'pre',
112
+ async transform (content, { path, filename, server }) {
89
113
  if (
90
- !options.filetypes.html.test(path) &&
91
- !options.filetypes.json.test(path) &&
92
- !content.startsWith('<script type="application/json" data-format="pug"')
114
+ !options.formats.find(format => filename.replace('.html', '').endsWith(format)) ||
115
+ (filename.replace('.html', '').endsWith('.json') && !content.startsWith('{'))
93
116
  ) {
94
117
  return content
95
118
  }
96
119
 
97
- if (content.startsWith('<script type="application/json" data-format="pug"')) {
98
- const matches = content.matchAll(/<script\b[^>]*data-format="(?<format>[^>]+)"[^>]*>(?<data>[\s\S]+?)<\/script>/gmi)
120
+ if (
121
+ (filename.replace('.html', '').endsWith('.json') && content.startsWith('{')) &&
122
+ (JSON.parse(content)?.format && !options.formats.includes(JSON.parse(content)?.format))
123
+ ) {
124
+ return content
125
+ }
99
126
 
100
- for (const match of matches) {
101
- content = JSON.parse(match.groups.data)
102
- }
127
+ if (options.ignoredPaths.find(ignoredPath => minimatch(path.replace('.html', ''), ignoredPath) === true)) {
128
+ return content
103
129
  }
104
130
 
105
- const render = await renderTemplate(filename, content, options)
106
-
107
- if (render.error) {
108
- if (!server) {
109
- console.error(chalk.red(render.error))
110
- return
111
- }
112
-
113
- setTimeout(() => server.ws.send({
114
- type: 'error',
115
- err: {
116
- message: render.error.message,
117
- plugin: name
118
- }
119
- }), 50)
131
+ const render = await renderTemplate({ filename, server, root: resolvedConfig.root }, content, options)
132
+ const renderError = pluginError(render.error, server, name)
133
+
134
+ if (renderError && server) {
135
+ return
136
+ } else if (renderError) {
137
+ return renderError
120
138
  }
121
139
 
122
140
  return render.content
123
141
  }
124
142
  },
125
- handleHotUpdate({ file, server }) {
126
- if (
127
- (typeof options.reload === 'function' && options.reload(file)) ||
128
- (options.reload && (options.filetypes.html.test(file) || options.filetypes.json.test(file)))
129
- ) {
130
- server.ws.send({ type: 'full-reload' })
131
- }
132
- }
133
- }
143
+ handleHotUpdate: ({ file, server }) => pluginReload({ file, server }, options)
144
+ }, pluginBundle(options.formats)]
134
145
  }
135
146
 
136
147
  export default plugin
package/package.json CHANGED
@@ -1,24 +1,37 @@
1
1
  {
2
2
  "name": "@vituum/vite-plugin-pug",
3
- "version": "0.1.4",
3
+ "version": "1.0.0-alpha.1",
4
4
  "type": "module",
5
5
  "main": "index.js",
6
+ "scripts": {
7
+ "tsc": "tsc",
8
+ "eslint": "eslint '**/*.js' --fix"
9
+ },
6
10
  "dependencies": {
7
11
  "pug": "^3.0.2",
8
12
  "lodash": "^4.17.21",
9
- "chalk": "^5.2.0",
10
- "fast-glob": "^3.2.12"
13
+ "fast-glob": "^3.2.12",
14
+ "vituum": "^1.0.0-alpha.23",
15
+ "minimatch": "^9.0.1"
11
16
  },
12
17
  "devDependencies": {
13
- "eslint": "^8.29.0",
14
- "eslint-config-standard": "^17.0.0"
18
+ "@types/node": "^20.3.1",
19
+ "eslint": "^8.43.0",
20
+ "eslint-config-standard": "^17.1.0",
21
+ "typescript": "^5.1.3",
22
+ "vite": "^4.3.9"
15
23
  },
16
24
  "files": [
17
- "index.js"
25
+ "index.js",
26
+ "types"
18
27
  ],
28
+ "exports": {
29
+ ".": "./index.js",
30
+ "./types": "./types/*"
31
+ },
19
32
  "engines": {
20
- "node": ">=16.0.0",
21
- "npm": ">=8.0.0"
33
+ "node": ">=18.0.0",
34
+ "npm": ">=9.0.0"
22
35
  },
23
36
  "repository": {
24
37
  "type": "git",
@@ -0,0 +1,37 @@
1
+ interface PugOptions {
2
+ /** The name of the file being compiled. Used in exceptions, and required for relative includes and extends. Defaults to 'Pug'. */
3
+ filename?: string | undefined,
4
+ /** The root directory of all absolute inclusion. */
5
+ basedir?: string | undefined,
6
+ /** If the doctype is not specified as part of the template, you can specify it here. It is sometimes useful to get self-closing tags and remove mirroring of boolean attributes; see doctype documentation for more information. */
7
+ doctype?: string | undefined,
8
+ /** Adds whitespace to the resulting HTML to make it easier for a human to read using ' ' as indentation. If a string is specified, that will be used as indentation instead (e.g. '\t'). Defaults to false. */
9
+ pretty?: boolean | string | undefined,
10
+ /** Hash table of custom filters. Defaults to undefined. */
11
+ filters?: any,
12
+ /** Use a self namespace to hold the locals. It will speed up the compilation, but instead of writing variable you will have to write self.variable to access a property of the locals object. Defaults to false. */
13
+ self?: boolean | undefined,
14
+ /** If set to true, the tokens and function body are logged to stdout. */
15
+ debug?: boolean | undefined,
16
+ /** If set to true, the function source will be included in the compiled template for better error messages (sometimes useful in development). It is enabled by default unless used with Express in production mode. */
17
+ compileDebug?: boolean | undefined,
18
+ /** Add a list of global names to make accessible in templates. */
19
+ globals?: Array<string> | undefined,
20
+ /** If set to true, compiled functions are cached. filename must be set as the cache key. Only applies to render functions. Defaults to false. */
21
+ cache?: boolean | undefined,
22
+ /** Inline runtime functions instead of require-ing them from a shared version. For compileClient functions, the default is true so that one does not have to include the runtime. For all other compilation or rendering types, the default is false. */
23
+ inlineRuntimeFunctions?: boolean | undefined,
24
+ /** The name of the template function. Only applies to compileClient functions. Defaults to 'template'. */
25
+ name?: string | undefined
26
+ }
27
+
28
+ export interface PluginUserConfig {
29
+ reload?: boolean | Function
30
+ root?: string
31
+ filters?: Object
32
+ globals?: Object
33
+ data?: string | string[]
34
+ formats?: string[]
35
+ options?: PugOptions
36
+ ignoredPaths?: string[]
37
+ }