From 3ade20ec4b432e0cbc4d31cdc9138b36a70ceb2e Mon Sep 17 00:00:00 2001 From: pengbo <57180744+PengBoUESTC@users.noreply.github.com> Date: Thu, 8 Feb 2024 22:49:04 +0800 Subject: [PATCH] fix: import cache (#259) * fix: import cache * chore: add test for import cache & config search error * chore: update describe name * chore: search ghost dir * chore: remove .href of pathToFileURL --------- Co-authored-by: pengbo43 --- src/req.js | 2 +- test/import.test.js | 20 ++++++++++++++++++++ test/postcss.config.mjs | 1 + test/rc.test.js | 9 +++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/import.test.js create mode 100644 test/postcss.config.mjs diff --git a/src/req.js b/src/req.js index db435eb..99fc8dd 100644 --- a/src/req.js +++ b/src/req.js @@ -16,7 +16,7 @@ async function req(name, rootFile = __filename) { let url = createRequire(rootFile).resolve(name) try { - return (await import(pathToFileURL(url).href)).default + return (await import(`${pathToFileURL(url)}?t=${Date.now()}`)).default } catch (err) { if (!TS_EXT_RE.test(url)) { /* c8 ignore start */ diff --git a/test/import.test.js b/test/import.test.js new file mode 100644 index 0000000..0ff624e --- /dev/null +++ b/test/import.test.js @@ -0,0 +1,20 @@ +const { describe, test } = require('node:test') +const { equal } = require('node:assert') +const { writeFileSync } = require('node:fs') +const postcssrc = require('../src/index.js') + +describe('.postcss.config.js Load Config', () => { + test('.postcss.config.js config update cache', () => { + + return postcssrc({}, 'test').then(config => { + let { file, options } = config + equal(options.map, false) + writeFileSync(file, `export default ${JSON.stringify({ map: true })}`) + postcssrc({}, 'test').then(({ options: newOptions }) => { + equal(newOptions.map, true) + }).finally(() => { + writeFileSync(file, `export default ${JSON.stringify({ map: false })}`) + }) + }) + }) +}) diff --git a/test/postcss.config.mjs b/test/postcss.config.mjs new file mode 100644 index 0000000..ef4d51b --- /dev/null +++ b/test/postcss.config.mjs @@ -0,0 +1 @@ +export default {"map":false} \ No newline at end of file diff --git a/test/rc.test.js b/test/rc.test.js index 558e306..0e58385 100644 --- a/test/rc.test.js +++ b/test/rc.test.js @@ -54,3 +54,12 @@ test('.postcssrc - {Object} - Process SSS', () => { }) }) }) + +describe('Load Config Error', () => { + test('no config found error', () => { + return postcssrc({}, 'ghostDir') + .catch(error => { + equal(error.message.startsWith("No PostCSS Config found in:"), true) + }) + }) +})