简体   繁体   English

浏览器中的javascript要求

[英]javascript require in browser

So I am working on a react single page app which won't run via node. 因此,我正在开发一个不会通过节点运行的React单页应用程序。 I want to serve the webpage using python but for nearly all modules I can find they use 'require' to include other modules. 我想使用python为网页提供服务,但对于几乎所有模块,我都能发现它们使用'require'来包含其他模块。 Is there anyway for me to painlessly either rewrite the existing requires to something that is understood by the browser or use them directly? 无论如何,我是否可以轻松地将现有需求重写为浏览器可以理解的内容,或者直接使用它们?

for instance I want to use this: https://github.com/STRML/react-resizable 例如我想使用这个: https : //github.com/STRML/react-resizable

but I cannot figure out how to get it to work in my browser... 但我不知道如何在浏览器中使用它...

You should take a look at Webpack. 您应该看一下Webpack。 It only takes a webpack.config.js that is pretty simple and then you're good to go. 它只需要一个非常简单的webpack.config.js ,然后您就可以开始了。

var webpack = require('webpack');  
module.exports = {  
  entry: [
    'webpack/hot/only-dev-server',
    "./src/index.js"
  ],

  output: {
    path: __dirname + '/build',
    filename: "app.js"
  },

  devtool: 'sourcemap',

  jshint: {
    esnext: true
  },

  module: {

    preLoaders: [{
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'eslint-loader'
    }],

    loaders: [
      { 
        test: /\.js?$/,
        exclude: /node_modules/,
        loaders: ['react-hot', 'babel']
      },
      { 
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          stage: 1
        }
      },
      { 
        test: /\.less$/,
        loader: 'style-loader!css-loader!less-loader'
      },
      { 
        test: /\.css$/,
        loader: "style-loader!css-loader"
      },
      { 
        test: /\.(png|jpg|woff)$/,
        loader: "url-loader?limit=100000"
      }, 
      { 
        test: /\.jpg$/,
        loader: "file-loader"
      }
    ]
  },

  plugins: [
    new webpack.NoErrorsPlugin()
  ]
};

In this example webpack will get my index.js (called entrypoint) and resolve every require down its path. 在此示例中,webpack将获取我的index.js (称为入口点)并解决所有需求。 The output file will be app.js thrown at /build folder. 输出文件将在/build文件夹中抛出app.js

The loaders are a bonus so Webpack can do some other fancy stuff! 加载程序是一个额外的好处,因此Webpack可以做一些其他有趣的事情!

As stated in the comments, Browserify is an excellent tool to help you use CommonJS modules for Javascript that runs in a browser. 如评论中所述,Browserify是一个出色的工具,可帮助您将CommonJS模块用于运行在浏览器中的Javascript。 It looks like you ran into some issues with it not understanding your JSX code. 好像您遇到了一些不了解您的JSX代码的问题。 You will need to transform your JSX code into valid Javascript for Browserify to function properly. 您需要将JSX代码转换为有效的JavaScript,以使Browserify正常运行。 I'll give you a gulp task that automate this whole process for you. 我将为您提供一项庞大的任务,可以自动完成整个过程。

First, install the required modules: 首先,安装所需的模块:

npm install gulp-uglify vinyl-source-stream browserify reactify gulp-streamify --save-dev

and

npm install -g gulp-cli

Next, create your Gulpfile.js and throw this into it: 接下来,创建您的Gulpfile.js并将其扔进去:

var gulp = require('gulp');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var reactify = require('reactify');
var streamify = require('gulp-streamify');

gulp.task('build', function () {
    browserify({
        entries   : 'path/to/mainComponent.js', //where your main component lives
        transform : [reactify] //transform jsx
    })
        .bundle()
        .pipe(source('bundle.js')) //create file named 'bundle.js'
        .pipe(streamify(uglify('bundle.js'))) //minify
        .pipe(gulp.dest('path/to/destination')); //where you want your 'bundle.js' to be placed
});

You will obviously need to change the entry path and destination path to match the needs of your project. 显然,您将需要更改输入路径和目标路径以匹配项目的需求。 You can optionally rename the bundle file if you don't want it to be called "bundle.js" 如果不想将其命名为“ bundle.js”,则可以选择重命名该捆绑文件。

Once that is done, simply type gulp build and your bundled Javascript will be created for you. 完成此操作后,只需键入gulp build即可为您创建捆绑的Javascript。

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

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