Skip to content

Commit

Permalink
wip: new hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 3, 2023
1 parent 632d3b0 commit a3c750f
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 48 deletions.
4 changes: 2 additions & 2 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ describe.runIf(isBuild)('css and assets in css in build watch', () => {
test('css will not be lost and css does not contain undefined', async () => {
editFile('index.html', (code) => code.replace('Assets', 'assets'), true)
await notifyRebuildComplete(watcher)
const cssFile = findAssetFile(/index-\w+\.css$/, 'foo')
const cssFile = findAssetFile(/index-[-\w]+\.css$/, 'foo')
expect(cssFile).not.toBe('')
expect(cssFile).not.toMatch(/undefined/)
})
Expand Down Expand Up @@ -472,6 +472,6 @@ test.skip('url() contains file in publicDir, as inline style', async () => {
test.runIf(isBuild)('assets inside <noscript> is rewrote', async () => {
const indexHtml = readFile('./dist/foo/index.html')
expect(indexHtml).toMatch(
/<img class="noscript" src="\/foo\/assets\/asset-\w+\.png" \/>/,
/<img class="noscript" src="\/foo\/assets\/asset-[-\w]+\.png" \/>/,
)
})
2 changes: 1 addition & 1 deletion playground/css-lightningcss/__tests__/lightningcss.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test('inline css modules', async () => {

test.runIf(isBuild)('minify css', async () => {
// should keep the rgba() syntax
const cssFile = findAssetFile(/index-\w+\.css$/)
const cssFile = findAssetFile(/index-[-\w]+\.css$/)
expect(cssFile).toMatch('rgba(')
expect(cssFile).not.toMatch('#ffff00b3')
})
Expand Down
14 changes: 7 additions & 7 deletions playground/css/__tests__/css.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ test('sass', async () => {
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getBg(urlStartsWithVariable)).toMatch(
isBuild ? /ok-\w+\.png/ : `${viteTestUrl}/ok.png`,
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)
expect(await getColor(partialImport)).toBe('orchid')

Expand Down Expand Up @@ -118,7 +118,7 @@ test('less', async () => {
isBuild ? /base64/ : '/nested/icon.png',
)
expect(await getBg(urlStartsWithVariable)).toMatch(
isBuild ? /ok-\w+\.png/ : `${viteTestUrl}/ok.png`,
isBuild ? /ok-[-\w]+\.png/ : `${viteTestUrl}/ok.png`,
)

editFile('less.less', (code) => code.replace('@color: blue', '@color: red'))
Expand Down Expand Up @@ -303,8 +303,8 @@ test('async chunk', async () => {
if (isBuild) {
// assert that the css is extracted into its own file instead of in the
// main css file
expect(findAssetFile(/index-\w+\.css$/)).not.toMatch('teal')
expect(findAssetFile(/async-\w+\.css$/)).toMatch('.async{color:teal}')
expect(findAssetFile(/index-[-\w]+\.css$/)).not.toMatch('teal')
expect(findAssetFile(/async-[-\w]+\.css$/)).toMatch('.async{color:teal}')
} else {
// test hmr
editFile('async.css', (code) => code.replace('color: teal', 'color: blue'))
Expand All @@ -322,8 +322,8 @@ test('treeshaken async chunk', async () => {
).toBeNull()
// assert that the css is not present anywhere
expect(findAssetFile(/\.css$/)).not.toMatch('plum')
expect(findAssetFile(/index-\w+\.js$/)).not.toMatch('.async{color:plum}')
expect(findAssetFile(/async-\w+\.js$/)).not.toMatch('.async{color:plum}')
expect(findAssetFile(/index-[-\w]+\.js$/)).not.toMatch('.async{color:plum}')
expect(findAssetFile(/async-[-\w]+\.js$/)).not.toMatch('.async{color:plum}')
// should have no chunk!
expect(findAssetFile(/async-treeshaken/)).toBe('')
} else {
Expand Down Expand Up @@ -423,7 +423,7 @@ test('minify css', async () => {
}

// should keep the rgba() syntax
const cssFile = findAssetFile(/index-\w+\.css$/)
const cssFile = findAssetFile(/index-[-\w]+\.css$/)
expect(cssFile).toMatch('rgba(')
expect(cssFile).not.toMatch('#ffff00b3')
})
Expand Down
4 changes: 2 additions & 2 deletions playground/css/__tests__/no-css-minify/no-css-minify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { findAssetFile, isBuild } from '~utils'

describe.runIf(isBuild)('no css minify', () => {
test('js minified but css not minified', () => {
expect(findAssetFile(/index-\w+\.js$/, 'no-css-minify')).not.toMatch(
expect(findAssetFile(/index-[-\w]+\.js$/, 'no-css-minify')).not.toMatch(
'(function polyfill() {',
)
expect(findAssetFile(/index-\w+\.css$/, 'no-css-minify')).toMatch(`\
expect(findAssetFile(/index-[-\w]+\.css$/, 'no-css-minify')).toMatch(`\
.test-minify {
color: rgba(255, 255, 0, 0.7);
}`)
Expand Down
2 changes: 1 addition & 1 deletion playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ test('tree-shake eager css', async () => {
)

if (isBuild) {
const content = findAssetFile(/index-\w+\.js/)
const content = findAssetFile(/index-[-\w]+\.js/)
expect(content).not.toMatch('.tree-shake-eager-css')
}
})
Expand Down
2 changes: 1 addition & 1 deletion playground/legacy/__tests__/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test('should load dynamic import with css', async () => {

test('asset url', async () => {
expect(await page.textContent('#asset-path')).toMatch(
isBuild ? /\/assets\/vite-\w+\.svg/ : '/vite.svg',
isBuild ? /\/assets\/vite-[-\w]+\.svg/ : '/vite.svg',
)
})

Expand Down
2 changes: 1 addition & 1 deletion playground/ssr-webworker/__tests__/ssr-webworker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ test('/', async () => {
})

test.runIf(isBuild)('inlineDynamicImports', () => {
const dynamicJsContent = findAssetFile(/dynamic-\w+\.js/, 'worker')
const dynamicJsContent = findAssetFile(/dynamic-[-\w]+\.js/, 'worker')
expect(dynamicJsContent).toBe('')
})
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ describe.runIf(isBuild)('build', () => {
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
const worker = files.find((f) => /^my-worker-\w+\.js$/.test(f))
const worker = files.find((f) => /^my-worker-[-\w]+\.js$/.test(f))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8',
)
const workerSourcemap = getSourceMapUrl(workerContent)
const sharedWorker = files.find((f) => /^my-shared-worker-\w+\.js$/.test(f))
const sharedWorker = files.find((f) =>
/^my-shared-worker-[-\w]+\.js$/.test(f),
)
const sharedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, sharedWorker),
'utf-8',
)
const sharedWorkerSourcemap = getSourceMapUrl(sharedWorkerContent)
const possibleTsOutputWorker = files.find((f) =>
/^possible-ts-output-worker-\w+\.js$/.test(f),
/^possible-ts-output-worker-[-\w]+\.js$/.test(f),
)
const possibleTsOutputWorkerContent = fs.readFileSync(
path.resolve(assetsDir, possibleTsOutputWorker),
Expand All @@ -37,7 +39,7 @@ describe.runIf(isBuild)('build', () => {
possibleTsOutputWorkerContent,
)
const workerNestedWorker = files.find((f) =>
/^worker-nested-worker-\w+\.js$/.test(f),
/^worker-nested-worker-[-\w]+\.js$/.test(f),
)
const workerNestedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, workerNestedWorker),
Expand All @@ -46,28 +48,30 @@ describe.runIf(isBuild)('build', () => {
const workerNestedWorkerSourcemap = getSourceMapUrl(
workerNestedWorkerContent,
)
const subWorker = files.find((f) => /^sub-worker-\w+\.js$/.test(f))
const subWorker = files.find((f) => /^sub-worker-[-\w]+\.js$/.test(f))
const subWorkerContent = fs.readFileSync(
path.resolve(assetsDir, subWorker),
'utf-8',
)
const subWorkerSourcemap = getSourceMapUrl(subWorkerContent)

expect(files).toContainEqual(expect.stringMatching(/^index-\w+\.js\.map$/))
expect(files).toContainEqual(
expect.stringMatching(/^my-worker-\w+\.js\.map$/),
expect.stringMatching(/^index-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^my-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^my-shared-worker-\w+\.js\.map$/),
expect.stringMatching(/^my-shared-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^possible-ts-output-worker-\w+\.js\.map$/),
expect.stringMatching(/^possible-ts-output-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^worker-nested-worker-\w+\.js\.map$/),
expect.stringMatching(/^worker-nested-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^sub-worker-\w+\.js\.map$/),
expect.stringMatching(/^sub-worker-[-\w]+\.js\.map$/),
)

// sourcemap should exist and have a data URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ describe.runIf(isBuild)('build', () => {
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
const worker = files.find((f) => /^my-worker-\w+\.js$/.test(f))
const worker = files.find((f) => /^my-worker-[-\w]+\.js$/.test(f))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8',
)
const workerSourcemap = getSourceMapUrl(workerContent)
const sharedWorker = files.find((f) => /^my-shared-worker-\w+\.js$/.test(f))
const sharedWorker = files.find((f) =>
/^my-shared-worker-[-\w]+\.js$/.test(f),
)
const sharedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, sharedWorker),
'utf-8',
)
const sharedWorkerSourcemap = getSourceMapUrl(sharedWorkerContent)
const possibleTsOutputWorker = files.find((f) =>
/^possible-ts-output-worker-\w+\.js$/.test(f),
/^possible-ts-output-worker-[-\w]+\.js$/.test(f),
)
const possibleTsOutputWorkerContent = fs.readFileSync(
path.resolve(assetsDir, possibleTsOutputWorker),
Expand All @@ -37,7 +39,7 @@ describe.runIf(isBuild)('build', () => {
possibleTsOutputWorkerContent,
)
const workerNestedWorker = files.find((f) =>
/^worker-nested-worker-\w+\.js$/.test(f),
/^worker-nested-worker-[-\w]+\.js$/.test(f),
)
const workerNestedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, workerNestedWorker),
Expand All @@ -46,7 +48,7 @@ describe.runIf(isBuild)('build', () => {
const workerNestedWorkerSourcemap = getSourceMapUrl(
workerNestedWorkerContent,
)
const subWorker = files.find((f) => /^sub-worker-\w+\.js$/.test(f))
const subWorker = files.find((f) => /^sub-worker-[-\w]+\.js$/.test(f))
const subWorkerContent = fs.readFileSync(
path.resolve(assetsDir, subWorker),
'utf-8',
Expand Down
38 changes: 21 additions & 17 deletions playground/worker/__tests__/sourcemap/sourcemap-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ describe.runIf(isBuild)('build', () => {
const index = files.find((f) => f.includes('main-module'))
const content = fs.readFileSync(path.resolve(assetsDir, index), 'utf-8')
const indexSourcemap = getSourceMapUrl(content)
const worker = files.find((f) => /^my-worker-\w+\.js$/.test(f))
const worker = files.find((f) => /^my-worker-[-\w]+\.js$/.test(f))
const workerContent = fs.readFileSync(
path.resolve(assetsDir, worker),
'utf-8',
)
const workerSourcemap = getSourceMapUrl(workerContent)
const sharedWorker = files.find((f) => /^my-shared-worker-\w+\.js$/.test(f))
const sharedWorker = files.find((f) =>
/^my-shared-worker-[-\w]+\.js$/.test(f),
)
const sharedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, sharedWorker),
'utf-8',
)
const sharedWorkerSourcemap = getSourceMapUrl(sharedWorkerContent)
const possibleTsOutputWorker = files.find((f) =>
/^possible-ts-output-worker-\w+\.js$/.test(f),
/^possible-ts-output-worker-[-\w]+\.js$/.test(f),
)
const possibleTsOutputWorkerContent = fs.readFileSync(
path.resolve(assetsDir, possibleTsOutputWorker),
Expand All @@ -36,7 +38,7 @@ describe.runIf(isBuild)('build', () => {
possibleTsOutputWorkerContent,
)
const workerNestedWorker = files.find((f) =>
/^worker-nested-worker-\w+\.js$/.test(f),
/^worker-nested-worker-[-\w]+\.js$/.test(f),
)
const workerNestedWorkerContent = fs.readFileSync(
path.resolve(assetsDir, workerNestedWorker),
Expand All @@ -45,41 +47,43 @@ describe.runIf(isBuild)('build', () => {
const workerNestedWorkerSourcemap = getSourceMapUrl(
workerNestedWorkerContent,
)
const subWorker = files.find((f) => /^sub-worker-\w+\.js$/.test(f))
const subWorker = files.find((f) => /^sub-worker-[-\w]+\.js$/.test(f))
const subWorkerContent = fs.readFileSync(
path.resolve(assetsDir, subWorker),
'utf-8',
)
const subWorkerSourcemap = getSourceMapUrl(subWorkerContent)

expect(files).toContainEqual(expect.stringMatching(/^index-\w+\.js\.map$/))
expect(files).toContainEqual(
expect.stringMatching(/^my-worker-\w+\.js\.map$/),
expect.stringMatching(/^index-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^my-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^my-shared-worker-\w+\.js\.map$/),
expect.stringMatching(/^my-shared-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^possible-ts-output-worker-\w+\.js\.map$/),
expect.stringMatching(/^possible-ts-output-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^worker-nested-worker-\w+\.js\.map$/),
expect.stringMatching(/^worker-nested-worker-[-\w]+\.js\.map$/),
)
expect(files).toContainEqual(
expect.stringMatching(/^sub-worker-\w+\.js\.map$/),
expect.stringMatching(/^sub-worker-[-\w]+\.js\.map$/),
)

// sourcemap should exist and have a data URL
expect(indexSourcemap).toMatch(/^main-module-\w+\.js\.map$/)
expect(workerSourcemap).toMatch(/^my-worker-\w+\.js\.map$/)
expect(sharedWorkerSourcemap).toMatch(/^my-shared-worker-\w+\.js\.map$/)
expect(indexSourcemap).toMatch(/^main-module-[-\w]+\.js\.map$/)
expect(workerSourcemap).toMatch(/^my-worker-[-\w]+\.js\.map$/)
expect(sharedWorkerSourcemap).toMatch(/^my-shared-worker-[-\w]+\.js\.map$/)
expect(possibleTsOutputWorkerSourcemap).toMatch(
/^possible-ts-output-worker-\w+\.js\.map$/,
/^possible-ts-output-worker-[-\w]+\.js\.map$/,
)
expect(workerNestedWorkerSourcemap).toMatch(
/^worker-nested-worker-\w+\.js\.map$/,
/^worker-nested-worker-[-\w]+\.js\.map$/,
)
expect(subWorkerSourcemap).toMatch(/^sub-worker-\w+\.js\.map$/)
expect(subWorkerSourcemap).toMatch(/^sub-worker-[-\w]+\.js\.map$/)

// worker should have all imports resolved and no exports
expect(workerContent).not.toMatch(`import`)
Expand Down

0 comments on commit a3c750f

Please sign in to comment.