@modern-js/node-bundle-require 1.1.2-canary.0 → 1.2.2

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,5 +1,21 @@
1
1
  # @modern-js/1.0.0-rc.19
2
2
 
3
+ ## 1.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+
9
+ ## 1.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - cfe11628: Make Modern.js self bootstraping
14
+
15
+ ### Patch Changes
16
+
17
+ - 59cbcb77: fix esbuild error when project's tsconfig.json sets "target:es5"
18
+
3
19
  ## 1.1.1
4
20
 
5
21
  ### Patch Changes
@@ -1,13 +1,13 @@
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';
8
7
  import path from 'path';
9
- import { randomUUID } from 'crypto';
8
+ import { fs } from '@modern-js/utils';
10
9
  import { build } from 'esbuild';
10
+ import { nanoid } from 'nanoid';
11
11
  const JS_EXT_RE = /\.(mjs|cjs|ts|js|tsx|jsx)$/; // Must not start with "/" or "./" or "../"
12
12
  // "/test/node_modules/foo"
13
13
  // "c:/node_modules/foo"
@@ -23,9 +23,7 @@ function inferLoader(ext) {
23
23
  return ext.slice(1);
24
24
  }
25
25
 
26
- const defaultGetOutputFile = filepath => path.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${randomUUID({
27
- disableEntropyCache: true
28
- })}.bundled.cjs`);
26
+ const defaultGetOutputFile = filepath => path.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${nanoid()}.bundled.cjs`);
29
27
 
30
28
  export async function bundleRequire(filepath, options) {
31
29
  if (!JS_EXT_RE.test(filepath)) {
@@ -39,7 +37,11 @@ export async function bundleRequire(filepath, options) {
39
37
  outfile,
40
38
  format: 'cjs',
41
39
  platform: 'node',
42
- bundle: true
40
+ bundle: true,
41
+ // fix transforming error when the project's tsconfig.json
42
+ // sets `target: "es5"`
43
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
44
+ target: 'esnext'
43
45
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
44
46
  plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
45
47
  {
@@ -6,19 +6,19 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.EXTERNAL_REGEXP = void 0;
7
7
  exports.bundleRequire = bundleRequire;
8
8
 
9
- var _fs = _interopRequireDefault(require("fs"));
10
-
11
9
  var _path = _interopRequireDefault(require("path"));
12
10
 
13
- var _crypto = require("crypto");
11
+ var _utils = require("@modern-js/utils");
14
12
 
15
13
  var _esbuild = require("esbuild");
16
14
 
15
+ var _nanoid = require("nanoid");
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
 
@@ -39,9 +39,7 @@ function inferLoader(ext) {
39
39
  return ext.slice(1);
40
40
  }
41
41
 
42
- const defaultGetOutputFile = filepath => _path.default.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${(0, _crypto.randomUUID)({
43
- disableEntropyCache: true
44
- })}.bundled.cjs`);
42
+ const defaultGetOutputFile = filepath => _path.default.resolve(CACHE_DIR, `${filepath}-${Date.now()}.${(0, _nanoid.nanoid)()}.bundled.cjs`);
45
43
 
46
44
  async function bundleRequire(filepath, options) {
47
45
  if (!JS_EXT_RE.test(filepath)) {
@@ -55,7 +53,11 @@ async function bundleRequire(filepath, options) {
55
53
  outfile,
56
54
  format: 'cjs',
57
55
  platform: 'node',
58
- bundle: true
56
+ bundle: true,
57
+ // fix transforming error when the project's tsconfig.json
58
+ // sets `target: "es5"`
59
+ // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
60
+ target: 'esnext'
59
61
  }, options === null || options === void 0 ? void 0 : options.esbuildOptions), {}, {
60
62
  plugins: [...((options === null || options === void 0 ? void 0 : options.esbuildPlugins) || []), // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
61
63
  {
@@ -110,7 +112,7 @@ async function bundleRequire(filepath, options) {
110
112
  ctx.onLoad({
111
113
  filter: JS_EXT_RE
112
114
  }, async args => {
113
- const contents = _fs.default.readFileSync(args.path, 'utf-8');
115
+ const contents = _utils.fs.readFileSync(args.path, 'utf-8');
114
116
 
115
117
  return {
116
118
  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}`)),
@@ -149,7 +151,7 @@ async function bundleRequire(filepath, options) {
149
151
  mod = await req(outfile);
150
152
  } finally {
151
153
  // Remove the outfile after executed
152
- _fs.default.unlinkSync(outfile);
154
+ _utils.fs.unlinkSync(outfile);
153
155
  }
154
156
 
155
157
  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.2-canary.0",
3
+ "version": "1.2.2",
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,28 +20,26 @@
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
  },
26
27
  "default": "./dist/js/treeshaking/index.js"
27
28
  }
28
29
  },
29
- "scripts": {
30
- "prepare": "pnpm build",
31
- "new": "modern new",
32
- "build": "modern build",
33
- "test": "modern test --passWithNoTests"
34
- },
35
30
  "dependencies": {
36
31
  "@babel/runtime": "^7",
37
- "esbuild": "^0.13.8"
32
+ "esbuild": "^0.13.8",
33
+ "nanoid": "^3.3.1"
38
34
  },
39
35
  "devDependencies": {
40
- "@modern-js/plugin-testing": "^1.1.1",
41
- "@modern-js/module-tools": "^1.1.1",
36
+ "@scripts/build": "0.0.0",
37
+ "@modern-js/utils": "^1.2.2",
42
38
  "@types/jest": "^26.0.9",
43
39
  "@types/node": "^14",
44
- "typescript": "^4"
40
+ "typescript": "^4",
41
+ "jest": "^27",
42
+ "@scripts/jest-config": "0.0.0"
45
43
  },
46
44
  "sideEffects": false,
47
45
  "modernConfig": {
@@ -51,6 +49,13 @@
51
49
  },
52
50
  "publishConfig": {
53
51
  "registry": "https://registry.npmjs.org/",
54
- "access": "public"
55
- }
56
- }
52
+ "access": "public",
53
+ "types": "./dist/types/index.d.ts"
54
+ },
55
+ "scripts": {
56
+ "new": "modern new",
57
+ "build": "modern build",
58
+ "test": "jest --passWithNoTests"
59
+ },
60
+ "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"
61
+ }
@@ -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';
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 = fs.readFileSync(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
- fs.unlinkSync(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' />