Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to read only property 'history' of object #109

Open
daviscai opened this issue Jan 18, 2017 · 1 comment
Open

Cannot assign to read only property 'history' of object #109

daviscai opened this issue Jan 18, 2017 · 1 comment

Comments

@daviscai
Copy link

daviscai commented Jan 18, 2017

rollup + antd + react 打包后 js文件在浏览器打开报错:

var history = shape({
  listen: func$1.isRequired,
  push: func$1.isRequired,
  replace: func$1.isRequired,
  go: func$1.isRequired,
  goBack: func$1.isRequired,
  goForward: func$1.isRequired
});

about.js:17033 Uncaught TypeError: Cannot assign to read only property 'history' of object '[object Window]'
    at about.js:17033

rollup.config.js :

import babel from 'rollup-plugin-babel';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
import replace from 'rollup-plugin-replace';
//import livereload from 'rollup-plugin-livereload';

export default {
  entry: 'assets/js/src/about.js',    //需要打包的文件
  dest: 'assets/js/dist/about.js',  //打包后的文件
  format: 'cjs',       // iife, cjs, es
  sourceMap: false,  //inline,线上环境设置为false
  plugins: [
    replace({
      'process.env.NODE_ENV': JSON.stringify( 'production' )
    }),
    babel({
      presets: [ "es2015-rollup",'stage-2', 'react'],
      plugins: [["import", { "libraryName": "antd", "libraryDirectory": "lib", "style": "true" }]],
      babelrc: false, // 不采用babelrc配置,否则上面的presets设置无效
      compact: true, // 事实上,只要不为auto就不会警告
      exclude: 'node_modules/**'
    }),
    commonjs({
      include: 'node_modules/**',
      namedExports : {
        'node_modules/react/react.js' : ['Component','Children','createElement','PropTypes'],
        'node_modules/react-dom/index.js' : ['ReactDOM'],
        'node_modules/antd/lib/index.js' : [
          'Table', 'Form', 'Calendar', 'Card', 'Input', 'Select', 'Checkbox', 'DatePicker',
          'Col', 'Radio', 'Button', 'Modal', 'message', 'Icon'
        ]
      }
    }),
    globals(),
    builtins(),
    resolve({
      jsnext: true,
      main: true,
      browser: true,
    }),
    (process.env.NODE_ENV === 'production' && uglify()),
    //livereload(),
  ],
};

其中,babel 下的plugins配置如果删掉,就不会报错:

//去掉这项配置就不会报错
plugins: [["import", { "libraryName": "antd", "libraryDirectory": "lib", "style": "true" }]],

可以clone 项目重现:

git clone https://github.com/daviscai/kwan.git
npm install -c 
npm run build
node index 
http://127.0.0.1:4000/home/about
@chalecao
Copy link

我用preact ,rollup打包,正常

import buble from 'rollup-plugin-buble'
// 通过这个插件可以方便的使用 javascript 的新特性,配置上稍微麻烦一些,如下安装相应插件(包括配置babel 需要的插件)
import babel from 'rollup-plugin-babel'
// 帮助 Rollup 查找外部模块,然后安装,相关配置
import resolve from 'rollup-plugin-node-resolve'
// import json from 'rollup-plugin-json';
// rollup-plugin-node-resolve 插件可以解决 ES6模块的查找导入,但是npm中的大多数包都是以CommonJS模块的形式出现的,所以需要使用这个插件将CommonJS模块转换为 ES2015 供 Rollup 处理
import commonjs from 'rollup-plugin-commonjs'
import replace from 'rollup-plugin-replace'
import alias from 'rollup-plugin-alias'
import path from "path"
import uglify from 'rollup-plugin-uglify'
import {minify} from 'uglify-es'
const isProduction = process.env.NODE_ENV === 'production';

export default [{
    input: 'src/module/main/main.js',
    output: {
        file: 'src/main.js',
        format: 'cjs'
    },
    // banner: 'define("zebra-pages/chaoshi-104576/module/main/index", function (require, exports, module) {',
    // footer: "})",
    external: [],
    plugins: [
        replace({
            "process.env.NODE_ENV": "\'production\'"
        }),
        alias({
            'react': path.resolve('./node_modules/preact-compat/dist/preact-compat.es.js'),
            'react-dom': path.resolve('./node_modules/preact-compat/dist/preact-compat.es.js'),
            'indexof': path.resolve('./node_modules/component-indexof/index.js'),
            // './locale/zh_CN': path.resolve('./node_modules/antd-mobile/es/picker/locale/zh_CN.js'),
        }),
        resolve({ "jsnext": true, module: true, main: true }),
        babel({
            "presets": [[
                "env",
                {
                    "modules": false,
                    loose: true,
                    exclude: ['transform-es2015-typeof-symbol'],
                    targets: {
                        browsers: ["Android >= 4.4", "ios > 7"]
                    },
                    "useBuiltIns": true
                }
            ]],
            "plugins": [
                ["transform-react-jsx", {
                    "pragma": "h"
                }],
                "add-module-exports",
                "external-helpers"//注意这个参数不能加,加了之后模块exports有问题,坑坑坑
            ]
        }),

        commonjs({
            include: 'node_modules/**', // 包括
            exclude: [],  // 排除
        }),
        buble(),
        (isProduction && 
        uglify({
            mangle: { toplevel: true }
        }, minify))
    ]
}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants