简体   繁体   中英

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.

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.

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. You can use this config to see your unminify code problem:

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 . 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

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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