我正在使用react-dates在一个electron项目中,需要通过以下方式进行初始化:
import 'react-dates/initialize';
这段代码内部设置了一些变量,以备后续使用。问题在于,当我进入下一个使用这些变量的代码块时,我会遇到异常,因为这些变量仍然为空。
事实证明,Chrome/Electron将它们视为两个不同的文件,尽管它们在磁盘上是相同的文件。当在设置文件中断点时,Chrome报告以下路径(第一次访问/第二次访问)
C:\src\Project\node_modules\react-with-styles\lib\ThemedStyleSheet.js
webpack:///./node_modules/react-with-styles/lib/ThemedStyleSheet.js
好的,这很奇怪——是怎么回事呢?这可能与我的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()
]
};
我的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/*"]
}
有关在哪里/如何找出更多信息的任何建议都将不胜感激!
-- 编辑:
我认为磁盘上没有两个物理副本,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>