2017-09-16 16 views
8

Symfony 3.3 ve yardımcı paket kullanıyorum. Benim config.yml içinde bu var:Assetic'in TypeScript filtresi için bir tsconfig.json nasıl belirleyebilirim?

assetic: 
    debug:   '%kernel.debug%' 
    use_controller: '%kernel.debug%' 
    filters: 
     cssrewrite: ~ 
     typescript: 
      bin: "%kernel.root_dir%/../node_modules/typescript/bin/tsc" 
      apply_to: "\\.ts$" 

benim typescript dosyalarını derlemek çalıştığınızda bu hatayı alıyorum: Ben import { Obj } from './my-obj.model'; gibi bir ithalat ifadeleri kullandıktan sonra

Output: 
../../../../private/var/folders/94/45yn02792ksfglm12pjxxlp00000gn/T/assetic_typescript_in59bccf08e6357/my-form.ts.ts(6,1): error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is 'amd' or 'system'. 
../../../../private/var/folders/94/45yn02792ksfglm12pjxxlp00000gn/T/assetic_typescript_in59bccf08e6357/my-form.ts.ts(6,22): error TS2307: Cannot find module './my-obj.model'. 

ortaya çıkar. İçe aktarma ifadeleri kullanmazsam, derleme iyi çalışıyor.

Assetic'in TypeScript filtresini --module system parametresini veya tsconfig.json parametresini nasıl eklerim?

cevap

3

error TS6131: Cannot compile modules using option 'out' unless the '--module' flag is 'amd' or 'system'.

Ekstra seçenekler tsc komut için AsseticBundle yoluyla şu anda mümkün değildir.

error TS2307: Cannot find module './model'.

ve burada AsseticBundle derlemek için bir tmp dir/dosya içine tek tek her ts dosyayı kaydedin, böylece o da bu özelliği desteklemiyor.

İyi haber, düzeltmek için assetic.filter.typescript hizmetinin davranışını geçersiz kılabilirsiniz. en uygulamaya Bu sınıf ekleyelim:

namespace AppBundle\Assetic\Filter; 

use Assetic\Asset\AssetInterface; 
use Assetic\Exception\FilterException; 
use Assetic\Util\FilesystemUtils; 

class TypeScriptFilter extends \Assetic\Filter\TypeScriptFilter 
{ 
    private $tscBin; 
    private $nodeBin; 

    public function __construct($tscBin = '/usr/bin/tsc', $nodeBin = null) 
    { 
     $this->tscBin = $tscBin; 
     $this->nodeBin = $nodeBin; 
    } 

    public function filterLoad(AssetInterface $asset) 
    { 
     $pb = $this->createProcessBuilder($this->nodeBin 
      ? array($this->nodeBin, $this->tscBin) 
      : array($this->tscBin)); 

     // using source path to compile and allow "imports". 
     $inputPath = $asset->getSourceRoot().DIRECTORY_SEPARATOR.$asset->getSourcePath(); 
     $outputPath = FilesystemUtils::createTemporaryFile('typescript_out'); 

     $pb 
      ->add($inputPath) 
      // passing the required options here 
      ->add('--module')->add('system') 
      ->add('--out') 
      ->add($outputPath) 
     ; 

     $proc = $pb->getProcess(); 
     $code = $proc->run(); 

     if (0 !== $code) { 
      if (file_exists($outputPath)) { 
       unlink($outputPath); 
      } 
      throw FilterException::fromProcess($proc)->setInput($asset->getContent()); 
     } 

     if (!file_exists($outputPath)) { 
      throw new \RuntimeException('Error creating output file.'); 
     } 

     $compiledJs = file_get_contents($outputPath); 
     unlink($outputPath); 

     $asset->setContent($compiledJs); 
    } 
} 

Sonra özgün hizmet geçersiz kılmak için parametre sınıfının değerini değiştirin: Bu sizin assetic yapılandırmasıyla benim için çalıştı

# app/config/services.yml 
parameters: 
    assetic.filter.typescript.class: 'AppBundle\Assetic\Filter\TypeScriptFilter' 

. Öte yandan ise


Symfony uygulamaları için varlıklarını yönetmek için yeni önerilen yol: Webpack Encore support for TypeScript ile, JavaScript modüllerinin, ön işleme CSS & JS donatılacak ve derleme ve varlıkları küçültülmesiyle size temiz bir & güçlü API vererek yükleyici.

1

Sadece bin yolunu TSC eklemek kesmek yapmalıyım :)

assetic: 
debug:   '%kernel.debug%' 
use_controller: '%kernel.debug%' 
filters: 
    cssrewrite: ~ 
    typescript: 
     bin: "%kernel.root_dir%/../node_modules/typescript/bin/tsc --module system" 
İlgili konular