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: ESNext by default, full ESM-CJS interop, max minification #1059

Merged
merged 17 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 16 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ export const sum = (a: number, b: number) => {
if (process.env.NODE_ENV === 'production') {
module.exports = require('./mylib.cjs.production.js');
} else {
module.exports = require('./mylib.cjs.development.js');
module.exports = require('./mylib.development.cjs');
}
```

```js
// CommonJS Development Build
// ./dist/mylib.cjs.development.js
// ./dist/mylib.development.cjs
'use strict';

const sum = (a, b) => {
Expand All @@ -158,7 +158,7 @@ const sum = (a, b) => {
};

exports.sum = sum;
//# sourceMappingURL=mylib.cjs.development.js.map
//# sourceMappingURL=mylib.development.cjs.map
```

```js
Expand Down
72 changes: 36 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"templates"
],
"engines": {
"node": ">=10"
"node": ">=14"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Logic assumes Node >14.

},
"dependencies": {
"@babel/core": "^7.4.4",
Expand All @@ -48,62 +48,62 @@
"@babel/preset-env": "^7.11.0",
"@babel/traverse": "^7.11.5",
"@rollup/plugin-babel": "^5.1.0",
"@rollup/plugin-commonjs": "^11.0.0",
"@rollup/plugin-commonjs": "^19.0.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-replace": "^2.2.1",
"@types/jest": "^25.2.1",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"@types/jest": "^26.0.24",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"ansi-escapes": "^4.2.1",
"asyncro": "^3.0.0",
"babel-eslint": "^10.0.3",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"babel-plugin-dev-expression": "^0.2.1",
"babel-plugin-macros": "^2.6.1",
"babel-plugin-polyfill-regenerator": "^0.0.4",
"babel-plugin-macros": "^3.1.0",
"babel-plugin-polyfill-regenerator": "^0.2.2",
"babel-plugin-transform-rename-import": "^2.3.0",
"camelcase": "^6.0.0",
"chalk": "^4.0.0",
"enquirer": "^2.3.4",
"eslint": "^6.1.0",
"eslint": "^7.30.0",
"eslint-config-prettier": "^6.0.0",
"eslint-config-react-app": "^5.2.1",
"eslint-plugin-flowtype": "^3.13.0",
"eslint-config-react-app": "^6.0.0",
"eslint-plugin-flowtype": "^5.8.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-react": "^7.14.3",
"eslint-plugin-react-hooks": "^2.2.0",
"execa": "^4.0.3",
"fs-extra": "^9.0.0",
"jest": "^25.3.0",
"jest-watch-typeahead": "^0.5.0",
"eslint-plugin-react-hooks": "^4.2.0",
"execa": "^5.1.1",
"fs-extra": "^10.0.0",
"jest": "^27.0.6",
"jest-watch-typeahead": "^0.6.4",
"jpjs": "^1.2.1",
"lodash.merge": "^4.6.2",
"ora": "^4.0.3",
"ora": "^5.4.1",
"pascal-case": "^3.1.1",
"prettier": "^1.19.1",
"progress-estimator": "^0.2.2",
"prettier": "^2.3.2",
"progress-estimator": "^0.3.0",
"regenerator-runtime": "^0.13.7",
"rollup": "^1.32.1",
"rollup": "^2.52.8",
"rollup-plugin-sourcemaps": "^0.6.2",
"rollup-plugin-terser": "^5.1.2",
"rollup-plugin-typescript2": "^0.27.3",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"sade": "^1.4.2",
"semver": "^7.1.1",
"shelljs": "^0.8.3",
"tiny-glob": "^0.2.6",
"ts-jest": "^25.3.1",
"tslib": "^1.9.3",
"typescript": "^3.7.3"
"ts-jest": "^27.0.3",
"tslib": "^2.3.0",
"typescript": "^4.3.5"
},
"devDependencies": {
"@types/eslint": "^6.1.2",
"@types/eslint": "^7.2.14",
"@types/fs-extra": "^9.0.1",
"@types/lodash": "^4.14.161",
"@types/node": "^14.11.1",
"@types/react": "^16.9.11",
"@types/node": "^16.0.1",
"@types/react": "^17.0.14",
"@types/rollup-plugin-json": "^3.0.2",
"@types/sade": "^1.6.0",
"@types/semver": "^7.1.0",
Expand All @@ -112,18 +112,18 @@
"autoprefixer": "^9.7.4",
"babel-plugin-replace-identifiers": "^0.1.1",
"cssnano": "^4.1.10",
"doctoc": "^1.4.0",
"husky": "^4.2.2",
"np": "^6.4.0",
"pretty-quick": "^2.0.0",
"react": "^16.8.6",
"react-dom": "^16.13.0",
"react-is": "^16.13.0",
"doctoc": "^2.0.1",
"husky": "^7.0.1",
"np": "^7.5.0",
"pretty-quick": "^3.1.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-is": "^17.0.2",
"rollup-plugin-postcss": "^2.5.0",
"styled-components": "^5.0.1",
"tiny-invariant": "^1.1.0",
"tiny-warning": "^1.0.3",
"yarn-deduplicate": "^2.1.1"
"yarn-deduplicate": "^3.1.0"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Package updates, do not cause test failures.

},
"husky": {
"hooks": {
Expand Down
75 changes: 51 additions & 24 deletions src/createRollupConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ export async function createRollupConfig(
...opts,
});

const isEsm = opts.format.includes('es') || opts.format.includes('esm');

const shouldMinify =
opts.minify !== undefined ? opts.minify : opts.env === 'production';
opts.minify !== undefined ? opts.minify : opts.env === 'production' || isEsm;
Comment on lines +36 to +39
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Minify ESM output also.


let formatString = ['esm', 'cjs'].includes(opts.format) ? '' : opts.format;
let fileExtension = opts.format === 'esm' ? 'mjs' : 'cjs';

const outputName = [
`${paths.appDist}/${safePackageName(opts.name)}`,
opts.format,
formatString,
opts.env,
shouldMinify ? 'min' : '',
'js',
fileExtension,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Encode ESM/CJS module status as *.mjs and *.cjs instead of *.esm.js etc.

]
.filter(Boolean)
.join('.');
Expand Down Expand Up @@ -102,14 +107,18 @@ export async function createRollupConfig(
esModule: Boolean(tsCompilerOptions?.esModuleInterop),
name: opts.name || safeVariableName(opts.name),
sourcemap: true,
globals: { react: 'React', 'react-native': 'ReactNative' },
globals: { react: 'React', 'react-native': 'ReactNative', 'lodash-es': 'lodashEs', 'lodash/fp': 'lodashFp' },
jaredpalmer marked this conversation as resolved.
Show resolved Hide resolved
exports: 'named',
},
plugins: [
!!opts.extractErrors && {
async transform(source: any) {
await findAndRecordErrorCodes(source);
return source;
async transform(code: string) {
try {
await findAndRecordErrorCodes(code);
} catch (e) {
return null;
}
return { code, map: null };
},
},
resolve({
Expand All @@ -118,7 +127,7 @@ export async function createRollupConfig(
'main',
opts.target !== 'node' ? 'browser' : undefined,
].filter(Boolean) as string[],
extensions: [...RESOLVE_DEFAULTS.extensions, '.jsx'],
extensions: [...RESOLVE_DEFAULTS.extensions, '.cjs', '.mjs', '.jsx'],
}),
// all bundled external modules need to be converted from CJS to ESM
commonjs({
Expand Down Expand Up @@ -189,30 +198,48 @@ export async function createRollupConfig(
extensions: [...DEFAULT_BABEL_EXTENSIONS, 'ts', 'tsx'],
passPerPreset: true,
custom: {
targets: opts.target === 'node' ? { node: '10' } : undefined,
targets: opts.target === 'node' ? { node: '14' } : undefined,
extractErrors: opts.extractErrors,
format: opts.format,
},
babelHelpers: 'bundled',
}),
opts.env !== undefined &&
replace({
'process.env.NODE_ENV': JSON.stringify(opts.env),
}),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(opts.env),
}),
sourceMaps(),
shouldMinify &&
terser({
sourcemap: true,
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
passes: 10,
},
ecma: 5,
toplevel: opts.format === 'cjs',
warnings: true,
}),
terser({
output: { comments: false },
compress: {
keep_infinity: true,
pure_getters: true,
passes: 10,
},
ecma: opts.legacy ? 5 : 2020,
module: isEsm,
toplevel: opts.format === 'cjs' || isEsm,
warnings: true,
}),
/**
* Ensure there's an empty default export to prevent runtime errors.
*
* @see https://www.npmjs.com/package/rollup-plugin-export-default
*/
{
renderChunk: async (code: string, chunk: any) => {
if (chunk.exports.includes('default') || !isEsm) {
return null;
}

return {
code: `${code}\nexport default {};`,
map: null,
};
},
},
],
};
}
15 changes: 9 additions & 6 deletions src/errors/extractErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,26 @@ const babelParserOptions: ParserOptions = {
} as ParserOptions; // workaround for trailingFunctionCommas syntax

export async function extractErrors(opts: any) {
if (!opts || !('errorMapFilePath' in opts)) {
if (!opts || !opts.errorMapFilePath) {
throw new Error(
'Missing options. Ensure you pass an object with `errorMapFilePath`.'
);
}

if (!opts.name || !('name' in opts)) {
if (!opts.name || !opts.name) {
throw new Error('Missing options. Ensure you pass --name flag to tsdx');
}

const errorMapFilePath = opts.errorMapFilePath;
let existingErrorMap: any;
try {
// Using `fs.readFile` instead of `require` here, because `require()`
// calls are cached, and the cache map is not properly invalidated after
// file changes.
existingErrorMap = JSON.parse(await fs.readFile(errorMapFilePath, 'utf8'));
/**
* Using `fs.readFile` instead of `require` here, because `require()` calls
* are cached, and the cache map is not properly invalidated after file
* changes.
*/
const fileContents = await fs.readFile(errorMapFilePath, 'utf-8');
existingErrorMap = JSON.parse(fileContents);
} catch (e) {
existingErrorMap = {};
}
Expand Down
Loading