pup*_*eno 6 javascript webpack
我正在为cljsjs重新包装react-toolbox,而我需要做的事情不是在完成的包中包含react和react-dom,所以,我明显地补充说:
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
Run Code Online (Sandbox Code Playgroud)
到webpack.config.js,但它似乎没有工作.在生成的.js文件中,我期望类似于:
123: function(...) {
module.exports = React;
},
Run Code Online (Sandbox Code Playgroud)
但相反发现:
/* 372 */
/***/ function(module, exports, __webpack_require__) {
/* WEBPACK VAR INJECTION */(function(process) {/**
* Copyright 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
'use strict';
var _assign = __webpack_require__(371);
var ReactChildren = __webpack_require__(373);
var ReactComponent = __webpack_require__(386);
Run Code Online (Sandbox Code Playgroud)
当使用库时,React初始化两次:
有什么想法吗?
我的完整webpack.config看起来像这样:
const path = require('path');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
process.env.NODE_ENV = "production";
var entryName = "react-toolbox.inc";
module.exports = {
entry: path.join(__dirname, "components", "index.js"),
externals: {
"react": "React",
"react-dom": "ReactDOM"
},
output: {
filename: entryName + ".js",
libraryTarget: "var",
library: "ReactToolbox"
},
resolve: {
extensions: ['', '.js', '.jsx', '.scss']
},
module: {
loaders: [
{
test: /(\.js|\.jsx)$/,
exclude: /(node_modules)/,
loader: "babel",
query: {
presets: ['es2015', 'react', 'stage-0'],
plugins: ['add-module-exports']
}
},
{
test: /(\.scss|\.css)$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap&modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!postcss')
}
]
},
postcss: [autoprefixer],
plugins: [
new ExtractTextPlugin(entryName + '.css', {allChunks: true})
]
};
Run Code Online (Sandbox Code Playgroud)
生成的文件是:https://gist.githubusercontent.com/pupeno/9b58dd86608378966e950500456286e8/raw/63cd63ad2eca50201ca622d6e7e759d473eeb011/react-toolbox.inc.js
该库需要以下列方式使用React:
import React, { PropTypes } from 'react';
import React, { Component, PropTypes } from 'react';
import React from 'react';
import ReactDOM from 'react-dom';
import React, { cloneElement, Component, PropTypes } from 'react';
Run Code Online (Sandbox Code Playgroud)
我正在使用webpack 1.14.
我也尝试过:
externals: {
"react": true,
"react-dom": true
}
Run Code Online (Sandbox Code Playgroud)
和
externals: {
"react": "commonjs2",
"react-dom": "commonjs2"
}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
/* main.js */
import React from 'react';
export default () => <div>Hello World!</div>
/* webpack.config.js */
module.exports = [
{
entry: `${__dirname}/main.js`,
output: {
filename: 'bundle.js',
path: `${__dirname}`,
library: 'Foobar',
libraryTarget: 'var'
},
externals: {
'react': 'React',
'react-dom': 'ReactDOM'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: [
'es2015',
'react',
'stage-0'
],
plugins: [
'add-module-exports'
]
}
}
]
}
}
];
/* bundle.js (compiled) */
var Foobar =
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = __webpack_require__(1);
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = function () {
return _react2.default.createElement(
'div',
null,
'Hello World!'
);
};
module.exports = exports['default'];
/***/ },
/* 1 */
/***/ function(module, exports) {
module.exports = React;
/***/ }
/******/ ]);
Run Code Online (Sandbox Code Playgroud)
唯一的区别是,我删除了 css 部分。您可以尝试该配置(当然还有您的文件名和路径)吗?
另一种替代方法:
不要将它们导入到您的脚本中并将它们作为全局模块添加到您的 linter 中。
| 归档时间: |
|
| 查看次数: |
2576 次 |
| 最近记录: |