-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
302 lines (258 loc) · 9.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// add polyfill for node.js >= 12.0.0 && < 15.0.0
require('./polyfills/string.replaceAll');
const fs = require('fs');
const path = require('path');
const pug = require('pug');
const walk = require('pug-walk');
const { isWin, trimIndent } = require('./utils');
const resolver = require('./resolver');
const loader = require('./loader');
const {
filterNotFoundException,
filterLoadException,
filterInitException,
getPugCompileErrorMessage,
} = require('./exeptions');
// path of embedded pug-loader filters
const filtersDir = path.join(__dirname, './filters/');
/**
* Dependencies in code for watching a changes.
*
* Public API
* @typedef {Object} LoaderDependency
* @property {Array<RegExp>} watchFiles
* @property {Function<loaderContext:Object>} init
* @property {Function<file:string>} add
* @property {Function|RegExp} watch
*/
const dependency = {
files: new Set(),
watchFiles: [/\.(pug|jade|js.{0,2}|.?js|ts.?|md|txt)$/i],
loaderContext: null,
isInit: false,
init(loaderContext, { watchFiles }) {
// avoid double push in array by watching
if (!this.isInit && watchFiles != null) {
if (!Array.isArray(watchFiles)) watchFiles = [watchFiles];
this.watchFiles.push(...watchFiles);
this.isInit = true;
}
this.loaderContext = loaderContext;
},
/**
* Add file to watch list.
*
* @param {string} file
*/
add(file) {
if (!this.watchFiles.find((regex) => regex.test(file))) {
return;
}
file = isWin ? path.normalize(file) : file;
// delete the file (.js, .json, etc.) from require.cache to reload cached files after changes by watch
delete require.cache[file];
this.files.add(file);
},
watch() {
const files = Array.from(this.files);
files.forEach(this.loaderContext.addDependency);
},
};
/**
* The pug plugin to resolve path for include, extends, require.
*
* @type {{resolve: (function(string, string, {}): string), preCodeGen: (function({}): *)}}
*/
const resolvePlugin = {
/**
* Resolve the filename for extends / include / raw include.
*
* @param {string} filename The extends/include filename in template.
* @param {string} templateFile The absolute path to template.
* @param {{}} options The options of pug compiler.
* @return {string}
*/
resolve(filename, templateFile, options) {
return resolver.resolve(filename.trim(), templateFile.trim());
},
/**
* Resolve the filename for require().
*
* @param {{}} ast The parsed tree of pug template.
* @return {{}}
*/
preCodeGen(ast) {
return walk(ast, (node) => {
if (node.type === 'Code') {
// resolving for require of a code, e.g.: `- var data = require('./data.js')`
if (containRequire(node)) {
const result = resolver.resolveResource(node.filename, node.val);
if (result != null && result !== node.val) node.val = result;
}
} else if (node.attrs) {
// resolving for tag attributes, e.g.: `img(src=require('./image.jpeg'))`
node.attrs.forEach((attr) => {
if (containRequire(attr)) {
const result =
node.name === 'script'
? resolver.resolveScript(attr.filename, attr.val)
: resolver.resolveResource(attr.filename, attr.val);
if (result != null && result !== attr.val) attr.val = result;
}
});
}
});
},
};
/**
* Whether the node value contains the require().
*
* @param {{val: *}} obj
* @return {boolean}
*/
const containRequire = (obj) => obj.val && typeof obj.val === 'string' && obj.val.indexOf('require(') >= 0;
/**
* Load embedded pug filters.
* @param {{filterName: string, options: boolean|string|object}} filters
*/
const loadFilters = (filters) => {
for (const filterName in filters) {
const options = filters[filterName];
if (options) {
let filterPath = path.resolve(filtersDir, filterName + '.js');
let filter;
try {
filter = require(filterPath);
} catch (error) {
const message = error.toString();
if (message.indexOf('Cannot find module') >= 0 && message.indexOf('Please install the module') < 0) {
const entries = fs.readdirSync(filtersDir, { withFileTypes: true });
const files = entries.filter((file) => !file.isDirectory()).map((file) => path.basename(file.name, '.js'));
filterNotFoundException(filterName, files.join(', '));
}
filterLoadException(filterName, filterPath, error);
}
try {
// filter module may have the `init(options)` method
if (filter.init != null) {
filter.init(options);
}
// add filter to pug compiler
pug.filters[filterName] = filter.apply.bind(filter);
} catch (error) {
filterInitException(filterName, error);
}
}
}
};
/**
* @param {string} content The pug template.
* @param {function(error: string|null, result: string?)?} callback The asynchronous callback function.
* @return {string|undefined}
*/
const compile = function (content, callback) {
const loaderContext = this;
const loaderOptions = loaderContext.getOptions() || {};
const { _compiler: webpackCompiler, resourcePath: filename, rootContext: context, resourceQuery } = loaderContext;
const webpackOptions = webpackCompiler ? webpackCompiler.options : {};
const resolverOptions = webpackOptions.resolve || {};
if (!loaderOptions.name) loaderOptions.name = 'template';
if (loaderOptions.embedFilters) loadFilters(loaderOptions.embedFilters);
const compilerOptions = {
// used to resolve import/extends and to improve errors
filename,
// the root directory of all absolute inclusion, defaults is `/`.
basedir: loaderOptions.basedir || '/',
doctype: loaderOptions.doctype || 'html',
self: loaderOptions.self || false,
globals: ['require', ...(loaderOptions.globals || [])],
// the name of template function, defaults `template`
name: loaderOptions.name,
// filters of rendered content, e.g. markdown-it
filters: loaderOptions.filters,
filterOptions: loaderOptions.filterOptions,
filterAliases: loaderOptions.filterAliases,
// add the plugin to resolve include, extends, require
plugins: [resolvePlugin, ...(loaderOptions.plugins || [])],
// include inline runtime functions must be true
inlineRuntimeFunctions: true,
// for the pure function code w/o exports the module, must be false
module: false,
// include the function source in the compiled template, defaults is false
compileDebug: loaderOptions.compileDebug === true,
// output compiled function to stdout, must be false
debug: loaderOptions.debug === true,
/**
* @deprecated This option is deprecated and must be false, see https://pugjs.org/api/reference.html#options
* Use the `pretty` option of the pug-plugin to format generated HTML.
**/
pretty: false,
};
resolver.init({
context,
basedir: compilerOptions.basedir,
options: resolverOptions,
});
loader.init({
resourceQuery,
// in pug can be used external data, e.g. htmlWebpackPlugin.options
customData: getHtmlWebpackPluginOptions(webpackOptions, filename),
options: loaderOptions,
});
dependency.init(loaderContext, loaderOptions);
resolver.setDependency(dependency);
resolver.setLoader(loader);
loader.setResolver(resolver);
if (loaderContext.cacheable) loaderContext.cacheable(true);
// remove indent in template
const template = trimIndent(content);
if (template !== false) content = template;
let compileResult;
try {
/** @type {{body: string, dependencies: []}} */
compileResult = pug.compileClientWithDependenciesTracked(content, compilerOptions).body;
// Note: don't use compileResult.dependencies because it is not available by compile error.
// We track all dependencies at compile process into `pugDependencies`,
// then by a compile error our pugDependencies are available to watch changes in corrupted pug files.
} catch (error) {
if (error.filename) {
dependency.add(error.filename);
}
dependency.watch();
callback(getPugCompileErrorMessage(error));
return;
}
const result = loader.export(filename, compileResult);
dependency.watch();
callback(null, result);
};
/**
* Get user options of HtmlWebpackPlugin({}).
*
* @param {Object} webpackOptions The webpack config options.
* @param {string} filename The filename of pug template.
* @returns {{htmlWebpackPlugin: {options: {}}}}
*/
const getHtmlWebpackPluginOptions = (webpackOptions, filename) => {
const plugins = webpackOptions.plugins;
let options = {};
if (plugins) {
const pluginData = plugins.find(
(item) => item.constructor.name === 'HtmlWebpackPlugin' && item.options.template.indexOf(filename) >= 0
);
if (pluginData) {
options = { htmlWebpackPlugin: { options: {} } };
if (pluginData.hasOwnProperty('userOptions')) {
options.htmlWebpackPlugin.options = pluginData.userOptions;
}
}
}
return options;
};
module.exports = function (content, map, meta) {
const callback = this.async();
compile.call(this, content, (err, result) => {
if (err) return callback(err);
callback(null, result, map, meta);
});
};