@grafana/create-plugin 0.5.2 → 0.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ # v0.5.3 (Thu Nov 17 2022)
2
+
3
+ #### 🐛 Bug Fix
4
+
5
+ - Create-Plugin: Transform known ES modules in jest config [#145](https://github.com/grafana/plugin-tools/pull/145) ([@jackw](https://github.com/jackw))
6
+
7
+ #### Authors: 1
8
+
9
+ - Jack Westbrook ([@jackw](https://github.com/jackw))
10
+
11
+ ---
12
+
1
13
  # v0.5.2 (Thu Nov 17 2022)
2
14
 
3
15
  #### 🐛 Bug Fix
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grafana/create-plugin",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "directory": "packages/create-plugin",
@@ -59,5 +59,5 @@
59
59
  "engines": {
60
60
  "node": ">=16"
61
61
  },
62
- "gitHead": "33c2b874ddfd16530302004a8089162f04d12c4d"
62
+ "gitHead": "ffc20a3bfe4c15077f48f958808d212cfb463fb1"
63
63
  }
@@ -16,11 +16,12 @@ to issues around working with the project.
16
16
  Edit the `.eslintrc` file in the project root in order to extend the ESLint configuration.
17
17
 
18
18
  **Example:**
19
+
19
20
  ```json
20
21
  {
21
22
  "extends": "./.config/.eslintrc",
22
23
  "rules": {
23
- "react/prop-types": "off"
24
+ "react/prop-types": "off"
24
25
  }
25
26
  }
26
27
  ```
@@ -32,10 +33,11 @@ Edit the `.eslintrc` file in the project root in order to extend the ESLint conf
32
33
  Edit the `.prettierrc.js` file in the project root in order to extend the Prettier configuration.
33
34
 
34
35
  **Example:**
36
+
35
37
  ```javascript
36
38
  module.exports = {
37
39
  // Prettier configuration provided by Grafana scaffolding
38
- ...require("./.config/.prettierrc.js"),
40
+ ...require('./.config/.prettierrc.js'),
39
41
 
40
42
  semi: false,
41
43
  };
@@ -50,7 +52,23 @@ There are two configuration in the project root that belong to Jest: `jest-setup
50
52
  **`jest-setup.js`:** A file that is run before each test file in the suite is executed. We are using it to
51
53
  set up the Jest DOM for the testing library and to apply some polyfills. ([link to Jest docs](https://jestjs.io/docs/configuration#setupfilesafterenv-array))
52
54
 
53
- **`jest.config.js`:** The main Jest configuration file that is extending our basic Grafana-tailored setup. ([link to Jest docs](https://jestjs.io/docs/configuration))
55
+ **`jest.config.js`:** The main Jest configuration file that extends the Grafana recommended setup. ([link to Jest docs](https://jestjs.io/docs/configuration))
56
+
57
+ #### ESM errors with Jest
58
+
59
+ A common issue found with the current jest config involves importing an npm package which only offers an ESM build. These packages cause jest to error with `SyntaxError: Cannot use import statement outside a module`. To work around this we provide a list of known packages to pass to the `[transformIgnorePatterns](https://jestjs.io/docs/configuration#transformignorepatterns-arraystring)` jest configuration property. If need be this can be extended in the following way:
60
+
61
+ ```javascript
62
+ process.env.TZ = 'UTC';
63
+ const { grafanaESModules, nodeModulesToTransform } = require('./jest/utils');
64
+
65
+ module.exports = {
66
+ // Jest configuration provided by Grafana
67
+ ...require('./.config/jest.config'),
68
+ // Inform jest to only transform specific node_module packages.
69
+ transformIgnorePatterns: [nodeModulesToTransform([...grafanaESModules, 'packageName'])],
70
+ };
71
+ ```
54
72
 
55
73
  ---
56
74
 
@@ -59,6 +77,7 @@ set up the Jest DOM for the testing library and to apply some polyfills. ([link
59
77
  Edit the `tsconfig.json` file in the project root in order to extend the TypeScript configuration.
60
78
 
61
79
  **Example:**
80
+
62
81
  ```json
63
82
  {
64
83
  "extends": "./.config/tsconfig.json",
@@ -80,6 +99,7 @@ Create a new config file that is going to extend the basic one provided by Grafa
80
99
  It can live in the project root, e.g. `webpack.config.ts`.
81
100
 
82
101
  #### 2. Merge the basic config provided by Grafana and your custom setup
102
+
83
103
  We are going to use [`webpack-merge`](https://github.com/survivejs/webpack-merge) for this.
84
104
 
85
105
  ```typescript
@@ -100,7 +120,6 @@ const config = async (env): Promise<Configuration> => {
100
120
  };
101
121
 
102
122
  export default config;
103
-
104
123
  ```
105
124
 
106
125
  #### 3. Update the `package.json` to use the new Webpack config
@@ -108,12 +127,14 @@ export default config;
108
127
  We need to update the `scripts` in the `package.json` to use the extended Webpack configuration.
109
128
 
110
129
  **Update for `build`:**
130
+
111
131
  ```diff
112
132
  -"build": "webpack -c ./.config/webpack/webpack.config.ts --env production",
113
133
  +"build": "webpack -c ./webpack.config.ts --env production",
114
134
  ```
115
135
 
116
136
  **Update for `dev`:**
137
+
117
138
  ```diff
118
139
  -"dev": "webpack -w -c ./.config/webpack/webpack.config.ts --env development",
119
140
  +"dev": "webpack -w -c ./webpack.config.ts --env development",
@@ -0,0 +1,8 @@
1
+ /*
2
+ * This utility function is useful in combination with jest `transformIgnorePatterns` config
3
+ * to transform specific packages (e.g.ES modules) in a projects node_modules folder.
4
+ */
5
+ export const nodeModulesToTransform = (moduleNames: string[]) => `node_modules\/(?!(${moduleNames.join('|')})\/)`;
6
+
7
+ // Array of known nested grafana package dependencies that only bundle an ESM version
8
+ export const grafanaESModules = ['ol', 'react-colorful'];
@@ -5,11 +5,12 @@
5
5
  */
6
6
 
7
7
  const path = require('path');
8
+ const { grafanaESModules, nodeModulesToTransform } = require('./jest/utils');
8
9
 
9
10
  module.exports = {
10
11
  moduleNameMapper: {
11
- "\\.(css|scss|sass)$": "identity-obj-proxy",
12
- "react-inlinesvg": path.resolve(__dirname, "mocks", "react-inlinesvg.tsx"),
12
+ '\\.(css|scss|sass)$': 'identity-obj-proxy',
13
+ 'react-inlinesvg': path.resolve(__dirname, 'jest', 'mocks', 'react-inlinesvg.tsx'),
13
14
  },
14
15
  modulePaths: ['<rootDir>/src'],
15
16
  setupFilesAfterEnv: ['<rootDir>/jest-setup.js'],
@@ -35,5 +36,7 @@ module.exports = {
35
36
  },
36
37
  ],
37
38
  },
38
- transformIgnorePatterns: [],
39
+ // Jest will throw `Cannot use import statement outside module` if it tries to load an
40
+ // ES module without it being transformed first. ./config/README.md#esm-errors-with-jest
41
+ transformIgnorePatterns: [nodeModulesToTransform(grafanaESModules)],
39
42
  };