Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
* [jsfm] refactor the build script and entry files of js framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanks10100 committed Feb 1, 2018
1 parent 7d1096a commit b4194ac
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 38 deletions.
6 changes: 4 additions & 2 deletions build/build.js
Expand Up @@ -71,11 +71,13 @@ async function runRollup (config) {
async function build (name) {
let pkgName = 'weex-js-framework'
switch (name) {
case 'jsfm':
case 'native': pkgName = 'weex-js-framework'; break
case 'env': pkgName = 'weex-env'; break
case 'vue': pkgName = 'weex-vue'; break
case 'rax': pkgName = 'weex-rax'; break
case 'runtime': pkgName = 'weex-js-runtime'; break
case 'legacy': pkgName = 'weex-legacy-framework'; break
case 'legacy': pkgName = 'weex-legacy'; break
case 'vanilla': pkgName = 'weex-vanilla-framework'; break
}

Expand All @@ -99,5 +101,5 @@ async function build (name) {
function report (filePath) {
const size = (fs.statSync(filePath).size / 1024).toFixed(2) + 'KB'
const file = path.relative(process.cwd(), filePath)
console.log(` => write ${file} (${size})`)
console.log(` => ${file} (${size})`)
}
37 changes: 24 additions & 13 deletions build/config.js
Expand Up @@ -30,9 +30,8 @@ const packageJSON = require('../package.json')
const deps = packageJSON.dependencies
const subversion = packageJSON.subversion

const frameworkBanner = `;(this.getJSFMVersion = function()`
+ `{return "${subversion.framework}"});\n`
+ `var global = this; var process = {env:{}}; var setTimeout = global.setTimeout;\n`
const frameworkBanner = `var global=this; var process={env:{}}; `
+ `var setTimeout=global.setTimeout;\n`

const configs = {
'weex-js-framework': {
Expand All @@ -46,6 +45,15 @@ const configs = {
+ frameworkBanner
}
},
'weex-env': {
input: absolute('runtime/entries/env.js'),
output: {
name: 'WeexEnvironmentAPIs',
file: absolute('pre-build/weex-env'),
banner: `/* Prepare Weex Environment APIs ${subversion.framework}, Build ${now()}. */\n\n`
+ `var global = this; var process = {env:{}};`
}
},
'weex-vue': {
input: absolute('runtime/entries/vue.js'),
output: {
Expand Down Expand Up @@ -76,12 +84,14 @@ const configs = {
banner: `/* Weex JS Runtime ${subversion.framework}, Build ${now()}. */\n\n`
}
},
'weex-legacy-framework': {
input: absolute('runtime/frameworks/legacy/index.js'),
'weex-legacy': {
input: absolute('runtime/entries/legacy.js'),
output: {
name: 'WeexLegacyFramework',
file: absolute('packages/weex-legacy-framework/index'),
banner: `/* Weex Legacy Framework ${subversion.framework}, Build ${now()}. */\n`
name: 'WeexLegacy',
file: absolute('pre-build/weex-legacy'),
banner: `(this.nativeLog || function(s) {console.log(s)})`
+ `('Weex Legacy Framework ${subversion.framework}, Build ${now()}.');\n`
+ frameworkBanner
}
},
'weex-vanilla-framework': {
Expand All @@ -107,33 +117,34 @@ function getConfig (name, minify, es6) {
name: output.name,
file: output.file + suffix,
format: output.format || 'umd',
banner: output.banner
banner: output.banner,
sourcemap: true
},
plugins: opt.plugins.concat([
nodeResolve({ jsnext: true, main: true }),
json(),
replace({
'__WEEX_VERSION__': JSON.stringify(subversion.framework),
'__WEEX_VERSION__': JSON.stringify(packageJSON.version),
'__WEEX_JS_FRAMEWORK_VERSION__': JSON.stringify(subversion.framework),
'process.env.NODE_ENV': JSON.stringify(minify ? 'production' : 'development'),
'process.env.VUE_ENV': JSON.stringify('WEEX'),
'process.env.WEEX_FREEZE': JSON.stringify(!!process.env.WEEX_FREEZE),
'process.env.SUPPORT_ES2015': !!es6,
'process.env.NODE_DEBUG': false
}),
commonjs()
commonjs({ ignoreGlobal: true })
])
}
if (!es6) {
config.plugins.push(buble())
}
if (minify) {
config.output.sourcemap = true
config.plugins.push(es6
? uglify({ safari10: true, toplevel: true }, uglifyES.minify)
: uglify()
)
}
else {
config.output.sourcemap = 'inline'
config.plugins.unshift(eslint({ exclude: ['**/*.json', '**/*.css'] }))
}
return config
Expand Down
8 changes: 5 additions & 3 deletions package.json
Expand Up @@ -35,6 +35,8 @@
"node": ">=8"
},
"scripts": {
"build:jsfm": "node build/build.js jsfm",
"build:env": "WEEX_FREEZE=true node build/build.js env",
"build:native": "node build/build.js native",
"build:vue": "node build/build.js vue",
"build:rax": "node build/build.js rax",
Expand All @@ -50,15 +52,15 @@
"build:ci:web": "webpack --watch --config build/webpack.ci.web.config.js",
"build:ci:native": "webpack --watch --config build/webpack.ci.config.js",
"build:ci": "npm run build:ci:native && npm run build:ci:web",
"build": "npm run build:native && npm run build:examples && npm run build:test",
"dev:native": "node build/build.js native --watch",
"build": "npm run build:env && npm run build:jsfm && npm run build:vue && npm run build:rax",
"dev:jsfm": "node build/build.js jsfm --watch",
"dev:runtime": "node build/build.js runtime --watch",
"dev:legacy": "node build/build.js legacy --watch",
"dev:vanilla": "node build/build.js vanilla --watch",
"dev:examples": "webpack --watch --config build/webpack.examples.config.js",
"dev:examples:web": "webpack --watch --config build/webpack.examples.web.config.js",
"dev:test": "webpack --watch --config build/webpack.test.config.js",
"lint": "eslint html5",
"lint": "eslint runtime",
"test:case": "mocha --require reify test/js-framework/case/tester.js",
"test:unit": "mocha --require reify test/js-framework/unit/**/*",
"test": "npm run lint && npm run test:unit && npm run test:case",
Expand Down
11 changes: 11 additions & 0 deletions runtime/api/init.js
Expand Up @@ -52,6 +52,14 @@ function getBundleType (code) {
return 'Weex'
}

/**
* Get js framework version at runtime.
*/
function getJSFMVersion () {
// It will be converted into a version string at build time
return __WEEX_JS_FRAMEWORK_VERSION__ // eslint-disable-line
}

function createServices (id, env, config) {
// Init JavaScript services for this instance.
const serviceMap = Object.create(null)
Expand Down Expand Up @@ -108,6 +116,8 @@ function createInstanceContext (id, options = {}, data) {
const runtimeContext = Object.create(null)
Object.assign(runtimeContext, services, {
weex,
getJSFMVersion,
__WEEX_CALL_JAVASCRIPT__: receiveTasks,
services // Temporary compatible with some legacy APIs in Rax
})
Object.freeze(runtimeContext)
Expand Down Expand Up @@ -216,6 +226,7 @@ const methods = {
createInstance,
createInstanceContext,
getRoot,
getJSFMVersion,
getDocument: getDoc,
registerService: register,
unregisterService: unregister,
Expand Down
27 changes: 27 additions & 0 deletions runtime/entries/env.js
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { setNativeConsole, freezePrototype } from '../shared'
export * from '../shared'

setNativeConsole()

if (process.env.WEEX_FREEZE) {
freezePrototype()
}
2 changes: 2 additions & 0 deletions runtime/entries/index.js
Expand Up @@ -17,7 +17,9 @@
* under the License.
*/

import { freezePrototype } from './env'
import setup from './setup'
import frameworks from '../frameworks'

setup(frameworks)
freezePrototype()
23 changes: 23 additions & 0 deletions runtime/entries/legacy.js
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import setup from './setup'
import * as Weex from '../frameworks/legacy/index'

setup({ Weex })
3 changes: 0 additions & 3 deletions runtime/entries/setup.js
Expand Up @@ -18,7 +18,6 @@
*/

import { subversion } from '../../package.json'
import * as shared from '../shared'
import runtime from '../api'
import services from '../services'

Expand All @@ -36,8 +35,6 @@ export default function (frameworks) {
runtime.service.register(serviceName, services[serviceName])
}

shared.freezePrototype()
shared.setNativeConsole()
runtime.freezePrototype()

// register framework meta info
Expand Down
5 changes: 1 addition & 4 deletions runtime/frameworks/legacy/app/ctrl/init.js
Expand Up @@ -198,10 +198,7 @@ function callFunctionNative (globalObjects, body) {
try {
const weex = globalObjects.weex || {}
const config = weex.config || {}
fn = compileAndRunBundle(script,
config.bundleUrl,
config.bundleDigest,
config.codeCachePath)
fn = compileAndRunBundle(script, config.bundleUrl, config.bundleDigest, config.codeCachePath)
if (fn && typeof fn === 'function') {
fn(...globalValues)
isNativeCompileOk = true
Expand Down
6 changes: 2 additions & 4 deletions runtime/frameworks/legacy/app/ctrl/misc.js
Expand Up @@ -35,8 +35,7 @@ import { extend, typof } from '../../util/index'
* @param {any} data
*/
export function refresh (app, data) {
console.debug(`[JS Framework] Refresh with`, data,
`in instance[${app.id}]`)
console.debug(`[JS Framework] Refresh with`, data, `in instance[${app.id}]`)
const vm = app.vm
if (vm && data) {
if (typeof vm.refreshData === 'function') {
Expand Down Expand Up @@ -162,8 +161,7 @@ export function fireEvent (app, ref, type, e, domChanges) {
* @param {boolean} ifKeepAlive
*/
export function callback (app, callbackId, data, ifKeepAlive) {
console.debug(`[JS Framework] Invoke a callback(${callbackId}) with`, data,
`in instance(${app.id})`)
console.debug(`[JS Framework] Invoke a callback(${callbackId}) with`, data, `in instance(${app.id})`)
const result = app.doc.taskCenter.callback(callbackId, data, ifKeepAlive)
updateActions(app)
app.doc.taskCenter.send('dom', { action: 'updateFinish' }, [])
Expand Down
20 changes: 11 additions & 9 deletions runtime/shared/index.js
Expand Up @@ -17,16 +17,18 @@
* under the License.
*/

import './polyfill/arrayFrom'
import './polyfill/objectAssign'
import './polyfill/objectSetPrototypeOf'
if (!process.env.SUPPORT_ES2015) {
require('./polyfill/arrayFrom')
require('./polyfill/objectAssign')
require('./polyfill/objectSetPrototypeOf')

// import promise hack and polyfills
import './polyfill/promise'
import 'core-js/modules/es6.object.to-string'
import 'core-js/modules/es6.string.iterator'
import 'core-js/modules/web.dom.iterable'
import 'core-js/modules/es6.promise'
// import promise hack and polyfills
require('./polyfill/promise')
require('core-js/modules/es6.object.to-string')
require('core-js/modules/es6.string.iterator')
require('core-js/modules/web.dom.iterable')
require('core-js/modules/es6.promise')
}

export * from './env/console'
export * from './env/setTimeout'
Expand Down

0 comments on commit b4194ac

Please sign in to comment.