Fro*_*iwi 5 javascript node.js typescript electron
我在电子项目中使用react-dates,需要通过以下方式进行初始化:
import 'react-dates/initialize';
Run Code Online (Sandbox Code Playgroud)
这会在内部设置一些变量,以供以后使用。问题是,当我下次输入使用这些变量的代码时,由于它们仍然为空,因此我得到了异常。
事实证明,Chrome / Electron将它们视为2个单独的文件,即使它们是磁盘上的相同文件也是如此。破坏设置文件时,Chrome会报告以下路径(首次访问/第二次访问)
C:\src\Project\node_modules\react-with-styles\lib\ThemedStyleSheet.js
webpack:///./node_modules/react-with-styles/lib/ThemedStyleSheet.js
Run Code Online (Sandbox Code Playgroud)
好:那很奇怪-有什么用?这在哪里/如何发生?我认为这与我的Webpack设置有关-如果重要的话,我正在使用TsConfigPathsPlugin和output / paths。从我的webpack:
/**
* Base webpack config used across other specific configs
*/
import path from 'path';
import webpack from 'webpack';
import { dependencies } from '../package.json';
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
export default {
externals: [...Object.keys(dependencies || {})],
module: {
rules: [
{
test: /\.tsx?$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true
}
},
'ts-loader'
]
},
{
test: /\.js$/, // Transform all .js files required somewhere with Babel
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
cacheDirectory: true
}
}
}
]
},
output: {
path: path.join(__dirname, '..', 'app'),
// https://github.com/webpack/webpack/issues/1114
libraryTarget: 'commonjs2'
},
/**
* Determine the array of extensions that should be used to resolve modules.
*/
resolve: {
extensions: ['.js', '.ts', '.tsx', '.json'],
plugins: [new TsconfigPathsPlugin({ configFile: './tsconfig.json' })]
},
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: 'production'
}),
new webpack.NamedModulesPlugin()
]
};
Run Code Online (Sandbox Code Playgroud)
我的tsconfig使用baseUrl重新映射路径
{
"compilerOptions": {
"target": "es5",
"baseUrl": "./app/",
"jsx": "react",
"module": "es2015",
"moduleResolution": "node",
"noUnusedLocals": true,
"pretty": true,
"sourceMap": true,
"resolveJsonModule": true,
"lib": ["dom", "es5", "es6", "es7", "es2017"],
"allowSyntheticDefaultImports": true // no errors with commonjs modules interop
//"strict": true,
//"strictFunctionTypes": false,
},
"exclude": ["node_modules", "**/node_modules/*"]
}
Run Code Online (Sandbox Code Playgroud)
任何关于在哪里/如何解决这个问题的建议都非常感谢!
-编辑:
我认为磁盘上没有两个物理副本,npm -ls react-dates的输出如下
C:\<project>\manager-ts>npm ls react-dates
erb-typescript-example@0.17.1 C:\<project>\manager-ts
`-- react-dates@20.1.0
C:\<project>\manager-ts>
Run Code Online (Sandbox Code Playgroud)
这不是一个解决方案,可能对其他人的搜索没有太大帮助,但它确实让我解除了封锁。
我创建了第二个打字稿模块,它使用反应日期定义组件。该模块被编译为 es5,然后“yarn link”进入我的主应用程序。尽管我从未弄清楚为什么存在差异(并且我仍然/也从 ethers.js 收到重复的初始化警告),但这至少解决了这个问题。