diff --git a/package-lock.json b/package-lock.json index d4273da..3130797 100644 --- a/package-lock.json +++ b/package-lock.json @@ -39,7 +39,7 @@ "typescript": "^4.6.2" }, "engines": { - "node": ">=16.0.0" + "node": ">=14" } }, "node_modules/@ampproject/remapping": { diff --git a/readme.md b/readme.md index 516fe69..40d5922 100644 --- a/readme.md +++ b/readme.md @@ -61,6 +61,7 @@ config() - `assetFolder`: When set assets are placed inside a sub folder with that name. - `keepName`: If `true`, generated filenames will be `${filename}~${hash}.${ext}` instead of just `${hash}.${ext}` +- `skipHash`: If `true`, filenames will not include hash and will keep names - `verbose`: If `true`, increases log level - `include`: Standard include option for rollup plugins. - `exclude`: Standard exclude option for rollup plugins. diff --git a/src/index.js b/src/index.js index 05bb427..8426c2b 100644 --- a/src/index.js +++ b/src/index.js @@ -21,22 +21,22 @@ const styleParser = { ".sass": postcssSass } -function getPostCssPlugins(keepName) { +function getPostCssPlugins(keepName, skipHash) { return [ postcssImport(), postcssSmartAsset({ url: "copy", - useHash: true, - keepName + useHash: !skipHash, + keepName, }) ] } /* eslint-disable max-params */ -async function processStyle(id, fileDest, keepName) { +async function processStyle(id, fileDest, keepName, skipHash) { const content = await fs.readFile(id) const parser = styleParser[path.extname(id)] - const processor = postcss(getPostCssPlugins(keepName)) + const processor = postcss(getPostCssPlugins(keepName, skipHash)) const text = content.toString() const result = await processor.process(text, { @@ -61,7 +61,8 @@ export default function rebase(options = {}) { exclude, verbose = false, keepName = false, - assetFolder = "" + skipHash = false, + assetFolder = "", } = options const filter = createFilter(include, exclude) @@ -142,10 +143,17 @@ export default function rebase(options = {}) { } const fileName = path.basename(importee, fileExt) - const fileHash = await getHash(fileSource) - const fileTarget = keepName - ? `${fileName}~${fileHash}${fileExt}` - : `${fileHash}${fileExt}` + + // Set default file target name + let fileTarget = `${fileName}${fileExt}` + + if (!skipHash) { + // If using hash, decide whether we should keep name or hash only + const fileHash = await getHash(fileSource); + fileTarget = keepName + ? `${fileName}~${fileHash}${fileExt}` + : `${fileHash}${fileExt}` + } // Registering for our copying job when the bundle is created (kind of a job queue) // and respect any sub folder given by the configuration options. @@ -208,7 +216,7 @@ export default function rebase(options = {}) { ) } - await processStyle(fileSource, fileDest, keepName) + await processStyle(fileSource, fileDest, keepName, skipHash) } else { if (verbose) { console.log( diff --git a/test/skip-hash/__snapshots__/index.test.js.snap b/test/skip-hash/__snapshots__/index.test.js.snap new file mode 100644 index 0000000..b017470 --- /dev/null +++ b/test/skip-hash/__snapshots__/index.test.js.snap @@ -0,0 +1,19 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Skip hash 1`] = ` +"content.md +image.png +index.js" +`; + +exports[`Skip hash 2`] = ` +"import content from './content.md'; +import image from './image.png'; + +function index() { + return content + image +} + +export { index as default }; +" +`; diff --git a/test/skip-hash/content.md b/test/skip-hash/content.md new file mode 100644 index 0000000..fec5601 --- /dev/null +++ b/test/skip-hash/content.md @@ -0,0 +1 @@ +# Hello diff --git a/test/skip-hash/image.png b/test/skip-hash/image.png new file mode 100644 index 0000000..c5916f2 Binary files /dev/null and b/test/skip-hash/image.png differ diff --git a/test/skip-hash/index.js b/test/skip-hash/index.js new file mode 100644 index 0000000..eb32e25 --- /dev/null +++ b/test/skip-hash/index.js @@ -0,0 +1,6 @@ +import content from "./content.md" +import image from "./image.png" + +export default function() { + return content + image +} diff --git a/test/skip-hash/index.test.js b/test/skip-hash/index.test.js new file mode 100644 index 0000000..2d58c24 --- /dev/null +++ b/test/skip-hash/index.test.js @@ -0,0 +1,14 @@ +import { bundle, clean, list, read } from "../util" + +const root = __dirname + +test("Skip hash", async () => { + await bundle(root, "index.js", "output/index.js", { + skipHash: true, + }) + + expect(await list(root, "output")).toMatchSnapshot() + expect(await read(root, "output/index.js")).toMatchSnapshot() + + await clean(root, "output") +})