简体   繁体   English

Moment 无法使用 webpack (typescript + babel)

[英]Moment failed to work with webpack (typescript + babel)

Just as the demo , when i add moment to the project, bundle built by webpack failed, once i remove the moment it is ok.就像演示一样,当我向项目添加 moment 时,webpack 构建的 bundle 失败了,一旦我删除了它就可以了。

Note: the module in tsconfig.json is set to es6, which is exactly what i need, when switch it to commonjs, it is ok too.注意:tsconfig.json 中的模块设置为es6,这正是我需要的,当它切换到commonjs时,也可以。

I am told you guys are acitve here, thanks very much to you~~~听说你们这里很活跃,非常感谢你们~~~

After got it running and disable minify mode, I saw that problem is how you using moment.让它运行并禁用缩小模式后,我发现问题在于您如何使用 moment.js 。 You can use this config to see your unminify code problem:您可以使用此配置来查看您的 unminify 代码问题:

entry: './src/index.tsx',
  mode: 'production',
  output: {
    path: path.join(__dirname, 'lib'),
    filename: 'index.js',
  },
  module: {
    rules: [
      {
        test: /\.(tsx|ts)$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'ts-loader'],
      },
    ],
  },
  optimization: {
        // We no not want to minimize our code.
        minimize: false
  },

After change, I got error moment is not a function .更改后,我得到错误moment is not a function So it's a problem when webpack bundle and how moment export their function https://github.com/palantir/blueprint/issues/959 You can fix it by所以当 webpack bundle 以及如何导出它们的函数时,这是一个问题https://github.com/palantir/blueprint/issues/959你可以通过

const moment = require('moment')

to use the function as document or将该函数用作文档或

moment.toString()

to just got the current time.刚刚得到当前时间。

Or change options in tsconfig like in https://momentjs.com/docs/#/use-it/typescript/或更改 tsconfig 中的选项,如https://momentjs.com/docs/#/use-it/typescript/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM