Skip to content

Commit

Permalink
⚡ 优化 eslint-loader babel-loader 配置 & 优化打包信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Jogiter committed Sep 18, 2018
1 parent 09e9af1 commit e51cbd1
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 201 deletions.
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
dist
__dist
assets
__thunder
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
rules: {
indent: ["warn", 4],
quotes: ["error", "single"],
semi: ["error", "always"]
semi: ["error", "always"],
"no-console": ["warn"]
},
globals: {
$: true,
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Thumbs.db
node_modules/
bower_components/
.deploy*/
dist
__dist
assets
__thunder
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ module.exports = {
template: 'index.html',
publicPath: '/',
hashDigestLength: 6,
__cacheDir: './__dist/',
__destination: './dist/',
__cacheDir: './__thunder/',
__destination: './assets/',
__ftp: {
'host': 'host',
'port': 'port',
Expand All @@ -96,8 +96,8 @@ module.exports = {
|template|可选|[html-webpack-plugin 中的模板页面](https://github.com/jantimon/html-webpack-plugin)|配置文件同级目录下的 `index.html`|
|publicPath|可选|[此输出目录对应的公开 URL](https://webpack.docschina.org/configuration/output/#output-publicpath)|`/`|
|hashDigestLength|可选|散列摘要的前缀长度|6|
|__cacheDir|可选|缓存打包后生成的文件,用于上传及拷贝到上线目录|`./__dist/`|
|__destination|可选|打包后实际生成目录|`./dist/`|
|__cacheDir|可选|缓存打包后生成的文件,用于上传及拷贝到上线目录|`./__thunder/`|
|__destination|可选|打包后实际生成目录|`./assets/`|
|__ftp|可选|ftp 配置,参考[node-scp2](https://github.com/spmjs/node-scp2)|默认不开启ftp|


Expand All @@ -111,10 +111,14 @@ module.exports = {
+ [ ] 本地的 `ssi` 使用 ssi-loader 打包到页面中,减少 `ssi` 的使用。
+ [ ] prefetch & preload 插件
+ [ ] performence 性能配置
+ [ ] `.browserslistrc` 配置
+ [ ] `.eslintrc` 配置
+ [ ] 添加测试


## 阅读链接

+ [Webpack 中使用的Node.js API](https://www.jianshu.com/p/d3272c8dd9bf)
+ [Webpack progress using node.js API](https://stackoverflow.com/questions/31052991/webpack-progress-using-node-js-api)
+ [@babel/preset-env](https://babeljs.io/docs/en/next/babel-preset-env.html)
+ [@babel/plugin-transform-runtime](https://babeljs.io/docs/en/next/babel-plugin-transform-runtime.html)
4 changes: 2 additions & 2 deletions demo/.thunderrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module.exports = {
template: 'index.html',
publicPath: '/',
hashDigestLength: 12,
__cacheDir: './__dist/',
__destination: './dist/'
__cacheDir: './__thunder/',
__destination: './assets/'
};
20 changes: 18 additions & 2 deletions demo/js/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
var msg = 'world';
alert('hello' + msg);
async function t() {
let msg = await new Promise(resolve => {
setTimeout(function() {
resolve('hello');
}, 1000);
});
alert(msg);
}

function main() {
let [a, b, c] = [1, 2, 3];
let msg = 'world';
let total = a + b + c;
document.write(`<h2>hello${msg}:${total}</h2>`);
t();
}

main();
77 changes: 39 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function initWebpackConfig(option, isProduction) {
chunkFilename: 'chunk[id].js',
publicPath: publicPath
},
devtool: isProduction ? false : 'eval',
devtool: isProduction ? false : 'cheap-module-eval-source-map',
// 在第一个错误出现时抛出失败结果,而不是容忍它
bail: isProduction,
optimization: {
minimize: isProduction
},
Expand All @@ -40,6 +42,7 @@ function initWebpackConfig(option, isProduction) {
{
test: /\.js$/,
enforce: 'pre',
exclude: /node_modules/,
use: [
{
loader: 'eslint-loader',
Expand All @@ -57,15 +60,7 @@ function initWebpackConfig(option, isProduction) {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: [
[
'@babel/plugin-transform-runtime',
{
helpers: false,
regenerator: true
}
]
]
plugins: ['@babel/plugin-transform-runtime']
}
}
]
Expand Down Expand Up @@ -141,7 +136,9 @@ function initWebpackConfig(option, isProduction) {
},
plugins: [
new FriendlyErrorsWebpackPlugin(),
new CleanWebpackPlugin([__cacheDir]),
new CleanWebpackPlugin([__cacheDir], {
verbose: false
}),
new HtmlWebpackPlugin({
filename: 'index.html',
template: template,
Expand All @@ -163,22 +160,24 @@ function initWebpackConfig(option, isProduction) {
});
}

config = merge(config, {
plugins: [
new FileManagerPlugin({
onEnd: [
{
copy: [
{
source: __cacheDir,
destination: __destination
}
]
}
]
})
]
});
if (isProduction) {
config = merge(config, {
plugins: [
new FileManagerPlugin({
onEnd: [
{
copy: [
{
source: __cacheDir,
destination: __destination
}
]
}
]
})
]
});
}
return config;
}

Expand All @@ -188,8 +187,8 @@ function setConfig(config) {
template: 'index.html',
hashDigestLength: 6,
publicPath: '/',
__cacheDir: './__dist/',
__destination: './dist/'
__cacheDir: './__thunder/',
__destination: './assets/'
};
config = Object.assign({}, DEFAULT, config);
if (config.__ftp) {
Expand All @@ -206,15 +205,17 @@ module.exports = function bundle(config, cli) {
let webpackConfig = initWebpackConfig(config, isProduction);

function handler(err, stats) {
console.log(
stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
})
);
if (!stats.hasErrors() && !stats.hasWarnings()) {
console.log(
stats.toString({
colors: true,
modules: false,
children: false,
chunks: false,
chunkModules: false
})
);
}
}

if (isProduction) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@babel/core": "^7.0.1",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.0.0",
"babel-eslint": "^9.0.0",
"babel-loader": "^8.0.2",
"clean-webpack-plugin": "^0.1.19",
Expand Down
5 changes: 2 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const thunder = require('../');

test('title', t => {
process.chdir('../demo');
console.log(thunder);
thunder()
t.true()
thunder();
t.true();
});
Loading

0 comments on commit e51cbd1

Please sign in to comment.