diff --git a/demo/Gulpfile.js b/demo/Gulpfile.js index fa2d8072..8d2117b3 100644 --- a/demo/Gulpfile.js +++ b/demo/Gulpfile.js @@ -3,7 +3,7 @@ const gulpSanSSR = require('./plugins/gulp-san-ssr.js') function php () { return src('src/index.ts') - .pipe(gulpSanSSR({ target: 'php' })) + .pipe(gulpSanSSR({ target: 'php', nsPrefix: 'demo\\' })) .pipe(dest('dist')) } diff --git a/demo/bin/render.php b/demo/bin/render.php index ae18ee59..3326fd1a 100755 --- a/demo/bin/render.php +++ b/demo/bin/render.php @@ -4,6 +4,6 @@ $data = json_decode(file_get_contents(__DIR__ . "/../data.json")); $noDataOutput = false; -$html = \san\renderer\render($data, $noDataOutput); +$html = \demo\renderer\render($data, $noDataOutput); echo $html; diff --git a/demo/package.json b/demo/package.json index 82e6f698..83425aa1 100644 --- a/demo/package.json +++ b/demo/package.json @@ -6,7 +6,7 @@ "main": "index.js", "scripts": { "build": "npm run build-js && npm run build-php", - "build-php": "san-ssr -t php src/index.ts > dist/index.php", + "build-php": "san-ssr -t php -p 'demo\\' src/index.ts > dist/index.php", "build-js": "san-ssr -t js src/index.ts > dist/index.js", "gulpbuild": "gulp js php", "gulpbuild-js": "gulp js", diff --git a/demo/plugins/gulp-san-ssr.js b/demo/plugins/gulp-san-ssr.js index 4e6f171f..b5d50a8d 100644 --- a/demo/plugins/gulp-san-ssr.js +++ b/demo/plugins/gulp-san-ssr.js @@ -5,7 +5,7 @@ const { ToJSCompiler, ToPHPCompiler } = require('san-ssr') const PLUGIN_NAME = 'gulp-san-ssr' -module.exports = function ({ target = 'js', ssrOptions } = {}) { +module.exports = function (options = {}) { return through2.obj(function (file, _, cb) { if (file.isNull()) { this.emit('error', new PluginError(PLUGIN_NAME, 'File: "' + file.relative + '" without content. You have to read it with gulp.src(..)')) @@ -19,11 +19,11 @@ module.exports = function ({ target = 'js', ssrOptions } = {}) { } if (file.isBuffer()) { - const targetCode = compile(file, target, ssrOptions) + const targetCode = compile(file, options.target, options) file.contents = Buffer.from(targetCode) const path = file.path const ext = extname(path) - file.path = path.substr(0, path.length - ext.length) + '.' + target + file.path = path.substr(0, path.length - ext.length) + '.' + options.target } cb(null, file) @@ -33,6 +33,6 @@ module.exports = function ({ target = 'js', ssrOptions } = {}) { function compile (file, target, ssrOptions) { const Compiler = target === 'php' ? ToPHPCompiler : ToJSCompiler const compiler = new Compiler(ssrOptions) - const targetCode = compiler.compile(file.path) + const targetCode = compiler.compile(file.path, ssrOptions) return targetCode } diff --git a/src/bin/ssr.ts b/src/bin/ssr.ts index 742f7dd3..46214ab3 100755 --- a/src/bin/ssr.ts +++ b/src/bin/ssr.ts @@ -22,6 +22,11 @@ yargs choices: ['php', 'js'], description: 'target SSR file' }) + .option('prefix', { + alias: 'p', + default: 'san\\', + description: 'namespace prefix for ssr.php' + }) .option('tsconfig', { alias: 'c', description: 'tsconfig path, will auto resolve if not specified' @@ -34,6 +39,7 @@ yargs }) const target = yargs.argv.target as OptionValue +const nsPrefix = yargs.argv.prefix as OptionValue const tsConfigFilePath = yargs.argv.tsconfig as OptionValue const outputFile = yargs.argv.output as OptionValue const componentFile = resolve(yargs.argv._[0]) @@ -41,7 +47,7 @@ console.error(chalk.gray('compiling'), componentFile, 'to', target) const Compiler = target === 'php' ? ToPHPCompiler : ToJSCompiler const compiler = new Compiler({ tsConfigFilePath }) -const targetCode = compiler.compile(componentFile) +const targetCode = compiler.compile(componentFile, { nsPrefix }) if (outputFile !== undefined) { writeFileSync(outputFile, targetCode)