Skip to content

Commit 88b6c2b

Browse files
committed
fix: resolving asset files on windows
1 parent 4d188f5 commit 88b6c2b

18 files changed

Lines changed: 63 additions & 31 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change log
22

3+
## 3.6.3 (2024-03-14)
4+
5+
- fix: resolving asset files on windows
6+
37
## 3.6.2 (2024-03-11)
48

59
- fix: avoid recompiling all entry templates after changes of a non-entry partial file, [pug-plugin issue](https://github.com/webdiscus/pug-plugin/issues/66)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-bundler-webpack-plugin",
3-
"version": "3.6.2",
3+
"version": "3.6.3",
44
"description": "HTML bundler plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in HTML, supports template engines like Eta, EJS, Handlebars, Nunjucks.",
55
"keywords": [
66
"html",

src/Loader/Modes/Compile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const PreprocessorMode = require('./PreprocessorMode');
22
const { errorToHtml } = require('../Messages/Exeptions');
33
const { decodeReservedChars, escapeSequences } = require('../Utils');
4+
const { isWin, pathToPosix } = require('../../Common/Helpers');
45

56
/**
67
* Compile into JS function and export as a JS module.
@@ -28,6 +29,8 @@ class Compile extends PreprocessorMode {
2829
requireExpression(file) {
2930
const quote = this.enclosingQuotes;
3031

32+
if (isWin) file = pathToPosix(file);
33+
3134
return `${quote} + require('${file}') + ${quote}`;
3235
}
3336

src/Loader/Preprocessors/Eta/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ const preprocessor = (loaderContext, options) => {
8282
* @return {string} The exported template function.
8383
*/
8484
export(templateFunction, { data }) {
85-
// note: resolved the file is for node, therefore, we need to get the module path plus file for browser
86-
const runtimeFile = path.join(path.dirname(require.resolve('eta')), 'browser.module.mjs');
85+
// resolved the file is for node, therefore, we need to get the module path plus file for browser,
86+
// fix windows-like path into the posix standard :-/
87+
const runtimeFile = path.join(path.dirname(require.resolve('eta')), 'browser.module.mjs').replace(/\\/g, '/');
8788
const exportFunctionName = 'templateFn';
8889
const exportCode = 'module.exports=';
8990

src/Loader/Preprocessors/Handlebars/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ const preprocessor = (loaderContext, options) => {
212212
* @return {string} The exported template function.
213213
*/
214214
export(precompiledTemplate, { data }) {
215-
const runtimeFile = require.resolve('handlebars/dist/handlebars.runtime.min');
215+
// fix windows-like path
216+
const runtimeFile = require.resolve('handlebars/dist/handlebars.runtime.min').replace(/\\/g, '/');
216217
const exportFunctionName = 'templateFn';
217218
const exportCode = 'module.exports=';
218219

src/Loader/Preprocessors/Nunjucks/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ const preprocessor = (loaderContext, options = {}, { esModule, watch }) => {
7575
if (requiredTemplates.has(templateFile)) continue;
7676

7777
// try to resolve the template file in multiple paths
78-
const file = require.resolve(templateFile, { paths: viewPaths });
78+
let file = require.resolve(templateFile, { paths: viewPaths });
7979

8080
if (file) {
81-
// unique template name as the template path
82-
const templatePath = path.relative(rootContext, file);
81+
// unique template name as the template path, fix windows-like path
82+
const templatePath = path.relative(rootContext, file).replace(/\\/g, '/');
83+
84+
// fix windows-like path
85+
file = file.replace(/\\/g, '/');
8386
dependencies += `dependencies["${templatePath}"] = require("${file}");`;
8487

8588
// if used partial paths (defined in `views` option) to include a partial,
@@ -113,7 +116,8 @@ const preprocessor = (loaderContext, options = {}, { esModule, watch }) => {
113116
* @return {string} The exported template function.
114117
*/
115118
export(precompiledTemplate, { data }) {
116-
const runtimeFile = require.resolve('nunjucks/browser/nunjucks-slim.min');
119+
// fix windows-like path
120+
const runtimeFile = require.resolve('nunjucks/browser/nunjucks-slim.min').replace(/\\/g, '/');
117121

118122
return `
119123
var nunjucks = require('${runtimeFile}');

src/Loader/Preprocessors/Pug/ResolvePlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const Resolver = require('../../Resolver');
3-
const { encodeReservedChars, isWin, pathToPosix } = require('../../Utils');
3+
const { encodeReservedChars } = require('../../Utils');
4+
const { isWin, pathToPosix } = require('../../../Common/Helpers');
45

56
const scriptExtensionRegexp = /\.js[a-z\d]*$/i;
67
const isRequireableScript = (file) => !path.extname(file) || scriptExtensionRegexp.test(file);
@@ -146,7 +147,8 @@ const LoaderResolvers = {
146147
const requireExpression = (value, issuer, type = 'default') => {
147148
const [, requiredFile] = /require\((.+?)(?=\))/.exec(value) || [];
148149
const file = requiredFile || value;
149-
//if (isWin) issuer = pathToPosix(issuer);
150+
151+
if (isWin) issuer = pathToPosix(issuer);
150152

151153
if (ResolvePlugin.mode === 'render') {
152154
const requireType = requireTypes[type];
@@ -155,7 +157,9 @@ const requireExpression = (value, issuer, type = 'default') => {
155157
}
156158

157159
if (ResolvePlugin.mode === 'compile') {
158-
const interpolatedValue = Resolver.interpolate(file, issuer, type);
160+
let interpolatedValue = Resolver.interpolate(file, issuer, type);
161+
162+
if (isWin) interpolatedValue = pathToPosix(interpolatedValue);
159163

160164
return requiredFile ? `require('${interpolatedValue}')` : `require(${interpolatedValue})`;
161165
}

src/Loader/Preprocessors/Twig/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ const preprocessor = (loaderContext, options) => {
1919
const resolveDependency = async (token) => {
2020
// if the `namespaces` twig option contains not absolute path, then a parsed path is the path relative to root context
2121
const filePath = TwigEngine.path.parsePath(template, token.value);
22-
const file = path.isAbsolute(filePath) ? filePath : path.resolve(rootContext, filePath);
22+
let file = path.isAbsolute(filePath) ? filePath : path.resolve(rootContext, filePath);
2323

2424
token.value = makeTemplateId(rootContext, file);
25+
26+
// fix windows-like path
27+
file = file.replace(/\\/g, '/');
2528
dependencies.add(file);
2629
loaderContext.addDependency(file);
2730
};
@@ -144,7 +147,8 @@ const preprocessor = (loaderContext, options) => {
144147
* @return {string} The exported template function.
145148
*/
146149
export(precompiledTemplate, { data, hot }) {
147-
const runtimeFile = require.resolve('twig/twig.min.js');
150+
// fix windows-like path
151+
const runtimeFile = require.resolve('twig/twig.min.js').replace(/\\/g, '/');
148152
const exportFunctionName = 'templateFn';
149153
const exportCode = 'module.exports=';
150154
let loadDependencies = '';

src/Loader/Resolver.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ class Resolver {
285285
return value;
286286
}
287287

288-
// TODO: check on win
289-
//if (isWin) interpolatedValue = pathToPosix(interpolatedValue);
290-
291288
// remove quotes: '/path/to/file.js' -> /path/to/file.js
292289
let resolvedValue = interpolatedValue.slice(1, -1);
293290
let resolvedFile;
@@ -304,8 +301,6 @@ class Resolver {
304301
}
305302
if (isScript) resolvedFile = this.resolveScriptExtension(resolvedFile);
306303

307-
// TODO: check on win
308-
//return isWin ? pathToPosix(resolvedFile) : resolvedFile;
309304
return resolvedFile;
310305
}
311306

src/Loader/Template.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ class Template {
9292
* - mailto:admin@test.com
9393
* - `\\u0027 + require(\\u0027/resolved/path/to/file.ext\\u0027) + \\u0027` // an expression of resolved file via a template engine
9494
*
95+
* Allow special cases when value contains `:`
96+
* - C:\path\to\file.ext
97+
* - image.png?{size:600}
98+
*
9599
* @param {boolean} isBasedir Whether is used the `root` option.
96100
* @param {string} type The type of source: 'style', 'script', 'asset'.
97101
* @param {string} value The attribute value to resolve as an absolute file path.
@@ -108,7 +112,7 @@ class Template {
108112
value.startsWith('//') ||
109113
value.startsWith('#') ||
110114
value.startsWith('\\u0027') ||
111-
(value.indexOf(':') > 0 && value.indexOf('?{') < 0)
115+
(value.indexOf(':') > 0 && value.indexOf(':\\') < 0 && value.indexOf('?{') < 0)
112116
) {
113117
return false;
114118
}

0 commit comments

Comments
 (0)