1 Star 1 Fork 0

yubo_725 / WeexHandbook

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
webpack.config.js 3.37 KB
一键复制 编辑 原始数据 按行查看 历史
yubo 提交于 2018-01-12 17:35 . first commit
const pathTo = require('path');
const fs = require('fs-extra');
const webpack = require('webpack');
const entry = {};
const weexEntry = {};
const vueWebTemp = 'temp';
const hasPluginInstalled = fs.existsSync('./web/plugin.js');
var isWin = /^win/.test(process.platform);
function getEntryFileContent(entryPath, vueFilePath) {
let relativePath = pathTo.relative(pathTo.join(entryPath, '../'), vueFilePath);
let contents = '';
if (hasPluginInstalled) {
const plugindir = pathTo.resolve('./web/plugin.js');
contents = 'require(\'' + plugindir + '\') \n';
}
if (isWin) {
relativePath = relativePath.replace(/\\/g,'\\\\');
}
contents += 'var App = require(\'' + relativePath + '\')\n';
contents += 'App.el = \'#root\'\n';
contents += 'new Vue(App)\n';
return contents;
}
var fileType = '';
function walk(dir) {
dir = dir || '.';
const directory = pathTo.join(__dirname, 'src', dir);
fs.readdirSync(directory)
.forEach((file) => {
const fullpath = pathTo.join(directory, file);
const stat = fs.statSync(fullpath);
const extname = pathTo.extname(fullpath);
if (stat.isFile() && extname === '.vue' || extname === '.we') {
if (!fileType) {
fileType = extname;
}
if (fileType && extname !== fileType) {
console.log('Error: This is not a good practice when you use ".we" and ".vue" togither!');
}
const name = pathTo.join(dir, pathTo.basename(file, extname));
if (extname === '.vue') {
const entryFile = pathTo.join(vueWebTemp, dir, pathTo.basename(file, extname) + '.js');
fs.outputFileSync(pathTo.join(entryFile), getEntryFileContent(entryFile, fullpath));
entry[name] = pathTo.join(__dirname, entryFile) + '?entry=true';
}
weexEntry[name] = fullpath + '?entry=true';
} else if (stat.isDirectory() && file !== 'build' && file !== 'include') {
const subdir = pathTo.join(dir, file);
walk(subdir);
}
});
}
walk();
// web need vue-loader
const plugins = [
new webpack.optimize.UglifyJsPlugin({minimize: true}),
new webpack.BannerPlugin({
banner: '// { "framework": ' + (fileType === '.vue' ? '"Vue"' : '"Weex"') + '} \n',
raw: true,
exclude: 'Vue'
})
];
const webConfig = {
context: pathTo.join(__dirname, ''),
entry: entry,
output: {
path: pathTo.join(__dirname, 'dist'),
filename: '[name].web.js',
},
module: {
// webpack 2.0
rules: [
{
test: /\.js$/,
use: [{
loader: 'babel-loader'
}],
exclude: /node_modules/
},
{
test: /\.vue(\?[^?]+)?$/,
use: [{
loader: 'vue-loader'
}]
}
]
},
plugins: plugins
};
const weexConfig = {
entry: weexEntry,
output: {
path: pathTo.join(__dirname, 'dist'),
filename: '[name].js',
},
module: {
rules: [
{
test: /\.js$/,
use: [{
loader: 'babel-loader',
}],
exclude: /node_modules(?!\/.*(weex).*)/
},
{
test: /\.vue(\?[^?]+)?$/,
use: [{
loader: 'weex-loader'
}]
},
{
test: /\.we(\?[^?]+)?$/,
use: [{
loader: 'weex-loader'
}]
}
]
},
plugins: plugins,
};
var exports = [webConfig, weexConfig];
if (fileType === '.we') {
exports = weexConfig;
}
module.exports = exports;
1
https://gitee.com/yubo725/WeexHandbook.git
git@gitee.com:yubo725/WeexHandbook.git
yubo725
WeexHandbook
WeexHandbook
master

搜索帮助