webpack babel构建和外部文件夹问题(k6 TypeScript)

huangapple go评论62阅读模式
英文:

webpack babel build and external folder issue (k6 typescript)

问题

为项目创建压力测试,我找到了k6。项目使用TypeScript制作,然后我使用了模板https://github.com/k6io/template-typescript。

我的目录结构是:

- 项目(包含我的TypeScript的Angular项目)
-- src文件夹
- k6
-- webpack.config.ts
-- package.json
-- src文件夹(包含我的TypeScript的测试文件)

k6使用webpack/babel将ts文件转换为js文件,而我对此不熟悉。

我的一些测试导入文件位于我的项目/src文件夹,但在构建过程中出现错误。

错误示例:

ERROR in ../project/src/config-values.ts
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/fred/Documents/project/src/config-values.ts: Unexpected token, expected "{" (4:7)

  2 | import { getRandomBoolean } from "./random-values"
  3 |
> 4 | export enum ConfigType {
    |        ^

如果我将位于项目文件夹中的文件复制到k6文件夹中,就不会出现错误,但我不想必须保持两个文件夹的内容相同。

尽管进行了一些研究,但我尚未找到解决方案,我对于导入位于k6文件夹外的ts文件感到有些困惑。

这是我的webpack配置:

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const GlobEntries = require('webpack-glob-entries');

module.exports = {
  mode: 'production',
  entry: GlobEntries('./src/*test*.ts'), // 为每个测试生成多个入口
  output: {
    path: path.join(__dirname, 'dist'),
    libraryTarget: 'commonjs',
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
  target: 'web',
  externals: /^(k6|https?\:\/\/)(\/.*)?/,
  // 为编译后的脚本生成映射文件
  devtool: "source-map",
  stats: {
    colors: true,
  },
  plugins: [
    new CleanWebpackPlugin(),
    // 将资源复制到目标文件夹
    // 请参阅 `src/post-file-test.ts` 以查看使用资源的示例测试
    new CopyPlugin({
      patterns: [{
        from: path.resolve(__dirname, 'assets'),
        noErrorOnMissing: true
      }],
    }),
  ],
  optimization: {
    // 不要进行最小化,因为它不在浏览器中使用
    minimize: false,
  },
};

还有我的package.json:

{
  "name": "typescript",
  "version": "1.0.0",
  "main": "index.js",
  "repository": "ssh://git@github.com/k6io/example-typescript.git",
  "author": "Simon Aronsson <simme@k6.io>",
  "license": "MIT",
  "devDependencies": {
    "@babel/core": "7.13.16",
    "@babel/plugin-proposal-class-properties": "7.13.0",
    "@babel/plugin-proposal-object-rest-spread": "7.13.8",
    "@babel/preset-env": "7.13.15",
    "@babel/preset-typescript": "7.13.0",
    "@graphql-codegen/cli": "^2.13.7",
    "@graphql-codegen/client-preset": "^1.0.7",
    "@graphql-codegen/fragment-matcher": "^3.3.1",
    "@graphql-codegen/gql-tag-operations-preset": "^1.6.0",
    "@graphql-codegen/introspection": "2.2.1",
    "@graphql-codegen/typescript": "^2.7.4",
    "@graphql-codegen/typescript-apollo-angular": "^3.5.4",
    "@graphql-codegen/typescript-graphql-request": "^4.5.8",
    "@graphql-codegen/typescript-operations": "^2.5.4",
    "@types/k6": "~0.35.0",
    "@types/webpack": "5.28.0",
    "babel-loader": "8.2.2",
    "clean-webpack-plugin": "4.0.0-alpha.0",
    "copy-webpack-plugin": "^9.0.1",
    "gql-tag": "^1.0.1",
    "graphql": "^16.6.0",
    "ramda": "^0.28.0",
    "ramda-adjunct": "^3.2.0",
    "typescript": "^4.8.4",
    "webpack": "^5.87.0",
    "webpack-cli": "4.6.0",
    "webpack-glob-entries": "^1.0.1"
  },
  "scripts": {
    "format": "webpack",
    "codegen": "graphql-codegen --config codegen.ts"
  }
}
英文:

For a project, I have to create some stress test and I found k6.
The project is made with typescript then I use the template https://github.com/k6io/template-typescript.

My tree is :

- project (containing my angular project in typescript)
-- src folder
- k6
-- webpack.config.ts
-- package.json
-- src folder (containing my test files in typescript)

k6 is using webpack/babel to transform ts files in js files and I'm not familiar with.

Some of my test import files that are in my project/src folder but I have errors during build.

Error example:

ERROR in ../project/src/config-values.ts
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /home/fred/Documents/project/src/config-values.ts: Unexpected token, expected &quot;{&quot; (4:7)
2 | import { getRandomBoolean } from &quot;./random-values&quot;
3 |
&gt; 4 | export enum ConfigType {
|        ^

If I copy the files that are in my project folder in my k6 folder, I have no error but I don't want to have to keep update two folders which must be identical.

Despite some research, I haven't found a solution yet and I'm a little bit stuck to import ts files that are outside of my k6 folder.

Here is my webpack config

const path = require(&#39;path&#39;);
const { CleanWebpackPlugin } = require(&#39;clean-webpack-plugin&#39;);
const CopyPlugin = require(&#39;copy-webpack-plugin&#39;);
const GlobEntries = require(&#39;webpack-glob-entries&#39;);
module.exports = {
mode: &#39;production&#39;,
entry: GlobEntries(&#39;./src/*test*.ts&#39;), // Generates multiple entry for each test
output: {
path: path.join(__dirname, &#39;dist&#39;),
libraryTarget: &#39;commonjs&#39;,
filename: &#39;[name].js&#39;,
},
resolve: {
extensions: [&#39;.ts&#39;, &#39;.js&#39;],
},
module: {
rules: [
{
test: /\.ts$/,
use: &#39;babel-loader&#39;,
exclude: /node_modules/,
},
],
},
target: &#39;web&#39;,
externals: /^(k6|https?\:\/\/)(\/.*)?/,
// Generate map files for compiled scripts
devtool: &quot;source-map&quot;,
stats: {
colors: true,
},
plugins: [
new CleanWebpackPlugin(),
// Copy assets to the destination folder
// see `src/post-file-test.ts` for an test example using an asset
new CopyPlugin({
patterns: [{
from: path.resolve(__dirname, &#39;assets&#39;),
noErrorOnMissing: true
}],
}),
],
optimization: {
// Don&#39;t minimize, as it&#39;s not used in the browser
minimize: false,
},
};

and my package.json

{
&quot;name&quot;: &quot;typescript&quot;,
&quot;version&quot;: &quot;1.0.0&quot;,
&quot;main&quot;: &quot;index.js&quot;,
&quot;repository&quot;: &quot;ssh://git@github.com/k6io/example-typescript.git&quot;,
&quot;author&quot;: &quot;Simon Aronsson &lt;simme@k6.io&gt;&quot;,
&quot;license&quot;: &quot;MIT&quot;,
&quot;devDependencies&quot;: {
&quot;@babel/core&quot;: &quot;7.13.16&quot;,
&quot;@babel/plugin-proposal-class-properties&quot;: &quot;7.13.0&quot;,
&quot;@babel/plugin-proposal-object-rest-spread&quot;: &quot;7.13.8&quot;,
&quot;@babel/preset-env&quot;: &quot;7.13.15&quot;,
&quot;@babel/preset-typescript&quot;: &quot;7.13.0&quot;,
&quot;@graphql-codegen/cli&quot;: &quot;^2.13.7&quot;,
&quot;@graphql-codegen/client-preset&quot;: &quot;^1.0.7&quot;,
&quot;@graphql-codegen/fragment-matcher&quot;: &quot;^3.3.1&quot;,
&quot;@graphql-codegen/gql-tag-operations-preset&quot;: &quot;^1.6.0&quot;,
&quot;@graphql-codegen/introspection&quot;: &quot;2.2.1&quot;,
&quot;@graphql-codegen/typescript&quot;: &quot;^2.7.4&quot;,
&quot;@graphql-codegen/typescript-apollo-angular&quot;: &quot;^3.5.4&quot;,
&quot;@graphql-codegen/typescript-graphql-request&quot;: &quot;^4.5.8&quot;,
&quot;@graphql-codegen/typescript-operations&quot;: &quot;^2.5.4&quot;,
&quot;@types/k6&quot;: &quot;~0.35.0&quot;,
&quot;@types/webpack&quot;: &quot;5.28.0&quot;,
&quot;babel-loader&quot;: &quot;8.2.2&quot;,
&quot;clean-webpack-plugin&quot;: &quot;4.0.0-alpha.0&quot;,
&quot;copy-webpack-plugin&quot;: &quot;^9.0.1&quot;,
&quot;gql-tag&quot;: &quot;^1.0.1&quot;,
&quot;graphql&quot;: &quot;^16.6.0&quot;,
&quot;ramda&quot;: &quot;^0.28.0&quot;,
&quot;ramda-adjunct&quot;: &quot;^3.2.0&quot;,
&quot;typescript&quot;: &quot;^4.8.4&quot;,
&quot;webpack&quot;: &quot;^5.87.0&quot;,
&quot;webpack-cli&quot;: &quot;4.6.0&quot;,
&quot;webpack-glob-entries&quot;: &quot;^1.0.1&quot;
},
&quot;scripts&quot;: {
&quot;format&quot;: &quot;webpack&quot;,
&quot;codegen&quot;: &quot;graphql-codegen --config codegen.ts&quot;
}
}

Do you have an idea to resolve this ?

答案1

得分: 0

我找到了一个解决方案。

我不知道是我的树结构变化还是配置文件变化。

我的新树结构是

- webpack.config.ts
- package.json
- project
- k6

package.json

{
  "name": "webpack-ts-babel",
  "version": "1.0.0",
  "main": "webpack.config.js",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.22.5",
    "babel-loader": "^9.1.2",
    "babel-preset-es2015": "^6.24.1",
    "ts-loader": "^9.4.3",
    "typescript": "^4.8.4",
    "webpack": "^5.88.0",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.0",
    "webpack-glob-entries": "^1.0.1"
  },
  "scripts": {
    "k6": "webpack"
  }
}

webpack.config.js

const path = require('path');
const GlobEntries = require('webpack-glob-entries');

module.exports = {
  mode: 'production',
  entry: GlobEntries('./k6/src/*test*.ts'),
  devtool: 'inline-source-map',
  target: ['web', 'es5'],
  externals: /^(k6|https?\:\/\/)(\/.*)?/,
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  output: {
    path: path.join(__dirname, 'k6/dist'),
    libraryTarget: 'commonjs',
    filename: '[name].js',
  },
};

k6文件夹中的tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "moduleResolution": "node",
    "module": "commonjs",
    "noEmit": false,
    "allowJs": true,
    "removeComments": true,

    "strict": false,
    "noImplicitAny": false,
    "noImplicitThis": false,

    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": false,

    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,

    "skipLibCheck": true
  }
}

我知道我的tsconfig去掉了很多TypeScript验证,但这是必需的,因为我不是项目文件夹上的唯一开发者,不能修改所有文件以确保在构建过程中没有错误。

现在我的k6测试工作正常,使用了从我的项目导入的文件。

英文:

I found a solution.

I don't know if it's the change in my tree structure or the change in my configuration files.

My new tree is

- webpack.config.ts
- package.json
- project
- k6

package.json

{
&quot;name&quot;: &quot;webpack-ts-babel&quot;,
&quot;version&quot;: &quot;1.0.0&quot;,
&quot;main&quot;: &quot;webpack.config.js&quot;,
&quot;license&quot;: &quot;ISC&quot;,
&quot;devDependencies&quot;: {
&quot;@babel/core&quot;: &quot;^7.22.5&quot;,
&quot;babel-loader&quot;: &quot;^9.1.2&quot;,
&quot;babel-preset-es2015&quot;: &quot;^6.24.1&quot;,
&quot;ts-loader&quot;: &quot;^9.4.3&quot;,
&quot;typescript&quot;: &quot;^4.8.4&quot;,
&quot;webpack&quot;: &quot;^5.88.0&quot;,
&quot;webpack-cli&quot;: &quot;^5.1.4&quot;,
&quot;webpack-dev-server&quot;: &quot;^4.15.0&quot;,
&quot;webpack-glob-entries&quot;: &quot;^1.0.1&quot;
},
&quot;scripts&quot;: {
&quot;k6&quot;: &quot;webpack&quot;
}
}

webpack.config.js

const path = require(&#39;path&#39;);
const GlobEntries = require(&#39;webpack-glob-entries&#39;);
module.exports = {
mode: &#39;production&#39;,
entry: GlobEntries(&#39;./k6/src/*test*.ts&#39;),
devtool: &#39;inline-source-map&#39;,
target: [&#39;web&#39;, &#39;es5&#39;],
externals: /^(k6|https?\:\/\/)(\/.*)?/,
module: {
rules: [
{
test: /\.tsx?$/,
use: &#39;ts-loader&#39;,
exclude: /node_modules/,
},
],
},
resolve: {
extensions: [ &#39;.ts&#39;, &#39;.js&#39; ],
},
output: {
path: path.join(__dirname, &#39;k6/dist&#39;),
libraryTarget: &#39;commonjs&#39;,
filename: &#39;[name].js&#39;,
},
};

tsconfig.json in k6 folder

{
&quot;compilerOptions&quot;: {
&quot;target&quot;: &quot;es5&quot;,
&quot;moduleResolution&quot;: &quot;node&quot;,
&quot;module&quot;: &quot;commonjs&quot;,
&quot;noEmit&quot;: false,
&quot;allowJs&quot;: true,
&quot;removeComments&quot;: true,
&quot;strict&quot;: false,
&quot;noImplicitAny&quot;: false,
&quot;noImplicitThis&quot;: false,
&quot;noUnusedLocals&quot;: false,
&quot;noUnusedParameters&quot;: false,
&quot;noImplicitReturns&quot;: false,
&quot;noFallthroughCasesInSwitch&quot;: false,
&quot;allowSyntheticDefaultImports&quot;: true,
&quot;esModuleInterop&quot;: true,
&quot;experimentalDecorators&quot;: true,
&quot;emitDecoratorMetadata&quot;: true,
&quot;skipLibCheck&quot;: true
}
}

I know that my tsconfig removed a lot of typescript verification but it's mandatory as I'm not alone on the project folder and I can't modify all files in it to have no error during build.

Now my k6 tests are working and use the files imported from my project.

huangapple
  • 本文由 发表于 2023年6月21日 23:19:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定