Skip to content

Commit 1ba3e61

Browse files
authored
Merge pull request #225 from PepsRyuu/AMDFormat
AMD Format Support
2 parents c91b2f4 + 446d114 commit 1ba3e61

16 files changed

+264
-38
lines changed

docs/rollup-config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ See [Rollup](https://rollupjs.org/guide/en/) documentation for more information
2222
* ```entryFileNames``` - See below note.
2323
* ```chunkFileNames``` - See below note.
2424
* ```assetFileNames``` - See below note.
25-
* ```format``` - Only support for ```es```, ```cjs``` or ```iife```.
25+
* ```format``` - Only support for ```es```, ```cjs```, ```amd``` or ```iife```.
2626
* ```globals``` - Remapping for window variables.
2727

2828
For file name pattern options, when the bundle is generated, it will serve files based on what the pattern says. The ```dir``` option is completely ignored and not part of the generated URL.

examples/example-amd/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"scripts": {
3+
"clean": "shx rm -rf dist",
4+
"start": "cross-env NODE_ENV=development node ../../lib/cli.js -c --hot --content-base public --port 9001",
5+
"build": "npm run clean && cross-env NODE_ENV=production rollup -c"
6+
},
7+
"dependencies": {
8+
"requirejs": "^2.3.6"
9+
},
10+
"devDependencies": {
11+
"@babel/core": "^7.13.14",
12+
"@rollup/plugin-babel": "^5.3.0",
13+
"@rollup/plugin-node-resolve": "^11.2.1",
14+
"cross-env": "^7.0.3",
15+
"rollup": "^2.44.0",
16+
"rollup-plugin-hot-css": "^0.2.1",
17+
"rollup-plugin-static-files": "^0.2.0",
18+
"shx": "^0.3.2"
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>AMD Format</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script src="/require.js"></script>
11+
<script>
12+
require(['single-export-main', 'multiple-export-main', 'dynamic-import-main'], function (single, multiple) {
13+
document.body.textContent = JSON.stringify({
14+
'single-message-main': single,
15+
'multiple-message-main': multiple
16+
}, null, 4);
17+
18+
document.body.style.whiteSpace = 'pre';
19+
});
20+
</script>
21+
</body>
22+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define(function () {
2+
return {
3+
message: 'my-external-module-dynamic'
4+
}
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
define(function () {
2+
return {
3+
message: 'my-external-module'
4+
}
5+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import node_resolve from '@rollup/plugin-node-resolve';
2+
import babel from '@rollup/plugin-babel';
3+
import static_files from 'rollup-plugin-static-files';
4+
5+
let config = {
6+
input: ['./src/single-export-main.js', './src/multiple-export-main.js', './src/dynamic-import-main.js'],
7+
external: ['my-external-module', 'my-external-module-dynamic'],
8+
output: {
9+
dir: 'dist',
10+
format: 'amd',
11+
entryFileNames: '[name].js',
12+
assetFileNames: '[name][extname]'
13+
},
14+
plugins: [
15+
{
16+
generateBundle() {
17+
let fs = require('fs');
18+
19+
this.emitFile({
20+
type: 'asset',
21+
fileName: 'require.js',
22+
source: fs.readFileSync('node_modules/requirejs/require.js')
23+
});
24+
}
25+
},
26+
babel({
27+
exclude: 'node_modules/**'
28+
}),
29+
node_resolve()
30+
]
31+
}
32+
33+
if (process.env.NODE_ENV === 'production') {
34+
config.plugins = config.plugins.concat([
35+
static_files({
36+
include: ['./public']
37+
})
38+
]);
39+
}
40+
41+
export default config;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import('./my-dynamic-module').then(mod => {
2+
let el = document.createElement('div');
3+
el.textContent = mod.default;
4+
document.body.appendChild(el);
5+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default 'multiple-export-default';
2+
export var named = 'multiple-export-named';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import MyExternalModuleDynamic from 'my-external-module-dynamic';
2+
3+
export default MyExternalModuleDynamic.message;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { message } from 'my-external-module';
2+
3+
export default message;

0 commit comments

Comments
 (0)