Skip to content

Commit

Permalink
[ESM] Detect modules with get-package-type (#201) (#274)
Browse files Browse the repository at this point in the history
* [ESM] Detect modules with get-package-type (#201)

* [ESM] Add test for ESM packages
  • Loading branch information
lehni committed Mar 22, 2022
1 parent a564a0c commit 6d58dd1
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 3 deletions.
10 changes: 9 additions & 1 deletion .eslintrc
Expand Up @@ -13,5 +13,13 @@
"no-trailing-spaces": "error",
"prefer-arrow-callback": "error",
"semi": "error"
}
},
"overrides": [
{
"files": ["./**/*.mjs"],
"parserOptions": {
"sourceType": "module"
}
}
]
}
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -2,6 +2,7 @@ const { fork } = require('child_process');
const filewatcher = require('filewatcher');
const { extname } = require('path');
const semver = require('semver');
const getPackageType = require('get-package-type');

const { clearFactory } = require('./clear');
const { configureDeps, configureIgnore } = require('./ignore');
Expand Down Expand Up @@ -94,7 +95,7 @@ module.exports = function (
isPaused = false;
const cmd = nodeArgs.concat(wrapper, script, scriptArgs);

if (extname(script) === '.mjs') {
if (extname(script) === '.mjs' || getPackageType.sync(script) === 'module') {
if (semver.satisfies(process.version, '>=10 <12.11.1')) {
const resolveLoader = resolveMain(localPath('resolve-loader.mjs'));
cmd.unshift('--experimental-modules', `--loader=${resolveLoader}`);
Expand Down
3 changes: 2 additions & 1 deletion lib/wrap.js
@@ -1,6 +1,7 @@
const { dirname, extname } = require('path');
const childProcess = require('child_process');
const { sync: resolve } = require('resolve');
const getPackageType = require('get-package-type');

const { getConfig } = require('./cfg');
const hook = require('./hook');
Expand Down Expand Up @@ -67,4 +68,4 @@ if (typeof mod === 'object' && mod.name) {
}

// Execute the wrapped script
ext === 'mjs' ? import(main) : require(main);
ext === 'mjs' || getPackageType.sync(main) === 'module' ? import(main) : require(main);
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"dateformat": "^3.0.3",
"dynamic-dedupe": "^0.3.0",
"filewatcher": "~3.0.0",
"get-package-type": "^0.1.0",
"minimist": "^1.2.5",
"node-notifier": "^8.0.1",
"resolve": "^1.0.0",
Expand Down
5 changes: 5 additions & 0 deletions test/fixture/ecma-script-module-package/.eslintrc
@@ -0,0 +1,5 @@
{
"parserOptions": {
"sourceType": "module"
}
}
3 changes: 3 additions & 0 deletions test/fixture/ecma-script-module-package/.node-dev.json
@@ -0,0 +1,3 @@
{
"extensions": {}
}
6 changes: 6 additions & 0 deletions test/fixture/ecma-script-module-package/index.js
@@ -0,0 +1,6 @@
import message from './message.js';

console.log(message);

// So it doesn't immediately exit.
setTimeout(() => {}, 10000);
1 change: 1 addition & 0 deletions test/fixture/ecma-script-module-package/message.js
@@ -0,0 +1 @@
export default 'Please touch ecma-script-module-package/message.js now';
3 changes: 3 additions & 0 deletions test/fixture/ecma-script-module-package/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
16 changes: 16 additions & 0 deletions test/spawn/esmodule.js
Expand Up @@ -35,3 +35,19 @@ tap.test('Supports ECMAScript modules', t => {
}
});
});

tap.test('Supports ECMAScript module packages', t => {
if (process.platform === 'win32') return t.skip('ESM support on windows is broken');

spawn('ecma-script-module-package/index.js', out => {
if (out.match(/touch ecma-script-module-package\/message.js/)) {
touchFile('ecma-script-module-package/message.js');
return out2 => {
if (out2.match(/Restarting/)) {
t.match(out2, /\[INFO\] \d{2}:\d{2}:\d{2} Restarting/);
return { exit: t.end.bind(t) };
}
};
}
});
});

0 comments on commit 6d58dd1

Please sign in to comment.