2016-02-18 5 views
15

मैं spec तहत src और परीक्षण के तहत मेरे कोड डाल कहते हैं:कल्पना/परीक्षण फ़ोल्डर के साथ tsconfig की स्थापना

+ spec 
+ --- classA.spec.ts 
+ src 
+ --- classA.ts 
+ --- classB.ts 
+ --- index.ts 
+ tsconfig.json 

मैं चाहता हूँ केवल transpile dist फ़ोल्डर में src। तो मैं उन में निर्भरता हल नहीं कर सका

{ 
    "compileOptions": { 
    "module": "commonjs" 
    "outDir": "dist" 
    }, 
    "files": { 
    "src/index.ts", 
    "typings/main.d.ts" 
    } 
} 

हालांकि, इस tsconfig.json परीक्षण फ़ाइलें शामिल नहीं है: index.ts चूंकि मेरे पैकेज के प्रवेश बिंदु, मेरे tsconfig.json देखो इस तरह है।

दूसरी तरफ, यदि मैं परीक्षण फ़ाइलों को tsconfig.json में शामिल करता हूं तो उन्हें dist फ़ोल्डर में भी ट्रांसफर किया जाता है।

मैं इस समस्या को कैसे हल करूं?

उत्तर

13

मैं परिभाषित करने समाप्त हो गया एकाधिक कॉन्फ़िगरेशन फ़ाइलें और उन्हें सरल बनाने के लिए extends का उपयोग करें। tsconfig.json और tsconfig.build.json

// tsconfig.json 
{ 
    ... 
    "exclude": [...] 
} 

// tsconfig.build.json 
{ 
    ... 
    "files": [ "typings/index.d.ts", "src/index.ts" ] 
} 

इस तरह, मैं क्या (tsc -p tsconfig.build.json का प्रयोग करके) का निर्माण करने पर ठीक नियंत्रण और क्या ts language service (आईडीई) संभालती है हो सकता है:

मैं दो फ़ाइलें कहो।

अपडेट: अब जब मेरी परियोजनाएं बढ़ती हैं, तो मैं अधिक कॉन्फ़िगरेशन फाइलें समाप्त कर देता हूं। मैं "विस्तार" विशेषता यह है कि अब टाइपप्रति में उपलब्ध है का उपयोग करें:

// tsconfig.base.json 
{ 
    // your common settings. Mostly "compilerOptions". 
    // Do not include "files" and "include" here, 
    // let individual config handles that. 
    // You can use "exclude" here, but with "include", 
    // It's pretty much not necessary. 
} 

// tsconfig.json 
{ 
    // This is used by `ts language service` and testing. 
    // Includes source and test files. 
    "extends": "./tsconfig.base.json", 
    "atom": { ... }, 
    "compilerOptions": { 
    // I set outDir to place all test build in one place, 
    // and avoid accidentally running `tsc` littering test build to my `src` folder. 
    "outDir": "out/spec" 
    } 
    "include": [ ... ] 
} 

// tsconfig.commonjs.json or tsconfig.systemjs.json or tsconfig.global.json etc 
{ 
    "extends": "./tsconfig.base.json", 
    "compilerOptions": { 
    // for some build this does not apply 
    "declaration": true/false, 
    "outDir": "dist/<cjs, sys, global, etc>", 
    "sourceRoot": "..." 
    }, 
    // Only point to typings and the start of your source, e.g. `src/index.ts` 
    "files": [ ... ], 
    "include": [ ... ] 
} 
2

मुझे लगता है कि आपको अपनी कॉन्फ़िगरेशन में 'फ़ाइलें' विकल्प का उपयोग नहीं करना चाहिए। इसके बजाय आप इस तरह अवांछित फ़ाइलों को बाहर निकालने और हो सकता है यह:

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "outDir": "dist" 
    }, 
    "exclude": [ 
     "node_modules", 
     "dist", 
     "typings/browser.d.ts", 
     "typings/browser/**" 
    ] 
} 

यह परीक्षण और एप्लिकेशन js फ़ाइलों मिश्रण के बिना 'जिले' फ़ोल्डर में अपने मूल संरचना को सुरक्षित करेगा:

--dist 
----spec 
-------.... 
----src 
-------.... 
2

यह आप उपयोग कर रहे जो कुछ परीक्षण ढांचे पर कुछ हद तक निर्भर है, लेकिन मैं ts-node उपयोग करने के लिए अपने परीक्षण फ़ाइलों को संकलित करने की तरह है। मोचा का उपयोग करना, अपने npm test स्क्रिप्ट दिखाई देंगे:

"mocha": "mocha test/ --compilers ts:ts-node/register --recursive" 

अपने tsconfig.json में, rootDir विकल्प को हटाना न भूलें।

{ 
"compilerOptions": { 
    "module": "commonjs", 
    "target": "es6", 
    "noImplicitAny": false, 
    "removeComments": true, 
    "sourceMap": true, 
    "outDir": "lib" 
}, 
"include": [ 
    "src/**/*.ts" 
], 
"exclude": [ 
    "node_modules", 
    "lib", 
    "typings/**" 
] 
} 

जब आप src या जो कुछ भी आपके आवेदन कोड के लिए आधार फ़ोल्डर है rootDir सेट के साथ टाइपप्रति चलाने का प्रयास है, यह एक निर्देशिका है कि बाहर बैठता है, इस तरह के एक tests में किसी भी संकलन की अनुमति नहीं देने देंगे। ts-node का उपयोग करके, आप आसानी से अलग टाइपस्क्रिप्ट कॉन्फ़िगरेशन फ़ाइलों के बिना सबकुछ अलग रख सकते हैं।

+1

एकाधिक कॉन्फ़िगरेशन फ़ाइलों में अलग नहीं होने पर पैकेज में अतिरिक्त (परीक्षण) फ़ाइलों को वितरित करने की कमी है। इसके अलावा, आपके दो 'शामिल' अनावश्यक हैं। आपको केवल 'src/**/*। Ts' – unional

+0

btw की आवश्यकता है, '***'' टाइपिंग/***' में क्या है? कभी नहीं सुना। – unional

+0

'**' होना चाहिए, केवल एक टाइपो – barndog

संबंधित मुद्दे