前言

创建自己的项目脚手架

安装开发工具

1
npm install -g yo generator-generator

创建脚手架项目

1
yo generator

输入脚手架名称 说明、版权、作者、官网等等信息

目录分析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
├── generators                    脚手架目录
│   └── app                       脚手架主程序目录
│       ├── templates             脚手架模板代码
│       └── index.js              脚手架主程序
├── test                          测试目录
│   └── app.js
├── .editorconfig                 书写规范文件
├── .gitattributes                指定非文本文件的对比合并方式
├── .gitignore                    用于忽略你不想提交到Git上的文件
├── .travis.yml                   Travis持续集成配置
├── LICENSE                       版权
├── README.md                     说明
├── gulpfile.js                   gulp配置
└── package.json                  nodejs包配置

入口程序index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'use strict';
var Generator = require('yeoman-generator');
var chalk = require('chalk');
var yosay = require('yosay');

module.exports = Generator.extend({

  // 交互输入选项
  prompting: function () {
    
    // 启动画面文字
    this.log(yosay(
      'Welcome to the legendary ' + chalk.red('generator-itcast-webapp') + ' generator!'
    ));

    // 是非询问
    var prompts = [{
      type: 'confirm',
      name: 'someAnswer',
      message: 'Would you like to enable this option?',
      default: true
    }];

    // 保存交互输入信息
    return this.prompt(prompts).then(function (props) {
      // To access props later use this.props.someAnswer;
      this.props = props;
    }.bind(this));
  },

  // 写操作
  writing: function () {
    // 支持模板
    this.fs.copy(
      this.templatePath('dummyfile.txt'),
      this.destinationPath('dummyfile.txt')
    );
  },

  // 最后安装依赖项目
  install: function () {
    this.installDependencies();
  }
});

本地方式发布

1
npm link

你可以把脚手架项目放到git空间 用户可以git clone下来 如果有更新git pull操作

npm平台发布

如果你有 npmjs 平台的账号直接发布上传 用户每次就可以直接 npm install -g generator-

1
你的脚手架项目名称
貌似每次脚手架更新还是需要更新一次,性质和git一样,但是都是
1
npm cli
操作比较方便

  • 注册账号

https://www.npmjs.com/signup

  • cli登录
1
npm login

输入账号、密码、联系email

  • 发布
1
npm publish
  • 常见错误

no_perms Private mode enable, only admin can publish this module 修改仓库地址 npm config set registry http://registry.npmjs.org 修改完后 还是需要重新 npm login

每次上传前记得修改版本号 package.json version ++

安装使用

1
yo 你的脚手架项目名称

如何使用就不多说了,yo 命令就行 ,你可以发现

1
generator-
是不用输入的

Read More

安装

1
brew install tree

打印目录

  • 过滤文件夹
1
tree -L 3 -I "node_modules"
  • 只显示目录
1
tree d -L 3 -I "node_modules"

– 排序目录优先

1
tree -L 3 -I "node_modules" --dirsfirst

– 所有文件目录

1
tree -L 3 -I "node_modules" --dirsfirst -a

– 支持中文

1
tree -N

帮助

1
tree --help

Read More

前言

配置一个可实时预览的环境

安装

1
npm install --save-dev gulp-live-server

配置

1
2
3
4
5
6
7
var gulp = require('gulp');
var webserver = require('gulp-webserver');
 
gulp.task('webserver', function() {
  var server = gls.static('release/dist', 8080);
  server.start();
});

release/dist 请指向你的发布目录

运行

  • 输入
1
gulp webserver
  • 输出
1
2
3
4
5
[14:31:06] Using gulpfile ~/gulpfile.js
[14:31:06] Starting 'webserver'...
[14:31:06] Finished 'webserver' after 10 ms
livereload[tiny-lr] listening on 35729 ...
folder "release/dist" serving at http://localhost:8080

参考文

https://www.npmjs.com/package/gulp-live-server

Read More

安装

1
2
3
4
5
6
7
8
9
10
11
1. 全局安装 gulp
npm install gulp-cli -g

2. 作为项目的开发依赖(devDependencies)安装
npm install gulp --save-dev

3. 在项目根目录下创建一个名为 gulpfile.js 的文件
touch gulpfile.js

4. 运行
gulp

配置 gulpfile.js

  • 标准格式
1
2
3
4
5
var gulp = require('gulp');

gulp.task('default', function() {
  // place code for your default task here
});

default 是默认任务

运行

  • 输入
1
gulp
  • 输出
1
2
3
[13:26:37] Using gulpfile ~/Documents/test/gulp-test/gulpfile.js
[13:26:37] Starting 'default'...
[13:26:37] Finished 'default' after 67 μs

上面跑通了,但是啥都没干。。。

官文

https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md

一个构建工具基础功能需要满足如下

  • webserver环境 gulp-live-server

  • 定位资源 gulp-html-replace

  • 内容嵌入

  • 支持模板编译 gulp-ejs

  • css模块编译(sass less)
  • js模块编译(amd cmd es6)
  • 资源压缩(图片、css、js)
  • md5文件版本控制 gulp-rev gulp-rev-collector

  • 清理文件 gulp-clear

  • 部署(zip、git、ftp、sftp、http) gulp-git gulp-ssh

见后续文章

插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var gulp = require('gulp'),  
browserify = require('browserify'),//这里用不上,管理js依赖的
source = require('vinyl-source-stream'),//同样这里用不上,和上面那个一起的
uglify = require('gulp-uglify'),//混淆js
clean = require('gulp-clean'),//清理文件
notify = require('gulp-notify'),//加控制台文字描述用的
buffer = require('vinyl-buffer'),
less = require('gulp-less'),//转换less用的
autoprefixer = require('gulp-autoprefixer'),//增加私有变量前缀
minifycss = require('gulp-minify-css'),//压缩
concat = require('gulp-concat'),//合并
fileinclude = require('gulp-file-include'),// include 文件用
template = require('gulp-template'),//替换变量以及动态html用
rename = require('gulp-rename'),//重命名
webserver = require('gulp-webserver'),//一个简单的server,用python的SimpleHttpServer会锁文件夹
imagemin = require('gulp-imagemin'),//图片压缩
gulpif  = require('gulp-if'),//if判断,用来区别生产环境还是开发环境的
rev  = require('gulp-rev'),//加MD5后缀
revReplace = require('gulp-rev-replace'),//替换引用的加了md5后缀的文件名,修改过,用来加cdn前缀
addsrc = require('gulp-add-src'),//pipeline中途添加文件夹,这里没有用到
del = require('del'),//也是个删除··· 
vinylPaths = require('vinyl-paths'),//操作pipe中文件路径的,加md5的时候用到了
runSequence = require('run-sequence');//控制task顺序

Read More

Plugin

插件(Plugins)是用来拓展Webpack功能的,它们会在整个构建过程中生效,执行相关的任务。 Loaders和Plugins常常被弄混,但是他们其实是完全不同的东西,可以这么来说,loaders是在打包构建过程中用来处理源文件的(JSX,Scss,Less..),一次处理一个,插件并不直接操作单个文件,它直接对整个构建过程其作用。

内置插件 - BannerPlugin

  • webpack.config.js
1
2
3
4
5
6
7
8
    var webpack = require('webpack');

    ...

    plugins: [
        new webpack.BannerPlugin("Copyright Flying Unicorns inc.")//在这个数组中new一个就可以了
    ],
  • 运行
1
npm start

第三方插件 HtmlWebpackPlugin

  • 安装
1
npm install --save-dev html-webpack-plugin
  • app/index.tmpl.html
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Webpack Sample Project</title>
  </head>
  <body>
    <div id='root'>
    </div>
  </body>
</html>
  • webpack.config.js
1
2
3
4
5
    plugins: [
        new HtmlWebpackPlugin({
            template: __dirname + "/app/index.tmpl.html"//new 一个这个插件的实例,并传入相关的参数
        })
    ],
  • 运行
1
npm start

Hot Module Replacement 自动刷新

Hot Module Replacement(HMR)也是webpack里很有用的一个插件,它允许你在修改组件代码后,自动刷新实时预览修改后的效果。 在webpack中实现HMR也很简单,只需要做两项配置

在webpack配置文件中添加HMR插件; 在Webpack Dev Server中添加“hot”参数;

不过配置完这些后,JS模块其实还是不能自动热加载的,还需要在你的JS模块中执行一个Webpack提供的API才能实现热加载,虽然这个API不难使用,但是如果是React模块,使用我们已经熟悉的Babel可以更方便的实现功能热加载。

整理下我们的思路,具体实现方法如下

Babel和webpack是独立的工具 二者可以一起工作 二者都可以通过插件拓展功能 HMR是一个webpack插件,它让你能浏览器中实时观察模块修改后的效果,但是如果你想让它工作,需要对模块进行额外的配额; Babel有一个叫做react-transform-hrm的插件,可以在不对React模块进行额外的配置的前提下让HMR正常工作;

  • 安装
1
npm install --save-dev babel-plugin-react-transform react-transform-hmr
  • webpack.config.js
1
2
3
4
    plugins: [
        ...,
        new webpack.HotModuleReplacementPlugin()//热加载插件
    ],

.babelrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    "presets": [
        "es2015",
        "react"
    ],
    "env": {
        "development": {
            "plugins": [
                [
                    "react-transform",
                    {
                        "transforms": [
                            {
                                "transform": "react-transform-hmr",
                                "imports": [
                                    "react"
                                ],
                                "locals": [
                                    "module"
                                ]
                            }
                        ]
                    }
                ]
            ]
        }
    }
}

产品阶段的构建

  • 独立一个 webpack.production.config.js
1
2
3
4
5
6
    ....
    output: {
        path: __dirname + "/release",//打包后的文件存放的地方
        filename: "bundle.js"//打包后输出文件的文件名
    },
    ...
  • package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  "name": "webpack-sample-project",
  "version": "1.0.0",
  "description": "Sample webpack project",
  "scripts": {
    "start": "webpack-dev-server --progress",
    "release": "NODE_ENV=production webpack --config ./webpack.production.config.js --progress"
  },
  "author": "Cássio Zen",
  "license": "ISC",
  "devDependencies": {...},
  "dependencies": {...}
}

就是指定配置文件

  • 运行
1
npm run release

优化插件 extract-text-webpack-plugin

webpack提供了一些在发布阶段非常有用的优化插件,它们大多来自于webpack社区,可以通过npm安装,通过以下插件可以完成产品发布阶段所需的功能

OccurenceOrderPlugin :为组件分配ID,通过这个插件webpack可以分析和优先考虑使用最多的模块,并为它们分配最小的ID UglifyJsPlugin:压缩JS代码; ExtractTextPlugin:分离CSS和JS文件

https://webpack.js.org/guides/production-build/

Read More

Loader

介绍

Loaders是webpack中最让人激动人心的功能之一了。

  • 配置选项

test:一个匹配loaders所处理的文件的拓展名的正则表达式(必须) loader:loader的名称(必须) include/exclude:手动添加必须处理的文件(文件夹)或屏蔽不需要处理的文件(文件夹)(可选); query:为loaders提供额外的设置选项(可选)

处理 json 的例子

  • 安装 json-loader
1
npm install --save-dev json-loader
  • webpack.config.js
1
2
3
4
5
6
7
8
    module: {
        loaders: [
            {
                test: /\.json$/,
                loader: "json-loader"
            }
        ]
    },
  • 运行
1
npm start

Babel 一个编译JavaScript的平台

  • 安装插件
1
2
3
4
5
6
7
8
9
10
11
核心包
npm install --save-dev babel-core babel-loader

解析Es6的babel-preset-es2015包
npm install --save-dev babel-preset-es2015

解析JSX的babel-preset-react包
npm install --save-dev babel-preset-react

使用React包
npm install --save-dev react react-dom
  • Greeter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import React, {Component} from 'react'
import config from './config.json';

class Greeter extends Component{
  render() {
    return (
      <div>
        {config.greetText}
      </div>
    );
  }
}

export default Greeter
  • main.js
1
2
3
4
5
import React from 'react';
import {render} from 'react-dom';
import Greeter from './greeter';

render(<Greeter />, document.getElementById('root'));
  • webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    module: {
        loaders: [
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader',//在webpack的module部分的loaders里进行配置即可
                query: {
                    presets: ['es2015','react']
                }
            }
        ]
    },
  • 运行
1
npm start
  • webpack会自动调用.babelrc里的babel配置选项
1
2
3
4
//.babelrc
{
  "presets": ["react", "es2015"]
}

CSS

  • 安装
1
npm install --save-dev style-loader css-loader
  • main.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
html {
  box-sizing: border-box;
  -ms-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

*, *:before, *:after {
  box-sizing: inherit;
}

body {
  margin: 0;
  font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background-color: blanchedalmond;
}

h1, h2, h3, h4, h5, h6, p, ul {
  margin: 0;
  padding: 0;
}
  • main.js
1
2
3
4
5
6
7
import React from 'react';
import {render} from 'react-dom';
import Greeter from './greeter';

import './main.css';//使用require导入css文件

render(<Greeter />, document.getElementById('root'));
  • Greeter.css
1
2
3
4
5
.root {
  background-color: #eee;
  padding: 10px;
  border: 3px solid #ccc;
}
  • Greeter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import React, {Component} from 'react'
import config from './config.json';
import styles from './Greeter.css';//导入

class Greeter extends Component{
  render() {
    return (
      <div className={styles.root}>
        {config.greetText}
      </div>
    );
  }
}

export default Greeter
  • webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module.exports = {
    devtool: 'eval-source-map',
    entry:  __dirname + "/app/main.js",//已多次提及的唯一入口文件
    output: {
        path: __dirname + "/build",//打包后的文件存放的地方
        filename: "bundle.js"//打包后输出文件的文件名
    },

    module: {
        loaders: [
            {
                test: /\.json$/,
                loader: "json-loader"
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel-loader'//在webpack的module部分的loaders里进行配置即可
            },
            {
                test: /\.css$/,
                loaders: [
                    'style-loader',
                    'css-loader?modules',
                    'postcss-loader'
                ]
            }
        ]
    },

    devServer: {
        contentBase: "./public",//本地服务器所加载的页面所在的目录
        compress: true,
        port: 9000
    }
}
  • 运行
1
npm start

PostCSS

一个CSS的处理平台-PostCSS https://github.com/postcss/postcss

  • webpack.config.js
1
2
3
4
5
6
7
8
    {
        test: /\.css$/,
        loaders: [
            'style-loader',
            'css-loader?modules',
            'postcss-loader'
        ]
    }
  • 配置文件 postcss.config.js
1
2
3
4
5
module.exports = {
    plugins: [
        require('autoprefixer')
    ]
}

Read More

开始

初始目录

1
npm init

本地安装

1
npm install webpack --save-dev

编译文件

  • 新建 ./public/index.html
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Webpack Sample Project</title>
  </head>
  <body>
    <div id='root'>
    </div>
    <script src="bundle.js"></script>
  </body>
</html>
  • 新建 ./app/main.js
1
2
3
4
5
module.exports = function() {
  var greet = document.createElement('div');
  greet.textContent = "Hi there and greetings!";
  return greet;
};
  • 新建 ./app/greeter.js
1
2
var greeter = require('./greeter.js');
document.getElementById('root').appendChild(greeter());
  • 编译
1
node_modules/.bin/webpack app/main.js public/bundle.js

配置文件方式

  • 添加 webpack.config.js
1
2
3
4
5
6
7
module.exports = {
  entry:  __dirname + "/app/main.js",//已多次提及的唯一入口文件
  output: {
    path: __dirname + "/public",//打包后的文件存放的地方
    filename: "bundle.js"//打包后输出文件的文件名
  }
}
  • 编译
1
node_modules/.bin/webpack

简化本地调用

  • 修改 package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "name": "02",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^2.2.1"
  }
}

package.json中的脚本部分已经默认在命令前添加了node_modules/.bin路径,所以无论是全局还是局部安装的Webpack,你都不需要写前面那指明详细的路径了。

  • 运行
1
npm start

npm的start是一个特殊的脚本名称,它的特殊性表现在,在命令行中使用npm start就可以执行相关命令,如果对应的此脚本名称不是start,想要在命令行中运行时,需要这样用npm run {script name}如npm run build

Source Maps 帮助调试

  • devtool选项
选项 配置结果
source-map 在一个单独的文件中产生一个完整且功能完全的文件。这个文件具有最好的source map,但是它会减慢打包文件的构建速度;
cheap-module-source-map 在一个单独的文件中生成一个不带列映射的map,不带列映射提高项目构建速度,但是也使得浏览器开发者工具只能对应到具体的行,不能对应到具体的列(符号),会对调试造成不便;
eval-source-map 使用eval打包源文件模块,在同一个文件中生成干净的完整的source map。这个选项可以在不影响构建速度的前提下生成完整的sourcemap,但是对打包后输出的JS文件的执行具有性能和安全的隐患。不过在开发阶段这是一个非常好的选项,但是在生产阶段一定不要用这个选项;
cheap-module-eval-source-map 这是在打包文件时最快的生成source map的方法,生成的Source Map 会和打包后的JavaScript文件同行显示,没有列映射,和eval-source-map选项具有相似的缺点;
  • webpack.config.js
1
2
3
4
module.exports = {
    devtool: 'eval-source-map',
    entry:  ...
}

本地WebServer开发

  • 安装
1
npm install webpack-dev-server --save-dev
  • 配置选项

https://webpack.js.org/configuration/dev-server/#components/sidebar/sidebar.jsx

  • webpack.config.js
1
2
3
4
5
6
7
8
module.exports = {
    ...,
    devServer: {
        contentBase: "./public",//本地服务器所加载的页面所在的目录
        compress: true,
        port: 9000
    }
}
  • 运行
1
node_modules/.bin/webpack-dev-server

or package.json

1
2
3
4
5
6
7
8
{
  ...,
  "scripts": {
    "start": "webpack",
    "server": "webpack-dev-server"
  },
  ...
}
1
npm run server

Loader

Read More

本文用到的版本是 webpack v1

安装

全局

1
sudo npm install webpack -g

当前项目

1
2
npm install webpack@1.14.x --save
node_modules/.bin/webpack entry.js bundle.js

查看版本信息

1
npm info webpack

安装指定版本

1
sudo npm install webpack@1.14.x -g

卸载

1
2
卸载全局安装模块
npm uninstall -g webpack

使用

由于现在webpack出了2版本,简单起见我都是项目本地方式运行

1
node_modules/.bin/webpack ...

初始项目目录

1
npm init

打包文件 bundle.js

  • 文件 index.html
1
2
3
4
5
6
7
8
<html>
<head>
  <meta charset="utf-8">
</head>
<body>
  <script src="bundle.js"></script>
</body>
</html>
  • 文件 entry.js
1
document.write('It works.')
  • 编译
1
node_modules/.bin/webpack ./entry.js bundle.js

require模块引用

  • 新建 content.js
1
module.exports = "It works from content.js.";
  • 修改 entry.js
1
+ document.write(require("./content.js"));
  • 编译
1
node_modules/.bin/webpack ./entry.js bundle.js

编译 css

  • 安装插件
1
npm install css-loader style-loader --save
  • 添加 style.css
1
2
3
body {
    background: yellow;
}
  • 修改 entry.js
1
2
require("!style!css!./style.css");
document.write(require("./content.js"));
  • 编译
1
node_modules/.bin/webpack ./entry.js bundle.js

简化 css 页面标签

  • 修改 entry.js
1
2
require("./style.css");
document.write(require("./content.js"));
  • 编译
1
node_modules/.bin/webpack ./entry.js bundle.js --module-bind 'css=style!css'

加入了参数 –module-bind “css=style!css”

使用配置文件

  • 添加 webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
    entry: "./entry.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            { test: /\.css$/, loader: "style!css" }
        ]
    }
};
  • 编译
1
node_modules/.bin/webpack

编译时显示彩色和进度

1
node_modules/.bin/webpack --progress --colors

监控模式

1
node_modules/.bin/webpack --watch

本地开始一个express server 方便调试

  • 安装
1
2
3
sudo npm install webpack-dev-server -g
or
npm install webpack-dev-server@1.x --save
  • 运行
1
2
3
webpack-dev-server --progress --colors
or
node_modules/.bin/webpack-dev-server --progress --colors

http://localhost:8080

打印错误信息

1
node_modules/.bin/webpack --display-error-details

文档

http://webpack.github.io/docs/usage.html

Read More

前言

在nodejs的帮助下,我们可以用 javascript+html+css 创建出丰富的客户端

在某些情况下,希望自己写的程序能够独立运行,而不是打开浏览器的方式

一次开发在多个平台上运行 macos linux windows

安装electron环境

  • 官网

http://electron.atom.io/

  • 我们这里安装全局方式
1
npm install -g electron

创建一个helloword项目

  • 创建
1
2
3
4
mkdir ele-app-demo
cd ele-app-demo
npm init
npm install electron --save
  • 项目结构
1
2
3
\package.json
\index.html
\main.js
  • 配置文件package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
  "name": "ele-app-demo",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^1.4.1"
  }
}

main 启动文件 scripts->start 启动的脚本

  • 主程序 main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// 全局窗口对象
let win

function createWindow () {

  // 创建浏览器窗口对象
  win = new BrowserWindow({width: 800, height: 600})

  // 读取index.html 为首页
  win.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))

  // 打开调试工具,发布的时候请关闭
  win.webContents.openDevTools()

  // 关闭 - 消息处理
  win.on('closed', () => {
    
    // 关闭不必要的资源占用(内存、流)
    win = null
  })
}

// 首次启动环境加载完成 - 消息处理
app.on('ready', createWindow)

// 当所有的窗口被关闭 - 消息处理
app.on('window-all-closed', () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

// 激活窗口 - 消息处理
app.on('activate', () => {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (win === null) {
    createWindow()
  }
})

  • 界面 index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

运行

1
2
3
4
5
npm run

或者

electron .

打包发布 electron-packager 方式

  • 官网

https://github.com/electron-userland/electron-packager

  • 安装
1
2
3
4
5
项目内
npm install electron-packager --save-dev

全局使用
npm install electron-packager -g
  • 命令格式
1
electron-packager <sourcedir> <appname> --platform=<platform> --arch=<arch> [optional flags...]
  • 文档参考

https://github.com/electron-userland/electron-packager/blob/master/usage.txt

  • 发布成3平台
1
2
3
4
5
6
7
8
electron-packager ./ --all
electron-packager ./ --platform=win32 --arch=x64 --overwrite
electron-packager ./ --platform=darwin --arch=x64 --overwrite

成功后项目根目录下有3个平台的文件夹
xxx-darwin-x64
xxx-linux-ia32
xxx-window-x64

Electron API 文档

https://github.com/electron/electron-api-demos/

坑1 - node install.js 卡卡卡

1
2
3
4
5
vi ~/.npmrc
registry = https://registry.npm.taobao.org
sass_binary_site = https://npm.taobao.org/mirrors/node-sass/
phantomjs_cdnurl = http://npm.taobao.org/mirrors/phantomjs/
electron_mirror = http://npm.taobao.org/mirrors/electron/

坑2 - 发布windows下的包需要 wine

  • 文档参考

https://www.davidbaumgold.com/tutorials/wine-mac/ https://github.com/electron-userland/electron-packager#building-windows-apps-from-non-windows-platforms https://www.winehq.org/announce/1.8.6

  • Homebrew 方式安装 ``` ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)” brew doctor sudo xcodebuild -license

brew tap caskroom/cask brew cask install java xquartz brew install wine ==> Installing dependencies for wine: jpeg, pkg-config, libtool, libusb, libusb-compat, fontconfig, libtiff, webp, gd, libgphoto2, little-cms2, cmake, jasper, libicns, makedepend, openssl, net-snmp, sane-backends, libtasn1, gmp, nettle, libunistring, libffi, p11-kit, gnutls 很多wine的依赖包都是国外的,耐心点下载吧 ```

我的博客

Read More

官网

http://www.semantic-ui.cn/

方式一:下载最终源码直接使用

下载地址

https://github.com/Semantic-Org/Semantic-UI

使用

/dist/ 目录就是最终发行代码文件 /dist/semantic.min.js /dist/semantic.min.css

1
2
3
<script src=".public/jquery/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="./public/semantic/semantic.css">
<script src="./public/semantic/semantic.js"></script>

请先包含jquery.min.js文件

方式二:源码编译方式

第一步: 安装NodeJS环境

  • Homebrew 方式
1
brew install node
  • Git 方式
1
2
3
4
5
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

第二步: 安装Gulp

1
npm install -g gulp

第三步: npm install 安装并编译

1
2
3
npm install semantic-ui --save
cd semantic/
gulp build

使用

1
2
<link rel="stylesheet" type="text/css" href="semantic/dist/semantic.min.css">
<script src="semantic/dist/semantic.min.js"></script>

我的博客

Read More