Skip to content

Commit

Permalink
feat: --prefix to specify namespace prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 18, 2019
1 parent 16d7e1c commit 394a80d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion demo/Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}

Expand Down
2 changes: 1 addition & 1 deletion demo/bin/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions demo/plugins/gulp-san-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(..)'))
Expand All @@ -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)
Expand All @@ -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
}
8 changes: 7 additions & 1 deletion src/bin/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -34,14 +39,15 @@ 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])
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)
Expand Down

0 comments on commit 394a80d

Please sign in to comment.