@modern-js/node-bundle-require 1.3.0 → 1.3.1

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/.eslintrc.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@modern-js'],
4
+ parserOptions: {
5
+ tsconfigRootDir: __dirname,
6
+ project: ['./tsconfig.json'],
7
+ },
8
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @modern-js/node-bundle-require
2
2
 
3
+ ## 1.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - b8599d09: fix: failed to generate webpack cache
8
+ - 6cffe99d: chore:
9
+ remove react eslint rules for `modern-js` rule set.
10
+ add .eslintrc for each package to speed up linting
11
+ - 04ae5262: chore: bump @modern-js/utils to v1.4.1 in dependencies
12
+ - 60f7d8bf: feat: add tests dir to npmignore
13
+
3
14
  ## 1.3.0
4
15
 
5
16
  ### Minor Changes
@@ -1,15 +1,29 @@
1
1
  import { fs } from '@modern-js/utils';
2
2
  import { bundle } from "./bundle";
3
3
  export { bundle };
4
+
5
+ function deleteRequireCache(path) {
6
+ if (require.cache[path]) {
7
+ delete require.cache[path];
8
+ }
9
+
10
+ if (module.children) {
11
+ module.children = module.children.filter(item => item.filename !== path);
12
+ }
13
+ }
14
+
4
15
  export async function bundleRequire(filepath, options) {
5
16
  const configFile = await bundle(filepath, options);
6
17
  let mod;
7
18
  const req = (options === null || options === void 0 ? void 0 : options.require) || require;
8
19
 
9
20
  try {
10
- mod = await req(configFile);
21
+ mod = await req(configFile); // Webpack will check require history for persistent cache.
22
+ // If webpack can not resolve the file, the previous cache pack will become invalid.
23
+ // The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
24
+
25
+ deleteRequireCache(configFile);
11
26
  } finally {
12
- // Remove the configFile after executed
13
27
  fs.unlinkSync(configFile);
14
28
  }
15
29
 
@@ -15,15 +15,28 @@ var _utils = require("@modern-js/utils");
15
15
 
16
16
  var _bundle = require("./bundle");
17
17
 
18
+ function deleteRequireCache(path) {
19
+ if (require.cache[path]) {
20
+ delete require.cache[path];
21
+ }
22
+
23
+ if (module.children) {
24
+ module.children = module.children.filter(item => item.filename !== path);
25
+ }
26
+ }
27
+
18
28
  async function bundleRequire(filepath, options) {
19
29
  const configFile = await (0, _bundle.bundle)(filepath, options);
20
30
  let mod;
21
31
  const req = (options === null || options === void 0 ? void 0 : options.require) || require;
22
32
 
23
33
  try {
24
- mod = await req(configFile);
34
+ mod = await req(configFile); // Webpack will check require history for persistent cache.
35
+ // If webpack can not resolve the file, the previous cache pack will become invalid.
36
+ // The bundled file is temporary, so we should clear the require history to avoid breaking the webpack cache.
37
+
38
+ deleteRequireCache(configFile);
25
39
  } finally {
26
- // Remove the configFile after executed
27
40
  _utils.fs.unlinkSync(configFile);
28
41
  }
29
42
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "The meta-framework suite designed from scratch for frontend-focused modern web development.",
5
5
  "homepage": "https://modernjs.dev",
6
6
  "bugs": "https://github.com/modern-js-dev/modern.js/issues",
@@ -44,7 +44,7 @@
44
44
  "nanoid": "^3.3.2"
45
45
  },
46
46
  "devDependencies": {
47
- "@modern-js/utils": "^1.4.0",
47
+ "@modern-js/utils": "^1.5.0",
48
48
  "@scripts/build": "0.0.0",
49
49
  "@scripts/jest-config": "0.0.0",
50
50
  "@types/jest": "^26.0.9",
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- extends: '@modern-js',
3
- parserOptions: {
4
- project: require.resolve('./tsconfig.json'),
5
- },
6
- };
@@ -1,2 +0,0 @@
1
- export const filename: string = __filename;
2
- export const showFileName = () => filename;
@@ -1,5 +0,0 @@
1
- import * as a from './a';
2
-
3
- export default {
4
- a,
5
- };
@@ -1,27 +0,0 @@
1
- import path from 'path';
2
- import { bundleRequire } from '../src';
3
- import { EXTERNAL_REGEXP } from '../src/bundle';
4
-
5
- test('require', async () => {
6
- const result = await bundleRequire(
7
- path.join(__dirname, './fixture/input.ts'),
8
- );
9
- // when tsconfig.json sets `compilerOptions.target` to `es5`
10
- // normally it will met error
11
- // So we need to manually set esbuild's target to esnext to avoid that
12
- // These two cases above use ES6+ ability, to test whether esbuild successfuly
13
- // works on non-ES5 files
14
- // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
15
- expect(result.default.a.filename.endsWith('a.ts')).toEqual(true);
16
- expect(result.default.a.showFileName().endsWith('a.ts')).toEqual(true);
17
- });
18
-
19
- describe('external regexp', () => {
20
- expect(EXTERNAL_REGEXP.test('./test')).toBeFalsy();
21
- expect(EXTERNAL_REGEXP.test('/test')).toBeFalsy();
22
- expect(EXTERNAL_REGEXP.test('c:/foo')).toBeTruthy();
23
- expect(EXTERNAL_REGEXP.test('C:/foo')).toBeTruthy();
24
- expect(EXTERNAL_REGEXP.test('c:/node_modules/foo')).toBeTruthy();
25
- expect(EXTERNAL_REGEXP.test('foo')).toBeTruthy();
26
- expect(EXTERNAL_REGEXP.test('/test/node_modules')).toBeFalsy();
27
- });
@@ -1,2 +0,0 @@
1
- /// <reference types="@modern-js/module-tools/types" />
2
- /// <reference types="@modern-js/plugin-testing/types" />
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "@modern-js/tsconfig/base",
3
- "compilerOptions": {
4
- "declaration": false,
5
- "target": "es5",
6
- "jsx": "preserve",
7
- "baseUrl": "./",
8
- "paths": {}
9
- }
10
- }