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 mjs #341

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 44 additions & 6 deletions .babelrc-npm.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
{
"plugins": ["@babel/plugin-transform-flow-strip-types"],
"presets": [
[
"@babel/preset-env",
{ "modules": "commonjs", "targets": { "node": "12" } }
]
]
"env": {
"cjs": {
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "12"
}
}
]
],
"plugins": [
[
"./resources/add-extension-to-import-paths",
{
"extension": "js"
}
]
]
},
"mjs": {
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"targets": {
"node": "12"
}
}
]
],
"plugins": [
[
"./resources/add-extension-to-import-paths",
{
"extension": "mjs"
}
]
]
}
}
}
39 changes: 39 additions & 0 deletions resources/add-extension-to-import-paths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

/**
* Adds extension to all paths imported inside MJS files
*
* Transforms:
*
* import { foo } from './bar';
* export { foo } from './bar';
*
* to:
*
* import { foo } from './bar.mjs';
* export { foo } from './bar.mjs';
*
*/
module.exports = function addExtensionToImportPaths(context, { extension }) {
const { types } = context;

return {
visitor: {
ImportDeclaration: replaceImportPath,
ExportNamedDeclaration: replaceImportPath,
},
};

function replaceImportPath(path) {
// bail if the declaration doesn't have a source, e.g. "export { foo };"
if (!path.node.source) {
return;
}

const source = path.node.source.value;
if (source.startsWith('./') || source.startsWith('../')) {
const newSourceNode = types.stringLiteral(source + '.' + extension);
path.get('source').replaceWith(newSourceNode);
}
}
};
17 changes: 15 additions & 2 deletions resources/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ const path = require('path');
const assert = require('assert');

const babel = require('@babel/core');
const prettier = require('prettier');

const { readdirRecursive, showDirStats } = require('./utils');

const prettierConfig = JSON.parse(
fs.readFileSync(require.resolve('../.prettierrc'), 'utf-8'),
);

if (require.main === module) {
fs.rmSync('./npmDist', { recursive: true, force: true });
fs.mkdirSync('./npmDist');
Expand All @@ -20,10 +25,13 @@ if (require.main === module) {
fs.mkdirSync(path.dirname(destPath), { recursive: true });
if (filepath.endsWith('.js')) {
const flowBody = '// @flow strict\n' + fs.readFileSync(srcPath, 'utf-8');
fs.writeFileSync(destPath + '.flow', flowBody);
writeGeneratedFile(destPath + '.flow', flowBody);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saihaj Can you please extract prettifying output into separate PR?
It will help us to compare generated files before and after TS migration.


const cjs = babelBuild(srcPath, { envName: 'cjs' });
fs.writeFileSync(destPath, cjs);
writeGeneratedFile(destPath, cjs);

const mjs = babelBuild(srcPath, { envName: 'mjs' });
writeGeneratedFile(destPath.replace(/\.js$/, '.mjs'), mjs);
} else if (filepath.endsWith('.d.ts')) {
fs.copyFileSync(srcPath, destPath);
}
Expand Down Expand Up @@ -81,3 +89,8 @@ function buildPackageJSON() {

return packageJSON;
}

function writeGeneratedFile(filepath, body) {
const formatted = prettier.format(body, { filepath, ...prettierConfig });
fs.writeFileSync(filepath, formatted);
}