如何配置带有Shebang Loader的Webpack忽略Hashbang将Cesium React组件导入Typescript React组件
How to Configure Webpack with Shebang Loader to Ignore Hashbang Importing Cesium React Component into Typescript React Component
我正在尝试创建一个故事书测试,该测试在使用 typescript、babel 和 webpack 的模块中使用 react 组件。
我的地图 react 组件引用并使用了 cesium 和 cesium-react 组件。
当我尝试 运行 启动故事书(纱线故事书)时出现错误:
ERROR in ./node_modules/requirejs/bin/r.js
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #!/usr/bin/env node
| /**
| * @license r.js 2.3.5 Copyright jQuery Foundation and other contributors.
@ ./node_modules/cesium/index.js 5:16-36
@ ./node_modules/cesium-react/dist/cesium-react.es.js
@ ./src/components/Map.js
@ ./stories/map.stories.js
@ ./stories .stories.(ts?x|js)$
@ ./.storybook/config.js
@ multi ./node_modules/@storybook/react/dist/server/config/polyfills.js ./node_modules/@storybook/react/dist/server/config/globals.js (webpack)-hot-middleware/client.js?reload=
如果我google这个错误'Module parse failed. Unexpected character '#'。 #4603' 我在结果列表的顶部看到了类似的问题,建议尝试一些事情。
- 添加自定义加载程序(参考 4603)
- 尝试使用 shebang 加载程序(参考 4603/531)
- 干脆不测试(参考170)
- 此外,其中一个 link 与 jsonstream 的一个非常相似的错误可以通过谷歌搜索找到 'node target should ignore hashbang #2168'
我的主要问题具体是:
- 我曾尝试使用自定义加载器和 shebang 加载器(参考 2168),但似乎都不起作用。甚至可以在故事书中解决此错误和 运行 吗?关于 JSONstream 的研究表明使用 shebang 加载器可以解决问题,但我只是没有正确配置。
- 假设 shebang loader 是推荐的可能解决方案中最好的,那么我在 .storybook/webpack.config.js 中可能做错了什么(显然我正在做的是行不通的)。
我应该注意:
- 如果我不尝试使用打字稿加载程序,我可以将故事书添加到 运行 我的地图组件。如果我将它添加到来自 github 的铯反应项目副本,我可以 运行 我的地图故事。
- 此处显示的我的地图组件已尽可能简化,以便尝试识别和解决我的导入问题。但是,目的是我的实际地图组件将使用 Typescript 实现。
以下是我认为相关文件的片段,但如果有人询问,我很乐意 post 其他文件或片段。
.storybook/webpack.config.js
const genDefaultConfig = require('@storybook/react/dist/server/config/defaults/webpack.config.js');
module.exports = (baseConfig, env) => {
const config = genDefaultConfig(baseConfig, env);
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('ts-loader')
});
// Add shebang to try to workaround unexpected character error.
config.module.rules.push({
test: /node_modules\/cesium\/index\.js$/,
loader: require.resolve('shebang-loader')
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
.storybook/config.js
import { configure } from '@storybook/react';
// automatically import all files ending in *.stories.js or *.stories.ts?x
const req = require.context('../stories', true, /.stories.(ts?x|js)$/);
//interates through all files in ../stories that match the regex above
//and adds them to list of stories to show
function loadStories() {
req.keys().forEach((filename) => req(filename));
}
configure(loadStories, module);
stories/map.stories.js
import * as React from 'react';
import Map from "../src/components/Map";
import { withInfo } from '@storybook/addon-info';
import { storiesOf } from '@storybook/react';
storiesOf("Map", module)
.add('Sample Map',
withInfo('Map Stuff')(() =>
<Map />
));
src/components/Map.js。如果我在 Map.js.
中取消注释我的铯反应导入,就会发生错误
import * as React from 'react';
// Uncomment to see unexpected character error.
import { Viewer } from "cesium-react";
export default class Map extends React.Component {
render() {
return (
<div>hello</div>
);
}
}
package.json(片段,如有必要,我可以提供更多文件)
"scripts": {
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:ci":"jest --ci --testResultsProcessor='jest-junit'",
"build": "webpack",
"build:prod": "cross-env NODE_ENV=production webpack -p",
"clean": "yarn clean:storybook-static && yarn clean:build && yarn clean:docs && yarn clean:node_modules",
"clean:node_modules": "node ./utility/rimraf-standalone.js node_modules",
"clean:build": "rimraf build",
"clean:storybook-static": "rimraf storybook-static",
"clean:docs": "rimraf docs",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"pre:publish": "yarn lint && yarn test && yarn build:prod",
"lint": "tslint -c config/tslint.json '/src/**/*.{ts,tsx}'",
"generate:docs": "typedoc --out ./docs ./src/index.tsx"
},
"devDependencies": {
"@types/enzyme": "^3.1.6",
"@types/jest": "^22.0.1",
"@types/react": "^16.0.34",
"@types/react-dom": "^16.0.3",
"@types/react-test-renderer": "^16.0.0",
"@types/storybook__addon-info": "^3.2.1",
"@types/storybook__react": "^3.0.6",
"@babel/core": "^7.0.0-beta.37",
"@babel/plugin-proposal-class-properties": "^7.0.0-beta.37",
"@babel/plugin-proposal-export-default-from": "^7.0.0-beta.37",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0-beta.37",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.37",
"@babel/preset-env": "^7.0.0-beta.37",
"@babel/preset-react": "^7.0.0-beta.37",
"@storybook/addon-actions": "^3.4.0-alpha.1",
"@storybook/addon-info": "^3.3.8",
"@storybook/addon-links": "^3.4.0-alpha.1",
"@storybook/addon-storyshots": "^3.3.8",
"@storybook/react": "^3.4.0-alpha.1",
"babel-core": "^7.0.0-beta.37",
"babel-eslint": "^8.2.1",
"babel-loader": "^8.0.0-beta.0",
"babel-plugin-istanbul": "^4.1.5",
"cesium": "^1.41.0",
"cesium-react": "^0.2.0",
"classnames": "^2.2.5",
"copy-webpack-plugin": "^4.3.1",
"cross-env": "^5.1.3",
"css-loader": "^0.28.8",
"del-cli": "^1.1.0",
"eslint": "^4.15.0",
"eslint-config-prettier": "^2.9.0",
"eslint-import-resolver-webpack": "^0.8.4",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-react": "^7.5.1",
"extract-text-webpack-plugin": "^3.0.2",
"html-webpack-include-assets-plugin": "^1.0.2",
"html-webpack-plugin": "^2.30.1",
"jasmine-core": "^2.8.0",
"karma": "^2.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^1.0.1",
"karma-coverage": "^1.1.1",
"karma-jasmine": "^1.1.1",
"karma-rollup-preprocessor": "^5.1.1",
"karma-source-map-support": "^1.2.0",
"npm-run-all": "^4.1.2",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"prettier": "^1.10.2",
"prettier-eslint": "^8.8.1",
"prettier-eslint-cli": "^4.7.0",
"prop-types": "^15.6.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-hot-loader": "^3.1.3",
"react-router-dom": "^4.2.2",
"rollup": "^0.54.0",
"rollup-plugin-babel": "^4.0.0-beta.0",
"rollup-plugin-commonjs": "^8.2.6",
"rollup-plugin-node-resolve": "^3.0.2",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-uglify": "^2.0.1",
"storybook-addon-jsx": "^5.3.0",
"style-loader": "^0.19.1",
"uglify-es": "^3.3.5",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.10.1",
"ts-jest": "^22.0.1",
"ts-loader": "^3.2.0",
"tslint": "^5.9.1",
"tslint-config-airbnb": "^5.4.2",
"tslint-react": "^3.4.0",
"typedoc": "^0.9.0",
"typescript": "^2.6.2",
"url-loader": "^0.6.2",
"shebang-loader": "^0.0.1"
},
"dependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0"
}
我不知道你的 webpack
配置,这对我来说很奇怪,我以前不喜欢它,但实际上你需要 shebang-loader
所以如果你使用 npm
运行:
npm install --save shebang-loader
如果你使用 yarn
那么 运行:
yarn add shebang-loader
之后你必须把 use
键放在你的加载器中,并在其中加载多个加载器,我猜你的代码如下所示:
const genDefaultConfig = require('@storybook/react/dist/server/config/defaults/webpack.config.js');
module.exports = (baseConfig, env) => {
const config = genDefaultConfig(baseConfig, env);
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader')
},
{
loader: 'shebang-loader'
}
]
});
// Add shebang to try to workaround unexpected character error.
config.module.rules.push({
test: /node_modules\/cesium\/index\.js$/,
loader: require.resolve('shebang-loader')
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
在我的例子中,它变成了下面这样:
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules\/)/,
use: [
{
loader: 'babel-loader'
},
{
loader: 'shebang-loader'
}
]
}
]
}
希望对你有帮助。
我创建这个 webpack.config.js 是为了自己解决这个问题:
var path = require('path');
module.exports = {
entry: './src/root.jsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'shebang-loader'
}
]
}
}
我正在尝试创建一个故事书测试,该测试在使用 typescript、babel 和 webpack 的模块中使用 react 组件。 我的地图 react 组件引用并使用了 cesium 和 cesium-react 组件。
当我尝试 运行 启动故事书(纱线故事书)时出现错误:
ERROR in ./node_modules/requirejs/bin/r.js
Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| #!/usr/bin/env node
| /**
| * @license r.js 2.3.5 Copyright jQuery Foundation and other contributors.
@ ./node_modules/cesium/index.js 5:16-36
@ ./node_modules/cesium-react/dist/cesium-react.es.js
@ ./src/components/Map.js
@ ./stories/map.stories.js
@ ./stories .stories.(ts?x|js)$
@ ./.storybook/config.js
@ multi ./node_modules/@storybook/react/dist/server/config/polyfills.js ./node_modules/@storybook/react/dist/server/config/globals.js (webpack)-hot-middleware/client.js?reload=
如果我google这个错误'Module parse failed. Unexpected character '#'。 #4603' 我在结果列表的顶部看到了类似的问题,建议尝试一些事情。
- 添加自定义加载程序(参考 4603)
- 尝试使用 shebang 加载程序(参考 4603/531)
- 干脆不测试(参考170)
- 此外,其中一个 link 与 jsonstream 的一个非常相似的错误可以通过谷歌搜索找到 'node target should ignore hashbang #2168'
我的主要问题具体是:
- 我曾尝试使用自定义加载器和 shebang 加载器(参考 2168),但似乎都不起作用。甚至可以在故事书中解决此错误和 运行 吗?关于 JSONstream 的研究表明使用 shebang 加载器可以解决问题,但我只是没有正确配置。
- 假设 shebang loader 是推荐的可能解决方案中最好的,那么我在 .storybook/webpack.config.js 中可能做错了什么(显然我正在做的是行不通的)。
我应该注意:
- 如果我不尝试使用打字稿加载程序,我可以将故事书添加到 运行 我的地图组件。如果我将它添加到来自 github 的铯反应项目副本,我可以 运行 我的地图故事。
- 此处显示的我的地图组件已尽可能简化,以便尝试识别和解决我的导入问题。但是,目的是我的实际地图组件将使用 Typescript 实现。
以下是我认为相关文件的片段,但如果有人询问,我很乐意 post 其他文件或片段。
.storybook/webpack.config.js
const genDefaultConfig = require('@storybook/react/dist/server/config/defaults/webpack.config.js'); module.exports = (baseConfig, env) => { const config = genDefaultConfig(baseConfig, env); config.module.rules.push({ test: /\.(ts|tsx)$/, loader: require.resolve('ts-loader') }); // Add shebang to try to workaround unexpected character error. config.module.rules.push({ test: /node_modules\/cesium\/index\.js$/, loader: require.resolve('shebang-loader') }); config.resolve.extensions.push('.ts', '.tsx'); return config; };
.storybook/config.js
import { configure } from '@storybook/react'; // automatically import all files ending in *.stories.js or *.stories.ts?x const req = require.context('../stories', true, /.stories.(ts?x|js)$/); //interates through all files in ../stories that match the regex above //and adds them to list of stories to show function loadStories() { req.keys().forEach((filename) => req(filename)); } configure(loadStories, module);
stories/map.stories.js
import * as React from 'react'; import Map from "../src/components/Map"; import { withInfo } from '@storybook/addon-info'; import { storiesOf } from '@storybook/react'; storiesOf("Map", module) .add('Sample Map', withInfo('Map Stuff')(() => <Map /> ));
src/components/Map.js。如果我在 Map.js.
中取消注释我的铯反应导入,就会发生错误import * as React from 'react'; // Uncomment to see unexpected character error. import { Viewer } from "cesium-react"; export default class Map extends React.Component { render() { return ( <div>hello</div> ); } }
package.json(片段,如有必要,我可以提供更多文件)
"scripts": { "test": "jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage", "test:ci":"jest --ci --testResultsProcessor='jest-junit'", "build": "webpack", "build:prod": "cross-env NODE_ENV=production webpack -p", "clean": "yarn clean:storybook-static && yarn clean:build && yarn clean:docs && yarn clean:node_modules", "clean:node_modules": "node ./utility/rimraf-standalone.js node_modules", "clean:build": "rimraf build", "clean:storybook-static": "rimraf storybook-static", "clean:docs": "rimraf docs", "storybook": "start-storybook -p 6006", "build-storybook": "build-storybook", "pre:publish": "yarn lint && yarn test && yarn build:prod", "lint": "tslint -c config/tslint.json '/src/**/*.{ts,tsx}'", "generate:docs": "typedoc --out ./docs ./src/index.tsx" }, "devDependencies": { "@types/enzyme": "^3.1.6", "@types/jest": "^22.0.1", "@types/react": "^16.0.34", "@types/react-dom": "^16.0.3", "@types/react-test-renderer": "^16.0.0", "@types/storybook__addon-info": "^3.2.1", "@types/storybook__react": "^3.0.6", "@babel/core": "^7.0.0-beta.37", "@babel/plugin-proposal-class-properties": "^7.0.0-beta.37", "@babel/plugin-proposal-export-default-from": "^7.0.0-beta.37", "@babel/plugin-proposal-export-namespace-from": "^7.0.0-beta.37", "@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.37", "@babel/preset-env": "^7.0.0-beta.37", "@babel/preset-react": "^7.0.0-beta.37", "@storybook/addon-actions": "^3.4.0-alpha.1", "@storybook/addon-info": "^3.3.8", "@storybook/addon-links": "^3.4.0-alpha.1", "@storybook/addon-storyshots": "^3.3.8", "@storybook/react": "^3.4.0-alpha.1", "babel-core": "^7.0.0-beta.37", "babel-eslint": "^8.2.1", "babel-loader": "^8.0.0-beta.0", "babel-plugin-istanbul": "^4.1.5", "cesium": "^1.41.0", "cesium-react": "^0.2.0", "classnames": "^2.2.5", "copy-webpack-plugin": "^4.3.1", "cross-env": "^5.1.3", "css-loader": "^0.28.8", "del-cli": "^1.1.0", "eslint": "^4.15.0", "eslint-config-prettier": "^2.9.0", "eslint-import-resolver-webpack": "^0.8.4", "eslint-plugin-babel": "^4.1.2", "eslint-plugin-import": "^2.8.0", "eslint-plugin-node": "^5.2.1", "eslint-plugin-react": "^7.5.1", "extract-text-webpack-plugin": "^3.0.2", "html-webpack-include-assets-plugin": "^1.0.2", "html-webpack-plugin": "^2.30.1", "jasmine-core": "^2.8.0", "karma": "^2.0.0", "karma-chrome-launcher": "^2.2.0", "karma-cli": "^1.0.1", "karma-coverage": "^1.1.1", "karma-jasmine": "^1.1.1", "karma-rollup-preprocessor": "^5.1.1", "karma-source-map-support": "^1.2.0", "npm-run-all": "^4.1.2", "optimize-css-assets-webpack-plugin": "^3.2.0", "prettier": "^1.10.2", "prettier-eslint": "^8.8.1", "prettier-eslint-cli": "^4.7.0", "prop-types": "^15.6.0", "react": "^16.2.0", "react-dom": "^16.2.0", "react-hot-loader": "^3.1.3", "react-router-dom": "^4.2.2", "rollup": "^0.54.0", "rollup-plugin-babel": "^4.0.0-beta.0", "rollup-plugin-commonjs": "^8.2.6", "rollup-plugin-node-resolve": "^3.0.2", "rollup-plugin-replace": "^2.0.0", "rollup-plugin-uglify": "^2.0.1", "storybook-addon-jsx": "^5.3.0", "style-loader": "^0.19.1", "uglify-es": "^3.3.5", "webpack": "^3.10.0", "webpack-dev-server": "^2.10.1", "ts-jest": "^22.0.1", "ts-loader": "^3.2.0", "tslint": "^5.9.1", "tslint-config-airbnb": "^5.4.2", "tslint-react": "^3.4.0", "typedoc": "^0.9.0", "typescript": "^2.6.2", "url-loader": "^0.6.2", "shebang-loader": "^0.0.1" }, "dependencies": { "react": "^16.2.0", "react-dom": "^16.2.0" }
我不知道你的 webpack
配置,这对我来说很奇怪,我以前不喜欢它,但实际上你需要 shebang-loader
所以如果你使用 npm
运行:
npm install --save shebang-loader
如果你使用 yarn
那么 运行:
yarn add shebang-loader
之后你必须把 use
键放在你的加载器中,并在其中加载多个加载器,我猜你的代码如下所示:
const genDefaultConfig = require('@storybook/react/dist/server/config/defaults/webpack.config.js');
module.exports = (baseConfig, env) => {
const config = genDefaultConfig(baseConfig, env);
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader')
},
{
loader: 'shebang-loader'
}
]
});
// Add shebang to try to workaround unexpected character error.
config.module.rules.push({
test: /node_modules\/cesium\/index\.js$/,
loader: require.resolve('shebang-loader')
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
在我的例子中,它变成了下面这样:
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules\/)/,
use: [
{
loader: 'babel-loader'
},
{
loader: 'shebang-loader'
}
]
}
]
}
希望对你有帮助。
我创建这个 webpack.config.js 是为了自己解决这个问题:
var path = require('path');
module.exports = {
entry: './src/root.jsx',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'shebang-loader'
}
]
}
}