From 1007d3ec242328c90bd84d1a75d02540f88a26b1 Mon Sep 17 00:00:00 2001 From: Orta Date: Mon, 5 Oct 2020 15:08:15 -0400 Subject: [PATCH] Address feedback, and adds more tests --- .github/workflows/CI.yml | 83 ++++++++++++------- .npmignore | 1 + .npmrc | 1 + test/.gitignore | 4 +- test/cjs/index.js | 9 +- test/esm-node-native/index.js | 7 +- test/rollup-modules/index.js | 7 +- test/rollup-modules/package.json | 2 +- test/rollup-modules/rollup.config.js | 2 +- test/snowpack-modules/index.js | 7 +- .../index.js | 23 +++++ .../package.json | 6 ++ test/webpack-modules/package.json | 2 +- test/webpack-modules/webpack.config.js | 13 ++- 14 files changed, 104 insertions(+), 63 deletions(-) create mode 100644 .npmrc create mode 100644 test/validateModuleExportsMatchCommonJS/index.js create mode 100644 test/validateModuleExportsMatchCommonJS/package.json diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 90a1635..58c9fdd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,41 +1,64 @@ name: CI -on: pull_request + +on: + push: + branches: + - master + - release-* + pull_request: + branches: + - master + - release-* jobs: - test: + ci: runs-on: ubuntu-latest + strategy: + matrix: + node-version: [10.x, 12.x, 14.x] + steps: - - uses: actions/checkout@v1 - - uses: actions/setup-node@v1 - with: - node-version: "14.x" - - name: Setup Testing Infra - run: | - cd test - npm install + - uses: actions/checkout@v2 + + - name: Use node version ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: Setup Testing Infra + run: | + cd test + npm install - - name: "CommonJS Test" - run: | - cd test/cjs - npm run test + - name: "CommonJS Test" + run: | + cd test/cjs + npm run test - - name: "ES Modules Test" - run: | - cd test/esm-node-native - npm run test + - name: "ES Modules Test" + run: | + cd test/esm-node-native + npm run test + if: ${{ matrix.node-version == "14.x" }} - - name: "Rollup Tree-shaking Test" - run: | - cd test/rollup-modules - npm run test + - name: "Validate ES Modules == CommonJS" + run: | + cd test/validateModuleExportsMatchCommonJS + npm run test + if: ${{ matrix.node-version == "14.x" }} - - name: "Webpack Tree-shaking Test" - run: | - cd test/webpack-modules - npm run test + - name: "Rollup Tree-shaking Test" + run: | + cd test/rollup-modules + npm run test - - name: "Snowpack Tree-shaking Test" - run: | - cd test/snowpack-modules - npm run test + - name: "Webpack Tree-shaking Test" + run: | + cd test/webpack-modules + npm run test + + - name: "Snowpack Tree-shaking Test" + run: | + cd test/snowpack-modules + npm run test diff --git a/.npmignore b/.npmignore index 5cd7269..00d2841 100644 --- a/.npmignore +++ b/.npmignore @@ -3,3 +3,4 @@ bower.json docs test +.npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/test/.gitignore b/test/.gitignore index 665d366..66f4b38 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,4 +1,2 @@ package-lock.json -rollup-modules/output -webpack-modules/dist -snowpack-modules/build +*/build diff --git a/test/cjs/index.js b/test/cjs/index.js index dac3714..761ae43 100644 --- a/test/cjs/index.js +++ b/test/cjs/index.js @@ -1,7 +1,2 @@ -const { __awaiter } = require("tslib"); - -const testFunction = (textToPrint) => __awaiter(void 0, void 0, void 0, function* () { - console.log(`State: ${textToPrint}`); -}); - -testFunction("Works") +const tslib = require("tslib"); +if (typeof tslib.__awaiter !== "function") throw new Error("Missing expected helper __awaiter"); diff --git a/test/esm-node-native/index.js b/test/esm-node-native/index.js index 9a3e965..a147352 100644 --- a/test/esm-node-native/index.js +++ b/test/esm-node-native/index.js @@ -1,7 +1,2 @@ import { __awaiter } from "tslib"; - -export const testFunction = (textToPrint) => __awaiter(void 0, void 0, void 0, function* () { - console.log(`State: ${textToPrint}`); -}); - -testFunction("Works") +if (typeof __awaiter !== "function") throw new Error("Missing expected helper __awaiter"); diff --git a/test/rollup-modules/index.js b/test/rollup-modules/index.js index 9a3e965..a147352 100644 --- a/test/rollup-modules/index.js +++ b/test/rollup-modules/index.js @@ -1,7 +1,2 @@ import { __awaiter } from "tslib"; - -export const testFunction = (textToPrint) => __awaiter(void 0, void 0, void 0, function* () { - console.log(`State: ${textToPrint}`); -}); - -testFunction("Works") +if (typeof __awaiter !== "function") throw new Error("Missing expected helper __awaiter"); diff --git a/test/rollup-modules/package.json b/test/rollup-modules/package.json index 37aee62..7a9215a 100644 --- a/test/rollup-modules/package.json +++ b/test/rollup-modules/package.json @@ -1,5 +1,5 @@ { "scripts": { - "test": "../node_modules/.bin/rollup -c rollup.config.js && node output/index.js" + "test": "../node_modules/.bin/rollup -c rollup.config.js && node build/index.js" } } diff --git a/test/rollup-modules/rollup.config.js b/test/rollup-modules/rollup.config.js index 255eb78..407442c 100644 --- a/test/rollup-modules/rollup.config.js +++ b/test/rollup-modules/rollup.config.js @@ -3,7 +3,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'; export default { input: 'index.js', output: { - dir: 'output', + dir: 'build', format: 'cjs' }, plugins: [nodeResolve()] diff --git a/test/snowpack-modules/index.js b/test/snowpack-modules/index.js index 9a3e965..a147352 100644 --- a/test/snowpack-modules/index.js +++ b/test/snowpack-modules/index.js @@ -1,7 +1,2 @@ import { __awaiter } from "tslib"; - -export const testFunction = (textToPrint) => __awaiter(void 0, void 0, void 0, function* () { - console.log(`State: ${textToPrint}`); -}); - -testFunction("Works") +if (typeof __awaiter !== "function") throw new Error("Missing expected helper __awaiter"); diff --git a/test/validateModuleExportsMatchCommonJS/index.js b/test/validateModuleExportsMatchCommonJS/index.js new file mode 100644 index 0000000..0c1b613 --- /dev/null +++ b/test/validateModuleExportsMatchCommonJS/index.js @@ -0,0 +1,23 @@ +// When on node 14, it validates that all of the commonjs exports +// are correctly re-exported for es modules importers. + +const nodeMajor = Number(process.version.split(".")[0].slice(1)) +if (nodeMajor < 14) { + console.log("Skipping because node does not support module exports.") + process.exit(0) +} + +// ES Modules import via the ./modules folder +import * as esTSLib from "../../modules/index.js" + +// Force a commonjs resolve +import { createRequire } from "module"; +const commonJSTSLib = createRequire(import.meta.url)("../../tslib.js"); + +for (const key in commonJSTSLib) { + if (commonJSTSLib.hasOwnProperty(key)) { + if(!esTSLib[key]) throw new Error(`ESModules is missing ${key} - it needs to be re-exported in ./modules/index.js`) + } +} + +console.log("All exports in commonjs are available for es module consumers.") diff --git a/test/validateModuleExportsMatchCommonJS/package.json b/test/validateModuleExportsMatchCommonJS/package.json new file mode 100644 index 0000000..166e509 --- /dev/null +++ b/test/validateModuleExportsMatchCommonJS/package.json @@ -0,0 +1,6 @@ +{ + "type": "module", + "scripts": { + "test": "node index.js" + } +} diff --git a/test/webpack-modules/package.json b/test/webpack-modules/package.json index e913381..3601cac 100644 --- a/test/webpack-modules/package.json +++ b/test/webpack-modules/package.json @@ -1,5 +1,5 @@ { "scripts": { - "test": "../node_modules/.bin/webpack && node dist/main.js" + "test": "../node_modules/.bin/webpack && node build/main.js" } } diff --git a/test/webpack-modules/webpack.config.js b/test/webpack-modules/webpack.config.js index 35cae99..36cb711 100644 --- a/test/webpack-modules/webpack.config.js +++ b/test/webpack-modules/webpack.config.js @@ -1,4 +1,13 @@ -module.exports = { +const path = require('path'); + +/** @type {import("webpack").Configuration} */ +const config = { mode: "production", - entry: "./index" + entry: "./index", + output: { + path: path.join(process.cwd(), 'build') + } + } + +module.exports = config