Skip to content

Commit

Permalink
feat: use hashed filenames for entry and css
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 18, 2020
1 parent c40978f commit 0cc57c8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/node/build/buildPluginCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
parseWithQuery
} from '../utils'
import { Transform } from '../config'
import hash_sum from 'hash-sum'

const debug = require('debug')('vite:build:css')

Expand All @@ -17,7 +18,6 @@ export const createBuildCssPlugin = (
root: string,
publicBase: string,
assetsDir: string,
cssFileName: string,
minify: boolean,
inlineLimit: number,
transforms: Transform[]
Expand Down Expand Up @@ -127,6 +127,8 @@ export const createBuildCssPlugin = (
).css
}

const cssFileName = `style.${hash_sum(css)}.css`

bundle[cssFileName] = {
isAsset: true,
type: 'asset',
Expand Down
9 changes: 6 additions & 3 deletions src/node/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
root = process.cwd(),
base = '/',
outDir = path.resolve(root, 'dist'),
assetsDir = 'assets',
assetsDir = '_assets',
assetsInlineLimit = 4096,
alias = {},
transforms = [],
Expand Down Expand Up @@ -168,7 +168,6 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
const indexPath = path.resolve(root, 'index.html')
const publicBasePath = base.replace(/([^/])$/, '$1/') // ensure ending slash
const resolvedAssetsPath = path.join(outDir, assetsDir)
const cssFileName = 'style.css'

const resolver = createResolver(root, resolvers, alias)

Expand Down Expand Up @@ -219,7 +218,6 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
root,
publicBasePath,
assetsDir,
cssFileName,
!!minify,
assetsInlineLimit,
transforms
Expand All @@ -244,11 +242,16 @@ export async function build(options: BuildConfig = {}): Promise<BuildResult> {
const { output } = await bundle.generate({
format: 'es',
sourcemap,
entryFileNames: `[name].[hash].js`,
chunkFileNames: `common.[hash].js`,
...rollupOutputOptions
})

spinner && spinner.stop()

const cssFileName = output.find(
(a) => a.type === 'asset' && a.fileName.endsWith('.css')
)!.fileName
const indexHtml = emitIndex ? renderIndex(output, cssFileName) : ''

if (write) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface BuildConfig extends SharedConfig {
/**
* Directory relative from `outDir` where the built js/css/image assets will
* be placed.
* @default 'assets'
* @default '_assets'
*/
assetsDir?: string
/**
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('vite', () => {
expect(await getText('.asset-import')).toMatch(
isBuild
? // hashed in production
/\/assets\/testAssets\.([\w\d]+)\.png$/
/\/_assets\/testAssets\.([\w\d]+)\.png$/
: // only resolved to absolute in dev
'/testAssets.png'
)
Expand Down

0 comments on commit 0cc57c8

Please sign in to comment.