From 9b1ca4443ebc45120d89f476fafdf3ddbd0c4ffe Mon Sep 17 00:00:00 2001 From: twp0217 Date: Sun, 4 Jun 2017 23:17:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(umd):=20=E5=A2=9E=E5=8A=A0echarts-ng2.umd.?= =?UTF-8?q?js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + config/helpers.js | 32 +++++++ package.json | 18 +++- src/component/echarts-ng2.component.ts | 8 +- src/index.ts | 2 +- tslint.json | 116 +++++++++++++++++++++++++ webpack.config.js | 64 ++++++++++++++ webpack.config.uglify.js | 70 +++++++++++++++ 8 files changed, 303 insertions(+), 8 deletions(-) create mode 100644 config/helpers.js create mode 100644 tslint.json create mode 100644 webpack.config.js create mode 100644 webpack.config.uglify.js diff --git a/.gitignore b/.gitignore index 07e6e47..d7c8ff8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules +/bundles diff --git a/config/helpers.js b/config/helpers.js new file mode 100644 index 0000000..609b9e1 --- /dev/null +++ b/config/helpers.js @@ -0,0 +1,32 @@ +/** + * taken from angular2-webpack-starter + */ +var path = require('path'); + +// Helper functions +var ROOT = path.resolve(__dirname, '..'); + +function hasProcessFlag(flag) { + return process.argv.join('').indexOf(flag) > -1; +} + +function isWebpackDevServer() { + return process.argv[1] && !! (/webpack-dev-server$/.exec(process.argv[1])); +} + +function root(args) { + args = Array.prototype.slice.call(arguments, 0); + return path.join.apply(path, [ROOT].concat(args)); +} + +function checkNodeImport(context, request, cb) { + if (!path.isAbsolute(request) && request.charAt(0) !== '.') { + cb(null, 'commonjs ' + request); return; + } + cb(); +} + +exports.hasProcessFlag = hasProcessFlag; +exports.isWebpackDevServer = isWebpackDevServer; +exports.root = root; +exports.checkNodeImport = checkNodeImport; \ No newline at end of file diff --git a/package.json b/package.json index 0c705e4..0e94e8b 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,19 @@ "name": "echarts-ng2", "version": "1.4.1", "description": "Angular2 ECharts component", - "main": "src/index.js", + "main": "bundles/echarts-ng2.umd.js", + "module": "src/index.js", "typings": "src/index.d.ts", "scripts": { "ngc": "ngc", + "webpack": "webpack", + "clean": "rimraf bundles", + "webpack:uglify": "webpack --config webpack.config.uglify.js", "changelog:init": "conventional-changelog -i CHANGELOG.md -s -p angular -r 0", "changelog": "conventional-changelog -i CHANGELOG.md -s -p angular", - "prepublish": "ngc" + "bundles": "npm run clean && npm run webpack && npm run webpack:uglify", + "build": "npm run bundles && npm run ngc", + "prepublish": "npm run build" }, "repository": { "type": "git", @@ -34,9 +40,15 @@ "@angular/compiler": "^2.4.0", "@angular/compiler-cli": "^2.4.0", "@angular/core": "^2.4.0", + "awesome-typescript-loader": "^3.1.3", + "codelyzer": "^3.0.1", "conventional-changelog-cli": "^1.3.1", + "rimraf": "^2.6.1", "rxjs": "^5.2.0", + "tslint": "^5.4.2", + "tslint-loader": "^3.5.3", "typescript": "^2.1.5", + "webpack": "^2.6.1", "zone.js": "^0.7.6" } -} +} \ No newline at end of file diff --git a/src/component/echarts-ng2.component.ts b/src/component/echarts-ng2.component.ts index 11fa784..6350d46 100644 --- a/src/component/echarts-ng2.component.ts +++ b/src/component/echarts-ng2.component.ts @@ -17,7 +17,7 @@ export class EchartsNg2Component implements AfterViewInit, OnDestroy, ECharts { this.chart && (typeof this._group !== 'undefined') && ((this.chart).group = this._group); } - @Input() theme: Object | string = "default"; + @Input() theme: Object | string = 'default'; @Input() set option(option: EChartOption) { this._option = option; @@ -39,7 +39,7 @@ export class EchartsNg2Component implements AfterViewInit, OnDestroy, ECharts { @Output() onAfterInit: EventEmitter = new EventEmitter(); @Output() onOptionChange: EventEmitter = new EventEmitter(); - @ViewChild('host') host; + @ViewChild('host') host: ElementRef; constructor( private el: ElementRef, @@ -59,7 +59,7 @@ export class EchartsNg2Component implements AfterViewInit, OnDestroy, ECharts { if (!this.chart) { this.ngZone.runOutsideAngular(() => { this.onBeforeInit.emit(); - this.chart = echarts.init(this.host.nativeElement, this.theme, this.style ? {} : this.opts); + this.chart = (echarts.init(this.host.nativeElement, this.theme, this.style ? {} : this.opts)); this.onAfterInit.emit(); this.option && this.chart.setOption(this.option); this.setGroup(); @@ -193,6 +193,6 @@ export class EchartsNg2Component implements AfterViewInit, OnDestroy, ECharts { } disconnect = (group: string): void => { - echarts.disconnect(group); + (echarts).disconnect(group); } } diff --git a/src/index.ts b/src/index.ts index 714e9c1..c7f64db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export * from './component/api'; export * from './component/echarts-ng2.component'; -export * from './component/echarts-ng2.module'; \ No newline at end of file +export * from './component/echarts-ng2.module'; diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..9113f13 --- /dev/null +++ b/tslint.json @@ -0,0 +1,116 @@ +{ + "rulesDirectory": [ + "node_modules/codelyzer" + ], + "rules": { + "callable-types": true, + "class-name": true, + "comment-format": [ + true, + "check-space" + ], + "curly": true, + "eofline": true, + "forin": true, + "import-blacklist": [true, "rxjs"], + "import-spacing": true, + "indent": [ + true, + "spaces" + ], + "interface-over-type-literal": true, + "label-position": true, + "max-line-length": [ + true, + 140 + ], + "member-access": false, + "member-ordering": [ + true, + "static-before-instance", + "variables-before-functions" + ], + "no-arg": true, + "no-bitwise": true, + "no-console": [ + true, + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-construct": true, + "no-debugger": true, + "no-duplicate-variable": true, + "no-empty": false, + "no-empty-interface": true, + "no-eval": true, + "no-inferrable-types": [true, "ignore-params"], + "no-shadowed-variable": true, + "no-string-literal": false, + "no-string-throw": true, + "no-switch-case-fall-through": true, + "no-trailing-whitespace": true, + "no-unused-expression": true, + "no-use-before-declare": true, + "no-var-keyword": true, + "object-literal-sort-keys": false, + "one-line": [ + true, + "check-open-brace", + "check-catch", + "check-else", + "check-whitespace" + ], + "prefer-const": true, + "quotemark": [ + true, + "single" + ], + "radix": true, + "semicolon": [ + "always" + ], + "triple-equals": [ + true, + "allow-null-check" + ], + "typedef-whitespace": [ + true, + { + "call-signature": "nospace", + "index-signature": "nospace", + "parameter": "nospace", + "property-declaration": "nospace", + "variable-declaration": "nospace" + } + ], + "typeof-compare": true, + "unified-signatures": true, + "variable-name": false, + "whitespace": [ + true, + "check-branch", + "check-decl", + "check-operator", + "check-separator", + "check-type" + ], + + "directive-selector": [true, "attribute", "app", "camelCase"], + "component-selector": [true, "element", "app", "kebab-case"], + "use-input-property-decorator": true, + "use-output-property-decorator": true, + "use-host-property-decorator": true, + "no-input-rename": true, + "no-output-rename": true, + "use-life-cycle-interface": true, + "use-pipe-transform-interface": true, + "component-class-suffix": true, + "directive-class-suffix": true, + "no-access-missing-member": true, + "templates-use-public": true, + "invoke-injectable": true + } +} diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..bb836f9 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,64 @@ +/** + * Adapted from angular2-webpack-starter + */ + +const helpers = require('./config/helpers'), + webpack = require('webpack'); + +/** + * Webpack Plugins + */ +const ProvidePlugin = require('webpack/lib/ProvidePlugin'); +const DefinePlugin = require('webpack/lib/DefinePlugin'); +const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); + +module.exports = { + devtool: 'inline-source-map', + + resolve: { + extensions: ['.ts', '.js'] + }, + + entry: helpers.root('src/index.ts'), + + output: { + path: helpers.root('bundles'), + publicPath: '/', + filename: 'echarts-ng2.umd.js', + libraryTarget: 'umd', + library: 'echarts-ng2' + }, + + // require those dependencies but don't bundle them + externals: [/^\@angular\//, /^rxjs\//], + + module: { + rules: [{ + enforce: 'pre', + test: /\.ts$/, + loader: 'tslint-loader', + exclude: [helpers.root('node_modules')] + }, { + test: /\.ts$/, + loader: 'awesome-typescript-loader?declaration=false', + exclude: [/\.e2e\.ts$/] + }] + }, + + plugins: [ + // fix the warning in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js + new webpack.ContextReplacementPlugin( + /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/, + helpers.root('./src') + ), + + new webpack.LoaderOptionsPlugin({ + options: { + tslintLoader: { + emitErrors: false, + failOnHint: false + } + } + }) + ] +}; \ No newline at end of file diff --git a/webpack.config.uglify.js b/webpack.config.uglify.js new file mode 100644 index 0000000..f741546 --- /dev/null +++ b/webpack.config.uglify.js @@ -0,0 +1,70 @@ +/** + * Adapted from angular2-webpack-starter + */ + +const helpers = require('./config/helpers'), + webpack = require('webpack'); + +/** + * Webpack Plugins + */ +const ProvidePlugin = require('webpack/lib/ProvidePlugin'); +const DefinePlugin = require('webpack/lib/DefinePlugin'); +const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); + +module.exports = { + devtool: 'inline-source-map', + + resolve: { + extensions: ['.ts', '.js'] + }, + + entry: helpers.root('src/index.ts'), + + output: { + path: helpers.root('bundles'), + publicPath: '/', + filename: 'echarts-ng2.umd.min.js', + libraryTarget: 'umd', + library: 'echarts-ng2' + }, + + // require those dependencies but don't bundle them + externals: [/^\@angular\//, /^rxjs\//], + + module: { + rules: [{ + enforce: 'pre', + test: /\.ts$/, + loader: 'tslint-loader', + exclude: [helpers.root('node_modules')] + }, { + test: /\.ts$/, + loader: 'awesome-typescript-loader?declaration=false', + exclude: [/\.e2e\.ts$/] + }] + }, + + plugins: [ + // fix the warning in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js + new webpack.ContextReplacementPlugin( + /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/, + helpers.root('./src') + ), + + new webpack.LoaderOptionsPlugin({ + options: { + tslintLoader: { + emitErrors: false, + failOnHint: false + } + } + }), + + new webpack.optimize.UglifyJsPlugin({ + compress: { + warnings: false + } + }) + ] +}; \ No newline at end of file