Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip hash #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 19 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -61,7 +61,8 @@ export default function rebase(options = {}) {
exclude,
verbose = false,
keepName = false,
assetFolder = ""
skipHash = false,
assetFolder = "",
} = options

const filter = createFilter(include, exclude)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
19 changes: 19 additions & 0 deletions test/skip-hash/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -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 };
"
`;
1 change: 1 addition & 0 deletions test/skip-hash/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hello
Binary file added test/skip-hash/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/skip-hash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import content from "./content.md"
import image from "./image.png"

export default function() {
return content + image
}
14 changes: 14 additions & 0 deletions test/skip-hash/index.test.js
Original file line number Diff line number Diff line change
@@ -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")
})