Skip to content

Commit d4a5042

Browse files
committed
Use esm package locally when testing to use ESM syntax from CJS code
These changes are primarily aiming for having an acceptable developer experience when working on changes locally and want to run the test suite. By using `esm` as a required package to be executed *before* the individual test files are executed, `esm` allows CommonJS/`require()` based code to use code written with ES Modules syntax. In practise that means our existing CommonJS based test suite that is `require()`ing the source code witten in ES Modules, is seamless and totally transparent. It just works without any build or transpiling pipeline like `babel` involved. Caveat: the `esm` package only support Node.js 6.x and above. That is more than okey, as we've got continous integration to verify how our changes works on different versions of Node.js, browsers & deno. Refs https://www.npmjs.com/package/esm
1 parent a93c39e commit d4a5042

4 files changed

Lines changed: 47 additions & 13 deletions

File tree

.esmrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
cjs: {
3+
// Ensure ESM `export default` ends up as the root, e.g. `module.exports` when
4+
// being `require()`d from CJS code. This is not spec compliant, but that does
5+
// not matter because only use this `esm` package trickery locally while testing
6+
dedefault: true
7+
}
8+
}

package-lock.json

Lines changed: 36 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"build": "cp mustache.js mustache.mjs && rollup mustache.mjs --file mustache.js --format umd --name Mustache && uglifyjs mustache.js > mustache.min.js",
3232
"test": "npm run test-lint && npm run test-unit",
3333
"test-lint": "eslint mustache.js bin/mustache test/**/*.js",
34-
"test-unit": "mocha --reporter spec test/*-test.js",
34+
"test-unit": "mocha --reporter spec --require esm test/*-test.js",
3535
"test-render": "mocha --reporter spec test/render-test",
3636
"pre-test-browser": "node test/create-browser-suite.js",
3737
"test-browser": "npm run pre-test-browser && zuul -- test/context-test.js test/parse-test.js test/scanner-test.js test/render-test-browser.js",
@@ -41,6 +41,7 @@
4141
"devDependencies": {
4242
"chai": "^3.4.0",
4343
"eslint": "^6.5.1",
44+
"esm": "^3.2.25",
4445
"jshint": "^2.9.5",
4546
"mocha": "^3.0.2",
4647
"puppeteer": "^2.0.0",

test/cli-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
99
var moduleVersion = require('../package').version;
1010

1111
function changeForOS (command) {
12+
command = command.replace('bin/mustache', 'node --require esm bin/mustache')
1213

1314
if (process.platform === 'win32') {
1415
return command
15-
.replace(/bin\/mustache/g, 'node bin\\mustache')
1616
.replace(/\bcat\b/g, 'type')
1717
.replace(/\//g, '\\');
1818
}

0 commit comments

Comments
 (0)