feat(node): allow executing esm compiled scripts (#10414)
This commit is contained in:
parent
dfae1bf35a
commit
1c791dbd8e
@ -21,6 +21,7 @@ import {
|
|||||||
tmpProjPath,
|
tmpProjPath,
|
||||||
uniq,
|
uniq,
|
||||||
updateFile,
|
updateFile,
|
||||||
|
updateJson,
|
||||||
updateProjectConfig,
|
updateProjectConfig,
|
||||||
} from '@nx/e2e/utils';
|
} from '@nx/e2e/utils';
|
||||||
import { exec, execSync } from 'child_process';
|
import { exec, execSync } from 'child_process';
|
||||||
@ -230,6 +231,48 @@ describe('Node Applications', () => {
|
|||||||
expect(err).toBeFalsy();
|
expect(err).toBeFalsy();
|
||||||
}
|
}
|
||||||
}, 120000);
|
}, 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', () => {
|
describe('Build Node apps', () => {
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
const Module = require('module');
|
const Module = require('module');
|
||||||
const originalLoader = Module._load;
|
const originalLoader = Module._load;
|
||||||
|
|
||||||
|
const dynamicImport = new Function('specifier', 'return import(specifier)');
|
||||||
|
|
||||||
const mappings = JSON.parse(process.env.NX_MAPPINGS);
|
const mappings = JSON.parse(process.env.NX_MAPPINGS);
|
||||||
const keys = Object.keys(mappings);
|
const keys = Object.keys(mappings);
|
||||||
const fileToRun = process.env.NX_FILE_TO_RUN;
|
const fileToRun = process.env.NX_FILE_TO_RUN;
|
||||||
@ -17,4 +19,4 @@ Module._load = function (request, parent) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
require(fileToRun);
|
dynamicImport(fileToRun);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user