@modern-js/node-bundle-require 1.2.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,5 +1,11 @@
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
+
3
9
  ## 1.2.0
4
10
 
5
11
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/node-bundle-require",
3
- "version": "1.2.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",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@scripts/build": "0.0.0",
36
- "@modern-js/utils": "^1.2.0",
36
+ "@modern-js/utils": "^1.2.2",
37
37
  "@types/jest": "^26.0.9",
38
38
  "@types/node": "^14",
39
39
  "typescript": "^4",
@@ -1,3 +0,0 @@
1
- {
2
- "extends": ["@modern-js-app"]
3
- }
package/src/index.ts DELETED
@@ -1,167 +0,0 @@
1
- import path from 'path';
2
- import { randomUUID } from 'crypto';
3
- import { fs } from '@modern-js/utils';
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
- // fix transforming error when the project's tsconfig.json
70
- // sets `target: "es5"`
71
- // reference: https://github.com/evanw/esbuild/releases/tag/v0.12.6
72
- target: 'esnext',
73
- ...options?.esbuildOptions,
74
- plugins: [
75
- ...(options?.esbuildPlugins || []),
76
- // https://github.com/evanw/esbuild/issues/1051#issuecomment-806325487
77
- {
78
- name: 'native-node-modules',
79
- // eslint-disable-next-line @typescript-eslint/no-shadow
80
- setup(build) {
81
- // If a ".node" file is imported within a module in the "file" namespace, resolve
82
- // it to an absolute path and put it into the "node-file" virtual namespace.
83
- build.onResolve({ filter: /\.node$/, namespace: 'file' }, args => ({
84
- path: require.resolve(args.path, { paths: [args.resolveDir] }),
85
- namespace: 'node-file',
86
- }));
87
-
88
- // Files in the "node-file" virtual namespace call "require()" on the
89
- // path from esbuild of the ".node" file in the output directory.
90
- build.onLoad({ filter: /.*/, namespace: 'node-file' }, args => ({
91
- contents: `
92
- import path from ${JSON.stringify(args.path)}
93
- try { module.exports = require(path) }
94
- catch {}
95
- `,
96
- }));
97
-
98
- // If a ".node" file is imported within a module in the "node-file" namespace, put
99
- // it in the "file" namespace where esbuild's default loading behavior will handle
100
- // it. It is already an absolute path since we resolved it to one above.
101
- build.onResolve(
102
- { filter: /\.node$/, namespace: 'node-file' },
103
- args => ({
104
- path: args.path,
105
- namespace: 'file',
106
- }),
107
- );
108
-
109
- // Tell esbuild's default loading behavior to use the "file" loader for
110
- // these ".node" files.
111
- const opts = build.initialOptions;
112
- opts.loader = opts.loader || {};
113
- opts.loader['.node'] = 'file';
114
- },
115
- },
116
- {
117
- name: 'replace-path',
118
- setup(ctx) {
119
- ctx.onLoad({ filter: JS_EXT_RE }, async args => {
120
- const contents = fs.readFileSync(args.path, 'utf-8');
121
- return {
122
- contents: contents
123
- .replace(/\b__filename\b/g, JSON.stringify(args.path))
124
- .replace(
125
- /\b__dirname\b/g,
126
- JSON.stringify(path.dirname(args.path)),
127
- )
128
- .replace(
129
- /\bimport\.meta\.url\b/g,
130
- JSON.stringify(`file://${args.path}`),
131
- ),
132
- loader: inferLoader(path.extname(args.path)),
133
- };
134
- });
135
- },
136
- },
137
- // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294
138
- {
139
- name: 'make-all-packages-external',
140
- setup(_build) {
141
- _build.onResolve({ filter: EXTERNAL_REGEXP }, args => {
142
- let external = true;
143
- // FIXME: windows external entrypoint
144
- if (args.kind === 'entry-point') {
145
- external = false;
146
- }
147
- return {
148
- path: args.path,
149
- external,
150
- };
151
- });
152
- },
153
- },
154
- ],
155
- });
156
-
157
- let mod: any;
158
- const req = options?.require || require;
159
- try {
160
- mod = await req(outfile);
161
- } finally {
162
- // Remove the outfile after executed
163
- fs.unlinkSync(outfile);
164
- }
165
-
166
- return mod;
167
- }
@@ -1,2 +0,0 @@
1
- /// <reference types='@modern-js/module-tools' />
2
- /// <reference types='@modern-js/plugin-testing/type' />