I'm writing a Typescript testcase, which needs some static xml, loaded from an file which is part of the project. I'm using webpack, in a node/typescript project and am finding that VSC typescript is out of step with the project's typescript.
The line which is causing a problem (from VSC perspective) is this (the testcase and the xml file are in the same directory):
import data from './app.config.xml';
In order to get imports to work (in the project) I had to do the following things (Importing Other Assets):
1) ensure that I was using raw-loader in webpack for xml files:
module: {
rules: [
{
test: /\.xml$/i,
use: 'raw-loader'
},
2) declaring a module in a declaration file (./lib/declarations.d.ts)
declare module '*.xml' {
const content: string;
export default content;
}
3) include this declation file in the types property of tsconfig
"types": [
"mocha", "node", "./lib/declarations"
],
In my test case, I am trying to import this as follows:
import data from './app.config.xml';
However, Typescript in VSC is reporting this error:
Cannot find module './app.config.xml'.ts(2307)
This however is not an issue when I build the project. If I remove the module declaration from the declaration file, then the build does indeed fail with this identical error. So somehow, Typescript in VSC is not in sync with the Typescript installed in the project. The version of typescript in VSC and the project are identical (version 3.7.2).
Since the build works, I don't know wether its 'check-in-able'. I don't like seeing errors in VSC particulalry as it is out of step with the project. So far in my Typescript journey, typescript in the project and typescript in VSC have always been in step, so I can't understand whats happening here. I've tried to clean my repo and rebuild but this made no difference.
In VSC I'm using the "Typescript extension Pack (0.2.0)" (which includes TSLine, TypeScript Hero, json2ts, Move TS, Path Intellisense, Tyescript Impoerter, Prettier, Debugger for Chrome) However, I don't believe that any of these plugins are responsible for the error that VSC is reporting. I think that the error is coming from the Typescript support built directly into VSC.
For the sake of completeness, my tsconfig is:
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "Node",
"noImplicitAny": true,
"sourceMap": true,
"strictNullChecks": true,
"target": "es5",
"types": [
"mocha", "node", "./lib/declarations"
],
"lib": [
"es5",
"es2015",
"es6",
"dom"
]
},
"include": [
"lib/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
and webpack file:
const path = require('path');
const nodeExternals = require('webpack-node-externals');
module.exports = {
devtool: 'source-map',
mode: 'development',
entry: ['./tests/all-tests-entry.js', './lib'],
target: 'node',
externals: [nodeExternals()],
module: {
rules: [
{
test: /\.xml$/i,
use: 'raw-loader'
},
{ test: /\.ts(x?)$/, loader: 'ts-loader' },
{ test: /\.json$/, loader: 'json-loader' }
]
},
resolve: {
extensions: ['.ts', '.js', '.json']
},
watchOptions: {
ignored: /node_modules/
},
output: {
filename: 'test-bundle.js',
sourceMapFilename: 'test-bundle.js.map',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'commonjs'
}
};
and ./tests/all-tests-entry.js:
var context = require.context('./', true, /.spec.ts$/);
context.keys().forEach(context);
module.exports = context;
and directory structure with relevant files:
/project-root/
tsconfig.json
webpack.config.test.ts
/project-root/lib/
declaration.d.ts
/project-root/tests/
widget.class.spec.ts
app.config.xml
An here is a snapshot of VSC (with all extensions disabled) reporting an error that doesnt exist in the project build: