From 5ff24db2ff83d30529d4df4020a74f353d35244a Mon Sep 17 00:00:00 2001 From: "qingwei.li" Date: Mon, 26 Sep 2016 19:01:53 +0800 Subject: [PATCH] Refactor build scripts, fix #148 Add 'packages' folder to package.json#files --- .eslintignore | 3 ++ {bin => build/bin}/build-all.js | 6 ++-- {bin => build/bin}/build-entry.js | 6 ++-- {bin => build/bin}/iconInit.js | 4 +-- {bin => build/bin}/new.js | 8 ++--- build/config.js | 28 +++++++++++++++ {scripts => build}/cooking.common.js | 11 +++--- build/cooking.component.js | 27 ++++++++++++++ build/cooking.conf.js | 18 ++++++++++ {scripts => build}/cooking.demo.js | 11 ++---- build/release.sh | 23 ++++++++++++ {examples/utils => build}/strip-tags.js | 0 package.json | 22 ++++++------ scripts/cooking.component.js | 48 ------------------------- scripts/cooking.conf.js | 28 --------------- 15 files changed, 130 insertions(+), 113 deletions(-) rename {bin => build/bin}/build-all.js (68%) rename {bin => build/bin}/build-entry.js (90%) rename {bin => build/bin}/iconInit.js (64%) rename {bin => build/bin}/new.js (92%) create mode 100644 build/config.js rename {scripts => build}/cooking.common.js (51%) create mode 100644 build/cooking.component.js create mode 100644 build/cooking.conf.js rename {scripts => build}/cooking.demo.js (91%) create mode 100644 build/release.sh rename {examples/utils => build}/strip-tags.js (100%) delete mode 100644 scripts/cooking.component.js delete mode 100644 scripts/cooking.conf.js diff --git a/.eslintignore b/.eslintignore index 9385523f1..a16b33b19 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,5 @@ popper.js fecha.js +release.sh +node_modules +lib diff --git a/bin/build-all.js b/build/bin/build-all.js similarity index 68% rename from bin/build-all.js rename to build/bin/build-all.js index f1f4e7b99..121aa6ada 100644 --- a/bin/build-all.js +++ b/build/bin/build-all.js @@ -1,6 +1,6 @@ 'use strict'; -const components = require('../components.json'); +const components = require('../../components.json'); const execSync = require('child_process').execSync; const existsSync = require('fs').existsSync; const path = require('path'); @@ -11,10 +11,10 @@ delete components.index; delete components.font; Object.keys(components).forEach(key => { - const filePath = path.join(__dirname, `../packages/${key}/cooking.conf.js`); + const filePath = path.join(__dirname, `../../packages/${key}/cooking.conf.js`); if (existsSync(filePath)) { - componentPaths.push(`packages/${key}/cooking.conf.js`); + componentPaths.push(`../packages/${key}/cooking.conf.js`); } }); diff --git a/bin/build-entry.js b/build/bin/build-entry.js similarity index 90% rename from bin/build-entry.js rename to build/bin/build-entry.js index eee8023e6..0d03f2922 100644 --- a/bin/build-entry.js +++ b/build/bin/build-entry.js @@ -1,10 +1,10 @@ -var Components = require('../components.json'); +var Components = require('../../components.json'); var fs = require('fs'); var render = require('json-templater/string'); var uppercamelcase = require('uppercamelcase'); var path = require('path'); -var OUTPUT_PATH = path.join(__dirname, '../src/index.js'); +var OUTPUT_PATH = path.join(__dirname, '../../src/index.js'); var IMPORT_TEMPLATE = 'import {{name}} from \'../packages/{{package}}/index.js\';'; var ISNTALL_COMPONENT_TEMPLATE = ' Vue.component({{name}}.name, {{name}});'; var MAIN_TEMPLATE = `{{include}} @@ -65,7 +65,7 @@ ComponentNames.forEach(name => { var template = render(MAIN_TEMPLATE, { include: includeComponentTemplate.join('\n'), install: installTemplate.join('\n'), - version: require('../package.json').version, + version: process.env.VERSION || require('../../package.json').version, list: listTemplate.join(',\n') }); diff --git a/bin/iconInit.js b/build/bin/iconInit.js similarity index 64% rename from bin/iconInit.js rename to build/bin/iconInit.js index bdbd75554..1d23dba82 100644 --- a/bin/iconInit.js +++ b/build/bin/iconInit.js @@ -3,7 +3,7 @@ var postcss = require('postcss'); var fs = require('fs'); var path = require('path'); -var fontFile = fs.readFileSync(path.resolve(__dirname, '../packages/theme-default/src/icon.css'), 'utf8'); +var fontFile = fs.readFileSync(path.resolve(__dirname, '../../packages/theme-default/src/icon.css'), 'utf8'); var nodes = postcss.parse(fontFile).nodes; var classList = []; @@ -17,4 +17,4 @@ nodes.forEach((node) => { } }); -fs.writeFile(path.resolve(__dirname, '../examples/icon.json'), JSON.stringify(classList)); +fs.writeFile(path.resolve(__dirname, '../../examples/icon.json'), JSON.stringify(classList)); diff --git a/bin/new.js b/build/bin/new.js similarity index 92% rename from bin/new.js rename to build/bin/new.js index 4a8832b3c..7ca76e093 100644 --- a/bin/new.js +++ b/build/bin/new.js @@ -97,13 +97,13 @@ export default { ]; // 添加到 components.json -const componentsFile = require('../components.json'); +const componentsFile = require('../../components.json'); if (componentsFile[componentname]) { console.error(`${componentname} 已存在.`); process.exit(1); } componentsFile[componentname] = [`./packages/${componentname}/index.js`]; -fileSave(path.join(__dirname, '../components.json')) +fileSave(path.join(__dirname, '../../components.json')) .write(JSON.stringify(componentsFile, null, ' '), 'utf8') .end('\n'); @@ -115,7 +115,7 @@ Files.forEach(file => { }); // 添加到 nav.config.json -const navConfigFile = require('../examples/nav.config.json'); +const navConfigFile = require('../../examples/nav.config.json'); navConfigFile[navConfigFile.length - 1].list.push({ path: `/${componentname}`, name: `${chineseName} (${componentname})`, @@ -124,7 +124,7 @@ navConfigFile[navConfigFile.length - 1].list.push({ : `${componentname} ${chineseName}` }); -fileSave(path.join(__dirname, '../examples/nav.config.json')) +fileSave(path.join(__dirname, '../../examples/nav.config.json')) .write(JSON.stringify(navConfigFile, null, ' '), 'utf8') .end('\n'); diff --git a/build/config.js b/build/config.js new file mode 100644 index 000000000..90558ded1 --- /dev/null +++ b/build/config.js @@ -0,0 +1,28 @@ +var Components = require('../components.json'); +var path = require('path'); +var externals = {}; + +Object.keys(Components).forEach(function(key) { + externals[`packages/${key}/index.js`] = `element-ui/lib/${key}`; + externals[`packages/${key}/style.css`] = `element-ui/lib/${key}/style.css`; + externals['main/utils/clickoutside'] = 'element-ui/lib/utils/clickoutside'; + externals['main/utils/popper'] = 'element-ui/lib/utils/popper'; + externals['main/utils/vue-popper'] = 'element-ui/lib/utils/vue-popper'; +}); + +exports.externals = Object.assign({ + vue: { + root: 'Vue', + commonjs: 'vue', + commonjs2: 'vue', + amd: 'vue' + } +}, externals); + +exports.alias = { + main: path.resolve(__dirname, '../src'), + packages: path.resolve(__dirname, '../packages'), + examples: path.resolve(__dirname, '../examples') +}; + +exports.jsexclude = /node_modules|utils\/popper\.js|utils\/date.\js/; diff --git a/scripts/cooking.common.js b/build/cooking.common.js similarity index 51% rename from scripts/cooking.common.js rename to build/cooking.common.js index cc52048ba..79c8f7690 100644 --- a/scripts/cooking.common.js +++ b/build/cooking.common.js @@ -1,5 +1,5 @@ var cooking = require('cooking'); -var path = require('path'); +var config = require('./config'); cooking.set({ entry: './src/index.js', @@ -8,14 +8,11 @@ cooking.set({ format: 'cjs', extends: ['vue2'], minimize: false, - alias: { - main: path.join(__dirname, '../src'), - packages: path.join(__dirname, '../packages'), - examples: path.join(__dirname, '../examples') - }, + alias: config.alias, externals: { vue: 'vue' } }); cooking.add('output.filename', 'element-ui.common.js'); -cooking.add('loader.js.exclude', /node_modules|utils\/popper\.js|util\/fecha.\js/); +cooking.add('loader.js.exclude', config.jsexclude); + module.exports = cooking.resolve(); diff --git a/build/cooking.component.js b/build/cooking.component.js new file mode 100644 index 000000000..05a94e6c2 --- /dev/null +++ b/build/cooking.component.js @@ -0,0 +1,27 @@ +var cooking = require('cooking'); +var path = require('path'); +var Components = require('../components.json'); +var config = require('./config'); +var entries = {}; + +Object.keys(Components).forEach(function(key) { + const compo = Components[key]; + + compo[0] = path.join(process.cwd(), compo[0]); + entries[key] = compo; +}); + +cooking.set({ + entry: entries, + dist: './lib', + clean: false, + format: 'cjs', + extends: ['vue2'], + minimize: false, + externals: config.externals, + alias: config.alias +}); + +cooking.add('output.filename', '[name]/index.js'); +cooking.add('loader.js.exclude', config.jsexclude); +module.exports = cooking.resolve(); diff --git a/build/cooking.conf.js b/build/cooking.conf.js new file mode 100644 index 000000000..b4dd70d0c --- /dev/null +++ b/build/cooking.conf.js @@ -0,0 +1,18 @@ +var cooking = require('cooking'); +var config = require('./config'); + +cooking.set({ + entry: './src/index.js', + dist: './lib', + clean: false, + format: 'umd', + moduleName: 'ELEMENT', + extends: ['vue2'], + alias: config.alias, + externals: { vue: config.externals.vue } +}); + +cooking.add('output.filename', 'index.js'); +cooking.add('loader.js.exclude', config.jsexclude); + +module.exports = cooking.resolve(); diff --git a/scripts/cooking.demo.js b/build/cooking.demo.js similarity index 91% rename from scripts/cooking.demo.js rename to build/cooking.demo.js index d6be91829..8da2f11c5 100644 --- a/scripts/cooking.demo.js +++ b/build/cooking.demo.js @@ -1,8 +1,8 @@ var cooking = require('cooking'); -var path = require('path'); +var config = require('./config'); var md = require('markdown-it')(); var Components = require('../components.json'); -var striptags = require('../examples/utils/strip-tags'); +var striptags = require('./strip-tags'); function convert(str) { str = str.replace(/(&#x)(\w{4});/gi, function($0) { @@ -26,6 +26,7 @@ cooking.set({ chunk: true, extractCSS: true, sourceMap: true, + alias: config.alias, extends: ['vue2', 'lint'], postcss: function(webapck) { return [ @@ -99,12 +100,6 @@ var wrap = function(render) { }; }; -cooking.add('resolve.alias', { - 'main': path.join(__dirname, '../src'), - 'packages': path.join(__dirname, '../packages'), - 'examples': path.join(__dirname, '../examples') -}); - var externals = {}; Object.keys(Components).forEach(function(key) { externals[`packages/${key}/style.css`] = 'null'; diff --git a/build/release.sh b/build/release.sh new file mode 100644 index 000000000..d6e72878f --- /dev/null +++ b/build/release.sh @@ -0,0 +1,23 @@ +set -e +echo "Enter release version: " +read VERSION + +read -p "Releasing $VERSION - are you sure? (y/n)" -n 1 -r +echo # (optional) move to a new line +if [[ $REPLY =~ ^[Yy]$ ]] +then + echo "Releasing $VERSION ..." + + # build + VERSION=$VERSION npm run dist + + # commit + git add -A + git commit -m "[build] $VERSION" + npm version $VERSION --message "[release] $VERSION" + + # publish + git push origin refs/tags/v$VERSION + git push + npm publish --tag next +fi diff --git a/examples/utils/strip-tags.js b/build/strip-tags.js similarity index 100% rename from examples/utils/strip-tags.js rename to build/strip-tags.js diff --git a/package.json b/package.json index 056c8af00..4e8a32e3e 100644 --- a/package.json +++ b/package.json @@ -5,19 +5,21 @@ "main": "lib/element-ui.common.js", "files": [ "lib", - "src" + "src", + "packages" ], "scripts": { - "build:file": "node bin/iconInit.js & node bin/build-entry.js", - "dev": "npm run bootstrap && npm run build:file && cooking watch -c scripts/cooking.demo.js -p", - "dist": "npm run lint && npm run build:file && del -f lib && cooking build -c scripts/cooking.conf.js,scripts/cooking.common.js,scripts/cooking.component.js -p && npm run build:theme", - "dist:all": "node bin/build-all.js && npm run build:theme", + "build:file": "node build/bin/iconInit.js & node build/bin/build-entry.js", + "dev": "npm run bootstrap && npm run build:file && cooking watch -c build/cooking.demo.js -p", + "dist": "npm run lint && npm run build:file && del -f lib && cooking build -c build/cooking.conf.js,build/cooking.common.js,build/cooking.component.js -p && npm run build:utils && npm run build:theme", + "dist:all": "node build/bin/build-all.js && npm run build:theme", "build:theme": "gulp build --gulpfile packages/theme-default/gulpfile.js && cp-cli packages/theme-default/lib lib/theme-default", - "deploy": "npm run build:file && cooking build -c scripts/cooking.demo.js -p && echo element.eleme.io>>examples/element-ui/CNAME && gh-pages -d examples/element-ui --remote eleme && del examples/element-ui", - "bootstrap": "npm i && lerna bootstrap --loglevel=error", - "pub": "npm run dist && kp", + "deploy": "npm run build:file && cooking build -c build/cooking.demo.js -p && echo element.eleme.io>>examples/element-ui/CNAME && gh-pages -d examples/element-ui --remote eleme && del examples/element-ui", + "bootstrap": "npm i && lerna bootstrap", + "pub": "sh build/release.sh", "pub:all": "npm run dist:all && lerna publish", - "lint": "eslint src/** packages/** scripts/** bin/** --ext .js,.vue --ignore-pattern node_modules" + "build:utils": "babel src/utils --out-dir lib/utils", + "lint": "eslint src/** packages/** build/** --ext .js,.vue --quiet" }, "repository": { "type": "git", @@ -33,6 +35,7 @@ "url": "https://github.com/elemefe/element/issues" }, "devDependencies": { + "babel-cli": "^6.14.0", "babel-core": "^6.14.0", "babel-helper-vue-jsx-merge-props": "^2.0.0", "babel-loader": "^6.2.5", @@ -56,7 +59,6 @@ "html-webpack-plugin": "^2.22.0", "json-loader": "^0.5.4", "json-templater": "^1.0.4", - "kanpai": "^0.7.0", "lerna": "2.0.0-beta.18", "markdown-it": "^6.1.1", "markdown-it-container": "^2.0.0", diff --git a/scripts/cooking.component.js b/scripts/cooking.component.js deleted file mode 100644 index 56cfbeba4..000000000 --- a/scripts/cooking.component.js +++ /dev/null @@ -1,48 +0,0 @@ -var cooking = require('cooking'); -var path = require('path'); -var Components = require('../components.json'); -var entries = {}; - -Object.keys(Components).forEach(function(key) { - const compo = Components[key]; - - compo[0] = path.join(process.cwd(), compo[0]); - entries[key] = compo; -}); - -cooking.set({ - entry: entries, - dist: './lib', - clean: false, - format: 'cjs', - extends: ['vue2'], - minimize: false, - alias: { - main: path.join(__dirname, '../src'), - packages: path.join(__dirname, '../packages'), - examples: path.join(__dirname, '../examples') - } -}); - -cooking.add('output.filename', '[name]/index.js'); - -var externals = {}; -Object.keys(Components).forEach(function(key) { - externals[`packages/${key}/index.js`] = `element-ui/lib/${key}`; - externals[`packages/${key}/style.css`] = `element-ui/lib/${key}/style.css`; - externals['main/utils/clickoutside'] = 'element-ui/src/utils/clickoutside'; - externals['main/utils/popper'] = 'element-ui/src/utils/popper'; - externals['main/utils/vue-popper'] = 'element-ui/src/utils/vue-popper'; -}); - -cooking.add('externals', Object.assign({ - vue: { - root: 'Vue', - commonjs: 'vue', - commonjs2: 'vue', - amd: 'vue' - } -}, externals)); - -cooking.add('loader.js.exclude', /node_modules|utils\/popper\.js|util\/fecha.\js/); -module.exports = cooking.resolve(); diff --git a/scripts/cooking.conf.js b/scripts/cooking.conf.js deleted file mode 100644 index d36648def..000000000 --- a/scripts/cooking.conf.js +++ /dev/null @@ -1,28 +0,0 @@ -var cooking = require('cooking'); -var path = require('path'); - -cooking.set({ - entry: './src/index.js', - dist: './lib', - clean: false, - format: 'umd', - moduleName: 'ELEMENT', - extends: ['vue2'], - alias: { - main: path.join(__dirname, '../src'), - packages: path.join(__dirname, '../packages'), - examples: path.join(__dirname, '../examples') - }, - externals: { - vue: { - root: 'Vue', - commonjs: 'vue', - commonjs2: 'vue', - amd: 'vue' - } - } -}); - -cooking.add('output.filename', 'index.js'); -cooking.add('loader.js.exclude', /node_modules|utils\/popper\.js|util\/fecha.\js/); -module.exports = cooking.resolve();