我正在尝试在 Webpack 2 中使用以下语法:
import someSvc = require("./some.svc.js");
但我收到错误:
error TS2307: Cannot find module './some.svc.js'.
我究竟做错了什么?!如何在 Webpack 2 中导入我的 js 模块?
为了彻底起见,我将我的项目归结为最小的示例,并将提供以下文件:
webpack.config.js
var path = require('path');
module.exports = function makeWebpackConfig() {
var config = {};
config.entry = { 'app': './src/main.ts' };
config.output = {
path: root('dist'),
filename: 'js/[name].js'
};
config.module = {
rules: [
{
test: /\.ts$/,
loaders: ['ts-loader']
}
]
};
return config;
}();
// Helper functions
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}
包.json
{
"name": "webpack",
"version": "1.0.0",
"description": "",
"main": "src/main.ts",
"dependencies": {},
"devDependencies": {
"ts-loader": "^0.9.5",
"typescript": "^2.0.3"
},
"scripts": {},
"author": "",
"license": "ISC"
}
tsconfig.js
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
"noEmitHelpers": true
},
"compileOnSave": false,
"buildOnSave": false,
"exclude": [
"node_modules"
]
}
src/main.ts
import someSvc = require("./some.svc.js");
src/some.svc.js
if (typeof module !== 'undefined' && module.exports) {
module.exports.config = function (conf) {
return { abc: 123 };
};
}
将这些文件粘贴在一起,运行webpack
,您应该会看到相同的错误。
我错过了一些简单的东西来让它工作吗?