Skip to content

Commit

Permalink
fix: #16 增加longTermCachingSymbol参数,让编译产物名称具有更多灵活性
Browse files Browse the repository at this point in the history
有个恶心的 Nginx 正则,会导致纯数字的 hash 访问时404
```
location ~ /dev/ {
  rewrite ^(.*?)-(\d+[\d.])\.(js|css)$ $1-dev.$3 last;
}
```
  • Loading branch information
zhongzhi107 committed Feb 11, 2018
1 parent 3508bdf commit f3e2440
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
9 changes: 6 additions & 3 deletions src/config/packing.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export default {
// 编译时做文件 md5
longTermCaching: true,

// 文件名与 md5 连接使用的字符串
longTermCachingSymbol: '_',

// 静态文件md5保留长度
fileHashLength: 8,

// 编译时做代码压缩
minimize: true,

Expand Down Expand Up @@ -122,9 +128,6 @@ export default {
'svg'
],

// 静态文件md5保留长度
fileHashLength: 8,

// URL转发路由规则配置
// require! 表示使用本地mock文件
rewriteRules: {
Expand Down
35 changes: 18 additions & 17 deletions src/config/webpack.build.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ const JS_DIRECTORY_NAME = 'js';
// js输出文件保持目录名称
const CSS_DIRECTORY_NAME = 'css';

const { cdnRoot } = pRequire(`src/profiles/${process.env.NODE_ENV}`);
const { NODE_ENV } = process.env;

const { cdnRoot } = pRequire(`src/profiles/${NODE_ENV}`);
const {
assetExtensions,
commonChunks,
fileHashLength,
templateExtension,
longTermCaching,
longTermCachingSymbol,
fileHashLength,
minimize,
sourceMap,
cssModules,
Expand All @@ -42,6 +45,15 @@ const {
}
} = pRequire('config/packing');

const getHashPattern = (type) => {
let hashPattern = '';

if (longTermCaching) {
hashPattern = `${longTermCachingSymbol}[${type}:${fileHashLength}]`;
}
return hashPattern;
};

/**
* 生成webpack配置文件
* @param {object} program 程序进程,可以获取启动参数
Expand All @@ -51,8 +63,8 @@ const {
const webpackConfig = () => {
const projectRootPath = process.cwd();
const assetsPath = path.resolve(projectRootPath, assetsDist);
const chunkhash = longTermCaching ? `-[chunkhash:${fileHashLength}]` : '';
const contenthash = longTermCaching ? `-[contenthash:${fileHashLength}]` : '';
const chunkhash = getHashPattern('chunkhash');
const contenthash = getHashPattern('contenthash');
const context = projectRootPath;
const entry = isFunction(entries) ? entries() : entries;

Expand Down Expand Up @@ -119,7 +131,7 @@ const webpackConfig = () => {
test: new RegExp(`.(${assetExtensions.join('|')})$`, 'i'),
loader: 'url-loader',
options: {
name: `[path][name]-[hash:${fileHashLength}].[ext]`,
name: `[path][name]${getHashPattern('hash')}.[ext]`,
context: assets,
limit: 100
}
Expand All @@ -144,7 +156,7 @@ const webpackConfig = () => {

new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
NODE_ENV: JSON.stringify(NODE_ENV),
CDN_ROOT: JSON.stringify(cdnRoot)
}
}),
Expand Down Expand Up @@ -209,17 +221,6 @@ const webpackConfig = () => {
);
}

// 在编译机上编译需要调整目录结构
// 把编译后的模版目录移动到根目录
// if (process.env.NODE_ENV !== '') {
// plugins.push(
// new MoveWebpackPlugin({
// src: templatesDist,
// dest: ''
// }, 'done')
// );
// }

return {
context,
entry,
Expand Down

0 comments on commit f3e2440

Please sign in to comment.