From eed59bae15ad09177d62537ee51ee97660e7b86e Mon Sep 17 00:00:00 2001 From: Karson Date: Sat, 22 Apr 2017 23:16:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EJS=E3=80=81CSS=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=8F=B0=E4=B8=80=E9=94=AE=E5=8E=8B=E7=BC=A9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=20=E7=A7=BB=E9=99=A4=E5=8E=9F=E6=9C=89JS=E3=80=81CSS?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/command/Min.php | 151 ++++++++++++++++++ .../admin/command/Min}/r.js | 0 application/admin/command/Min/stubs/css.stub | 5 + application/admin/command/Min/stubs/js.stub | 10 ++ application/admin/controller/Dashboard.php | 2 +- application/admin/view/common/meta.html | 2 +- application/command.php | 1 + application/common/model/Category.php | 2 + application/index/view/layout/bootstrap.html | 2 +- public/assets/build/build-backend-css.js | 5 - public/assets/build/build-backend.js | 139 ---------------- public/assets/build/build-frontend-css.js | 5 - public/assets/build/build-frontend.js | 139 ---------------- public/assets/build/build.sh | 6 - public/assets/{build => css}/backend.css | 0 public/assets/{build => css}/frontend.css | 0 public/assets/js/require-backend.js | 3 + public/assets/js/require-backend.min.js | 3 + public/assets/js/require-frontend.js | 3 + public/assets/js/require-frontend.min.js | 3 + 20 files changed, 184 insertions(+), 297 deletions(-) create mode 100644 application/admin/command/Min.php rename {public/assets/build => application/admin/command/Min}/r.js (100%) create mode 100644 application/admin/command/Min/stubs/css.stub create mode 100644 application/admin/command/Min/stubs/js.stub delete mode 100644 public/assets/build/build-backend-css.js delete mode 100644 public/assets/build/build-backend.js delete mode 100644 public/assets/build/build-frontend-css.js delete mode 100644 public/assets/build/build-frontend.js delete mode 100755 public/assets/build/build.sh rename public/assets/{build => css}/backend.css (100%) rename public/assets/{build => css}/frontend.css (100%) diff --git a/application/admin/command/Min.php b/application/admin/command/Min.php new file mode 100644 index 00000000..c950d5bf --- /dev/null +++ b/application/admin/command/Min.php @@ -0,0 +1,151 @@ + 'public/assets/css/', + 'cssBaseName' => '{module}', + 'jsBaseUrl' => 'public/assets/js/', + 'jsBaseName' => 'require-{module}', + ]; + + protected function configure() + { + $this + ->setName('min') + ->addOption('module', 'm', Option::VALUE_REQUIRED, 'module name(frontend or backend),use \'all\' when build all modules', null) + ->addOption('resource', 'r', Option::VALUE_REQUIRED, 'resource name(js or css),use \'all\' when build all resources', null) + ->setDescription('Compress js and css file'); + } + + protected function execute(Input $input, Output $output) + { + $module = $input->getOption('module') ? : ''; + $resource = $input->getOption('resource') ? : ''; + + if (!$module || !in_array($module, ['frontend', 'backend', 'all'])) + { + throw new Exception('Please input correct module name'); + } + if (!$resource || !in_array($resource, ['js', 'css', 'all'])) + { + throw new Exception('Please input correct resource name'); + } + + $moduleArr = $module == 'all' ? ['frontend', 'backend'] : [$module]; + $resourceArr = $resource == 'all' ? ['js', 'css'] : [$resource]; + + $minPath = __DIR__ . DS . 'Min' . DS; + $publicPath = ROOT_PATH . 'public' . DS; + $tempFile = $minPath . 'temp.js'; + + try + { + $nodeExec = exec("which node"); + if (!$nodeExec) + { + throw new Exception("node environment not found!please install node first!"); + } + } + catch (Exception $e) + { + throw new Exception($e->getMessage()); + } + + foreach ($moduleArr as $mod) + { + foreach ($resourceArr as $res) + { + $data = [ + 'publicPath' => $publicPath, + 'jsBaseName' => str_replace('{module}', $mod, $this->options['jsBaseName']), + 'jsBaseUrl' => $this->options['jsBaseUrl'], + 'cssBaseName' => str_replace('{module}', $mod, $this->options['cssBaseName']), + 'cssBaseUrl' => $this->options['cssBaseUrl'], + 'jsBasePath' => ROOT_PATH . $this->options['jsBaseUrl'], + 'cssBasePath' => ROOT_PATH . $this->options['cssBaseUrl'], + 'ds' => DS, + ]; + + //源文件 + $from = $data["{$res}BasePath"] . $data["{$res}BaseName"] . '.' . $res; + if (!is_file($from)) + { + $output->error("{$res} source file not found!file:{$from}"); + continue; + } + if ($res == "js") + { + $content = file_get_contents($from); + preg_match("/require\.config\(\{[\n]+(.*?)\n\}\);/is", $content, $matches); + if (!isset($matches[1])) + { + $output->error("js config not found!"); + continue; + } + $config = preg_replace("/(urlArgs|baseUrl):(.*)\n/", '', $matches[1]); + $data['config'] = $config; + } + // 生成压缩文件 + $this->writeToFile($res, $data, $tempFile); + + $output->info("Compress " . $data["{$res}BaseName"] . ".{$res}"); + + // 执行压缩 + echo exec("{$nodeExec} {$minPath}r.js -o {$tempFile} >> {$minPath}node.log"); + } + } + + @unlink($tempFile); + + $output->info("Build Successed!"); + } + + /** + * 写入到文件 + * @param string $name + * @param array $data + * @param string $pathname + * @return mixed + */ + protected function writeToFile($name, $data, $pathname) + { + $search = $replace = []; + foreach ($data as $k => $v) + { + $search[] = "{%{$k}%}"; + $replace[] = $v; + } + $stub = file_get_contents($this->getStub($name)); + $content = str_replace($search, $replace, $stub); + + if (!is_dir(dirname($pathname))) + { + mkdir(strtolower(dirname($pathname)), 0755, true); + } + return file_put_contents($pathname, $content); + } + + /** + * 获取基础模板 + * @param string $name + * @return string + */ + protected function getStub($name) + { + return __DIR__ . '/Min/stubs/' . $name . '.stub'; + } + +} diff --git a/public/assets/build/r.js b/application/admin/command/Min/r.js similarity index 100% rename from public/assets/build/r.js rename to application/admin/command/Min/r.js diff --git a/application/admin/command/Min/stubs/css.stub b/application/admin/command/Min/stubs/css.stub new file mode 100644 index 00000000..e2cafa3b --- /dev/null +++ b/application/admin/command/Min/stubs/css.stub @@ -0,0 +1,5 @@ +({ + cssIn: "{%cssBasePath%}{%cssBaseName%}.css", + out: "{%cssBasePath%}{%cssBaseName%}.min.css", + optimizeCss: "default" +}) \ No newline at end of file diff --git a/application/admin/command/Min/stubs/js.stub b/application/admin/command/Min/stubs/js.stub new file mode 100644 index 00000000..7f573102 --- /dev/null +++ b/application/admin/command/Min/stubs/js.stub @@ -0,0 +1,10 @@ +({ + {%config%} + , + optimizeCss: "standard", + optimize: "none", + removeCombined: false, + baseUrl: "{%jsBasePath%}", //JS文件所在的基础目录 + name: "{%jsBaseName%}", //来源文件,不包含后缀 + out: "{%jsBasePath%}{%jsBaseName%}.min.js" //目标文件 +}); \ No newline at end of file diff --git a/application/admin/controller/Dashboard.php b/application/admin/controller/Dashboard.php index 914482d4..7017b757 100644 --- a/application/admin/controller/Dashboard.php +++ b/application/admin/controller/Dashboard.php @@ -27,7 +27,7 @@ class Dashboard extends Backend $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day])); } $this->view->assign([ - 'totaluser' => 3500, + 'totaluser' => 35200, 'totalviews' => 219390, 'totalorder' => 32143, 'totalorderamount' => 174800, diff --git a/application/admin/view/common/meta.html b/application/admin/view/common/meta.html index fa46ef86..be31fb45 100644 --- a/application/admin/view/common/meta.html +++ b/application/admin/view/common/meta.html @@ -4,7 +4,7 @@ - + - +