diff --git a/.babelrc b/.babelrc deleted file mode 100644 index 1717e30..0000000 --- a/.babelrc +++ /dev/null @@ -1,16 +0,0 @@ -{ - "presets": [ - [ - "@babel/preset-env", - { - "loose": true, - "targets": { - "node": 8 - } - } - ] - ], - "plugins": [ - "babel-plugin-add-module-exports" - ] -} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 77c334c..c643e2d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,7 +15,7 @@ jobs: - name: Use Node.js uses: actions/setup-node@v4 with: - node-version: 20.x + node-version: 22.x cache: yarn - name: Install run: yarn --prefer-offline --frozen-lockfile --non-interactive --silent diff --git a/babel.config.json b/babel.config.json new file mode 100644 index 0000000..b9a9f51 --- /dev/null +++ b/babel.config.json @@ -0,0 +1,5 @@ +{ + "presets": [ + ["@babel/preset-env", { "targets": { "node": 22 } }] + ] +} diff --git a/package.json b/package.json index e117446..f341dd2 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.6", "description": "A Babel preset that targets modern browsers by fixing engine bugs.", "main": "lib/index.js", + "type": "module", "license": "MIT", "scripts": { "start": "concurrently -r 'npm:watch:* -s'", @@ -43,6 +44,7 @@ } }, "jest": { + "runner": "jest-light-runner", "testEnvironment": "node", "roots": [ "src", @@ -64,19 +66,19 @@ "Jason Miller " ], "peerDependencies": { - "@babel/core": "^7.0.0-0 || ^8.0.0-0 <8.0.0" + "@babel/core": "^8.0.0" }, "devDependencies": { - "@babel/cli": "^7.7.0", - "@babel/core": "^7.7.2", - "@babel/helper-fixtures": "^7.6.3", - "@babel/helper-plugin-test-runner": "^7.14.5", - "@babel/plugin-transform-modules-commonjs": "^7.5.0", - "@babel/plugin-transform-react-jsx": "^7.7.0", - "@babel/preset-env": "^7.9.6", + "@babel/cli": "^8.0.1", + "@babel/core": "^8.0.1", + "@babel/helper-fixtures": "^8.0.1", + "@babel/helper-plugin-test-runner": "^8.0.0", + "@babel/plugin-transform-modules-commonjs": "^8.0.1", + "@babel/plugin-transform-react-jsx": "^8.0.1", + "@babel/preset-env": "^8.0.1", "acorn-jsx": "^5.0.1", "babel-eslint": "^10.0.3", - "babel-plugin-add-module-exports": "^1.0.2", + "babel-plugin-external-helpers": "npm:@babel/plugin-external-helpers@^8.0.1", "chalk": "^2.4.2", "concurrently": "^4.1.0", "eslint": "^6.6.0", @@ -86,21 +88,25 @@ "eslint-plugin-prettier": "^3.1.1", "gzip-size": "^5.1.1", "if-env": "^1.0.4", - "jest": "^24.8.0", + "jest": "^30.4.2", + "jest-light-runner": "^0.7.12", "karmatic": "^1.4.0", "prettier": "^1.19.1", "pretty-bytes": "^5.2.0", + "@rollup/plugin-babel": "^5.3.1", "rollup": "^1.16.3", - "rollup-plugin-babel": "^4.3.3", "rollup-plugin-node-resolve": "^5.2.0", "terser": "^4.0.2", "webpack": "^4.35.0" }, "dependencies": { - "@babel/helper-plugin-utils": "^7.0.0", - "@babel/plugin-proposal-unicode-property-regex": "^7.4.4", - "@babel/plugin-transform-dotall-regex": "^7.4.4", - "@babel/types": "^7.4.4", + "@babel/helper-plugin-utils": "^8.0.1", + "@babel/plugin-transform-unicode-property-regex": "^8.0.1", + "@babel/plugin-transform-dotall-regex": "^8.0.1", + "@babel/types": "^8.0.0", "esutils": "^2.0.2" + }, + "resolutions": { + "chokidar": "npm:@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3" } } diff --git a/src/index.js b/src/index.js index 8dd7bed..1ddf55d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,9 @@ -import path from "path"; import { declare } from "@babel/helper-plugin-utils"; +import { fileURLToPath } from "url"; + +// import.meta.resolve returns a file:// URL, but Babel resolves plugins with +// require.resolve when transforming synchronously, which only accepts paths. +const resolve = specifier => fileURLToPath(import.meta.resolve(specifier)); /** * @babel/preset-modules produces clean, minimal output for ES Modules-supporting browsers. @@ -7,24 +11,21 @@ import { declare } from "@babel/helper-plugin-utils"; * @param {boolean} [options.loose=false] Loose mode skips seldom-needed transforms that increase output size. */ export default declare((api, opts) => { - api.assertVersion(7); + api.assertVersion("^8.0.0"); const loose = opts.loose === true; return { plugins: [ - path.resolve(__dirname, "./plugins/transform-edge-default-parameters"), - path.resolve(__dirname, "./plugins/transform-tagged-template-caching"), - path.resolve(__dirname, "./plugins/transform-jsx-spread"), - path.resolve(__dirname, "./plugins/transform-safari-for-shadowing"), - path.resolve(__dirname, "./plugins/transform-safari-block-shadowing"), - path.resolve(__dirname, "./plugins/transform-async-arrows-in-class"), - !loose && - path.resolve(__dirname, "./plugins/transform-edge-function-name"), - - // Proposals - require.resolve("@babel/plugin-proposal-unicode-property-regex"), - require.resolve("@babel/plugin-transform-dotall-regex"), + resolve("./plugins/transform-edge-default-parameters/index.js"), + resolve("./plugins/transform-tagged-template-caching/index.js"), + resolve("./plugins/transform-jsx-spread/index.js"), + resolve("./plugins/transform-safari-for-shadowing/index.js"), + resolve("./plugins/transform-safari-block-shadowing/index.js"), + resolve("./plugins/transform-async-arrows-in-class/index.js"), + !loose && resolve("./plugins/transform-edge-function-name/index.js"), + resolve("@babel/plugin-transform-unicode-property-regex"), + resolve("@babel/plugin-transform-dotall-regex"), ].filter(Boolean), }; }); diff --git a/test/__snapshots__/index.test.js.snap b/test/__snapshots__/index.test.js.snap index b247b86..26c8720 100644 --- a/test/__snapshots__/index.test.js.snap +++ b/test/__snapshots__/index.test.js.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`@babel/preset-modules should pass a smoke test 1`] = ` "const foo = async (tag, { a: _a = 1 }, { data: { props: _props = 1 } }, ...children) => { diff --git a/test/_util.js b/test/_util.js index cab8ab3..3354776 100644 --- a/test/_util.js +++ b/test/_util.js @@ -1,7 +1,7 @@ import { promisify } from "util"; import fs, { mkdir } from "fs"; import path from "path"; -import { transform } from "@babel/core"; +import { transformSync } from "@babel/core"; import gzipSize from "gzip-size"; import terser from "terser"; import chalk from "chalk"; @@ -23,7 +23,7 @@ export function dent(...args) { } export function babel(code, config) { - return transform(code, { + return transformSync(code, { ...options, ...config, }) diff --git a/test/external/safari-await-negation.test.js b/test/external/safari-await-negation.test.js index 3c8a237..25f87d7 100644 --- a/test/external/safari-await-negation.test.js +++ b/test/external/safari-await-negation.test.js @@ -1,5 +1,5 @@ -import { transform } from "@babel/core"; -import preset from "../.."; +import { transformSync } from "@babel/core"; +import preset from "../../lib/index.js"; const CONFIG = { babelrc: false, @@ -10,7 +10,7 @@ const CONFIG = { }; function babel(code) { - return transform(code, CONFIG).code; + return transformSync(code, CONFIG).code; } // https://bugs.webkit.org/show_bug.cgi?id=176685 diff --git a/test/fixtures.test.js b/test/fixtures.test.js index 1b534b8..21a55ce 100644 --- a/test/fixtures.test.js +++ b/test/fixtures.test.js @@ -1,3 +1,3 @@ import runner from "@babel/helper-plugin-test-runner"; -runner(__dirname); +runner(import.meta.url); diff --git a/test/fixtures/transform-async-arrows-in-class/arguments/output.js b/test/fixtures/transform-async-arrows-in-class/arguments/output.js index 8f7a241..71af5f8 100644 --- a/test/fixtures/transform-async-arrows-in-class/arguments/output.js +++ b/test/fixtures/transform-async-arrows-in-class/arguments/output.js @@ -1,10 +1,8 @@ class Foo { constructor() { var _arguments = arguments; - this.x = async function () { return await _arguments[0]; }; } - } diff --git a/test/fixtures/transform-async-arrows-in-class/async-arrow/output.js b/test/fixtures/transform-async-arrows-in-class/async-arrow/output.js index c294f48..b49d3db 100644 --- a/test/fixtures/transform-async-arrows-in-class/async-arrow/output.js +++ b/test/fixtures/transform-async-arrows-in-class/async-arrow/output.js @@ -4,9 +4,7 @@ class Foo { return await 1; }; } - bar() { (async function () {})(); } - } diff --git a/test/fixtures/transform-async-arrows-in-class/callback/output.js b/test/fixtures/transform-async-arrows-in-class/callback/output.js index 1ef952b..5904835 100644 --- a/test/fixtures/transform-async-arrows-in-class/callback/output.js +++ b/test/fixtures/transform-async-arrows-in-class/callback/output.js @@ -4,5 +4,4 @@ class Foo { return await 1; }); } - } diff --git a/test/fixtures/transform-async-arrows-in-class/nested/output.js b/test/fixtures/transform-async-arrows-in-class/nested/output.js index fe0de4f..e5165ac 100644 --- a/test/fixtures/transform-async-arrows-in-class/nested/output.js +++ b/test/fixtures/transform-async-arrows-in-class/nested/output.js @@ -1,10 +1,8 @@ class Foo { constructor() { var _this = this; - this.x = () => async function () { return await _this; }; } - } diff --git a/test/fixtures/transform-async-arrows-in-class/non-async-arrow/output.js b/test/fixtures/transform-async-arrows-in-class/non-async-arrow/output.js index 948e3e1..2530805 100644 --- a/test/fixtures/transform-async-arrows-in-class/non-async-arrow/output.js +++ b/test/fixtures/transform-async-arrows-in-class/non-async-arrow/output.js @@ -2,5 +2,4 @@ class Foo { constructor() { this.x = () => {}; } - } diff --git a/test/fixtures/transform-async-arrows-in-class/this/output.js b/test/fixtures/transform-async-arrows-in-class/this/output.js index 7ed6b12..f7e1d61 100644 --- a/test/fixtures/transform-async-arrows-in-class/this/output.js +++ b/test/fixtures/transform-async-arrows-in-class/this/output.js @@ -1,10 +1,8 @@ class Foo { constructor() { var _this = this; - this.x = async function () { return await _this; }; } - } diff --git a/test/fixtures/transform-edge-function-name/shadowed-reassignment/output.js b/test/fixtures/transform-edge-function-name/shadowed-reassignment/output.js index deea74f..47df23e 100644 --- a/test/fixtures/transform-edge-function-name/shadowed-reassignment/output.js +++ b/test/fixtures/transform-edge-function-name/shadowed-reassignment/output.js @@ -1,17 +1,13 @@ const a = function a() { a; }; - const _b = function b() { _b = 1; }; - const _c = function c() { _c; }; - _c = 1; - const _d = function d() { return function () { return () => { @@ -19,11 +15,9 @@ const _d = function d() { }; }; }; - const _e = function e() { _e; }; - () => { _e = 1; }; diff --git a/test/fixtures/transform-jsx-spread-useSpread/leading/options.json b/test/fixtures/transform-jsx-spread-useSpread/leading/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/leading/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/leading/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/leading/output.js b/test/fixtures/transform-jsx-spread-useSpread/leading/output.js index 1d9dfc5..5e0a004 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/leading/output.js +++ b/test/fixtures/transform-jsx-spread-useSpread/leading/output.js @@ -1,3 +1,4 @@ -h("div", { ...props, +h("div", { + ...props, b: "hi" }, "a"); diff --git a/test/fixtures/transform-jsx-spread-useSpread/lone/options.json b/test/fixtures/transform-jsx-spread-useSpread/lone/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/lone/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/lone/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/middle/options.json b/test/fixtures/transform-jsx-spread-useSpread/middle/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/middle/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/middle/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/multiple/options.json b/test/fixtures/transform-jsx-spread-useSpread/multiple/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/multiple/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/multiple/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/multiple/output.js b/test/fixtures/transform-jsx-spread-useSpread/multiple/output.js index a30df7f..62d3d63 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/multiple/output.js +++ b/test/fixtures/transform-jsx-spread-useSpread/multiple/output.js @@ -1,3 +1,4 @@ -h("div", { ...a, +h("div", { + ...a, ...b }); diff --git a/test/fixtures/transform-jsx-spread-useSpread/nested/options.json b/test/fixtures/transform-jsx-spread-useSpread/nested/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/nested/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/nested/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/nested/output.js b/test/fixtures/transform-jsx-spread-useSpread/nested/output.js index 3a46017..4139027 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/nested/output.js +++ b/test/fixtures/transform-jsx-spread-useSpread/nested/output.js @@ -1,2 +1 @@ -h("div", { ...props -}); +h("div", props); diff --git a/test/fixtures/transform-jsx-spread-useSpread/none/options.json b/test/fixtures/transform-jsx-spread-useSpread/none/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/none/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/none/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread-useSpread/trailing/options.json b/test/fixtures/transform-jsx-spread-useSpread/trailing/options.json index a92c780..aa02a20 100644 --- a/test/fixtures/transform-jsx-spread-useSpread/trailing/options.json +++ b/test/fixtures/transform-jsx-spread-useSpread/trailing/options.json @@ -9,6 +9,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/leading/options.json b/test/fixtures/transform-jsx-spread/leading/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/leading/options.json +++ b/test/fixtures/transform-jsx-spread/leading/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/lone/options.json b/test/fixtures/transform-jsx-spread/lone/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/lone/options.json +++ b/test/fixtures/transform-jsx-spread/lone/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/middle/options.json b/test/fixtures/transform-jsx-spread/middle/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/middle/options.json +++ b/test/fixtures/transform-jsx-spread/middle/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/multiple/options.json b/test/fixtures/transform-jsx-spread/multiple/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/multiple/options.json +++ b/test/fixtures/transform-jsx-spread/multiple/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/nested/options.json b/test/fixtures/transform-jsx-spread/nested/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/nested/options.json +++ b/test/fixtures/transform-jsx-spread/nested/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/nested/output.js b/test/fixtures/transform-jsx-spread/nested/output.js index 3a46017..4139027 100644 --- a/test/fixtures/transform-jsx-spread/nested/output.js +++ b/test/fixtures/transform-jsx-spread/nested/output.js @@ -1,2 +1 @@ -h("div", { ...props -}); +h("div", props); diff --git a/test/fixtures/transform-jsx-spread/none/options.json b/test/fixtures/transform-jsx-spread/none/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/none/options.json +++ b/test/fixtures/transform-jsx-spread/none/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-jsx-spread/trailing/options.json b/test/fixtures/transform-jsx-spread/trailing/options.json index 639d65c..e4c0f63 100644 --- a/test/fixtures/transform-jsx-spread/trailing/options.json +++ b/test/fixtures/transform-jsx-spread/trailing/options.json @@ -4,6 +4,7 @@ [ "@babel/plugin-transform-react-jsx", { + "runtime": "classic", "pragma": "h" } ] diff --git a/test/fixtures/transform-safari-block-shadowing/shadowed-block/output.js b/test/fixtures/transform-safari-block-shadowing/shadowed-block/output.js index 145ae59..55aa6b8 100644 --- a/test/fixtures/transform-safari-block-shadowing/shadowed-block/output.js +++ b/test/fixtures/transform-safari-block-shadowing/shadowed-block/output.js @@ -7,23 +7,19 @@ a; let _c; } let c; - for (;;) { let _c2; } - () => { let c; { let _c3; - let d; { let _c4; } } }; - { const { a: _a2 = 1, @@ -34,7 +30,6 @@ for (;;) { { let d; } - (function () { try { { diff --git a/test/fixtures/transform-safari-block-shadowing/unshadowed/output.js b/test/fixtures/transform-safari-block-shadowing/unshadowed/output.js index a42f341..376e74c 100644 --- a/test/fixtures/transform-safari-block-shadowing/unshadowed/output.js +++ b/test/fixtures/transform-safari-block-shadowing/unshadowed/output.js @@ -1,20 +1,15 @@ const a = 1; - (() => { const a = 2; return a; })(); - let c; - () => { let c; - (() => { let c; })(); }; - (function () { let c; })(); diff --git a/test/fixtures/transform-safari-for-shadowing/shadowed-for/output.js b/test/fixtures/transform-safari-for-shadowing/shadowed-for/output.js index 0c31ff8..231c50a 100644 --- a/test/fixtures/transform-safari-for-shadowing/shadowed-for/output.js +++ b/test/fixtures/transform-safari-for-shadowing/shadowed-for/output.js @@ -1,21 +1,16 @@ const a = x => { for (let _x of {}) _x; }; - const b = x => { for (const _x2 in []) _x2; }; - const c = x => { for (let _x3 = 0;;) _x3; }; - const d = () => { let x; - for (let x of {}) x; }; - const e = x => { for (let _x4 of {}) { let x; diff --git a/test/fixtures/transform-safari-for-shadowing/unshadowed/output.js b/test/fixtures/transform-safari-for-shadowing/unshadowed/output.js index 0eaaaa3..e480c0f 100644 --- a/test/fixtures/transform-safari-for-shadowing/unshadowed/output.js +++ b/test/fixtures/transform-safari-for-shadowing/unshadowed/output.js @@ -1,19 +1,15 @@ const a = () => { for (let x of {}) x; }; - const b = () => { for (const x in []) x; }; - const c = () => { for (let x = 0;;) x; }; - const d = x => { for (let y of {}) y; }; - const e = x => { for (let y of {}) { let x; diff --git a/test/fixtures/transform-tagged-template-caching/block-scoped-tag/output.js b/test/fixtures/transform-tagged-template-caching/block-scoped-tag/output.js index f4be824..6b9246c 100644 --- a/test/fixtures/transform-tagged-template-caching/block-scoped-tag/output.js +++ b/test/fixtures/transform-tagged-template-caching/block-scoped-tag/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; for (let t of []) t(_t || (_t = _`a`)); diff --git a/test/fixtures/transform-tagged-template-caching/dynamic-expressions/output.js b/test/fixtures/transform-tagged-template-caching/dynamic-expressions/output.js index 899cf92..95eb3b6 100644 --- a/test/fixtures/transform-tagged-template-caching/dynamic-expressions/output.js +++ b/test/fixtures/transform-tagged-template-caching/dynamic-expressions/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; const f = t => t(_t || (_t = _`a${0}b${0}${0}`), 1, t, ["hello"]); diff --git a/test/fixtures/transform-tagged-template-caching/dynamic-tag/output.js b/test/fixtures/transform-tagged-template-caching/dynamic-tag/output.js index d027a8a..c4657ed 100644 --- a/test/fixtures/transform-tagged-template-caching/dynamic-tag/output.js +++ b/test/fixtures/transform-tagged-template-caching/dynamic-tag/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; fn()(_t || (_t = _``)); diff --git a/test/fixtures/transform-tagged-template-caching/function-scoped-tag/output.js b/test/fixtures/transform-tagged-template-caching/function-scoped-tag/output.js index 57e9650..59d637b 100644 --- a/test/fixtures/transform-tagged-template-caching/function-scoped-tag/output.js +++ b/test/fixtures/transform-tagged-template-caching/function-scoped-tag/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; const f = t => t(_t || (_t = _`a`)); diff --git a/test/fixtures/transform-tagged-template-caching/multiple-tags/output.js b/test/fixtures/transform-tagged-template-caching/multiple-tags/output.js index 9b7169a..1dc66ac 100644 --- a/test/fixtures/transform-tagged-template-caching/multiple-tags/output.js +++ b/test/fixtures/transform-tagged-template-caching/multiple-tags/output.js @@ -1,6 +1,5 @@ let _ = t => t, - _t, - _t2; - + _t, + _t2; t(_t || (_t = _`a`)); x(_t2 || (_t2 = _``)); diff --git a/test/fixtures/transform-tagged-template-caching/prevent-tag-collision/output.js b/test/fixtures/transform-tagged-template-caching/prevent-tag-collision/output.js index 2840b9b..d2dac7d 100644 --- a/test/fixtures/transform-tagged-template-caching/prevent-tag-collision/output.js +++ b/test/fixtures/transform-tagged-template-caching/prevent-tag-collision/output.js @@ -1,5 +1,4 @@ let _2 = t => t, - _t; - + _t; const _ = 1; t(_t || (_t = _2``)); diff --git a/test/fixtures/transform-tagged-template-caching/same-tag-safari-11/output.js b/test/fixtures/transform-tagged-template-caching/same-tag-safari-11/output.js index 2b950eb..52df381 100644 --- a/test/fixtures/transform-tagged-template-caching/same-tag-safari-11/output.js +++ b/test/fixtures/transform-tagged-template-caching/same-tag-safari-11/output.js @@ -1,5 +1,4 @@ let _ = t => t, - _t, - _t2; - + _t, + _t2; x(_t || (_t = _`a`)) === x(_t2 || (_t2 = _`a`)); diff --git a/test/fixtures/transform-tagged-template-caching/shared-strings-safari-11/output.js b/test/fixtures/transform-tagged-template-caching/shared-strings-safari-11/output.js index 79935eb..d2fb298 100644 --- a/test/fixtures/transform-tagged-template-caching/shared-strings-safari-11/output.js +++ b/test/fixtures/transform-tagged-template-caching/shared-strings-safari-11/output.js @@ -1,5 +1,4 @@ let _ = t => t, - _t, - _t2; - + _t, + _t2; x(_t || (_t = _`a`)) === y(_t2 || (_t2 = _`a`)); diff --git a/test/fixtures/transform-tagged-template-caching/single-tag-empty/output.js b/test/fixtures/transform-tagged-template-caching/single-tag-empty/output.js index bbf407a..71fd271 100644 --- a/test/fixtures/transform-tagged-template-caching/single-tag-empty/output.js +++ b/test/fixtures/transform-tagged-template-caching/single-tag-empty/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; x(_t || (_t = _``)); diff --git a/test/fixtures/transform-tagged-template-caching/single-tag/output.js b/test/fixtures/transform-tagged-template-caching/single-tag/output.js index 42d50f5..9322593 100644 --- a/test/fixtures/transform-tagged-template-caching/single-tag/output.js +++ b/test/fixtures/transform-tagged-template-caching/single-tag/output.js @@ -1,4 +1,3 @@ let _ = t => t, - _t; - + _t; t(_t || (_t = _`a`)); diff --git a/test/index.test.js b/test/index.test.js index ab076e5..a58cac8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,15 +1,19 @@ import path from "path"; +import { fileURLToPath } from "url"; import { babel, readdir, readFile, compressedSize, compareFixturesToPresetEnv, -} from "./_util"; +} from "./_util.js"; import chalk from "chalk"; -import preset from ".."; +import preset from "../lib/index.js"; -jest.setTimeout(30000); +const fixturesDir = fileURLToPath(import.meta.resolve("./fixtures/")); +const browserFixturesDir = fileURLToPath( + import.meta.resolve("./browser/fixtures/") +); function dedent(strings, ...expr) { const s = strings.reduce((acc, s, i) => acc + expr[i - 1] + s); @@ -22,7 +26,7 @@ function babelPretty(code) { } describe("@babel/preset-modules", () => { - it("should assert Babel 7", () => { + it("should assert Babel 8", () => { expect(preset).toEqual(expect.any(Function)); const assertVersion = jest.fn(); preset( @@ -31,7 +35,7 @@ describe("@babel/preset-modules", () => { }, {} ); - expect(assertVersion).toHaveBeenCalledWith(7); + expect(assertVersion).toHaveBeenCalledWith("^8.0.0"); }); it("should return a configuration object with plugins", () => { @@ -61,9 +65,9 @@ describe("@babel/preset-modules", () => { }); it("should produce minimal output", async () => { - for (const f of await readdir(path.join(__dirname, "fixtures"))) { + for (const f of await readdir(fixturesDir)) { if (!/\.js$/.test(f)) continue; - const file = await readFile(path.join(__dirname, "fixtures", f), "utf-8"); + const file = await readFile(path.join(fixturesDir, f), "utf-8"); const out = babel(file, { presets: [preset] }); const sizeBefore = await compressedSize(file); const size = await compressedSize(out); @@ -89,70 +93,36 @@ describe("@babel/preset-modules", () => { }); it("should be smaller than preset-env default", async () => { - const out = await compareFixturesToPresetEnv( - path.join(__dirname, "browser/fixtures"), - { - compile: ({ file }) => babel(file, { presets: [preset] }), - compileEnv: ({ file }) => - babel(file, { presets: ["@babel/preset-env"] }), - assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv), - } - ); + const out = await compareFixturesToPresetEnv(browserFixturesDir, { + compile: ({ file }) => babel(file, { presets: [preset] }), + compileEnv: ({ file }) => babel(file, { presets: ["@babel/preset-env"] }), + assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv), + }); console.log( chalk.blueBright(`Compared to @babel/preset-env's default output:`) + out ); }); it("should be smaller than preset-env targets.esmodules", async () => { - const out = await compareFixturesToPresetEnv( - path.join(__dirname, "browser/fixtures"), - { - compile: ({ file }) => babel(file, { presets: [preset] }), - compileEnv: ({ file }) => - babel(file, { - presets: [ - [ - "@babel/preset-env", - { - targets: { esmodules: true }, - }, - ], + const out = await compareFixturesToPresetEnv(browserFixturesDir, { + compile: ({ file }) => babel(file, { presets: [preset] }), + compileEnv: ({ file }) => + babel(file, { + presets: [ + [ + "@babel/preset-env", + { + targets: { esmodules: true }, + }, ], - }), - assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv), - } - ); + ], + }), + assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv), + }); console.log( chalk.blueBright( `Compared to @babel/preset-env's targets.esmodules output:` ) + out ); }); - - it("should be smaller than preset-env targets.esmodules in loose mode", async () => { - const out = await compareFixturesToPresetEnv( - path.join(__dirname, "browser/fixtures"), - { - compile: ({ file }) => babel(file, { presets: [preset] }), - compileEnv: ({ file }) => - babel(file, { - presets: [ - [ - "@babel/preset-env", - { - targets: { esmodules: true }, - loose: true, - }, - ], - ], - }), - assertion: ({ size, sizeEnv }) => expect(size).toBeLessThan(sizeEnv), - } - ); - console.log( - chalk.blueBright( - `Compared to @babel/preset-env's targets.esmodules ${chalk.bold`loose`} output:` - ) + out - ); - }); }); diff --git a/test/integration.test.js b/test/integration.test.js deleted file mode 100644 index 9335a86..0000000 --- a/test/integration.test.js +++ /dev/null @@ -1,127 +0,0 @@ -import path from "path"; -import { readdirSync } from "fs"; -import { rollup } from "rollup"; -import terser from "terser"; -import babel from "rollup-plugin-babel"; -import nodeResolve from "rollup-plugin-node-resolve"; -import chalk from "chalk"; -import bytes from "pretty-bytes"; -import preset from ".."; -import { compressedSize, writeFile, mkdirp } from "./_util"; - -jest.setTimeout(30000); - -const FIXTURES = path.resolve(__dirname, "integration"); - -async function writeBeautified(filename, code) { - await writeFile( - filename, - terser.minify(code, { - mangle: false, - compress: false, - sourceMap: false, - output: { beautify: true }, - }).code, - () => {} - ); -} - -const IGNORE_WARNINGS = [ - "UNRESOLVED_IMPORT", - "MISSING_GLOBAL_NAME", - "UNUSED_EXTERNAL_IMPORT", -]; - -async function compile(dir, babelOptions) { - let pkg = {}; - try { - pkg = require(path.resolve(dir, "package.json")); - } catch (e) {} - const bundle = await rollup({ - context: dir, - input: path.resolve(dir, pkg.main || "index.js"), - plugins: [ - nodeResolve({ - jail: dir, - }), - babelOptions !== false && - babel({ - babelrc: false, - ...babelOptions, - }), - ].filter(Boolean), - onwarn(warning, warn) { - if (IGNORE_WARNINGS.indexOf(warning.code) === -1) warn(warning); - }, - }); - const { output } = await bundle.generate({ - format: "iife", - }); - return output[0].code; -} - -describe("integration", () => { - let summary = ""; - - for (const f of readdirSync(FIXTURES)) { - if (f[0] === ".") continue; - it("should compile " + f + " to less code than preset-env", async () => { - const context = path.resolve(FIXTURES, f); - const before = await compile(context, { - presets: ["@babel/preset-env"], - plugins: ["@babel/plugin-transform-react-jsx"], - }); - const out = await compile(context, { - presets: [preset], - plugins: ["@babel/plugin-transform-react-jsx"], - }); - const outEnv = await compile(context, { - presets: [["@babel/preset-env", { targets: { esmodules: true } }]], - plugins: ["@babel/plugin-transform-react-jsx"], - }); - - // write to disk for easy inspection: (ignore write errors in CI) - try { - mkdirp(path.join(context, ".dist")); - writeBeautified(path.join(context, ".dist/preset-modules.js"), out); - writeBeautified(path.join(context, ".dist/preset-env.js"), outEnv); - } catch (e) {} - - const sizeBefore = await compressedSize(before, { - module: false, - }); - - const size = await compressedSize(out, { module: false }); - - const sizeEnv = await compressedSize(outEnv, { - module: false, - }); - - summary += `\n${f}: ${bytes(size)}`; - summary += - chalk.dim( - `\n → ${sizeEnv - - size}b smaller than preset-env's targets.esmodules (` - ) + - chalk.green(`${(((sizeEnv - size) / sizeEnv) * 100) | 0}% savings`) + - chalk.dim(`)`); - summary += - chalk.dim( - `\n → ${sizeBefore - size}b smaller than preset-env's default (` - ) + - chalk.green( - `${(((sizeBefore - size) / sizeBefore) * 100) | 0}% savings` - ) + - chalk.dim(`)`); - expect(size).toBeLessThan(sizeEnv); - }); - } - - afterAll(() => { - console.log( - chalk.blueBright( - `Compared to @babel/preset-env's targets.esmodules output:` - ) + summary - ); - }); -}); diff --git a/test/integration/react-hooks-demo/Button.js b/test/integration/react-hooks-demo/Button.js deleted file mode 100755 index 8d6ec98..0000000 --- a/test/integration/react-hooks-demo/Button.js +++ /dev/null @@ -1,87 +0,0 @@ -import React, { useState, useEffect, useRef } from "react"; -import styled, { keyframes } from "styled-components"; - -function Button(props) { - const button = useRef(); - const [clicked, setClicked] = useState(false); - - function animate(e) { - var d = document.createElement("div"); - d.className = "circle"; - const x = e.nativeEvent.offsetX; - const y = e.nativeEvent.offsetY; - d.style.left = `${x}px`; - d.style.top = `${y}px`; - button.current.appendChild(d); - d.addEventListener("animationend", function() { - d.parentElement.removeChild(d); - }); - } - - return ( - - ); -} - -export default styled(Button)` - overflow: hidden; - position: relative; - display: block; - font-weight: bold; - width: fit-content; - height: fit-content; - color: #ffffff; - cursor: pointer; - padding: 6px 16px; - border: none; - border-radius: 4px; - background: #4f9eed; - transition: 0.1s ease 0s; - - -webkit-touch-callout: none; /* iOS Safari */ - -webkit-user-select: none; /* Safari */ - -khtml-user-select: none; /* Konqueror HTML */ - -moz-user-select: none; /* Firefox */ - -ms-user-select: none; /* Internet Explorer/Edge */ - user-select: none; /* Non-prefixed version, currently - supported by Chrome and Opera */ - - &:hover { - background: #2e78c2; - } - - .circle { - position: absolute; -   box-sizing: border-box; - background: white; -   border-radius: 50%; -   animation: clickEffect 0.4s ease-out; -   z-index: 99999; - } - - @keyframes clickEffect{ -   0% { -     opacity: 1; -     width: 0.5em; - height: 0.5em; -     margin: -0.25em; -     border-width: 0.5em; -   } - -   100% { - opacity: 0; - width: 15em; - height: 15em; - margin: -7.5em; - border-width: 0.03em; - } - } -`; diff --git a/test/integration/react-hooks-demo/Card.js b/test/integration/react-hooks-demo/Card.js deleted file mode 100755 index d64d8e4..0000000 --- a/test/integration/react-hooks-demo/Card.js +++ /dev/null @@ -1,20 +0,0 @@ -import React, { useState, useContext } from "react"; -import styled from "styled-components"; - -import ThemeContext from "./context"; - -function Card(props) { - const theme = useContext(ThemeContext); - return ( -
- {props.children} -
- ); -} - -export default styled(Card)` - text-align: left; - padding: 24px; - background: #1d252c; - border-radius: 8px; -`; diff --git a/test/integration/react-hooks-demo/Comments.js b/test/integration/react-hooks-demo/Comments.js deleted file mode 100755 index e806963..0000000 --- a/test/integration/react-hooks-demo/Comments.js +++ /dev/null @@ -1,519 +0,0 @@ -import React, { useState, useEffect, useContext, createContext } from "react"; -import styled from "styled-components"; - -import TextArea from "react-textarea-autosize"; - -import Markdown from "./Markdown"; -import Card from "./Card"; -import Button from "./Button"; - -const CommentContext = createContext({}); - -function compare(a1, a2) { - if (JSON.stringify(a1) === JSON.stringify(a2)) { - return true; - } - return false; -} - -function gen_comments(comments, colorindex, path) { - return comments.map((comment, i) => { - return ( - - ); - }); -} - -function Reply(props) { - const [text, setText] = useState(""); - return ( -
-