Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement amd.autoId/amd.basePath options #3867

Merged
merged 33 commits into from Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cef70d8
implement `amd.idFromChunkName` option
Nov 13, 2020
a44bef3
update output-options-hook test
Nov 13, 2020
eadea09
update NormalizedOutputOptions type
Nov 13, 2020
462c69c
simiplify removeExtension()
Nov 13, 2020
287769a
fix `idFromChunkName` type
Nov 13, 2020
65f0921
make `idFromChunkName` required in normalized options
Nov 13, 2020
3a42519
reference actual types in normalizeOutputOptions
Nov 13, 2020
04ec70f
switch to using a pattern instead
tjenkinson Nov 15, 2020
3bcd1cd
also removeExtensionFromRelativeAmdId in UMD finaliser
tjenkinson Nov 15, 2020
02b8f21
Merge branch 'remove-extension-amd-umd' into amd-id-code-split
tjenkinson Nov 15, 2020
2e9d487
support id in output.amd.id in umd finaliser
tjenkinson Nov 15, 2020
cb76695
move test to chunking-form/samples
tjenkinson Nov 15, 2020
881b932
remove test from cli section
tjenkinson Nov 15, 2020
e3cf09c
remove idFromChunkName
tjenkinson Nov 15, 2020
95d605f
revert chunking-form index change
Nov 20, 2020
96088e2
break out to amd.autoId and amd.basePath options
Nov 20, 2020
bde40bc
Merge remote-tracking branch 'upstream/master' into amd-id-code-split
Nov 20, 2020
ad09886
remove unneeded changes
Nov 20, 2020
114364f
update docs
Nov 20, 2020
9434622
update test to also put chunks in folder
Nov 20, 2020
8eeece1
throw error if amd.id used with muli-file build
Nov 27, 2020
1654ded
more accurate amd options type
Nov 27, 2020
1d4efc9
update output.amd doc
Nov 27, 2020
84e8099
fix `@ts-expect-error` comments
Nov 27, 2020
96ea9c1
add tests for invalid options
Nov 27, 2020
c9ec971
add test for entryFileNames with output.preserveModules
Nov 27, 2020
48d01b1
ignore internal error in coverage
Nov 27, 2020
e1d9173
simplify type
Nov 27, 2020
e6944ae
fix output options test
Nov 27, 2020
9cecce3
fix type tests
Nov 27, 2020
6ef8520
improve types again
Nov 27, 2020
bba3d17
simply again
Nov 27, 2020
cbae7b8
make it a warning
tjenkinson Nov 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/help.md
Expand Up @@ -20,6 +20,7 @@ Basic options:
-v, --version Show version number
-w, --watch Watch files in bundle and rebuild on changes
--amd.id <id> ID for AMD module (default is anonymous)
--amd.idFromChunkName Make the ID the chunk file name (appended to `amd.id` if present)
--amd.define <name> Function to use in place of `define`
--assetFileNames <pattern> Name pattern for emitted assets
--banner <text> Code to insert at top of bundle (outside wrapper)
Expand Down
1 change: 1 addition & 0 deletions docs/01-command-line-reference.md
Expand Up @@ -278,6 +278,7 @@ Many options have command line equivalents. In those cases, any arguments passed
-v, --version Show version number
-w, --watch Watch files in bundle and rebuild on changes
--amd.id <id> ID for AMD module (default is anonymous)
--amd.idFromChunkName Make the ID the chunk file name (appended to `amd.id` if present)
--amd.define <name> Function to use in place of `define`
--assetFileNames <pattern> Name pattern for emitted assets
--banner <text> Code to insert at top of bundle (outside wrapper)
Expand Down
23 changes: 22 additions & 1 deletion docs/999-big-list-of-options.md
Expand Up @@ -1053,7 +1053,7 @@ Type: `((id: string) => string) | { [id: string]: string }`<br>
Same as [`context`](guide/en/#context), but per-module – can either be an object of `id: context` pairs, or an `id => context` function.

#### output.amd
Type: `{ id?: string, define?: string}`
Type: `{ id?: string, idFromChunkName?: boolean, define?: string}`

An object that can contain the following properties:

Expand All @@ -1076,6 +1076,27 @@ export default {
// -> define('my-bundle', ['dependency'], ...
```

**output.amd.idFromChunkName**<br>
Type: `boolean`<br>
CLI: `--amd.idFromChunkName`

Make the ID the chunk file name (appended to `amd.id` if present). This is useful with the `output.dir` option when code splitting occurs.

```js
// rollup.config.js
export default {
...,
format: 'amd',
amd: {
id: 'somewhere/',
idFromChunkName: true
}
};

// -> define('somewhere/main', ['dependency'], ...
// -> define('somewhere/chunk1-44b1f428', ['dependency'], ...
```

**output.amd.define**<br>
Type: `string`<br>
CLI: `--amd.define <defineFunctionName>`
Expand Down
1 change: 1 addition & 0 deletions src/Chunk.ts
Expand Up @@ -719,6 +719,7 @@ export default class Chunk {
dependencies: [...this.renderedDependencies!.values()],
exports: this.renderedExports!,
hasExports,
id: this.id as string,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am I correct assuming id is always a string at this point?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, yes.

indentString: this.indentString,
intro: addons.intro!,
isEntryFacade:
Expand Down
26 changes: 22 additions & 4 deletions src/finalisers/amd.ts
Expand Up @@ -5,12 +5,19 @@ import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
import warnOnBuiltins from './shared/warnOnBuiltins';

function removeExtension(name: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call this removeJsExtension, the name confused be as you definitely want to keep non-.js extensions!

if (name.endsWith('.js')) {
return name.slice(0, -3);
}
return name;
}

// AMD resolution will only respect the AMD baseUrl if the .js extension is omitted.
// The assumption is that this makes sense for all relative ids:
// https://requirejs.org/docs/api.html#jsfiles
function removeExtensionFromRelativeAmdId(id: string) {
if (id[0] === '.' && id.endsWith('.js')) {
return id.slice(0, -3);
if (id[0] === '.') {
return removeExtension(id);
}
return id;
}
Expand All @@ -22,6 +29,7 @@ export default function amd(
dependencies,
exports,
hasExports,
id,
indentString: t,
intro,
isEntryFacade,
Expand All @@ -32,7 +40,7 @@ export default function amd(
warn
}: FinaliserOptions,
{
amd: { define: amdDefine, id: amdId },
amd: { define: amdDefine, id: amdId, idFromChunkName: amdIdFromChunkName },
compact,
esModule,
externalLiveBindings,
Expand Down Expand Up @@ -64,8 +72,18 @@ export default function amd(
deps.unshift(`'module'`);
}

let completeAmdId = '';
if (amdId) {
completeAmdId += amdId;
}

if (amdIdFromChunkName) {
completeAmdId += removeExtension(id);
}

const params =
(amdId ? `'${amdId}',${_}` : ``) + (deps.length ? `[${deps.join(`,${_}`)}],${_}` : ``);
(completeAmdId ? `'${completeAmdId}',${_}` : ``) +
(deps.length ? `[${deps.join(`,${_}`)}],${_}` : ``);
const useStrict = strict ? `${_}'use strict';` : '';

magicString.prepend(
Expand Down
1 change: 1 addition & 0 deletions src/finalisers/index.ts
Expand Up @@ -13,6 +13,7 @@ export interface FinaliserOptions {
dependencies: ChunkDependencies;
exports: ChunkExports;
hasExports: boolean;
id: string;
indentString: string;
intro: string;
isEntryFacade: boolean;
Expand Down
13 changes: 9 additions & 4 deletions src/rollup/types.d.ts
Expand Up @@ -320,8 +320,12 @@ export type ResolveFileUrlHook = (
export type AddonHookFunction = (this: PluginContext) => string | Promise<string>;
export type AddonHook = string | AddonHookFunction;

export type ChangeEvent = 'create' | 'update' | 'delete'
export type WatchChangeHook = (this: PluginContext, id: string, change: {event: ChangeEvent}) => void
export type ChangeEvent = 'create' | 'update' | 'delete';
export type WatchChangeHook = (
this: PluginContext,
id: string,
change: { event: ChangeEvent }
) => void;

/**
* use this type for plugin annotation
Expand Down Expand Up @@ -576,6 +580,7 @@ export interface OutputOptions {
amd?: {
define?: string;
id?: string;
idFromChunkName?: boolean;
};
assetFileNames?: string | ((chunkInfo: PreRenderedAsset) => string);
banner?: string | (() => string | Promise<string>);
Expand Down Expand Up @@ -794,7 +799,7 @@ export interface RollupWatchOptions extends InputOptions {
watch?: WatcherOptions | false;
}

interface TypedEventEmitter<T extends {[event: string]: (...args: any) => any}> {
interface TypedEventEmitter<T extends { [event: string]: (...args: any) => any }> {
addListener<K extends keyof T>(event: K, listener: T[K]): this;
emit<K extends keyof T>(event: K, ...args: Parameters<T[K]>): boolean;
eventNames(): Array<keyof T>;
Expand Down Expand Up @@ -827,7 +832,7 @@ export type RollupWatcherEvent =

export interface RollupWatcher
extends TypedEventEmitter<{
change: (id: string, change: {event: ChangeEvent}) => void;
change: (id: string, change: { event: ChangeEvent }) => void;
close: () => void;
event: (event: RollupWatcherEvent) => void;
restart: () => void;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/options/normalizeOutputOptions.ts
Expand Up @@ -186,9 +186,11 @@ const getAmd = (
id?: string;
} => ({
define: 'define',
idFromChunkName: false,
...(config.amd as {
define?: string;
id?: string;
idFromChunkName?: boolean;
})
});

Expand Down
@@ -0,0 +1,5 @@
module.exports = {
description:
'sets AMD module ID for each chunk appended with chunk id when there is code splitting and `idFromChunkName` option',
command: 'rollup -i main.js -f amd --amd.id "foo/" --amd.idFromChunkName'
};
@@ -0,0 +1,21 @@
//→ main.js:
define('foo/main', ['require', 'exports'], function (require, exports) { 'use strict';

function getA() {
return new Promise(function (resolve, reject) { require(['./a-44b1f428'], resolve, reject) });
}

exports.getA = getA;

Object.defineProperty(exports, '__esModule', { value: true });

});

//→ a-44b1f428.js:
define('foo/a-44b1f428', ['exports'], function (exports) { 'use strict';

const something = 42;

exports.something = something;

});
@@ -0,0 +1 @@
export const something = 42;
@@ -0,0 +1,3 @@
export function getA() {
return import('./a');
}
@@ -0,0 +1,5 @@
module.exports = {
description:
'sets AMD module ID for each chunk as the chunk id when there is code splitting and `idFromChunkName` option',
command: 'rollup -i main.js -f amd --amd.idFromChunkName'
};
@@ -0,0 +1,21 @@
//→ main.js:
define('main', ['require', 'exports'], function (require, exports) { 'use strict';

function getA() {
return new Promise(function (resolve, reject) { require(['./a-44b1f428'], resolve, reject) });
}

exports.getA = getA;

Object.defineProperty(exports, '__esModule', { value: true });

});

//→ a-44b1f428.js:
define('a-44b1f428', ['exports'], function (exports) { 'use strict';

const something = 42;

exports.something = something;

});
@@ -0,0 +1 @@
export const something = 42;
@@ -0,0 +1,3 @@
export function getA() {
return import('./a');
}