Skip to content

Commit

Permalink
fix: support ie11 (#144)
Browse files Browse the repository at this point in the history
* fix: ie11 build

* use fs-extra instead of move-file

Ref: #143
  • Loading branch information
jorisre authored Mar 30, 2021
1 parent fed2b27 commit 260448a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ module.exports = {
overrides: [
{
files: ['config/*.js'],
rules: { '@typescript-eslint/no-var-requires': 'off' },
rules: {
'@typescript-eslint/no-var-requires': 'off',
'no-console': 'off',
},
},
],
};
54 changes: 54 additions & 0 deletions config/build-ie11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const path = require('path');
const fs = require('fs');
const { move } = require('fs-extra');
const microbundle = require('microbundle');
const getResolversName = require('./get-resolvers-name');

const IE11_PATH = 'ie11';
const OUTPUT = 'dist';

(async () => {
console.log(`Build IE11 resolvers to ${OUTPUT}`);
const resolvers = getResolversName();

for await (let resolver of resolvers) {
console.log(`> Build ${resolver}`);
const filePath = path.join(resolver, 'package.json');

const resolverPkg = JSON.parse(fs.readFileSync(filePath));
const resolverPkgCopy = Object.assign({}, resolverPkg);

// Temporary update `types` field
resolverPkg.types = `${IE11_PATH}/index.d.ts`;

await fs.writeFileSync(filePath, JSON.stringify(resolverPkg, null, 2));

try {
await microbundle({
cwd: resolver,
output: IE11_PATH,
globals: '@hookform/resolvers=hookformResolvers',
format: 'cjs',
alias: 'react-hook-form=react-hook-form/dist/index.ie11',
});

// Move `./{resolver}/ie11` -> `./dist/ie11/{resolver}`
await move(
`${resolver}/${IE11_PATH}`,
`${OUTPUT}/${IE11_PATH}/${resolver}`,
{
overwrite: true,
},
);
} catch (error) {
console.error(error);
process.exit(1);
} finally {
// Revert back changes on `{resolver}/package.json`
await fs.writeFileSync(
filePath,
JSON.stringify(resolverPkgCopy, null, 2),
);
}
}
})();
18 changes: 18 additions & 0 deletions config/check-ie11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const getResolversName = require('./get-resolvers-name');

(async () => {
console.log('Checking IE11 bundles...');
const resolvers = getResolversName();

for await (const resolver of resolvers) {
console.log(`> Checking ${resolver} IE11 bundle`);
const file = fs.readFileSync(`dist/ie11/${resolver}/${resolver}.js`);

if (!file.includes('react-hook-form/dist/index.ie11')) {
throw new Error(
'IE11 bundle should require `react-hook-form/dist/index.ie11`',
);
}
}
})();
7 changes: 7 additions & 0 deletions config/get-resolvers-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const pkg = require('../package.json');

module.exports = function () {
return Object.keys(pkg.exports)
.map((e) => e.replace(/(\.\/|\.)/, '').replace(/package.*/, ''))
.filter(Boolean);
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@
"access": "public"
},
"scripts": {
"prepare": "run-s build:src build && check-export-map",
"prepare": "run-s build:src build && check-export-map && node ./config/check-ie11.js",
"build": "npm-run-all --parallel build:*",
"build:src": "microbundle build",
"build:zod": "microbundle --cwd zod --globals '@hookform/resolvers=hookformResolvers'",
"build:yup": "microbundle --cwd yup --globals '@hookform/resolvers=hookformResolvers'",
"build:joi": "microbundle --cwd joi --globals '@hookform/resolvers=hookformResolvers'",
"build:superstruct": "microbundle --cwd superstruct --globals '@hookform/resolvers=hookformResolvers'",
"build:vest": "microbundle --cwd vest --globals '@hookform/resolvers=hookformResolvers'",
"postbuild": "node ./config/node-13-exports.js",
"build:ie11": "node ./config/build-ie11",
"postbuild": "node ./config/node-13-exports.js && node ./config/check-ie11.js",
"lint": "eslint . --ext .ts,.js --ignore-path .gitignore",
"lint:types": "tsc",
"test": "jest",
Expand Down Expand Up @@ -117,6 +118,7 @@
"eslint": "^7.16.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"fs-extra": "^9.1.0",
"husky": "^4.3.6",
"jest": "^26.6.3",
"joi": "^17.3.0",
Expand Down Expand Up @@ -154,6 +156,5 @@
"*.{md,json,yml}": [
"prettier --write"
]
},
"dependencies": {}
}
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4483,6 +4483,16 @@ fs-extra@^9.0.0:
jsonfile "^6.0.1"
universalify "^1.0.0"

fs-extra@^9.1.0:
version "9.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ==
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-minipass@^1.2.5:
version "1.2.7"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7"
Expand Down

0 comments on commit 260448a

Please sign in to comment.