Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
tmpProjPath,
uniq,
updateFile,
updateJson,
updateProjectConfig,
} from '@nx/e2e/utils';
import { exec, execSync } from 'child_process';
Expand Down Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,4 +19,4 @@ Module._load = function (request, parent) {
}
};

require(fileToRun);
dynamicImport(fileToRun);