Osc*_*nco 5 javascript typescript reactjs webpack electron
我正在使用电子和打字稿启动一个新项目,我设置了该项目并运行了一个基本的 hello world,但是我尝试添加一个库(https://github.com/benjamn/recast),导入语法是不工作。
import React from 'react'; // Works
import { parse, prettyPrint } from 'recast'; // Does not work, cannot read property parse of undefined
import tsParser from 'recast/parsers/typescript'; // Does not work
// const recast = require('recast'); // Works
// const tsParser = require('recast/parsers/typescript'); // Works
Run Code Online (Sandbox Code Playgroud)
我的 webpack 配置和 tsconfig 如下:
webpack.rules.js:
module.exports = [
// Add support for native node modules
{
test: /\.node$/,
use: "node-loader"
},
{
test: /\.(m?js|node)$/,
parser: { amd: false },
use: {
loader: "@marshallofsound/webpack-asset-relocator-loader",
options: {
outputAssetBase: "native_modules"
}
}
},
{
test: /\.tsx?$/,
exclude: /(node_modules|.webpack)/,
loaders: [
{
loader: "ts-loader",
options: {
transpileOnly: true
}
}
]
}
];
Run Code Online (Sandbox Code Playgroud)
webpack.renderer.config.js:
const rules = require("./webpack.rules");
const plugins = require("./webpack.plugins");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
rules.push({
test: /\.css$/,
use: [{ loader: "style-loader" }, { loader: "css-loader" }]
});
module.exports = {
// Put your normal webpack config below here
module: {
rules
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
plugins: [
new TsconfigPathsPlugin({
/*configFile: "./path/to/tsconfig.json" */
})
]
},
plugins: plugins
};
Run Code Online (Sandbox Code Playgroud)
tsconfig.json:
{
"compilerOptions": {
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"noEmit": true,
"jsx": "react",
"baseUrl": "src/"
},
"include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
| 归档时间: |
|
| 查看次数: |
394 次 |
| 最近记录: |