Skip to content

Commit

Permalink
Use esm package locally when testing to use ESM syntax from CJS code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
phillipj committed Mar 5, 2021
1 parent a93c39e commit d4a5042
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
8 changes: 8 additions & 0 deletions .esmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
cjs: {
// Ensure ESM `export default` ends up as the root, e.g. `module.exports` when
// being `require()`d from CJS code. This is not spec compliant, but that does
// not matter because only use this `esm` package trickery locally while testing
dedefault: true
}
}
47 changes: 36 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"build": "cp mustache.js mustache.mjs && rollup mustache.mjs --file mustache.js --format umd --name Mustache && uglifyjs mustache.js > mustache.min.js",
"test": "npm run test-lint && npm run test-unit",
"test-lint": "eslint mustache.js bin/mustache test/**/*.js",
"test-unit": "mocha --reporter spec test/*-test.js",
"test-unit": "mocha --reporter spec --require esm test/*-test.js",
"test-render": "mocha --reporter spec test/render-test",
"pre-test-browser": "node test/create-browser-suite.js",
"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",
Expand All @@ -41,6 +41,7 @@
"devDependencies": {
"chai": "^3.4.0",
"eslint": "^6.5.1",
"esm": "^3.2.25",
"jshint": "^2.9.5",
"mocha": "^3.0.2",
"puppeteer": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/cli-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ var cliPartialsTxt = path.resolve(_files, 'cli_with_partials.txt');
var moduleVersion = require('../package').version;

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

if (process.platform === 'win32') {
return command
.replace(/bin\/mustache/g, 'node bin\\mustache')
.replace(/\bcat\b/g, 'type')
.replace(/\//g, '\\');
}
Expand Down

0 comments on commit d4a5042

Please sign in to comment.