Skip to content

Commit

Permalink
feat(node): allow executing esm compiled scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
bulldog98 committed Jun 11, 2022
1 parent 9721844 commit 215ea0e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 41 additions & 0 deletions e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
tmpProjPath,
uniq,
updateFile,
updateJson,
updateProjectConfig,
} from '@nrwl/e2e/utils';
import { exec, execSync } from 'child_process';
Expand Down Expand Up @@ -255,6 +256,46 @@ describe('Node Applications', () => {
expect(err).toBeFalsy();
}
}, 120000);

it('should be able to run es module applications', async () => {
const esmapp = uniq('esmapp');

runCLI(`generate @nrwl/node:app ${esmapp} --linter=eslint`);
updateJson(`apps/${esmapp}/tsconfig.app.json`, (config) => {
config.module = 'esnext';
config.target = 'es2020';
return config;
});
updateProjectConfig(esmapp, (config) => {
config.targets.build.executor = '@nrwl/node:webpack';
config.targets.build.options.outputFileName = 'main.mjs';
config.targets.build.options.webpackConfig = `apps/${esmapp}/webpack.config.js`;
config.targets.serve.executor = '@nrwl/node:node';
config.targets.serve.options.watch = false;
return config;
});
updateFile(
`apps/${esmapp}/webpack.config.js`,
`module.exports = (config, context) => ({
...config,
experiments: {
...config.experiments,
outputModule: true,
topLevelAwait: true,
},
output: {
path: config.output.path,
chunkFormat: 'module',
library: {
type: 'module',
},
},
})`
);
await runCLIAsync(`build ${esmapp}`);
const { stdout } = await runCLIAsync(`serve ${esmapp}`);
expect(stdout).toBe('Hello World!');
}, 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);

0 comments on commit 215ea0e

Please sign in to comment.