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

build(package): ensure NodeJS considers ESM .js files as ESM #7130

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 27 additions & 17 deletions tools/prepare-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,37 @@ const { cleanSourceMapRoot } = require('../.make-helpers');
const ROOT = 'dist/';
const CJS_ROOT = ROOT + 'cjs/';
const ESM5_ROOT = ROOT + 'esm5/';
const ESM2015_ROOT = ROOT + 'esm2015/';
const ESM_ROOT = ROOT + 'esm/';
const UMD_ROOT = ROOT + 'global/';
const ESM5_FOR_ROLLUP_ROOT = ROOT + 'esm5_for_rollup/';
const TYPE_ROOT = ROOT + 'types/';
const UMD_PKG = ROOT + 'bundles/';

// License info for minified files
let licenseUrl = 'https://github.com/ReactiveX/RxJS/blob/master/LICENSE.txt';
let license = 'Apache License 2.0 ' + licenseUrl;
const licenseUrl = 'https://github.com/ReactiveX/RxJS/blob/master/LICENSE.txt';
const license = 'Apache License 2.0 ' + licenseUrl;

// Execute build optimizer transforms on ESM5 files
klawSync(ESM5_ROOT, {
nodir: true,
filter: function (item) {
return item.path.endsWith('.js');
}
},
})
.map(item => item.path.slice((`${__dirname}/${ESM5_ROOT}`).length))
.map(fileName => {
if (!bo) return fileName;
let fullPath = path.resolve(__dirname, ESM5_ROOT, fileName);
.map((item) => item.path.slice(`${__dirname}/${ESM5_ROOT}`.length))
.map((fileName) => {
if (!bo) {
return fileName;
}
const fullPath = path.resolve(__dirname, ESM5_ROOT, fileName);
// The file won't exist when running build_test as we don't create the ESM5 sources
if (!fs.existsSync(fullPath)) return fileName;
let content = fs.readFileSync(fullPath).toString();
let transformed = bo.transformJavascript({
if (!fs.existsSync(fullPath)) {
return fileName;
}
const content = fs.readFileSync(fullPath).toString();
const transformed = bo.transformJavascript({
content: content,
getTransforms: [bo.getPrefixClassesTransformer, bo.getPrefixFunctionsTransformer, bo.getFoldFileTransformer]
getTransforms: [bo.getPrefixClassesTransformer, bo.getPrefixFunctionsTransformer, bo.getFoldFileTransformer],
});
fs.writeFileSync(fullPath, transformed.content);
return fileName;
Expand All @@ -44,10 +48,10 @@ if (fs.existsSync(UMD_ROOT)) {
fs.copySync(UMD_ROOT, UMD_PKG);
// Clean up source map paths so they can be re-mapped
klawSync(UMD_PKG, { filter: (item) => item.path.endsWith('.js.map') })
.map(f => f.path)
.forEach(fName => {
.map((f) => f.path)
.forEach((fName) => {
const sourceMap = fs.readJsonSync(fName);
sourceMap.sources = sourceMap.sources.map(s => {
sourceMap.sources = sourceMap.sources.map((s) => {
const nm = 'node_modules/';
const rr = path.resolve(ESM5_FOR_ROLLUP_ROOT);
if (s.includes(nm)) {
Expand All @@ -73,6 +77,12 @@ fs.removeSync(CJS_ROOT + '/internal/umd.js');
fs.removeSync(CJS_ROOT + '/internal/umd.js.map');
fs.removeSync(ESM5_ROOT + '/internal/umd.js');
fs.removeSync(ESM5_ROOT + '/internal/umd.js.map');
fs.removeSync(ESM2015_ROOT + '/internal/umd.js');
fs.removeSync(ESM2015_ROOT + '/internal/umd.js.map');
fs.removeSync(ESM_ROOT + '/internal/umd.js');
fs.removeSync(ESM_ROOT + '/internal/umd.js.map');
fs.removeSync(TYPE_ROOT + '/internal/umd.d.ts');

// Create `package.json` files for the ESM5 and ESM2015 roots that
// instruct NodeJS to treat `.js` files inside as ESM.
const esmPkgJson = JSON.stringify({ type: 'module', sideEffects: false });
fs.writeFileSync(path.join(ESM5_ROOT, 'package.json'), esmPkgJson);
fs.writeFileSync(path.join(ESM_ROOT, 'package.json'), esmPkgJson);