From a69c299d8bd4b0425dcd98837f4feb13b5c75ea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Wed, 25 Oct 2023 20:20:02 +0200 Subject: [PATCH] feat(dynamic-import-vars): Error when files not found (#1611) * Add test for throwing if there are no files found * Throw error if no files were found * update error message with more context * chore: update readme and types --------- Co-authored-by: shellscape --- .github/workflows/release.yml | 3 +- package.json | 4 +-- packages/dynamic-import-vars/README.md | 7 ++++ packages/dynamic-import-vars/src/index.js | 10 +++++- .../test/fixtures/fixture-no-files.js | 3 ++ .../rollup-plugin-dynamic-import-vars.test.js | 32 ++++++++++++++++++ ...llup-plugin-dynamic-import-vars.test.js.md | 22 ++++++++++++ ...up-plugin-dynamic-import-vars.test.js.snap | Bin 976 -> 1006 bytes packages/dynamic-import-vars/types/index.d.ts | 6 ++++ pnpm-lock.yaml | 8 ++--- 10 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 packages/dynamic-import-vars/test/fixtures/fixture-no-files.js diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d5a0b898a..c24897769 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -27,6 +27,7 @@ jobs: run: | git pull --force --no-tags origin master:master git checkout master + git fetch --tags - name: Setup Node uses: actions/setup-node@v3 @@ -38,7 +39,7 @@ jobs: id: pnpm-setup run: | corepack enable - corepack prepare pnpm@latest --activate + corepack prepare pnpm@8 --activate pnpm config set script-shell "/usr/bin/bash" echo "::set-output name=pnpm_cache_dir::$(pnpm store path)" diff --git a/package.json b/package.json index 2311dd7a1..1e872b25a 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint:js": "eslint --cache packages scripts shared util --ext .js,.ts,.mjs", "lint:json": "prettier --write .github/**/*.yml **/tsconfig.json tsconfig.*.json pnpm-workspace.yaml", "lint:package": "prettier --write **/package.json", - "plugin:release": "ts-node ./scripts/release.ts", + "package:release": "versioner --stripShortName='^@.+/plugin-' --target", "preinstall": "node scripts/disallow-npm.js", "prepare": "husky install", "prettier": "prettier --write .", @@ -18,7 +18,7 @@ }, "devDependencies": { "@ava/babel": "2.0.0", - "@dot/versioner": "^0.2.0", + "@dot/versioner": "^0.3.0", "@rollup/plugin-typescript": "^9.0.1", "@types/conventional-commits-parser": "^3.0.2", "@types/node": "14.18.30", diff --git a/packages/dynamic-import-vars/README.md b/packages/dynamic-import-vars/README.md index cd14fdfc8..bd3eb82bd 100644 --- a/packages/dynamic-import-vars/README.md +++ b/packages/dynamic-import-vars/README.md @@ -61,6 +61,13 @@ Default: `[]` Files to exclude in this plugin (default none). +#### `errorWhenNoFilesFound` + +Type: `Boolean`
+Default: `false` + +By default, the plugin will not throw errors when target files are not found. Setting this option to true will result in errors thrown when encountering files which don't exist. + #### `warnOnError` Type: `Boolean`
diff --git a/packages/dynamic-import-vars/src/index.js b/packages/dynamic-import-vars/src/index.js index a8b036518..3d631b729 100644 --- a/packages/dynamic-import-vars/src/index.js +++ b/packages/dynamic-import-vars/src/index.js @@ -9,7 +9,7 @@ import { createFilter } from '@rollup/pluginutils'; import { dynamicImportToGlob, VariableDynamicImportError } from './dynamic-import-to-glob'; -function dynamicImportVariables({ include, exclude, warnOnError } = {}) { +function dynamicImportVariables({ include, exclude, warnOnError, errorWhenNoFilesFound } = {}) { const filter = createFilter(include, exclude); return { @@ -55,6 +55,14 @@ function dynamicImportVariables({ include, exclude, warnOnError } = {}) { r.startsWith('./') || r.startsWith('../') ? r : `./${r}` ); + if (errorWhenNoFilesFound && paths.length === 0) { + this.error( + new Error( + `No files found in ${glob} when trying to dynamically load concatted string from ${id}` + ) + ); + } + // create magic string if it wasn't created already ms = ms || new MagicString(code); // unpack variable dynamic import into a function with import statements per file, rollup diff --git a/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js b/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js new file mode 100644 index 000000000..361d07d60 --- /dev/null +++ b/packages/dynamic-import-vars/test/fixtures/fixture-no-files.js @@ -0,0 +1,3 @@ +export function importModule(name) { + return import(`./module-dir-c/${name}.js`); +} diff --git a/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js b/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js index e0e1f7d63..6a0ae9448 100644 --- a/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js +++ b/packages/dynamic-import-vars/test/rollup-plugin-dynamic-import-vars.test.js @@ -204,3 +204,35 @@ test('dynamic imports assertions', async (t) => { ); t.snapshot(output[0].code); }); + +test("doesn't throw if no files in dir when option isn't set", async (t) => { + let thrown = false; + try { + await rollup({ + input: 'fixture-no-files.js', + plugins: [dynamicImportVars()] + }); + } catch (_) { + thrown = true; + } + t.false(thrown); +}); + +test('throws if no files in dir when option is set', async (t) => { + let thrown = false; + try { + await rollup({ + input: 'fixture-no-files.js', + plugins: [dynamicImportVars({ errorWhenNoFilesFound: true })] + }); + } catch (error) { + t.deepEqual( + error.message, + `No files found in ./module-dir-c/*.js when trying to dynamically load concatted string from ${require.resolve( + './fixtures/fixture-no-files.js' + )}` + ); + thrown = true; + } + t.true(thrown); +}); diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md index 611c7f70f..0889053e5 100644 --- a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md +++ b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.md @@ -234,3 +234,25 @@ Generated by [AVA](https://avajs.dev). ␊ export { importModule };␊ ` + +## no files in dir + +> Snapshot 1 + + `function __variableDynamicImportRuntime0__(path) {␊ + switch (path) {␊ + ␊ + default: return new Promise(function(resolve, reject) {␊ + (typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(␊ + reject.bind(null, new Error("Unknown variable dynamic import: " + path))␊ + );␊ + })␊ + }␊ + }␊ + ␊ + function importModule(name) {␊ + return __variableDynamicImportRuntime0__(\`./module-dir-c/${name}.js\`);␊ + }␊ + ␊ + export { importModule };␊ + ` diff --git a/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap b/packages/dynamic-import-vars/test/snapshots/rollup-plugin-dynamic-import-vars.test.js.snap index 1239582e02b24caa0c221f4050bca72a8f23642d..8ee67b5d83ef2e95272bfbecccf13a9140b5b0e2 100644 GIT binary patch literal 1006 zcmVsFvLoV@lpjH>f|nf>N}XJ-Ai=`!K%sW0D9 zQ_ZlgDBooWK9y8)5~!BY>ho9MCz^a*>Mk!_UWh)6mlm!rxl7HEdzuOk_4+=Ml(-C^ z_BrWL?`0I*Qa5wJ6#NXufMaO!t}98LZ<)h<9GnC zrRY$F_ShJD>}SN^znv}rpwjcssnq*G#q>CSH)@xtmL0vQ^^HVj+C3475F7-TrXaC0RI%TC8w zod%=8RL3K8d60=rwtV3tGY8WhFWhNz8Up8b*Z)L5+<87T<`g*%fm6S~8}y!6X%40e z-3V~4Nqc5LI$>}?q_3RF_7i~fNS(H9cG<`6ut?HOk{Z(*R;S9;)q&YOJb8`=iC>}= zjit^hAUcz#IQIhbU-GmlA!nk5{A&#fJ<%~0qz9hh9?{52Bv?)@*~kRx&7zi^)ml=d zQhdqFQ$ldlCOV{^y;~Z^uoR8x`n-1?)4Z^3Ti^c8cZm;bGxsLxEiL@ zPl~_6RCEiuZIAvh!PdiAMl+fjmtcj7UE3PzfxcCSgv?ZY^UH58YR0=ysXW!q$NG#n zAI8GvJf`V>qv{J(+|uB+2;aqVw*k6EG?;cHWIk}A!Nr8aKh}us{L1tpKNm7)FqZ&1<@S<05II`QUCw| literal 976 zcmV;>126nRRzVsFvZKanQyzj=1+{_zha$vF<&>=L zNweu@*V$dS2~iFl`5Ey0_y=5AJ5Fo24oORqs@73*SZDT|_s-1vN7H4(+gIOzqNbW* zTT#Bp5PT}B;v`Tlq1Cq^zE3pyGT&X8yEPYmZrq%^Gw;qfL+)uRIMnM0L{j21e9`Bm zL%lbhKuG;Q8$CVN5x~~{07VnJA%?g^Jt;I%`>?sW3D$VG1XMycL4sa!Dfew2GFFbIc`2ok_We(MpNm5dW4C}m@=K!WI^2h4h&@nB022*|@p`yu zzKcih9c|i)O=BAz9O;x8VY>4hC%mvUh(LzMunoi3aKc`+0|psRFx*&7!m`sbR;R%z zFxBbETo_~`lPz4k$jrfX#|w9woQA-8(DlEP&ktYCj5$S4L*Ue(>;=77RhomTLN@|j zY0{qAk4_jI66q@^vi$_$JW{7En_c#0CoGaQlcdJ9hSj+;b!lKW56_;XLE@JvMPsRR z3W(06DPDL1`7e1|l#okNLjJXegr4Y_3ep2laF1x@BoZvAmaJ!jbh)S{m$jA@sT3dd zZ%rPY@33DQ#OHk@<&^mJj}V8AVZ%setysgew`-BI2FF-L$bL>8a5E|P;o=7gDTH>$ zQ9dEKGBggWJ)#d++j+T_CUj>2bDdhXPx;fQMOShWqeI%XXAZ^Q-Q&?AqespZaeE^Z zmgU4CmghJ?&ieDlk1E^5ZRFQ`1%&Bxk|Hy_5r zo%{i9j&2m(8vp>rCE1by diff --git a/packages/dynamic-import-vars/types/index.d.ts b/packages/dynamic-import-vars/types/index.d.ts index ca4af9c4c..353e9f823 100644 --- a/packages/dynamic-import-vars/types/index.d.ts +++ b/packages/dynamic-import-vars/types/index.d.ts @@ -15,6 +15,12 @@ interface RollupDynamicImportVariablesOptions { * By default no files are ignored. */ exclude?: FilterPattern; + /** + * By default, the plugin will not throw errors when target files are not found. + * Setting this option to true will result in errors thrown when encountering files which don't exist. + * @default false + */ + errorWhenNoFilesFound?: boolean; /** * By default, the plugin quits the build process when it encounters an error. * If you set this option to true, it will throw a warning instead and leave the code untouched. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0b62552ff..4f9922f37 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -12,8 +12,8 @@ importers: specifier: 2.0.0 version: 2.0.0 '@dot/versioner': - specifier: ^0.2.0 - version: 0.2.0 + specifier: ^0.3.0 + version: 0.3.0 '@rollup/plugin-typescript': specifier: ^9.0.1 version: 9.0.1(rollup@4.0.0-24)(typescript@4.8.4) @@ -2116,8 +2116,8 @@ packages: p-defer: 3.0.0 dev: true - /@dot/versioner@0.2.0: - resolution: {integrity: sha512-mT9c189tmfRUa8wcnD3zYQKMP9KfbtwqOTh5Y94tODDdSoAIfpS31Hs5ySxiQ7I+/PBdjLh+AUZ7rCOI9Rr/1Q==} + /@dot/versioner@0.3.0: + resolution: {integrity: sha512-PjC7OtuPvsjuN/8psfPUUD6bfb/LZCCefAUeV9cHry40VCmaPtthBY7AKskw3R8eE9BHrgjY6n1MIM/pWhU41w==} engines: {node: '>=18'} hasBin: true dependencies: