@modern-js/node-bundle-require 1.1.0 → 1.2.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/CHANGELOG.md CHANGED
@@ -1,85 +1,32 @@
1
1
  # @modern-js/1.0.0-rc.19
2
2
 
3
- ## 1.1.0
4
-
5
- ### Minor Changes
6
-
7
- - 96119db2: Relese v1.1.0
8
-
9
- 1. 全面支持 Windows 平台
10
-
11
- ISSUE:
12
-
13
- - [182](https://github.com/modern-js-dev/modern.js/issues/182)
14
- - [236](https://github.com/modern-js-dev/modern.js/issues/236)
15
-
16
- PR
17
-
18
- - [219](https://github.com/modern-js-dev/modern.js/pull/219)
19
- - [247](https://github.com/modern-js-dev/modern.js/pull/247)
20
- - [249](https://github.com/modern-js-dev/modern.js/pull/249)
21
- - [253](https://github.com/modern-js-dev/modern.js/pull/253)
22
- - [268](https://github.com/modern-js-dev/modern.js/pull/268)
23
- - [270](https://github.com/modern-js-dev/modern.js/pull/270)
24
- - [273](https://github.com/modern-js-dev/modern.js/pull/273)
25
- - [276](https://github.com/modern-js-dev/modern.js/pull/276)
26
-
27
- 2. 修复 Unbundled 模式启用问题
28
-
29
- ISSUE
30
-
31
- - [186](https://github.com/modern-js-dev/modern.js/issues/186)
32
- - [224](https://github.com/modern-js-dev/modern.js/issues/224)
33
- - [225](https://github.com/modern-js-dev/modern.js/issues/225)
34
- - [238](https://github.com/modern-js-dev/modern.js/issues/238)
35
-
36
- PR
37
-
38
- - [226](https://github.com/modern-js-dev/modern.js/pull/226)
39
- - [264](https://github.com/modern-js-dev/modern.js/pull/264)
40
- - [codesmith-12](https://github.com/modern-js-dev/codesmith/pull/12)
3
+ ## 1.2.1
41
4
 
42
- 3. 修复模块工程方案 .npmignore 文件初始化未生成
43
-
44
- ISSUE
45
-
46
- - [198](https://github.com/modern-js-dev/modern.js/issues/198)
47
-
48
- PR
49
-
50
- - [209](https://github.com/modern-js-dev/modern.js/pull/209)
51
-
52
- 4. 修复 Storybook 使用时浏览器打开页面报错
53
-
54
- ISSUE
55
-
56
- - [228](https://github.com/modern-js-dev/modern.js/issues/228)
5
+ ### Patch Changes
57
6
 
58
- PR
7
+ - 83166714: change .npmignore
59
8
 
60
- - [254](https://github.com/modern-js-dev/modern.js/pull/254)
9
+ ## 1.2.0
61
10
 
62
- 5. 修复 BFF 一体化开发不支持 unbundle 模式
11
+ ### Minor Changes
63
12
 
64
- ISSUE
13
+ - cfe11628: Make Modern.js self bootstraping
65
14
 
66
- - [235](https://github.com/modern-js-dev/modern.js/issues/235)
67
- - [257](https://github.com/modern-js-dev/modern.js/issues/257)
15
+ ### Patch Changes
68
16
 
69
- PR
17
+ - 59cbcb77: fix esbuild error when project's tsconfig.json sets "target:es5"
70
18
 
71
- - [269](https://github.com/modern-js-dev/modern.js/pull/269)
72
- - [271](https://github.com/modern-js-dev/modern.js/pull/271)
19
+ ## 1.1.1
73
20
 
74
- 6. 修复 Node17 dev 命令报错问题
21
+ ### Patch Changes
75
22
 
76
- ISSUE
23
+ - 0fa83663: support more .env files
77
24
 
78
- - [180](https://github.com/modern-js-dev/modern.js/issues/180)
25
+ ## 1.1.0
79
26
 
80
- PR
27
+ ### Minor Changes
81
28
 
82
- - [214](https://github.com/modern-js-dev/modern.js/pull/214)
29
+ - 96119db2: Relese v1.1.0
83
30
 
84
31
  ## 1.0.1
85
32
 
@@ -1,12 +1,12 @@
1
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
2
 
3
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
4
 
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
- import fs from 'fs/promises';
8
7
  import path from 'path';
9
8
  import { randomUUID } from 'crypto';
9
+ import { fs } from '@modern-js/utils';
10
10
  import { build } from 'esbuild';
11
11
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
12
12
  // "/test/node_modules/foo"
@@ -39,7 +39,11 @@ export async function bundleRequire(filepath, options) {
39
39
  outfile,
40
40
  format: 'cjs',
41
41
  platform: 'node',
42
- bundle: true
42
+ bundle: true,
43
+ // fix transforming error when the project's tsconfig.json
44
+ // sets `target: "es5"`
45
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
46
+ target: 'esnext'
43
47
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
44
48
  plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
45
49
  {
@@ -94,7 +98,7 @@ export async function bundleRequire(filepath, options) {
94
98
  ctx.onLoad({
95
99
  filter: JS_EXT_RE
96
100
  }, async args => {
97
- const contents = await fs.readFile(args.path, 'utf-8');
101
+ const contents = fs.readFileSync(args.path, 'utf-8');
98
102
  return {
99
103
  contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(path.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
100
104
  loader: inferLoader(path.extname(args.path))
@@ -132,7 +136,7 @@ export async function bundleRequire(filepath, options) {
132
136
  mod = await req(outfile);
133
137
  } finally {
134
138
  // Remove the outfile after executed
135
- await fs.unlink(outfile);
139
+ fs.unlinkSync(outfile);
136
140
  }
137
141
 
138
142
  return mod;
@@ -6,19 +6,19 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.EXTERNAL_REGEXP = void 0;
7
7
  exports.bundleRequire = bundleRequire;
8
8
 
9
- var _promises = _interopRequireDefault(require("fs/promises"));
10
-
11
9
  var _path = _interopRequireDefault(require("path"));
12
10
 
13
11
  var _crypto = require("crypto");
14
12
 
13
+ var _utils = require("@modern-js/utils");
14
+
15
15
  var _esbuild = require("esbuild");
16
16
 
17
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
18
 
19
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
19
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
20
20
 
21
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
22
22
 
23
23
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
24
24
 
@@ -55,7 +55,11 @@ async function bundleRequire(filepath, options) {
55
55
  outfile,
56
56
  format: 'cjs',
57
57
  platform: 'node',
58
- bundle: true
58
+ bundle: true,
59
+ // fix transforming error when the project's tsconfig.json
60
+ // sets `target: "es5"`
61
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
62
+ target: 'esnext'
59
63
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
60
64
  plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
61
65
  {
@@ -110,7 +114,8 @@ async function bundleRequire(filepath, options) {
110
114
  ctx.onLoad({
111
115
  filter: JS_EXT_RE
112
116
  }, async args => {
113
- const contents = await _promises.default.readFile(args.path, 'utf-8');
117
+ const contents = _utils.fs.readFileSync(args.path, 'utf-8');
118
+
114
119
  return {
115
120
  contents: contents.replace(/\b__filename\b/g, JSON.stringify(args.path)).replace(/\b__dirname\b/g, JSON.stringify(_path.default.dirname(args.path))).replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
116
121
  loader: inferLoader(_path.default.extname(args.path))
@@ -148,7 +153,7 @@ async function bundleRequire(filepath, options) {
148
153
  mod = await req(outfile);
149
154
  } finally {
150
155
  // Remove the outfile after executed
151
- await _promises.default.unlink(outfile);
156
+ _utils.fs.unlinkSync(outfile);
152
157
  }
153
158
 
154
159
  return mod;
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/modern.config.js CHANGED
@@ -1,10 +1,2 @@
1
1
  /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {
3
- testing: {
4
- jest: {
5
- collectCoverage: true,
6
- collectCoverageFrom: ['src/**/*.ts'],
7
- coveragePathIgnorePatterns: ['/node_modules/'],
8
- },
9
- },
10
- };
2
+ module.exports = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "1.1.0",
3
+ "version": "1.2.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",
@@ -20,6 +20,7 @@
20
20
  "exports": {
21
21
  ".": {
22
22
  "node": {
23
+ "jsnext:source": "./src/index.ts",
23
24
  "import": "./dist/js/modern/index.js",
24
25
  "require": "./dist/js/node/index.js"
25
26
  },
@@ -31,11 +32,13 @@
31
32
  "esbuild": "^0.13.8"
32
33
  },
33
34
  "devDependencies": {
34
- "@modern-js/plugin-testing": "^1.0.2",
35
- "@modern-js/module-tools": "^1.0.2",
35
+ "@scripts/build": "0.0.0",
36
+ "@modern-js/utils": "^1.2.2",
36
37
  "@types/jest": "^26.0.9",
37
38
  "@types/node": "^14",
38
- "typescript": "^4"
39
+ "typescript": "^4",
40
+ "jest": "^27",
41
+ "@scripts/jest-config": "0.0.0"
39
42
  },
40
43
  "sideEffects": false,
41
44
  "modernConfig": {
@@ -45,11 +48,13 @@
45
48
  },
46
49
  "publishConfig": {
47
50
  "registry": "https://registry.npmjs.org/",
48
- "access": "public"
51
+ "access": "public",
52
+ "types": "./dist/types/index.d.ts"
49
53
  },
50
54
  "scripts": {
51
55
  "new": "modern new",
52
56
  "build": "modern build",
53
- "test": "modern test --passWithNoTests"
54
- }
57
+ "test": "jest --passWithNoTests"
58
+ },
59
+ "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
55
60
  }
@@ -1 +1,2 @@
1
1
  export const filename: string = __filename;
2
+ export const showFileName = () => filename;
@@ -5,7 +5,14 @@ test('require', async () => {
5
5
  const result = await bundleRequire(
6
6
  path.join(__dirname, './fixture/input.ts'),
7
7
  );
8
+ // when tsconfig.json sets `compilerOptions.target` to `es5`
9
+ // normally it will met error
10
+ // So we need to manually set esbuild's target to esnext to avoid that
11
+ // These two cases above use ES6+ ability, to test whether esbuild successfuly
12
+ // works on non-ES5 files
13
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
8
14
  expect(result.default.a.filename.endsWith('a.ts')).toEqual(true);
15
+ expect(result.default.a.showFileName().endsWith('a.ts')).toEqual(true);
9
16
  });
10
17
 
11
18
  describe('external regexp', () => {
@@ -2,10 +2,9 @@
2
2
  "extends": "@modern-js/tsconfig/base",
3
3
  "compilerOptions": {
4
4
  "declaration": false,
5
+ "target": "es5",
5
6
  "jsx": "preserve",
6
7
  "baseUrl": "./",
7
- "paths": {
8
- "@/*": ["../src/*"]
9
- }
8
+ "paths": {}
10
9
  }
11
10
  }
package/tsconfig.json CHANGED
@@ -5,9 +5,7 @@
5
5
  "jsx": "preserve",
6
6
  "baseUrl": "./",
7
7
  "isolatedModules": true,
8
- "paths": {
9
- "@/*": ["./src/*"]
10
- }
8
+ "paths": {}
11
9
  },
12
10
  "include": ["src"]
13
11
  }
@@ -1,3 +0,0 @@
1
- {
2
- "extends": ["@modern-js-app"]
3
- }
package/src/index.ts DELETED
@@ -1,163 +0,0 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
3
- import { randomUUID } from 'crypto';
4
- import { build, Loader, Plugin, BuildOptions } from 'esbuild';
5
-
6
- const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/;
7
-
8
- // Must not start with "/" or "./" or "../"
9
- // "/test/node_modules/foo"
10
- // "c:/node_modules/foo"
11
- export const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
12
-
13
- const CACHE_DIR = path.relative(
14
- process.cwd(),
15
- './node_modules/.node-bundle-require',
16
- );
17
-
18
- function inferLoader(ext: string): Loader {
19
- if (ext === '.mjs' || ext === '.cjs') {
20
- return 'js';
21
- }
22
- return ext.slice(1) as Loader;
23
- }
24
-
25
- export interface Options {
26
- /**
27
- * The `require` function that is used to load the output file
28
- * Default to the global `require` function
29
- * This function can be asynchronous, i.e. returns a Promise
30
- */
31
- require?: (outfile: string) => any;
32
- /**
33
- * esbuild options
34
- */
35
- esbuildOptions?: BuildOptions;
36
- /**
37
- * esbuild plugin
38
- */
39
- esbuildPlugins?: Plugin[];
40
- /**
41
- * Get the path to the output file
42
- * By default we simply replace the extension with `.bundled.cjs`
43
- */
44
- getOutputFile?: (filepath: string) => string;
45
- }
46
-
47
- const defaultGetOutputFile = (filepath: string) =>
48
- path.resolve(
49
- CACHE_DIR,
50
- `${filepath}-${Date.now()}.${randomUUID({
51
- disableEntropyCache: true,
52
- })}.bundled.cjs`,
53
- );
54
-
55
- export async function bundleRequire(filepath: string, options?: Options) {
56
- if (!JS_EXT_RE.test(filepath)) {
57
- throw new Error(`${filepath} is not a valid JS file`);
58
- }
59
-
60
- const getOutputFile = options?.getOutputFile || defaultGetOutputFile;
61
- const outfile = getOutputFile(filepath);
62
-
63
- await build({
64
- entryPoints: [filepath],
65
- outfile,
66
- format: 'cjs',
67
- platform: 'node',
68
- bundle: true,
69
- ...options?.esbuildOptions,
70
- plugins: [
71
- ...(options?.esbuildPlugins || []),
72
- // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
73
- {
74
- name: 'native-node-modules',
75
- // eslint-disable-next-line @typescript-eslint/no-shadow
76
- setup(build) {
77
- // If a ".node" file is imported within a module in the "file" namespace, resolve
78
- // it to an absolute path and put it into the "node-file" virtual namespace.
79
- build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({
80
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
81
- namespace: 'node-file',
82
- }));
83
-
84
- // Files in the "node-file" virtual namespace call "require()" on the
85
- // path from esbuild of the ".node" file in the output directory.
86
- build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({
87
- contents: `
88
- import path from ${JSON.stringify(args.path)}
89
- try { module.exports = require(path) }
90
- catch {}
91
- `,
92
- }));
93
-
94
- // If a ".node" file is imported within a module in the "node-file" namespace, put
95
- // it in the "file" namespace where esbuild's default loading behavior will handle
96
- // it. It is already an absolute path since we resolved it to one above.
97
- build.onResolve(
98
- { filter: /\.node$/, namespace: 'node-file' },
99
- args => ({
100
- path: args.path,
101
- namespace: 'file',
102
- }),
103
- );
104
-
105
- // Tell esbuild's default loading behavior to use the "file" loader for
106
- // these ".node" files.
107
- const opts = build.initialOptions;
108
- opts.loader = opts.loader || {};
109
- opts.loader['.node'] = 'file';
110
- },
111
- },
112
- {
113
- name: 'replace-path',
114
- setup(ctx) {
115
- ctx.onLoad({ filter: JS_EXT_RE }, async args => {
116
- const contents = await fs.readFile(args.path, 'utf-8');
117
- return {
118
- contents: contents
119
- .replace(/\b__filename\b/g, JSON.stringify(args.path))
120
- .replace(
121
- /\b__dirname\b/g,
122
- JSON.stringify(path.dirname(args.path)),
123
- )
124
- .replace(
125
- /\bimport\.meta\.url\b/g,
126
- JSON.stringify(`file://${args.path}`),
127
- ),
128
- loader: inferLoader(path.extname(args.path)),
129
- };
130
- });
131
- },
132
- },
133
- // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
134
- {
135
- name: 'make-all-packages-external',
136
- setup(_build) {
137
- _build.onResolve({ filter: EXTERNAL_REGEXP }, args => {
138
- let external = true;
139
- // FIXME: windows external entrypoint
140
- if (args.kind === 'entry-point') {
141
- external = false;
142
- }
143
- return {
144
- path: args.path,
145
- external,
146
- };
147
- });
148
- },
149
- },
150
- ],
151
- });
152
-
153
- let mod: any;
154
- const req = options?.require || require;
155
- try {
156
- mod = await req(outfile);
157
- } finally {
158
- // Remove the outfile after executed
159
- await fs.unlink(outfile);
160
- }
161
-
162
- return mod;
163
- }
@@ -1,2 +0,0 @@
1
- /// <reference types='@modern-js/module-tools' />
2
- /// <reference types='@modern-js/plugin-testing/type' />