feat(node): allow executing esm compiled scripts (#10414)

This commit is contained in:
Jonathan Kolberg 2023-05-05 00:02:31 +02:00 committed by GitHub
parent dfae1bf35a
commit 1c791dbd8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import {
tmpProjPath,
uniq,
updateFile,
updateJson,
updateProjectConfig,
} from '@nx/e2e/utils';
import { exec, execSync } from 'child_process';
@ -230,6 +231,48 @@ describe('Node Applications', () => {
expect(err).toBeFalsy();
}
}, 120000);
it('should be able to run ESM applications', async () => {
const esmapp = uniq('esmapp');
runCLI(
`generate @nrwl/node:app ${esmapp} --linter=eslint --framework=none --bundler=webpack`
);
updateJson(`apps/${esmapp}/tsconfig.app.json`, (config) => {
config.module = 'esnext';
config.target = 'es2020';
return config;
});
updateProjectConfig(esmapp, (config) => {
config.targets.build.options.outputFileName = 'main.mjs';
config.targets.build.options.assets = [];
return config;
});
updateFile(
`apps/${esmapp}/webpack.config.js`,
`
const { composePlugins, withNx } = require('@nx/webpack');
module.exports = composePlugins(withNx(), (config) => {
config.experiments = {
...config.experiments,
outputModule: true,
topLevelAwait: true,
};
config.output = {
path: config.output.path,
chunkFormat: 'module',
library: { type: 'module' }
}
return config;
});
`
);
await runCLIAsync(`build ${esmapp}`);
const p = await runCommandUntil(`serve ${esmapp}`, (output) => {
return output.includes('Hello World');
});
p.kill();
}, 300000);
});
describe('Build Node apps', () => {

View File

@ -1,6 +1,8 @@
const Module = require('module');
const originalLoader = Module._load;
const dynamicImport = new Function('specifier', 'return import(specifier)');
const mappings = JSON.parse(process.env.NX_MAPPINGS);
const keys = Object.keys(mappings);
const fileToRun = process.env.NX_FILE_TO_RUN;
@ -17,4 +19,4 @@ Module._load = function (request, parent) {
}
};
require(fileToRun);
dynamicImport(fileToRun);