Authored by ccbikai(👎🏻🍜)

loader 整理

... ... @@ -13,8 +13,8 @@ const domains = {
liveApi: 'http://testapi.live.yohops.com:9999/',
singleApi: 'http://api-test3.yohops.com:9999/',
api: 'http://dev-api.yohops.com:9999/',
service: 'http://dev-service.yohops.com:9999/',
api: 'http://api.yoho.cn/',
service: 'http://service.yoho.cn/',
// liveApi: 'http://api.live.yoho.cn/',
// singleApi: 'http://single.yoho.cn/',
... ...
... ... @@ -18,9 +18,13 @@
<link rel="dns-prefetch" href="{{this}}">
{{/dnsPrefetch.hosts}}
{{!-- Android机器页面不再受到用户字体缩放强制改变大小 'http://m.blog.csdn.net/article/details?id=51275080' --}}
<script type="text/javascript">
var isWechat = /micromessenger/i.test(navigator.userAgent || '');
if (isWechat) document.title =document.title.replace(' | Yoho!Buy有货 | 潮流购物逛不停', '');
if (isWechat) {
document.title =document.title.replace(' | Yoho!Buy有货 | 潮流购物逛不停', '');
(function () { if (typeof (WeixinJSBridge) == "undefined") { document.addEventListener("WeixinJSBridgeReady", function (a) { setTimeout(function () { WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 }, function (b) { }) }, 0) }) } else { setTimeout(function () { WeixinJSBridge.invoke("setFontSizeCallback", { fontSize: 0 }, function (a) { }) }, 0) } })();
}
</script>
{{#if width750}}
... ... @@ -33,28 +37,7 @@
</script>
{{/if}}
{{#if isWechat}}
{{!--Android机器页面不再受到用户字体缩放强制改变大小 http://m.blog.csdn.net/article/details?id=51275080 --}}
<script type="text/javascript">
(function(){if(typeof(WeixinJSBridge)=="undefined"){document.addEventListener("WeixinJSBridgeReady",function(a){setTimeout(function(){WeixinJSBridge.invoke("setFontSizeCallback",{fontSize:0},function(b){})},0)})}else{setTimeout(function(){WeixinJSBridge.invoke("setFontSizeCallback",{fontSize:0},function(a){})},0)}})();
</script>
{{/if}}
{{#if devEnv}}
{{#if localCss}}
<link rel="stylesheet" media="all" href="//{{devHost}}:5001/common.css">
{{/if}}
{{#if isFeature}}
<link rel="stylesheet" media="all" href="//{{devHost}}:5001/feature.css">
{{else}}
{{#ifor localCss vue}}
<link rel="stylesheet" media="all" href="//{{devHost}}:5001/{{module}}.{{page}}.css">
{{^}}
<link rel="stylesheet" media="all" href="//{{devHost}}:5001/index.css">
{{/ifor}}
{{/if}}
{{^}}
{{#unless devEnv}}
{{#if localCss }}
<link rel="stylesheet" media="all" href="//cdn.yoho.cn/m-yohobuy-node/{{version}}/common.css">
{{/if}}
... ... @@ -68,7 +51,7 @@
<link rel="stylesheet" media="all" href="//cdn.yoho.cn/m-yohobuy-node/{{version}}/index.css">
{{/ifor}}
{{/if}}
{{/if}}
{{/unless}}
<link rel="apple-touch-icon-precomposed" href="http://static.yohobuy.com/m/v1/img/touch/apple-touch-icon-144x144-precomposed-new.png">
<link rel="apple-touch-startup-image" sizes="640x920" href="http://static.yohobuy.com/m/v1/img/startup/startup-retina.png" media="screen and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2)">
<link rel="apple-touch-startup-image" sizes="320x460" href="http://static.yohobuy.com/m/v1/img/startup/startup.png" media="screen and (max-device-width: 320)">
... ... @@ -98,21 +81,30 @@
{{/wechatShare}}
{{#if devEnv}}
<script>var STATIC_RESOURCE_PATH = '//{{devHost}}:5001';</script>
<script src="//{{devHost}}:5001/libs.js"></script>
{{!--开发环境的CSS需要hot reload, 所以使用 JS 方式加载--}}
{{#if localCss}}
<script src="//{{devHost}}:5001/common.js"></script>
{{else}}
<script src="//{{devHost}}:5001/index.js"></script>
{{/if}} {{#if isFeature}}
<script src="//{{devHost}}:5001/feature.js"></script>
{{/if}}
<script src="//{{devHost}}:5001/{{module}}.{{page}}.js"></script>
{{^}}
<script>var STATIC_RESOURCE_PATH = '//cdn.yoho.cn/m-yohobuy-node/assets';</script>
<script src="//cdn.yoho.cn/m-yohobuy-node/{{version}}/libs.js"></script>
<script src="//cdn.yoho.cn/m-yohobuy-node/{{version}}/{{module}}.{{page}}.js"></script>
{{/if}}
{{#loadJs}}
<script type="text/javascript" src="{{src}}"></script>
{{/loadJs}}
{{#isFeature}}
<script type="text/javascript" src="//cdn.yoho.cn/js-sdk/1.2.0/jssdk.js"></script>
{{/isFeature}}
{{#unless devEnv}}
{{> analysis}}
{{/unless}}
{{#loadJs}}
<script type="text/javascript" src="{{src}}"></script>
{{/loadJs}}
</body>
</html>
... ...
... ... @@ -36,7 +36,7 @@
"lodash": "^4.17.4",
"md5": "^2.1.0",
"memory-cache": "^0.1.6",
"moment": "^2.17.1",
"moment": "^2.18.1",
"oneapm": "^1.2.20",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
... ... @@ -59,7 +59,7 @@
"babel-core": "^6.24.0",
"babel-loader": "^6.4.1",
"babel-preset-env": "^1.2.2",
"css-loader": "^0.26.1",
"css-loader": "^0.27.3",
"cssnano": "^3.10.0",
"eslint": "^3.16.0",
"eslint-config-yoho": "^1.0.1",
... ... @@ -85,15 +85,15 @@
"postcss-use": "^2.3.0",
"precss": "^1.4.0",
"shelljs": "^0.7.6",
"style-loader": "^0.15.0",
"style-loader": "^0.16.0",
"stylelint": "^7.9.0",
"stylelint-config-yoho": "^1.2.8",
"stylelint-processor-html": "^1.0.0",
"stylelint-webpack-plugin": "^0.7.0",
"vue": "^2.1.10",
"vue-loader": "^11.1.4",
"vue-loader": "^11.2.0",
"vue-template-compiler": "^2.1.10",
"webpack": "^2.2.1",
"webpack": "^2.3.1",
"webpack-dev-server": "^2.4.2",
"webpack-uglify-parallel": "^0.1.3",
"yoho-cookie": "^1.2.0",
... ...
... ... @@ -72,13 +72,14 @@ exports.postcssPlugin = (et) => {
} else if (et === 'dev') {
assets = {
loadPaths: [path.join(__dirname, '../img/'), path.join(__dirname, '../font/')],
relativeTo: path.join(__dirname, './bundle')
relative: path.join(__dirname, './bundle'),
baseUrl: 'http://localhost:5001/',
};
Object.assign(sprites, {
basePath: path.join(__dirname, '../img/'),
basePath: path.join(__dirname, '../img/'), // basePath: 'http://localhost:5001/',
stylesheetPath: path.join(__dirname, './bundle'),
spritePath: path.join(__dirname, './bundle')
spritePath: path.join(__dirname, './bundle'),
});
}
... ...
... ... @@ -5,39 +5,81 @@
*/
'use strict';
const _ = require('lodash');
const os = require('os');
const path = require('path');
const scss = require('postcss-scss');
const shelljs = require('shelljs');
const _ = require('lodash');
const webpack = require('webpack');
const HappyPack = require('happypack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const scss = require('postcss-scss');
const postcssConfig = require('./postcss.config.js');
const entries = {
index: path.join(__dirname, '../scss/index.css'),
common: path.join(__dirname, '../scss/common.css'),
feature: path.join(__dirname, '../scss/feature.css')
const hbsLoader = {
loader: 'handlebars-loader',
options: {
helperDirs: [
path.join(__dirname, '../js/common/helpers')
],
partialDirs: [
path.join(__dirname, '../../doraemon/views/partial')
]
}
};
const cssLoader = (env, type) => {
let loaders = [{
loader: 'css-loader',
options: {
url: false,
sourceMap: env === 'dev'
}
}];
if (type === 'css') {
loaders.push({
loader: 'postcss-loader',
options: {
plugins: postcssConfig.postcssPlugin(env),
parser: scss
}
});
}
if (env === 'dev') {
loaders.unshift({
loader: 'style-loader'
});
return loaders;
}
return ExtractTextPlugin.extract({
fallback: type === 'css' ? 'style-loader' : 'vue-style-loader',
use: loaders
});
};
const happyThreadPool = HappyPack.ThreadPool({ // eslint-disable-line
size: os.cpus().length
});
// 构建各模块子页面JS。生成规则module.page.js
shelljs.ls(path.join(__dirname, '../js/**/*.page.js')).forEach((f) => {
const dir = _.slice(f.split('/'), -2); // [modulename, xx.page.js]
const getEntries = () => {
const entries = {
libs: ['yoho-jquery'],
index: path.join(__dirname, '../scss/index.css'),
common: path.join(__dirname, '../scss/common.css'),
feature: path.join(__dirname, '../scss/feature.css')
};
// 构建各模块子页面JS。生成规则module.page.js
shelljs.ls(path.join(__dirname, '../js/**/*.page.js')).forEach((f) => {
const dir = _.slice(f.split('/'), -2); // [modulename, xx.page.js]
// Important
// 生成规则:module.page: './js/module/xx.page.js'
entries[`${dir[0]}.${dir[1].match(/(.*).page.js/)[1]}`] = path.join(__dirname, `../js/${dir.join('/')}`);
entries.libs = [
'yoho-jquery'
];
});
// Important
// 生成规则:module.page: './js/module/xx.page.js'
entries[`${dir[0]}.${dir[1].match(/(.*).page.js/)[1]}`] = path.join(__dirname, `../js/${dir.join('/')}`);
});
return entries;
};
module.exports = (env) => {
return {
entry: entries,
entry: getEntries(),
output: {
path: path.join(__dirname, 'bundle'), // absolute path
filename: '[name].js'
... ... @@ -54,45 +96,23 @@ module.exports = (env) => {
},
autoprefixer: false,
loaders: {
css: ExtractTextPlugin.extract({
fallback: 'vue-style-loader',
use: [{
loader: 'css-loader',
options: {
url: false
}
}]
})
css: cssLoader(env, 'vue')
}
}
}],
}, {
test: /\.js$/,
exclude: [/node_modules/],
use: [{
use: env === 'dev' ? ['babel-loader'] : [{
loader: 'happypack/loader?id=js'
}]
}, {
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
url: false
}
}, {
loader: 'postcss-loader',
options: {
plugins: postcssConfig.postcssPlugin(env),
parser: scss
}
}],
})
use: cssLoader(env, 'css')
}, {
test: /\.hbs$/,
use: [{
loader: 'happypack/loader?id=hbs',
use: env === 'dev' ? [hbsLoader] : [{
loader: 'happypack/loader?id=hbs'
}]
}]
},
... ... @@ -112,27 +132,6 @@ module.exports = (env) => {
children: false
},
plugins: [
new HappyPack({
id: 'js',
threadPool: happyThreadPool,
loaders: ['babel-loader'],
}),
new HappyPack({
id: 'hbs',
threadPool: happyThreadPool,
loaders: [{
loader: 'handlebars-loader',
options: {
helperDirs: [
path.join(__dirname, '../js/common/helpers')
],
partialDirs: [
path.join(__dirname, '../../doraemon/views/partial')
]
}
}]
}),
new ExtractTextPlugin('[name].css'),
new webpack.optimize.CommonsChunkPlugin({
name: 'libs',
filename: 'libs.js'
... ... @@ -142,6 +141,9 @@ module.exports = (env) => {
jQuery: 'yoho-jquery',
'window.jQuery': 'yoho-jquery'
})
]
],
extends: {
hbsLoader
}
};
};
... ...
'use strict';
const path = require('path');
const os = require('os');
const path = require('path');
const shelljs = require('shelljs');
const webpack = require('webpack');
const HappyPack = require('happypack');
const UglifyJsParallelPlugin = require('webpack-uglify-parallel');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const config = require('../../package.json');
const distDir = path.join(__dirname, `../dist/${config.name}/${config.version}`);
let baseConfig = require('./webpack.base.config.js');
const happyThreadPool = HappyPack.ThreadPool({ // eslint-disable-line
size: os.cpus().length
});
// cope img font
shelljs.mkdir('-p', distDir);
shelljs.cp('-R', path.join(__dirname, '../img/'), distDir);
... ... @@ -17,6 +23,17 @@ shelljs.cp('-R', path.join(__dirname, '../font/'), distDir);
baseConfig = baseConfig('pro');
baseConfig.output.path = distDir;
baseConfig.plugins.push(
new ExtractTextPlugin('[name].css'),
new HappyPack({
id: 'js',
threadPool: happyThreadPool,
loaders: ['babel-loader'],
}),
new HappyPack({
id: 'hbs',
threadPool: happyThreadPool,
loaders: [baseConfig.extends.hbsLoader]
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
... ... @@ -31,4 +48,6 @@ baseConfig.plugins.push(
})
);
delete baseConfig.extends;
module.exports = baseConfig;
... ...