Skip to content

Commit c6c6bbb

Browse files
committed
Fix bundler issue with webpack 5
As explained in issue #1844 and in issue webpack/webpack#15007 (comment), the way we used the `browser`-field was wrong. The main reason for using the `browser`-field is the requirement of `require('fs')` in the main-entry-file. The workaround for this was using `require('handlebars/lib/handlebars')`, but now it will also work via `require('handlebars')` for bundlers that respect the `browser`-field. The `"./runtime"`-config was removed, because it didn't have any effect. In order to detect regressions, the webpack-integration test was extended to test with different webpack versions. Fixes #1174 Closes #1844
1 parent a0c5bb4 commit c6c6bbb

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@
7676
},
7777
"main": "lib/index.js",
7878
"types": "types/index.d.ts",
79-
"browser": {
80-
".": "./dist/cjs/handlebars.js",
81-
"./runtime": "./dist/cjs/handlebars.runtime.js"
82-
},
79+
"browser": "./dist/cjs/handlebars.js",
8380
"bin": {
8481
"handlebars": "bin/handlebars"
8582
},
+3-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
{
22
"name": "webpack-test",
3-
"description": "Various tests with Handlebars and Webpack",
3+
"description": "Various tests with Handlebars and multiple webpack versions",
44
"version": "1.0.0",
55
"main": "index.js",
66
"scripts": {
77
"build": "webpack --config webpack.config.js",
88
"test": "node dist/main.js"
99
},
1010
"private": true,
11-
"keywords": [],
12-
"author": "",
13-
"license": "ISC",
14-
"dependencies": {},
15-
"devDependencies": {
11+
"dependencies": {
1612
"handlebars": "file:../../..",
17-
"handlebars-loader": "^1.7.1",
18-
"webpack": "^4.39.3",
19-
"webpack-cli": "^3.3.7"
13+
"handlebars-loader": "^1.7.1"
2014
}
2115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Handlebars from 'handlebars/lib/handlebars';
2+
import { assertEquals } from './lib/assert';
3+
4+
const template = Handlebars.compile('Author: {{author}}');
5+
assertEquals(template({ author: 'Yehuda' }), 'Author: Yehuda');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import * as Handlebars from 'handlebars/runtime';
2+
import { assertEquals } from './lib/assert';
3+
4+
const template = Handlebars.template({
5+
compiler: [8, '>= 4.3.0'],
6+
main: function(container, depth0, helpers, partials, data) {
7+
var helper,
8+
lookupProperty =
9+
container.lookupProperty ||
10+
function(parent, propertyName) {
11+
if (Object.prototype.hasOwnProperty.call(parent, propertyName)) {
12+
return parent[propertyName];
13+
}
14+
return undefined;
15+
};
16+
17+
return (
18+
'Author: ' +
19+
container.escapeExpression(
20+
((helper =
21+
(helper =
22+
lookupProperty(helpers, 'author') ||
23+
(depth0 != null ? lookupProperty(depth0, 'author') : depth0)) !=
24+
null
25+
? helper
26+
: container.hooks.helperMissing),
27+
typeof helper === 'function'
28+
? helper.call(depth0 != null ? depth0 : container.nullContext || {}, {
29+
name: 'author',
30+
hash: {},
31+
data: data,
32+
loc: {
33+
start: { line: 1, column: 8 },
34+
end: { line: 1, column: 18 }
35+
}
36+
})
37+
: helper)
38+
)
39+
);
40+
},
41+
useData: true
42+
});
43+
assertEquals(template({ author: 'Yehuda' }), 'Author: Yehuda');

tests/integration/webpack-test/test.sh

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22

33
set -e
44

5+
run_tests () {
6+
for i in dist/*-test.js ; do
7+
echo "----------------------"
8+
echo "-- Running $i"
9+
echo "----------------------"
10+
node "$i"
11+
echo "Success"
12+
done
13+
}
14+
515
# Cleanup: package-lock and "npm ci" is not working with local dependencies
616
rm dist package-lock.json -rf
717
npm install --legacy-peer-deps
18+
19+
# Test with webpack 4
20+
npm install --legacy-peer-deps --no-save webpack@^4 webpack-cli@^3
821
npm run build
22+
run_tests
923

10-
for i in dist/*-test.js ; do
11-
echo "----------------------"
12-
echo "-- Running $i"
13-
echo "----------------------"
14-
node "$i"
15-
echo "Success"
16-
done
24+
# Test with webpack 5
25+
npm install --legacy-peer-deps --no-save webpack@^5 webpack-cli@^4
26+
npm run build
27+
run_tests

0 commit comments

Comments
 (0)