如何在 babel-loader 和 flow 中使用 webpack
How to use webpack with babel-loader and flow
我正在为我认为不应该太难的设置而苦苦挣扎。
我希望这三种技术协同工作:
- webpack 打包代码
- babel 这样我就可以写现代的 JavaScript
- 流程因为类型检查就像测试和 linter 一样有用
我已经完成了几次设置,但我在网上找到的 none 文章似乎很有帮助。
我在 package.json
、run
、flow
和 build
中定义了 3 个脚本。
使用 yarn run flow
进行类型检查非常有效,使用 yarn run start
.
通过 babel-node
执行脚本也是如此
但是当我执行 yarn run build
时,通过 webpack 出现以下错误:
$ ./node_modules/webpack/bin/webpack.js
Hash: 207d42dac5784520fc99
Version: webpack 3.10.0
Time: 49ms
Asset Size Chunks Chunk Names
bundle.js 2.65 kB 0 [emitted] main
[0] ./src/main.js 181 bytes {0} [built] [failed] [1 error]
ERROR in ./src/main.js
Module parse failed: Unexpected token (3:5)
You may need an appropriate loader to handle this file type.
| // @flow
|
| type Foo = {
| foo: string,
| };
error Command failed with exit code 2.
在我看来,类型注释没有在正确的位置正确删除。遗憾的是,如果我直接在 webpack 中指定 babel 选项而不是 .babelrc
.
,也会发生这种情况
这目前让我无法在使用流的同时捆绑一堆 .js
文件,而我发现几个插件据说只是使用 flow
预设来剥离流注释就是 flowtype.org 似乎推荐。
为了重现性,我的项目文件如下所示:
package.json:
{
…
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.3",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"flow-bin": "^0.60.1",
"webpack": "^3.9.1"
},
"scripts": {
"build": "./node_modules/webpack/bin/webpack.js",
"start": "./node_modules/babel-cli/bin/babel-node.js src/main.js",
"flow": "./node_modules/flow-bin/cli.js"
}
}
.flowconfig:
[include]
./src
[ignore]
[libs]
[lints]
[options]
.babelrc:
{
"presets": ["env", "flow"]
}
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/main.js',
output: {
path: __dirname,
filename: 'bundle.js',
},
resolve: {
modules: [
path.resolve('./src/'),
'node_modules',
],
extensions: ['.js'],
},
module: {
rules: [
{
test: '/.js$/',
loader: 'babel-loader',
},
],
},
};
src/main.js:
// @flow
type Foo = {
foo: string,
};
const defaultFoo: Foo = {
foo: 'bar',
};
console.log(defaultFoo);
下载相关预设并添加到webpack.config.js如下:
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'stage-0', 'react']
}
}
}
]
}
我正在为我认为不应该太难的设置而苦苦挣扎。 我希望这三种技术协同工作:
- webpack 打包代码
- babel 这样我就可以写现代的 JavaScript
- 流程因为类型检查就像测试和 linter 一样有用
我已经完成了几次设置,但我在网上找到的 none 文章似乎很有帮助。
我在 package.json
、run
、flow
和 build
中定义了 3 个脚本。
使用 yarn run flow
进行类型检查非常有效,使用 yarn run start
.
babel-node
执行脚本也是如此
但是当我执行 yarn run build
时,通过 webpack 出现以下错误:
$ ./node_modules/webpack/bin/webpack.js
Hash: 207d42dac5784520fc99
Version: webpack 3.10.0
Time: 49ms
Asset Size Chunks Chunk Names
bundle.js 2.65 kB 0 [emitted] main
[0] ./src/main.js 181 bytes {0} [built] [failed] [1 error]
ERROR in ./src/main.js
Module parse failed: Unexpected token (3:5)
You may need an appropriate loader to handle this file type.
| // @flow
|
| type Foo = {
| foo: string,
| };
error Command failed with exit code 2.
在我看来,类型注释没有在正确的位置正确删除。遗憾的是,如果我直接在 webpack 中指定 babel 选项而不是 .babelrc
.
这目前让我无法在使用流的同时捆绑一堆 .js
文件,而我发现几个插件据说只是使用 flow
预设来剥离流注释就是 flowtype.org 似乎推荐。
为了重现性,我的项目文件如下所示:
package.json:
{
…
"dependencies": {},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.3",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"flow-bin": "^0.60.1",
"webpack": "^3.9.1"
},
"scripts": {
"build": "./node_modules/webpack/bin/webpack.js",
"start": "./node_modules/babel-cli/bin/babel-node.js src/main.js",
"flow": "./node_modules/flow-bin/cli.js"
}
}
.flowconfig:
[include]
./src
[ignore]
[libs]
[lints]
[options]
.babelrc:
{
"presets": ["env", "flow"]
}
webpack.config.js:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './src/main.js',
output: {
path: __dirname,
filename: 'bundle.js',
},
resolve: {
modules: [
path.resolve('./src/'),
'node_modules',
],
extensions: ['.js'],
},
module: {
rules: [
{
test: '/.js$/',
loader: 'babel-loader',
},
],
},
};
src/main.js:
// @flow
type Foo = {
foo: string,
};
const defaultFoo: Foo = {
foo: 'bar',
};
console.log(defaultFoo);
下载相关预设并添加到webpack.config.js如下:
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['es2015', 'stage-0', 'react']
}
}
}
]
}