requirejs-esm 0.0.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/LICENSE +21 -0
- package/README.md +141 -0
- package/bin/esm2requirejs.js +63 -0
- package/dist/api.js +85671 -0
- package/dist/api.js.map +1 -0
- package/dist/plugin.js +85830 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +121 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ferdinand Prantl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# ESM to AMD Plugin for RequireJS
|
|
2
|
+
|
|
3
|
+
[
|
|
4
|
+

|
|
5
|
+
](https://www.npmjs.com/package/requirejs-esm)
|
|
6
|
+
|
|
7
|
+
A [RequireJS] plugin converting JavaScript modules from ESM to AMD. It takes care only of the module format; it does not transpile the language and that is why it is a lot faster than plugins using [Babel]. If you need to transpile the code to an earlier ECMAScript version, have a look at [requirejs-babel7].
|
|
8
|
+
|
|
9
|
+
The official [RequireJS optimizer] (`r.js`) does not wire up source maps from the original (not transpiled) sources to the source map of the output bundle. It makes this or similar plugins unfeasible for serious work. If you want the proper support for source maps, replace the official optimizer package ([`requirejs`]) with the forked [`@prantlf/requirejs`], which is fixed.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
This module can be installed in your project using [NPM], [PNPM] or [Yarn]. Make sure, that you use [Node.js] version 6 or newer.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm i -D requirejs-esm
|
|
17
|
+
pnpm i -D requirejs-esm
|
|
18
|
+
yarn add requirejs-esm
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
Add the following paths to the RequireJS configuration:
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
paths: {
|
|
27
|
+
esm: 'node_modules/requirejs-esm/dist/plugin'
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Reference ESM source files files via the `esm!` plugin prefix:
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
define(['esm!your-esm-module'], function (module) {
|
|
35
|
+
// ...
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can use the ESM module format in modules loaded by the `esm!` plugin including the keyword `import` for loading nested dependencies. The plugin `esm!` has to be used only in the topmost `require` or `define` statement.
|
|
40
|
+
|
|
41
|
+
This plugin transpiles only ESM source files. If it detects a statement calling functions `define`, `require` or `require.config` on the root level of the source file, it will return the text of the source file as-is. Source files, which are already AMD modules, are assumed to contain ES5 only.
|
|
42
|
+
|
|
43
|
+
If you use the RequireJS optimizer `r.js`, you have to bundle the `esm` plugin without the compiling functionality by adding the following to the RequireJS build configuration:
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
pragmasOnSave: {
|
|
47
|
+
excludeEsm: true // removes the transpiling code from esm.js
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
See also a [demo] project:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm start
|
|
55
|
+
open http://localhost:8967/demo/normal.html
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Advanced
|
|
59
|
+
|
|
60
|
+
You can customize the [default module name resolution] with the `resolvePath` key (see [resolvePath] for more information) to transpile only modules with a special file extension:
|
|
61
|
+
|
|
62
|
+
```js
|
|
63
|
+
// import * from 'es5module.js' -> define(['es5module])
|
|
64
|
+
// import * from 'es6module.mjs' -> define(['esm!es6module])
|
|
65
|
+
fileExtension: '.mjs',
|
|
66
|
+
resolvePath: function (sourcePath, currentFile, options, originalResolvePath) {
|
|
67
|
+
// Ignore paths with other plugins applied and the three built-in
|
|
68
|
+
// pseudo-modules of RequireJS.
|
|
69
|
+
if (sourcePath.includes('!') || sourcePath === 'require' ||
|
|
70
|
+
sourcePath === 'module' || sourcePath === 'exports') return
|
|
71
|
+
|
|
72
|
+
let lengthWithoutExtension = sourcePath.length - 3
|
|
73
|
+
if (sourcePath.lastIndexOf('.js') === lengthWithoutExtension) {
|
|
74
|
+
return sourcePath.substr(0, lengthWithoutExtension)
|
|
75
|
+
}
|
|
76
|
+
--lengthWithoutExtension
|
|
77
|
+
if (sourcePath.lastIndexOf('.mjs') === lengthWithoutExtension) {
|
|
78
|
+
return 'esm!' + sourcePath.substr(0, lengthWithoutExtension)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The default implementation of `resolvePath` ensures that every JavaScript dependency will be converted:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
function (sourcePath) {
|
|
87
|
+
if (sourcePath.includes('!') || sourcePath === 'require' ||
|
|
88
|
+
sourcePath === 'module' || sourcePath === 'exports') return
|
|
89
|
+
|
|
90
|
+
return 'esm!' + sourcePath
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Options
|
|
95
|
+
|
|
96
|
+
The `esm` plugin supports configuration with the following defaults:
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
{
|
|
100
|
+
esm: {
|
|
101
|
+
// Update paths of module dependencies.
|
|
102
|
+
resolvePath: func, // see above
|
|
103
|
+
// Allow using a different plugin alias than `esm` in the source code.
|
|
104
|
+
pluginName: 'esm',
|
|
105
|
+
// The file extension of source files to be transformed.
|
|
106
|
+
fileExtension: '.js',
|
|
107
|
+
// Skip modules already in the AMD format without trying to parse them.
|
|
108
|
+
// Module prefixes like "lib/vendor/" are accepted too. Skipped modules
|
|
109
|
+
// must have all their deep dependencies already transformed.
|
|
110
|
+
skipModules: [],
|
|
111
|
+
// Enforce transpiling even if a optimized module has been loaded.
|
|
112
|
+
mixedAmdAndEsm: false,
|
|
113
|
+
// Suppress transpiling even if an optimized module has not been loaded yet.
|
|
114
|
+
onlyAmd: false
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
In lieu of a formal styleguide, take care to maintain the existing coding style. Lint and test your code.
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
Copyright (c) 2022 Ferdinand Prantl
|
|
126
|
+
|
|
127
|
+
Licensed under the MIT license.
|
|
128
|
+
|
|
129
|
+
[Babel]: https://babeljs.io/
|
|
130
|
+
[RequireJS]: http://requirejs.org
|
|
131
|
+
[RequireJS optimizer]: https://requirejs.org/docs/optimization.html
|
|
132
|
+
[requirejs-babel7]: https://www.npmjs.com/package/requirejs-babel7
|
|
133
|
+
[`requirejs`]: https://www.npmjs.com/package/requirejs
|
|
134
|
+
[`@prantlf/requirejs`]: https://www.npmjs.com/package/@prantlf/requirejs
|
|
135
|
+
[Node.js]: http://nodejs.org/
|
|
136
|
+
[NPM]: https://www.npmjs.com/
|
|
137
|
+
[PNPM]: https://pnpm.io/
|
|
138
|
+
[Yarn]: https://yarnpkg.com/
|
|
139
|
+
[demo]: https://github.com/prantlf/requirejs-esm/tree/master/demo
|
|
140
|
+
[default module name resolution]: https://github.com/prantlf/requirejs-esm/blob/master/esm.js#L38
|
|
141
|
+
[resolvePath]: https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md#resolvepath
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Require module dependencies.
|
|
4
|
+
const commander = require('commander')
|
|
5
|
+
const glob = require('tiny-glob')
|
|
6
|
+
const { readFile, writeFile } = require('fs/promises')
|
|
7
|
+
const { transform } = require('../dist/api')
|
|
8
|
+
|
|
9
|
+
// Define the command-line interface.
|
|
10
|
+
commander.description('Transforms an ESM module to AMD or adapts an AMD module for requirejs-esm.')
|
|
11
|
+
.name('esm2requirejs')
|
|
12
|
+
.usage('[options] <files>')
|
|
13
|
+
// .option('-e, --ecma <version>', 'change the ECMAScript version from 2020 to other one')
|
|
14
|
+
.option('-p, --plugin <name>', 'change the plugin name from "esm" to other name')
|
|
15
|
+
.option('-o, --output <file>', 'write the adapted module source to a file')
|
|
16
|
+
.option('-r, --rewrite', 'rewrite the input files with the adapted output')
|
|
17
|
+
// .option('-m, --minify', 'minify the generated output')
|
|
18
|
+
.option('-s, --source-map', 'write inline source maps to the adapted output')
|
|
19
|
+
.option('-v, --verbose', 'print call stack in case of error')
|
|
20
|
+
.on('--help', function () {
|
|
21
|
+
console.log()
|
|
22
|
+
console.log('You can use one or more file names or one or more glob patterns to specify one')
|
|
23
|
+
console.log('or more files. If you set the plugin name to an empty string, modules already')
|
|
24
|
+
console.log('in the AMD format will be left intact.')
|
|
25
|
+
console.log()
|
|
26
|
+
console.log('Examples:')
|
|
27
|
+
console.log()
|
|
28
|
+
console.log(' $ esm2requirejs -s -o out/main.js src/main.js')
|
|
29
|
+
console.log(' $ esm2requirejs -p \'\' -r \'src/**/*.js\'')
|
|
30
|
+
})
|
|
31
|
+
.parse(process.argv)
|
|
32
|
+
|
|
33
|
+
const {
|
|
34
|
+
/*ecma: ecmaVersion,*/ plugin: pluginName, /*minify,*/ sourceMap, output: outputFile, rewrite, verbose
|
|
35
|
+
} = commander.opts()
|
|
36
|
+
const { args } = commander;
|
|
37
|
+
if (!args.length) {
|
|
38
|
+
commander.help()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Main entry point.
|
|
42
|
+
(async function () {
|
|
43
|
+
try {
|
|
44
|
+
const files = (await Promise.all(args.map(arg => glob(arg)))).flat()
|
|
45
|
+
if (!files.length) {
|
|
46
|
+
throw new Error('no files found')
|
|
47
|
+
}
|
|
48
|
+
for (const file of files) {
|
|
49
|
+
const text = await readFile(file, 'utf8')
|
|
50
|
+
const code = transform(text, file, { /*ecmaVersion,*/ pluginName, /*minify,*/ sourceMap })
|
|
51
|
+
if (outputFile) {
|
|
52
|
+
await writeFile(outputFile, code)
|
|
53
|
+
} else if (rewrite) {
|
|
54
|
+
await writeFile(file, code)
|
|
55
|
+
} else {
|
|
56
|
+
console.log(`// ${file}\n\n${code}`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error((!verbose && error.message) || error)
|
|
61
|
+
process.exitCode = 1
|
|
62
|
+
}
|
|
63
|
+
}());
|