Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jspm #46

Open
uniquejava opened this issue May 5, 2016 · 0 comments
Open

jspm #46

uniquejava opened this issue May 5, 2016 · 0 comments

Comments

@uniquejava
Copy link
Owner

uniquejava commented May 5, 2016

jspm is on top of babel and systemjs.

阅读:
https://www.zfanw.com/blog/tag/jspm

官方文档:
http://jspm.io/docs/getting-started.html

这里我写了个demo
https://github.com/uniquejava/jspm-demo
REAMD.md中详细的步骤...

以下是些最基本的.

安装

下载的包放在jspm_packages中, 配置文件为config.js

安装包

jspm install jquery 
会自动在config.js中加上map
"jquery": "npm:[email protected]",

或者
jspm install github:components/jquery
对应的map为
"components/jquery": "github:components/[email protected]",

或者手动给定别名
jspm install jquery=github:components/jquery
对应的map为
"jquery": "github:components/[email protected]",

根据别名移除包
jspm uninstall jquery 或 jspm unstall components/jquery
此命令1)从package.json中移除jspm下对应的dependencies, 2)从config.js中移除对应的map, 3)从jspm_packages目录下移除对应的文件.

所有已注册的包(能直接通过别名安装的):
https://github.com/jspm/registry/blob/master/registry.json

实例

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script src="jspm_packages/system.js"></script>
  <script src="config.js"></script>
  <script>
    System.import('app/main').then(function (app) {
      app.init();
    });
  </script>

</body>
</html>

app/main.js

'use strict';

import {sayHello} from './hello';

export function init() {
  sayHello();
}

app/hello.js

'use strict';

import $ from 'jquery';

export function sayHello() {
  $('body').text('sayHello');
}

运行

npm install -g jspm-server
然后在项目根目录下执行jspm-server, 会自动打开浏览器到http://127.0.0.1:8080/ 并开启livereload.

打包

以上System.import有个问题, 就是每个js文件都会发一次http请求,如果js文件很多, 就会产生大量的http请求.这时可以打包

jspm bundle app/main --minify --no-mangle --inject --skip-source-maps
  1. app/main是程序的入口.bundle会从这个文件开始将所有依赖的import全部打包到build.js中

  2. 后面的minify和no-mangle好理解和uglifyjs一样.(压缩, + 不混淆代码)

  3. inject是在bundle的过程中自动在config.js中加入如下这段

bundles: {
  "build.js": [
    "app/main.js",
    "app/hello.js",
    "npm:[email protected]",
    "npm:[email protected]/underscore.js",
    "npm:[email protected]",
    "npm:[email protected]/dist/jquery.js"
  ]
},

这样可以在index.html少写下面这行
<script src="build.js"></script>
也可以在jspm bundle 命令中指定生成的文件名, 默认为build.js

最终的index.html如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <script src="jspm_packages/system.js"></script>
  <script src="config.js"></script>
  <script>
    System.import('app/main');
  </script>
</head>
<body>
</body>
</html>

这样总共只会发送3次对js文件的请求..system.js, config.js以及build.js

更绝: 打包成一个自执行的js文件.

执行jspm bundle-sfx app/main --minify --no-mangle --skip-source-maps

这样index.html可以精简成如下形式:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <script src="build.js"></script>
</body>
</html>

以上的打包命令我写到了package.json中, 执行npm run all-in-one即可. 见: https://github.com/uniquejava/jspm-demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant