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 augmentChunkHash hook for proper content hashing #231

Merged
merged 4 commits into from Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -41,7 +41,7 @@
"jest": "^23.6.0",
"less": "^2.7.3",
"node-sass": "^4.13.1",
"rollup": "^1.1.2",
"rollup": "^1.32.0",
"stylus": "^0.54.5",
"sugarss": "^1.0.1",
"xo": "^0.18.2"
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Expand Up @@ -101,6 +101,15 @@ export default (options = {}) => {
}
},

augmentChunkHash() {
if (extracted.size === 0) return
const extractedValue = Array.from(extracted).reduce((obj, [key, value]) => ({
...obj,
[key]: value
}), {})
return JSON.stringify(extractedValue)
},

async generateBundle(opts, bundle) {
if (
extracted.size === 0 ||
Expand Down
6 changes: 4 additions & 2 deletions test/__snapshots__/index.test.js.snap
Expand Up @@ -635,9 +635,10 @@ const stylesheet=\\".style_foo {\\\\n color: red;\\\\n}\\\\n\\\\n.style_new {\\
styleInject(css);

var style$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
foo: foo,
$new$: $new$,
default: style,
'default': style,
stylesheet: stylesheet
});

Expand Down Expand Up @@ -683,9 +684,10 @@ const stylesheet=\\".style_foo {\\\\n color: red;\\\\n}\\\\n\\\\n.style_new {\\
styleInject(css);

var style$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
foohacked: foohacked,
newhacked: newhacked,
default: style,
'default': style,
stylesheet: stylesheet
});

Expand Down
28 changes: 28 additions & 0 deletions test/index.test.js
Expand Up @@ -334,3 +334,31 @@ test('onExtract', async () => {
expect(await res.jsCode()).toMatchSnapshot()
expect(await res.hasCssFile()).toBe(false)
})

test('augmentChunkHash', async () => {
const outDir = fixture('dist', 'augmentChunkHash')
const cssFiles = ['simple/foo.css', 'simple/foo.css', 'simple/bar.css']

const outputFiles = []
/* eslint-disable no-await-in-loop */
for (const file of cssFiles) {
const newBundle = await rollup({
input: fixture(file),
plugins: [postcss({ extract: true })]
})
const entryFileName = file.split('.')[0]
const { output } = await newBundle.write({
dir: outDir,
entryFileNames: `${entryFileName}.[hash].css`
})
outputFiles.push(output[0])
}
const [fooOne, fooTwo, barOne] = outputFiles

const fooHash = fooOne.fileName.split('.')[1]
expect(fooHash).toBeTruthy() // Verify that [hash] part of `foo.[hash].css` is truthy
expect(fooOne.fileName).toEqual(fooTwo.fileName) // Verify that the foo hashes to the same fileName

const barHash = barOne.fileName.split('.')[1]
expect(barHash).not.toEqual(fooHash) // Verify that foo and bar does not hash to the same
})
19 changes: 19 additions & 0 deletions yarn.lock
Expand Up @@ -646,6 +646,11 @@
lodash "^4.17.10"
to-fast-properties "^2.0.0"

"@types/estree@*":
version "0.0.42"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.42.tgz#8d0c1f480339efedb3e46070e22dd63e0430dd11"
integrity sha512-K1DPVvnBCPxzD+G51/cxVIoc2X8uUVl1zpJeE6iKcgHMj4+tbat5Xu4TjV7v2QSDbIeAfLi2hIk+u2+s0MlpUQ==

"@types/estree@0.0.39":
version "0.0.39"
resolved "https://registry.npmjs.org/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
Expand Down Expand Up @@ -706,6 +711,11 @@ acorn@^6.0.1, acorn@^6.0.5:
resolved "https://registry.npmjs.org/acorn/-/acorn-6.0.5.tgz#81730c0815f3f3b34d8efa95cb7430965f4d887a"
integrity sha512-i33Zgp3XWtmZBMNvCr4azvOFeWVw1Rk6p3hfi3LUDvIFraOMywb1kAtrbi+med14m4Xfpqm3zRZMT+c0FNE7kg==

acorn@^7.1.0:
version "7.1.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-7.1.1.tgz#e35668de0b402f359de515c5482a1ab9f89a69bf"
integrity sha512-add7dgA5ppRPxCFJoAGfMDi7PIBXq1RtGo7BhbLaxwrXPOmw8gq48Y9ozT01hUKy9byMjlR20EJhu5zlkErEkg==

ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.npmjs.org/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
Expand Down Expand Up @@ -6383,6 +6393,15 @@ rollup@^1.1.2:
"@types/node" "*"
acorn "^6.0.5"

rollup@^1.32.0:
version "1.32.0"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.32.0.tgz#c65ce134850aca1ce595fcac07d1dc5d53bf227c"
integrity sha512-ab2tF5pdDqm2zuI8j02ceyrJSScl9V2C24FgWQ1v1kTFTu1UrG5H0hpP++mDZlEFyZX4k0chtGEHU2i+pAzBgA==
dependencies:
"@types/estree" "*"
"@types/node" "*"
acorn "^7.1.0"

rsvp@^3.3.3:
version "3.6.2"
resolved "https://registry.npmjs.org/rsvp/-/rsvp-3.6.2.tgz#2e96491599a96cde1b515d5674a8f7a91452926a"
Expand Down