我尝试使用typescript 3.7Optional Chaining、Nullish Coalescing 等功能。但是在转换时webpack给我一个错误。
app: Module parse failed: Unexpected token (50:40)
app: File was processed with these loaders:
app: * ../../../node_modules/ts-loader/index.js
app: You may need an additional loader to handle the result of these loaders.
app: | export const Layout = (props) => {
app: | const regionsResults = useQuery(regionsQuery, { fetchPolicy: 'cache-first' });
app: > const regions = regionsResults.data?.regions ?? [];
app: | const userItem = useQuery(usersProfileQuery, { fetchPolicy: 'cache-first' });
app: | const …Run Code Online (Sandbox Code Playgroud) 我构建了一个继电器小webpack和打字稿演示.如果我用webpack.config.js运行webpack我收到此错误:
ERROR in ./js/app.ts
Module not found: Error: Can't resolve './MyModule' in '/Users/timo/Documents/Dev/Web/02_Tests/webpack_test/js'
@ ./js/app.ts 3:17-38
Run Code Online (Sandbox Code Playgroud)
我不知道问题是什么.模块导出应该是正确的.
webpack.config.js
const path = require('path');
module.exports = {
entry: './js/app.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{test: /\.ts$/, use: 'ts-loader'}
]
}
};Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"suppressImplicitAnyIndexErrors": true,
"strictNullChecks": false,
"lib": [
"es5", "es2015.core", "dom"
],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist"
},
"include": [
"js/**/*"
]
}Run Code Online (Sandbox Code Playgroud)
SRC/app.js
import { MyModule } …Run Code Online (Sandbox Code Playgroud)编辑 1:将 GitHub URL 添加到项目中
编辑2:删除baseUrl修复tsconfig.json所有问题并使用相对导入效果很好。
链接:Github
如何生成带有相对路径而不是 的打字稿声明文件alias?
我正在 UMD 模式下创建一个库(samplelibrary)并将其发布到 npm 中。打包的 npm 库只有build文件夹(带有类型),package.json并且README.md
当我尝试在另一个打字稿应用程序中使用该库时,由于生成的类型声明文件无效,构建失败。类型声明文件包含别名而不是相对路径。
编译日志:
ERROR in /workspace/myproject/node_modules/samplelibrary/build/typings/src/foo.d.ts(403,17):
TS2307: Cannot find module 'utils/bar
如何解决这个问题?
实际创建的声明文件foo.d.ts:
declare const Foo: {
bar: typeof import("utils/bar");
}
Run Code Online (Sandbox Code Playgroud)
预期文件:
declare const Foo: {
bar: typeof import("./utils/bar");
}
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"moduleResolution": "node",
"jsx": "react",
"sourceMap": true,
"rootDir": "./",
"baseUrl": "./src",
"paths": {
"@samplecompany/sampleproject": ["./"]
}, …Run Code Online (Sandbox Code Playgroud) Typescript 1.8现在支持无类型的JS文件.要启用此功能,只需添加编译器标志--allowJs或将"allowJs":true添加到tsconfig.json中的compilerOptions
通过https://blogs.msdn.microsoft.com/typescript/2016/01/28/announcing-typescript-1-8-beta/
我正在尝试导入没有打字文件的react-tap-event-plugin.
import * as injectTapEventPlugin from 'injectTapEventPlugin';
Run Code Online (Sandbox Code Playgroud)
说找不到模块.所以我试过:
import * as injectTapEventPlugin from '../node_modules/react-tap-event-plugin/src/injectTapEventPlugin.js';
Run Code Online (Sandbox Code Playgroud)
这表示模块解析为非模块实体,无法使用此结构导入.然后我尝试了:
import injectTapEventPlugin = require('../node_modules/react-tap-event-plugin/src/injectTapEventPlugin.js');
Run Code Online (Sandbox Code Playgroud)
它与崩溃ERROR in ./scripts/index.tsx
Module build failed: TypeError: Cannot read property 'kind' of undefined的node_modules/typescript/lib/typescript.js:39567
我的tsconfig:
{
"compilerOptions": {
"target": "ES5",
"removeComments": true,
"jsx": "react",
"module": "commonjs",
"sourceMap": true,
"allowJs": true
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)
我正在使用带有ts-loader的webpack:
{
test: /\.tsx?$/,
exclude: ['node_modules', 'tests'],
loader: 'ts-loader'
}
Run Code Online (Sandbox Code Playgroud) 有一个TypeScript,Babel,React和Karma示例.
Webpack配置包含带有ts-loader .tsx?文件的babel-loader .
请解释为什么需要它?为什么ts-loader不够用?
以下是我的webpack.config.js
var webpack = require('webpack'),
path = require('path'),
yargs = require('yargs');
var libraryName = 'MyLib',
plugins = [],
outputFile;
if (yargs.argv.p) {
plugins.push(new webpack.optimize.UglifyJsPlugin({
minimize: true
}));
outputFile = libraryName + '.min.js';
} else {
outputFile = libraryName + '.js';
}
var config = {
entry: [
__dirname + '/src/TestClass.ts'
],
devtool: 'source-map',
output: {
path: path.join(__dirname, '/dist'),
filename: outputFile,
library: libraryName,
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader', //Something wrong here
exclude: /node_modules/ …Run Code Online (Sandbox Code Playgroud) 所以我试图比较这两种组合的输出编译代码。
ts-loader
{
test: /\.tsx?$/,
use: 'ts-loader',
}
Run Code Online (Sandbox Code Playgroud)
babel-loader
use: {
loader: 'babel-loader',
options: {
presets:
[
"@babel/preset-react",
"@babel/preset-typescript",
]
}
}
Run Code Online (Sandbox Code Playgroud)
问题
我们的项目使用webpack resolve.root选项导入具有绝对路径的模块.(避免类似的东西../../../module)
在目前的状态下,该项目正在使用babel-loader,它可以很好地工作.
我的任务是将应用程序迁移到Angular 2.
因此,我目前正在转换到TypeScript.
不知何故,似乎ts-loader与resolve.rootwebpack配置的选项不兼容.
所述的实施例webpack.config.js
resolve: {
root: [
path.resolve('./node_modules'),
path.resolve('./app'),
path.resolve('./app/lib'),
]
},
Run Code Online (Sandbox Code Playgroud)
模块导入示例
import AbstractListState from 'states/abstract_list_state';
该states目录位于app/lib目录中.
执行时出错 webpack
ERROR in ./app/mainViews/panel/panel.controller.ts
Module not found: Error: Cannot resolve module 'states/abstract_list_state' in C:\Users\...\Project\app\mainViews\panel
@ ./app/mainViews/panel/panel.controller.ts 4:28-65
Run Code Online (Sandbox Code Playgroud) 我构建了一个Angular 2应用程序并将其与webpack捆绑在一起.目前,我的应用程序仍然很小,但webpack任务已经需要大约10秒钟.是否可以优化我的webpack配置或TypeSript编译选项以改进编译和打包持续时间?
这是我使用的webpack配置:
var webpack = require('webpack');
var LiveReloadPlugin = require('webpack-livereload-plugin');
module.exports = {
entry: __dirname + '/assets/app/app.ts',
output: {
filename: 'myApp.bundle.js',
path: __dirname + '/build/'
},
// Turn on sourcemaps
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.js']
},
plugins: [
new LiveReloadPlugin({
appendScriptTag: true
}),
// Fixes angular 2 warning
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
__dirname
)
],
module: {
rules: [{
enforce: 'pre',
test: /\.js$/,
loader: "source-map-loader"
},
{
enforce: 'pre',
test: /\.tsx?$/,
use: "ts-loader"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
和tsconfig: …
我使用webpack@4.16.1,webpack-dev-server@3.1.4和ts-loader@4.4.2。
我使用Interface / index.ts来管理导入,以组织多个接口导入。
但是,当我更改接口文件时,webpack-dev-server(或ts-loader,我不知道)不会重新加载并传输更改后的接口文件。
接口/IHelloState.ts
export interface IHelloState {
message: string;
}
Run Code Online (Sandbox Code Playgroud)
Interface.index.ts
export {IHelloState} from "./IHelloState";
Run Code Online (Sandbox Code Playgroud)
index.tsx
import * as React from "react";
import * as ReactDOM from "react-dom";
import "./index.css";
import {IHelloState} from "./Interface";
const helloState: IHelloState = {
message: "hello!"
};
ReactDOM.render(<div>{helloState.message}</div>, document.getElementById("root"));
Run Code Online (Sandbox Code Playgroud)
当我像这样更改Interface / IHelloState.ts时:
接口/IHelloState.ts
export interface IHelloState {
// message: string;
}
Run Code Online (Sandbox Code Playgroud)
没发生什么事。甚至没有“ [HMR]正在检查服务器上的更新...”或“ [HMR]没有最新更新。” 节目。
当我更改Interface / IHelloState.ts和index.tsx时,如下所示: …
ts-loader ×10
typescript ×10
webpack ×7
javascript ×4
babel-loader ×2
ecmascript-6 ×2
angular ×1
babeljs ×1
node-modules ×1
reactjs ×1
tsconfig ×1